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