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    /** The original publish list. */
099    private CmsPublishList m_originalPublishList;
100
101    /**
102     * Constructor used to initialize a job info bean from the database.<p>
103     *
104     * @param historyId publish history id
105     * @param projectId the id of the project
106     * @param projectName the name of the project
107     * @param userId the id of the user
108     * @param localeName the string representation of a locale
109     * @param flags flags of the publish job
110     * @param resourceCount number of published resources
111     * @param enqueueTime time when the job was enqueued
112     * @param startTime time when the job was started
113     * @param finishTime time when the job was finished
114     */
115    public CmsPublishJobInfoBean(
116        CmsUUID historyId,
117        CmsUUID projectId,
118        String projectName,
119        CmsUUID userId,
120        String localeName,
121        int flags,
122        int resourceCount,
123        long enqueueTime,
124        long startTime,
125        long finishTime) {
126
127        m_publishHistoryId = historyId;
128        m_projectId = projectId;
129
130        m_projectName = projectName;
131        m_userId = userId;
132        m_size = resourceCount;
133        m_directPublish = ((flags & C_PUBLISH_FLAG) == C_PUBLISH_FLAG);
134
135        m_enqueueTime = enqueueTime;
136        m_startTime = startTime;
137        m_finishTime = finishTime;
138
139        m_locale = CmsLocaleManager.getLocale(localeName);
140    }
141
142    /**
143     * The Default constructor.<p>
144     *
145     * @param cms the cms context to use for publishing
146     * @param publishList the list of resources to publish
147     * @param report the report to write to
148     *
149     * @throws CmsException if something goes wrong
150     */
151    protected CmsPublishJobInfoBean(CmsObject cms, CmsPublishList publishList, I_CmsReport report)
152    throws CmsException {
153
154        m_cms = OpenCms.initCmsObject(cms);
155        m_projectId = m_cms.getRequestContext().getCurrentProject().getUuid();
156        m_projectName = m_cms.getRequestContext().getCurrentProject().getName();
157        m_userId = m_cms.getRequestContext().getCurrentUser().getId();
158        m_locale = m_cms.getRequestContext().getLocale();
159
160        m_publishList = publishList;
161        m_originalPublishList = publishList;
162        m_publishHistoryId = m_publishList.getPublishHistoryId();
163
164        m_size = m_publishList.size();
165        m_directPublish = m_publishList.isDirectPublish();
166
167        m_report = report;
168    }
169
170    /**
171     * Returns the time this object has been created.<p>
172     *
173     * @return the time this object has been created
174     */
175    public long getEnqueueTime() {
176
177        return m_enqueueTime;
178    }
179
180    /**
181     * Returns the time the publish job ends.<p>
182     *
183     * @return the time the publish job ends
184     */
185    public long getFinishTime() {
186
187        return m_finishTime;
188    }
189
190    /**
191     * Returns the flags of this publish job.<p>
192     *
193     * @return the flags of this publish job
194     */
195    public int getFlags() {
196
197        return (m_directPublish) ? C_PUBLISH_FLAG : 0;
198    }
199
200    /**
201     * Returns the locale for this publish job.<p>
202     *
203     * @return the locale for this publish job
204     */
205    public Locale getLocale() {
206
207        return m_locale;
208    }
209
210    /**
211     * Gets the original publish list (not nulled after publish job is executed).
212     *
213     * @return the original publish list
214     */
215    public CmsPublishList getOriginalPublishList() {
216
217        return m_originalPublishList;
218
219    }
220
221    /**
222     * Returns the project id for this publish job.<p>
223     *
224     * @return the project id for this publish job
225     */
226    public CmsUUID getProjectId() {
227
228        return m_projectId;
229    }
230
231    /**
232     * Returns the originally stored project name.<p>
233     *
234     * @return the originally stored project name
235     */
236    public String getProjectName() {
237
238        return m_projectName;
239    }
240
241    /**
242     * Returns the publish history id.<p>
243     *
244     * @return the publish history id
245     */
246    public CmsUUID getPublishHistoryId() {
247
248        return m_publishHistoryId;
249    }
250
251    /**
252     * Returns the list of resources to publish.<p>
253     *
254     * @return the list of resources to publish
255     */
256    public CmsPublishList getPublishList() {
257
258        return m_publishList;
259    }
260
261    /**
262     * Returns the report for this publish job.<p>
263     *
264     * This is not the original report, it is wrapper that
265     * also writes to a temporary file.<p>
266     *
267     * It will be <code>null</code> before starting and after finishing.<p>
268     *
269     * @return the report for this publish job
270     *
271     * @see CmsPublishJobEnqueued#getReport()
272     */
273    public I_CmsReport getPublishReport() {
274
275        if ((m_publishReport == null) && (m_finishTime == 0) && (m_startTime > 0)) {
276            m_publishReport = getReport();
277            if (m_publishReport == null) {
278                m_publishReport = new CmsPublishReport(getCmsObject().getRequestContext().getLocale());
279            } else {
280                m_publishReport = CmsPublishReport.decorate(m_publishReport);
281            }
282        }
283        return m_publishReport;
284    }
285
286    /**
287     * Returns the report for this publish job.<p>
288     *
289     * @return the report for this publish job
290     */
291    public I_CmsReport getReport() {
292
293        return m_report;
294    }
295
296    /**
297     * Returns the number of resources in the publish list.<p>
298     *
299     * @return the number of resources in the publish list
300     */
301    public int getSize() {
302
303        return m_size;
304    }
305
306    /**
307     * Returns the time the publish job did actually start.<p>
308     *
309     * @return the time the publish job did actually start
310     */
311    public long getStartTime() {
312
313        return m_startTime;
314    }
315
316    /**
317     * Returns the UUID of the running publish thread.<p>
318     *
319     * @return the UUID of the running publish thread
320     */
321    public CmsUUID getThreadUUID() {
322
323        return m_threadUUID;
324    }
325
326    /**
327     * Returns the user for this publish job.<p>
328     *
329     * @return the user for this publish job
330     */
331    public CmsUUID getUserId() {
332
333        return m_userId;
334    }
335
336    /**
337     * Removes the assigned publish report.<p>
338     *
339     * @return the removed report
340     */
341    public I_CmsReport removePublishReport() {
342
343        I_CmsReport report = m_publishReport;
344        m_publishReport = null;
345        return report;
346    }
347
348    /**
349     * Revives this publish job.<p>
350     *
351     * @param adminCms an admin cms object
352     * @param publishList a publish list
353     *
354     * @throws CmsException if something goes wrong
355     */
356    public void revive(CmsObject adminCms, CmsPublishList publishList) throws CmsException {
357
358        CmsContextInfo context = new CmsContextInfo(adminCms.readUser(m_userId).getName());
359        CmsProject project = adminCms.readProject(m_projectId);
360        context.setLocale(m_locale);
361
362        m_cms = OpenCms.initCmsObject(adminCms, context);
363        m_cms.getRequestContext().setCurrentProject(project);
364
365        m_publishList = publishList;
366        m_originalPublishList = publishList;
367        m_publishList.revive(m_cms);
368    }
369
370    /**
371     * @see java.lang.Object#toString()
372     */
373    @Override
374    public String toString() {
375
376        StringBuffer result = new StringBuffer();
377
378        result.append("[");
379        result.append(this.getClass().getName());
380        result.append(", history id: ");
381        result.append(getPublishHistoryId().toString());
382        result.append(", project id ");
383        result.append(getProjectId().toString());
384        result.append(", project name: ");
385        result.append(getProjectName());
386        result.append(", user id: ");
387        result.append(getUserId().toString());
388        result.append(", locale: ");
389        result.append(getLocale().toString());
390        result.append(", flags: ");
391        result.append(getFlags());
392        result.append(", size: ");
393        result.append(getSize());
394        result.append(", enqueue time: ");
395        result.append(getEnqueueTime());
396        result.append(", start time: ");
397        result.append(getStartTime());
398        result.append(", finish time: ");
399        result.append(getFinishTime());
400        result.append("]");
401
402        return result.toString();
403    }
404
405    /**
406     * Signalizes that the publish job has been enqueued.<p>
407     * Actually sets the enqueue time only if it is not set already (re-enqueue during startup).<p>
408     */
409    protected void enqueue() {
410
411        if (m_enqueueTime == 0L) {
412            m_enqueueTime = System.currentTimeMillis();
413        }
414    }
415
416    /**
417     * Signalizes the end of the publish job.<p>
418     * Actually only sets the finish time and closes the publish report stream.<p>
419     */
420    protected void finish() {
421
422        if (m_finishTime != 0) {
423            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_PUBLISH_JOB_ALREADY_FINISHED_0));
424        }
425        m_cms = null;
426        m_report = null;
427        m_size = m_publishList.size();
428        m_publishList = null;
429        if (m_publishReport instanceof CmsPublishReport) {
430            ((CmsPublishReport)m_publishReport).finish();
431        }
432        m_threadUUID = null;
433        m_finishTime = System.currentTimeMillis();
434    }
435
436    /**
437     * Returns the cms object, will be set to <code>null</code> after publishing.<p>
438     *
439     * @return the cms object
440     */
441    protected CmsObject getCmsObject() {
442
443        return m_cms;
444    }
445
446    /**
447     * Returns <code>true</code> if this is a "direct publish" operation.<p>
448     *
449     * @return <code>true</code> if this is a "direct publish" operation
450     */
451    protected boolean isDirectPublish() {
452
453        return m_directPublish;
454    }
455
456    /**
457     * Returns if the publish job is already finished.<p>
458     *
459     * @return <code>true</code> if the publish job is already finished
460     */
461    protected boolean isFinished() {
462
463        return (m_finishTime != 0L);
464    }
465
466    /**
467     * Returns if the publish job is already started.<p>
468     *
469     * @return <code>true</code> if the publish job is already started
470     */
471    protected boolean isStarted() {
472
473        return (m_startTime != 0L);
474    }
475
476    /**
477     * Signalizes the start of the publish job.<p>
478     * Actually sets the starting time, writes the report header and sets the running thread uuid.<p>
479     *
480     * @param threadUUID the running thread uuid
481     */
482    protected void start(CmsUUID threadUUID) {
483
484        if (m_startTime != 0) {
485            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_PUBLISH_JOB_ALREADY_STARTED_0));
486        }
487        m_startTime = System.currentTimeMillis();
488        m_threadUUID = threadUUID;
489        if (getPublishReport() instanceof CmsPublishReport) {
490            ((CmsPublishReport)m_publishReport).start();
491        }
492    }
493}