001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.publish;
029
030import org.opencms.db.CmsPublishList;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsProject;
033import org.opencms.i18n.CmsLocaleManager;
034import org.opencms.main.CmsContextInfo;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsRuntimeException;
037import org.opencms.main.OpenCms;
038import org.opencms.report.I_CmsReport;
039import org.opencms.util.CmsUUID;
040
041import java.util.Locale;
042
043/**
044 * Publish job information bean.<p>
045 *
046 * @since 6.5.5
047 */
048public final class CmsPublishJobInfoBean {
049
050    /** The flag used to indicate a direct publish job. */
051    public static final int C_PUBLISH_FLAG = 1;
052
053    /** The cms context to use for publishing, will be set to <code>null</code> after publishing. */
054    private CmsObject m_cms;
055
056    /** If this is a "direct publish" operation. */
057    private boolean m_directPublish;
058
059    /** Time of creation of this object. */
060    private long m_enqueueTime;
061
062    /** Time the publish job did end. */
063    private long m_finishTime;
064
065    /** The locale to use for publishing. */
066    private Locale m_locale;
067
068    /** Project to use for publishing. */
069    private CmsUUID m_projectId;
070
071    /** Name of the project used for publishing. */
072    private String m_projectName;
073
074    /** Publish history id. */
075    private CmsUUID m_publishHistoryId;
076
077    /** List of resources to publish, will be set to <code>null</code> after publishing. */
078    private CmsPublishList m_publishList;
079
080    /** The report to use during the publish process, will be set to <code>null</code> after publishing. */
081    private I_CmsReport m_publishReport;
082
083    /** Report to log the publish job to, will be set to <code>null</code> after publishing. */
084    private I_CmsReport m_report;
085
086    /** Number of resources to publish. */
087    private int m_size;
088
089    /** Time the publish job did actually start. */
090    private long m_startTime;
091
092    /** The UUID of the running publish thread. */
093    private CmsUUID m_threadUUID;
094
095    /** User to use for publishing. */
096    private CmsUUID m_userId;
097
098    /**
099     * Constructor used to initialize a job info bean from the database.<p>
100     *
101     * @param historyId publish history id
102     * @param projectId the id of the project
103     * @param projectName the name of the project
104     * @param userId the id of the user
105     * @param localeName the string representation of a locale
106     * @param flags flags of the publish job
107     * @param resourceCount number of published resources
108     * @param enqueueTime time when the job was enqueued
109     * @param startTime time when the job was started
110     * @param finishTime time when the job was finished
111     */
112    public CmsPublishJobInfoBean(
113        CmsUUID historyId,
114        CmsUUID projectId,
115        String projectName,
116        CmsUUID userId,
117        String localeName,
118        int flags,
119        int resourceCount,
120        long enqueueTime,
121        long startTime,
122        long finishTime) {
123
124        m_publishHistoryId = historyId;
125        m_projectId = projectId;
126
127        m_projectName = projectName;
128        m_userId = userId;
129        m_size = resourceCount;
130        m_directPublish = ((flags & C_PUBLISH_FLAG) == C_PUBLISH_FLAG);
131
132        m_enqueueTime = enqueueTime;
133        m_startTime = startTime;
134        m_finishTime = finishTime;
135
136        m_locale = CmsLocaleManager.getLocale(localeName);
137    }
138
139    /**
140     * The Default constructor.<p>
141     *
142     * @param cms the cms context to use for publishing
143     * @param publishList the list of resources to publish
144     * @param report the report to write to
145     *
146     * @throws CmsException if something goes wrong
147     */
148    protected CmsPublishJobInfoBean(CmsObject cms, CmsPublishList publishList, I_CmsReport report)
149    throws CmsException {
150
151        m_cms = OpenCms.initCmsObject(cms);
152        m_projectId = m_cms.getRequestContext().getCurrentProject().getUuid();
153        m_projectName = m_cms.getRequestContext().getCurrentProject().getName();
154        m_userId = m_cms.getRequestContext().getCurrentUser().getId();
155        m_locale = m_cms.getRequestContext().getLocale();
156
157        m_publishList = publishList;
158        m_publishHistoryId = m_publishList.getPublishHistoryId();
159
160        m_size = m_publishList.size();
161        m_directPublish = m_publishList.isDirectPublish();
162
163        m_report = report;
164    }
165
166    /**
167     * Returns the time this object has been created.<p>
168     *
169     * @return the time this object has been created
170     */
171    public long getEnqueueTime() {
172
173        return m_enqueueTime;
174    }
175
176    /**
177     * Returns the time the publish job ends.<p>
178     *
179     * @return the time the publish job ends
180     */
181    public long getFinishTime() {
182
183        return m_finishTime;
184    }
185
186    /**
187     * Returns the flags of this publish job.<p>
188     *
189     * @return the flags of this publish job
190     */
191    public int getFlags() {
192
193        return (m_directPublish) ? C_PUBLISH_FLAG : 0;
194    }
195
196    /**
197     * Returns the locale for this publish job.<p>
198     *
199     * @return the locale for this publish job
200     */
201    public Locale getLocale() {
202
203        return m_locale;
204    }
205
206    /**
207     * Returns the project id for this publish job.<p>
208     *
209     * @return the project id for this publish job
210     */
211    public CmsUUID getProjectId() {
212
213        return m_projectId;
214    }
215
216    /**
217     * Returns the originally stored project name.<p>
218     *
219     * @return the originally stored project name
220     */
221    public String getProjectName() {
222
223        return m_projectName;
224    }
225
226    /**
227     * Returns the publish history id.<p>
228     *
229     * @return the publish history id
230     */
231    public CmsUUID getPublishHistoryId() {
232
233        return m_publishHistoryId;
234    }
235
236    /**
237     * Returns the list of resources to publish.<p>
238     *
239     * @return the list of resources to publish
240     */
241    public CmsPublishList getPublishList() {
242
243        return m_publishList;
244    }
245
246    /**
247     * Returns the report for this publish job.<p>
248     *
249     * This is not the original report, it is wrapper that
250     * also writes to a temporary file.<p>
251     *
252     * It will be <code>null</code> before starting and after finishing.<p>
253     *
254     * @return the report for this publish job
255     *
256     * @see CmsPublishJobEnqueued#getReport()
257     */
258    public I_CmsReport getPublishReport() {
259
260        if ((m_publishReport == null) && (m_finishTime == 0) && (m_startTime > 0)) {
261            m_publishReport = getReport();
262            if (m_publishReport == null) {
263                m_publishReport = new CmsPublishReport(getCmsObject().getRequestContext().getLocale());
264            } else {
265                m_publishReport = CmsPublishReport.decorate(m_publishReport);
266            }
267        }
268        return m_publishReport;
269    }
270
271    /**
272     * Returns the report for this publish job.<p>
273     *
274     * @return the report for this publish job
275     */
276    public I_CmsReport getReport() {
277
278        return m_report;
279    }
280
281    /**
282     * Returns the number of resources in the publish list.<p>
283     *
284     * @return the number of resources in the publish list
285     */
286    public int getSize() {
287
288        return m_size;
289    }
290
291    /**
292     * Returns the time the publish job did actually start.<p>
293     *
294     * @return the time the publish job did actually start
295     */
296    public long getStartTime() {
297
298        return m_startTime;
299    }
300
301    /**
302     * Returns the UUID of the running publish thread.<p>
303     *
304     * @return the UUID of the running publish thread
305     */
306    public CmsUUID getThreadUUID() {
307
308        return m_threadUUID;
309    }
310
311    /**
312     * Returns the user for this publish job.<p>
313     *
314     * @return the user for this publish job
315     */
316    public CmsUUID getUserId() {
317
318        return m_userId;
319    }
320
321    /**
322     * Removes the assigned publish report.<p>
323     *
324     * @return the removed report
325     */
326    public I_CmsReport removePublishReport() {
327
328        I_CmsReport report = m_publishReport;
329        m_publishReport = null;
330        return report;
331    }
332
333    /**
334     * Revives this publish job.<p>
335     *
336     * @param adminCms an admin cms object
337     * @param publishList a publish list
338     *
339     * @throws CmsException if something goes wrong
340     */
341    public void revive(CmsObject adminCms, CmsPublishList publishList) throws CmsException {
342
343        CmsContextInfo context = new CmsContextInfo(adminCms.readUser(m_userId).getName());
344        CmsProject project = adminCms.readProject(m_projectId);
345        context.setLocale(m_locale);
346
347        m_cms = OpenCms.initCmsObject(adminCms, context);
348        m_cms.getRequestContext().setCurrentProject(project);
349
350        m_publishList = publishList;
351        m_publishList.revive(m_cms);
352    }
353
354    /**
355     * @see java.lang.Object#toString()
356     */
357    @Override
358    public String toString() {
359
360        StringBuffer result = new StringBuffer();
361
362        result.append("[");
363        result.append(this.getClass().getName());
364        result.append(", history id: ");
365        result.append(getPublishHistoryId().toString());
366        result.append(", project id ");
367        result.append(getProjectId().toString());
368        result.append(", project name: ");
369        result.append(getProjectName());
370        result.append(", user id: ");
371        result.append(getUserId().toString());
372        result.append(", locale: ");
373        result.append(getLocale().toString());
374        result.append(", flags: ");
375        result.append(getFlags());
376        result.append(", size: ");
377        result.append(getSize());
378        result.append(", enqueue time: ");
379        result.append(getEnqueueTime());
380        result.append(", start time: ");
381        result.append(getStartTime());
382        result.append(", finish time: ");
383        result.append(getFinishTime());
384        result.append("]");
385
386        return result.toString();
387    }
388
389    /**
390     * Signalizes that the publish job has been enqueued.<p>
391     * Actually sets the enqueue time only if it is not set already (re-enqueue during startup).<p>
392     */
393    protected void enqueue() {
394
395        if (m_enqueueTime == 0L) {
396            m_enqueueTime = System.currentTimeMillis();
397        }
398    }
399
400    /**
401     * Signalizes the end of the publish job.<p>
402     * Actually only sets the finish time and closes the publish report stream.<p>
403     */
404    protected void finish() {
405
406        if (m_finishTime != 0) {
407            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_PUBLISH_JOB_ALREADY_FINISHED_0));
408        }
409        m_cms = null;
410        m_report = null;
411        m_size = m_publishList.size();
412        m_publishList = null;
413        if (m_publishReport instanceof CmsPublishReport) {
414            ((CmsPublishReport)m_publishReport).finish();
415        }
416        m_threadUUID = null;
417        m_finishTime = System.currentTimeMillis();
418    }
419
420    /**
421     * Returns the cms object, will be set to <code>null</code> after publishing.<p>
422     *
423     * @return the cms object
424     */
425    protected CmsObject getCmsObject() {
426
427        return m_cms;
428    }
429
430    /**
431     * Returns <code>true</code> if this is a "direct publish" operation.<p>
432     *
433     * @return <code>true</code> if this is a "direct publish" operation
434     */
435    protected boolean isDirectPublish() {
436
437        return m_directPublish;
438    }
439
440    /**
441     * Returns if the publish job is already finished.<p>
442     *
443     * @return <code>true</code> if the publish job is already finished
444     */
445    protected boolean isFinished() {
446
447        return (m_finishTime != 0L);
448    }
449
450    /**
451     * Returns if the publish job is already started.<p>
452     *
453     * @return <code>true</code> if the publish job is already started
454     */
455    protected boolean isStarted() {
456
457        return (m_startTime != 0L);
458    }
459
460    /**
461     * Signalizes the start of the publish job.<p>
462     * Actually sets the starting time, writes the report header and sets the running thread uuid.<p>
463     *
464     * @param threadUUID the running thread uuid
465     */
466    protected void start(CmsUUID threadUUID) {
467
468        if (m_startTime != 0) {
469            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_PUBLISH_JOB_ALREADY_STARTED_0));
470        }
471        m_startTime = System.currentTimeMillis();
472        m_threadUUID = threadUUID;
473        if (getPublishReport() instanceof CmsPublishReport) {
474            ((CmsPublishReport)m_publishReport).start();
475        }
476    }
477}