KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > commons > CmsPublishProject


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsPublishProject.java,v $
3  * Date : $Date: 2006/03/27 14:52:18 $
4  * Version: $Revision: 1.27 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.commons;
33
34 import org.opencms.db.CmsPublishList;
35 import org.opencms.file.CmsProject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsResourceFilter;
38 import org.opencms.jsp.CmsJspActionElement;
39 import org.opencms.lock.CmsLock;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsLog;
42 import org.opencms.security.CmsPermissionSet;
43 import org.opencms.util.CmsDateUtil;
44 import org.opencms.util.CmsStringUtil;
45 import org.opencms.workplace.CmsReport;
46 import org.opencms.workplace.CmsWorkplaceManager;
47 import org.opencms.workplace.CmsWorkplaceSettings;
48 import org.opencms.workplace.threads.CmsPublishThread;
49 import org.opencms.workplace.threads.CmsXmlDocumentLinkValidatorThread;
50
51 import java.text.DateFormat JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.Date JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.List JavaDoc;
56
57 import javax.servlet.http.HttpServletRequest JavaDoc;
58 import javax.servlet.http.HttpServletResponse JavaDoc;
59 import javax.servlet.jsp.JspException JavaDoc;
60 import javax.servlet.jsp.PageContext JavaDoc;
61
62 import org.apache.commons.logging.Log;
63
64 /**
65  * Creates the dialogs for publishing a project or a resource.<p>
66  *
67  * The following files use this class:
68  * <ul>
69  * <li>/commons/publishproject.jsp
70  * <li>/commons/publishresource.jsp
71  * </ul>
72  * <p>
73  *
74  * @author Andreas Zahner
75  *
76  * @version $Revision: 1.27 $
77  *
78  * @since 6.0.0
79  */

80 public class CmsPublishProject extends CmsReport {
81
82     /** Value for the action: show unlock confirmation. */
83     public static final int ACTION_UNLOCK_CONFIRMATION = 200;
84
85     /** Value for the action: unlock confirmed. */
86     public static final int ACTION_UNLOCK_CONFIRMED = 210;
87
88     /** The dialog type. */
89     public static final String JavaDoc DIALOG_TYPE = "publishproject";
90
91     /** Request parameter value for the action: show unlock confirmation. */
92     public static final String JavaDoc DIALOG_UNLOCK_CONFIRMATION = "unlockconfirmation";
93
94     /** Request parameter value for the action: unlock confirmed. */
95     public static final String JavaDoc DIALOG_UNLOCK_CONFIRMED = "unlockconfirmed";
96     
97     /** Request parameter name for the publishsiblings parameter. */
98     public static final String JavaDoc PARAM_PUBLISHSIBLINGS = "publishsiblings";
99     
100     /** Request parameter name for the subresources parameter. */
101     public static final String JavaDoc PARAM_SUBRESOURCES = "subresources";
102
103     /** The log object for this class. */
104     private static final Log LOG = CmsLog.getLog(CmsPublishProject.class);
105
106     private String JavaDoc m_paramDirectpublish;
107     private String JavaDoc m_paramModifieddate;
108     private String JavaDoc m_paramModifieduser;
109     private String JavaDoc m_paramProjectid;
110     private String JavaDoc m_paramProjectname;
111     private String JavaDoc m_paramPublishsiblings;
112     private String JavaDoc m_paramResourcename;
113     private String JavaDoc m_paramSubresources;
114
115     /**
116      * Public constructor.<p>
117      *
118      * @param jsp an initialized JSP action element
119      */

120     public CmsPublishProject(CmsJspActionElement jsp) {
121
122         super(jsp);
123     }
124
125     /**
126      * Public constructor with JSP variables.<p>
127      *
128      * @param context the JSP page context
129      * @param req the JSP request
130      * @param res the JSP response
131      */

132     public CmsPublishProject(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
133
134         this(new CmsJspActionElement(context, req, res));
135     }
136
137     /**
138      * Performs the publish report, will be called by the JSP page.<p>
139      *
140      * @throws JspException if problems including sub-elements occur
141      */

142     public void actionReport() throws JspException JavaDoc {
143
144         // save initialized instance of this class in request attribute for included sub-elements
145
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
146         switch (getAction()) {
147             case ACTION_REPORT_END:
148                 actionCloseDialog();
149                 break;
150             case ACTION_REPORT_UPDATE:
151                 setParamAction(REPORT_UPDATE);
152                 getJsp().include(FILE_REPORT_OUTPUT);
153
154                 break;
155             case ACTION_REPORT_BEGIN:
156             case ACTION_CONFIRMED:
157             default:
158                 try {
159                     List publishResources = null;
160                     boolean directPublish = Boolean.valueOf(getParamDirectpublish()).booleanValue();
161
162                     if (directPublish) {
163                         // get the offline resource(s) in direct publish mode
164
publishResources = new ArrayList JavaDoc(getResourceList().size());
165                         Iterator JavaDoc i = getResourceList().iterator();
166                         while (i.hasNext()) {
167                             String JavaDoc resName = (String JavaDoc)i.next();
168                             try {
169                                 CmsResource res = getCms().readResource(resName, CmsResourceFilter.ALL);
170                                 publishResources.add(res);
171                                 // check if the resource is locked
172
CmsLock lock = getCms().getLock(resName);
173                                 if (! lock.isNullLock()) {
174                                     // resource is locked, so unlock it
175
getCms().unlockResource(resName);
176                                 }
177                             } catch (CmsException e) {
178                                 addMultiOperationException(e);
179                             }
180                         }
181                         // for error(s) unlocking resource(s), throw exception
182
checkMultiOperationException(Messages.get(), Messages.ERR_PUBLISH_MULTI_UNLOCK_0);
183                     } else {
184                         if (getCms().getRequestContext().currentProject().getType() == CmsProject.PROJECT_TYPE_TEMPORARY) {
185                             // set the flag that this is a temporary project
186
setParamRefreshWorkplace(CmsStringUtil.TRUE);
187                         }
188                     }
189
190                     if (showUnlockConfirmation()) {
191                         // some subresources are locked, unlock them before publishing
192
if (directPublish) {
193                             // unlock subresources of a folder
194
Iterator JavaDoc i = getResourceList().iterator();
195                             while (i.hasNext()) {
196                                 String JavaDoc resName = (String JavaDoc)i.next();
197                                 try {
198                                     CmsResource res = getCms().readResource(resName, CmsResourceFilter.ALL);
199                                     if (res.isFolder()) {
200                                         String JavaDoc folderName = resName;
201                                         if (!folderName.endsWith("/")) {
202                                             folderName += "/";
203                                         }
204                                         getCms().lockResource(folderName);
205                                         getCms().unlockResource(folderName);
206                                     }
207                                 } catch (CmsException e) {
208                                     addMultiOperationException(e);
209                                 }
210                             }
211                             // for error(s) unlocking resource(s), throw exception
212
checkMultiOperationException(Messages.get(), Messages.ERR_PUBLISH_MULTI_UNLOCK_0);
213                         } else {
214                             // unlock all project resources
215
getCms().unlockProject(Integer.parseInt(getParamProjectid()));
216                         }
217                     }
218                     
219                     CmsPublishList publishList = null;
220                     if (directPublish) {
221                         // create publish list for direct publish
222
boolean publishSubResources = Boolean.valueOf(getParamSubresources()).booleanValue();
223                         publishList = getCms().getPublishList(
224                             publishResources,
225                             Boolean.valueOf(getParamPublishsiblings()).booleanValue(),
226                             publishSubResources);
227                         getCms().checkPublishPermissions(publishList);
228                     }
229                     
230                     // start the link validation thread before publishing
231
CmsXmlDocumentLinkValidatorThread thread = new CmsXmlDocumentLinkValidatorThread(
232                         getCms(),
233                         publishList,
234                         getSettings());
235                     setParamAction(REPORT_BEGIN);
236                     setParamThread(thread.getUUID().toString());
237
238                     // set the flag that another thread is following
239
setParamThreadHasNext(CmsStringUtil.TRUE);
240                     // set the key name for the continue checkbox
241
setParamReportContinueKey(Messages.GUI_PUBLISH_CONTINUE_BROKEN_LINKS_0);
242                     getJsp().include(FILE_REPORT_OUTPUT);
243
244                 } catch (Throwable JavaDoc e) {
245                     // error while unlocking resources, show error screen
246
includeErrorpage(this, e);
247                 }
248         }
249     }
250
251     /**
252      * Builds the HTML for the "publish siblings" and "publish sub resources" checkboxes when direct publishing a file.<p>
253      *
254      * @return the HTMl for the "publish siblings" and "publish sub resources" checkboxes
255      */

256     public String JavaDoc buildCheckSiblings() {
257
258         CmsResource res = null;
259         if (! isMultiOperation()) {
260             try {
261                 res = getCms().readResource(getParamResource(), CmsResourceFilter.ALL);
262             } catch (CmsException e) {
263                 // res will be null
264
if (LOG.isInfoEnabled()) {
265                     LOG.info(e.getLocalizedMessage());
266                 }
267             }
268         }
269         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
270         boolean showSiblingCheckBox = false;
271         if (isMultiOperation() || (res != null && res.isFile() && res.getSiblingCount() > 1) || (res != null && res.isFolder())) {
272             // resource is file and has siblings, so create checkbox
273

274             result.append(dialogSpacer());
275             result.append("<input type=\"checkbox\" name=\"");
276             result.append(PARAM_PUBLISHSIBLINGS);
277             result.append("\" value=\"true\"");
278             // set the checkbox state to the default value defined in the opencms.properties
279
if (getSettings().getUserSettings().getDialogPublishSiblings()) {
280                 result.append(" checked=\"checked\"");
281             }
282             result.append(">&nbsp;");
283             result.append(key(Messages.GUI_PUBLISH_ALLSIBLINGS_0));
284             showSiblingCheckBox = true;
285         }
286         if (isOperationOnFolder()) {
287             // at least one folder is selected, show "publish subresources" checkbox
288
if (showSiblingCheckBox) {
289                 result.append("<br>");
290             }
291             result.append("<input type=\"checkbox\" name=\"");
292             result.append(PARAM_SUBRESOURCES);
293             result.append("\" value=\"true\" checked=\"checked\">&nbsp;");
294             if (isMultiOperation()) {
295                 result.append(key(Messages.GUI_PUBLISH_MULTI_SUBRESOURCES_0));
296             } else {
297                 result.append(key(Messages.GUI_PUBLISH_SUBRESOURCES_0));
298             }
299         }
300         return result.toString();
301     }
302
303     /**
304      * Returns if a resource will be directly published.<p>
305      *
306      * @return <code>"true"</code> if a resource will be directly published
307      */

308     public String JavaDoc getParamDirectpublish() {
309
310         return m_paramDirectpublish;
311     }
312
313     /**
314      * Returns the last modification date of the resource which will be published.<p>
315      *
316      * @return the last modification date of the resource
317      */

318     public String JavaDoc getParamModifieddate() {
319
320         return m_paramModifieddate;
321     }
322
323     /**
324      * Returns the user who modified the resource which will be published.<p>
325      *
326      * @return the user who modified the resource
327      */

328     public String JavaDoc getParamModifieduser() {
329
330         return m_paramModifieduser;
331     }
332
333     /**
334      * Returns the value of the project id which will be published.<p>
335      *
336      * @return the String value of the project id
337      */

338     public String JavaDoc getParamProjectid() {
339
340         return m_paramProjectid;
341     }
342
343     /**
344      * Returns the value of the project name which will be published.<p>
345      *
346      * @return the String value of the project name
347      */

348     public String JavaDoc getParamProjectname() {
349
350         return m_paramProjectname;
351     }
352
353     /**
354      * Returns if siblings of the resource should be published.<p>
355      *
356      * @return <code>"true"</code> (String) if siblings of the resource should be published
357      */

358     public String JavaDoc getParamPublishsiblings() {
359
360         return m_paramPublishsiblings;
361     }
362
363     /**
364      * Returns the name of the resource which will be published.<p>
365      *
366      * @return the name of the resource
367      */

368     public String JavaDoc getParamResourcename() {
369
370         return m_paramResourcename;
371     }
372
373     /**
374      * Returns the value of the subresources parameter.<p>
375      *
376      * @return the value of the subresources parameter
377      */

378     public String JavaDoc getParamSubresources() {
379     
380         return m_paramSubresources;
381     }
382
383     /**
384      * Sets if a resource will be directly published.<p>
385      *
386      * @param value <code>"true"</code> (String) if a resource will be directly published
387      */

388     public void setParamDirectpublish(String JavaDoc value) {
389
390         m_paramDirectpublish = value;
391     }
392
393     /**
394      * Sets the last modification date of the resource which will be published.<p>
395      *
396      * @param value the last modification date of the resource
397      */

398     public void setParamModifieddate(String JavaDoc value) {
399
400         m_paramModifieddate = value;
401     }
402
403     /**
404      * Sets the user who modified the resource which will be published.<p>
405      *
406      * @param value the user who modified the resource
407      */

408     public void setParamModifieduser(String JavaDoc value) {
409
410         m_paramModifieduser = value;
411     }
412
413     /**
414      * Sets the value of the project id which will be published.<p>
415      *
416      * @param value the String value of the project id
417      */

418     public void setParamProjectid(String JavaDoc value) {
419
420         m_paramProjectid = value;
421     }
422
423     /**
424      * Sets the value of the project name which will be published.<p>
425      *
426      * @param value the String value of the project name
427      */

428     public void setParamProjectname(String JavaDoc value) {
429
430         m_paramProjectname = value;
431     }
432
433     /**
434      * Sets if siblings of the resource should be published.<p>
435      *
436      * @param value <code>"true"</code> (String) if siblings of the resource should be published
437      */

438     public void setParamPublishsiblings(String JavaDoc value) {
439
440         m_paramPublishsiblings = value;
441     }
442
443     /**
444      * Sets the name of the resource which will be published.<p>
445      *
446      * @param value the name of the resource
447      */

448     public void setParamResourcename(String JavaDoc value) {
449
450         m_paramResourcename = value;
451     }
452
453     /**
454      * Sets the value of the subresources parameter.<p>
455      *
456      * @param paramSubresources the value of the subresources parameter
457      */

458     public void setParamSubresources(String JavaDoc paramSubresources) {
459     
460         m_paramSubresources = paramSubresources;
461     }
462
463     /**
464      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
465      */

466     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
467
468         // fill the parameter values in the get/set methods
469
fillParamValues(request);
470         // set the dialog type
471
setParamDialogtype(DIALOG_TYPE);
472         // set the publishing type: publish project or direct publish
473

474         if (CmsStringUtil.isNotEmpty(getParamResource()) || isMultiOperation()) {
475             setParamDirectpublish(CmsStringUtil.TRUE);
476         }
477         // set the action for the JSP switch
478
if (DIALOG_CONFIRMED.equals(getParamAction())) {
479             if (showUnlockConfirmation()) {
480                 // show unlock confirmation dialog
481
setAction(ACTION_UNLOCK_CONFIRMATION);
482             } else {
483                 // skip unlock confirmation dialog
484
setAction(ACTION_CONFIRMED);
485             }
486         } else if (DIALOG_UNLOCK_CONFIRMED.equals(getParamAction())) {
487             setAction(ACTION_CONFIRMED);
488         } else if (REPORT_UPDATE.equals(getParamAction())) {
489             setAction(ACTION_REPORT_UPDATE);
490         } else if (REPORT_BEGIN.equals(getParamAction())) {
491             setAction(ACTION_REPORT_BEGIN);
492         } else if (REPORT_END.equals(getParamAction())) {
493             if (Boolean.valueOf(getParamThreadHasNext()).booleanValue()) {
494                 // after the link check start the publish thread
495
startPublishThread();
496
497                 setParamAction(REPORT_UPDATE);
498                 setAction(ACTION_REPORT_UPDATE);
499             } else {
500                 // ends the publish thread
501
setAction(ACTION_REPORT_END);
502             }
503         } else if (DIALOG_CANCEL.equals(getParamAction())) {
504             setAction(ACTION_CANCEL);
505         } else {
506             setAction(ACTION_DEFAULT);
507             // set parameters depending on publishing type
508
if (Boolean.valueOf(getParamDirectpublish()).booleanValue()) {
509                 // check the required permissions to publish the resource directly
510
if (!getCms().isManagerOfProject()
511                     && !checkResourcePermissions(CmsPermissionSet.ACCESS_DIRECT_PUBLISH, false)) {
512                     // no publish permissions for the single resource, set cancel action to close dialog
513
setAction(ACTION_CANCEL);
514                     return;
515                 }
516                 // determine resource name, last modified date and last modified user of resource
517
computePublishResource();
518                 // add the title for the direct publish dialog
519
setDialogTitle(Messages.GUI_PUBLISH_RESOURCE_1, Messages.GUI_PUBLISH_MULTI_2);
520             } else {
521                 // add the title for the publish project dialog
522
setParamTitle(key(Messages.GUI_PUBLISH_PROJECT_0));
523                 // determine the project id and name for publishing
524
computePublishProject();
525                 // determine target to close the report
526
}
527         }
528     }
529
530     /**
531      * Determine the right project id and name if no request parameter "projectid" is given.<p>
532      */

533     private void computePublishProject() {
534
535         String JavaDoc projectId = getParamProjectid();
536         int id;
537         if (CmsStringUtil.isEmptyOrWhitespaceOnly(projectId)) {
538             // projectid not found in request parameter,
539
id = getCms().getRequestContext().currentProject().getId();
540             setParamProjectname(getCms().getRequestContext().currentProject().getName());
541             setParamProjectid("" + id);
542         } else {
543             id = Integer.parseInt(projectId);
544             try {
545                 setParamProjectname(getCms().readProject(id).getName());
546             } catch (CmsException e) {
547                 LOG.error(Messages.get().getBundle().key(Messages.LOG_SET_PROJECT_NAME_FAILED_0), e);
548             }
549         }
550     }
551
552     /**
553      * Fills the resource information "resource name", "date last modified" and "last modified by" in parameter values.<p>
554      */

555     private void computePublishResource() {
556
557         if (! isMultiOperation()) {
558             try {
559                 CmsResource res = getCms().readResource(getParamResource(), CmsResourceFilter.ALL);
560                 setParamResourcename(res.getName());
561                 setParamModifieddate(CmsDateUtil.getDateTime(
562                     new Date JavaDoc(res.getDateLastModified()),
563                     DateFormat.SHORT,
564                     getLocale()));
565                 setParamModifieduser(getCms().readUser(res.getUserLastModified()).getName());
566             } catch (CmsException e) {
567                 LOG.error(Messages.get().getBundle().key(Messages.LOG_COMPUTING_PUBRES_FAILED_0), e);
568             }
569         }
570     }
571
572     /**
573      * Checks if the unlock confirmation dialog should be displayed.<p>
574      *
575      * @return true if some resources of the project are locked, otherwise false
576      */

577     private boolean showUnlockConfirmation() {
578
579         try {
580             if (Boolean.valueOf(getParamDirectpublish()).booleanValue()) {
581                 // direct publish: check sub resources of a folder
582
if (isOperationOnFolder()) {
583                     int count = 0;
584                     Iterator JavaDoc i = getResourceList().iterator();
585                     while (i.hasNext()) {
586                         String JavaDoc resName = (String JavaDoc)i.next();
587                         CmsResource res = getCms().readResource(resName, CmsResourceFilter.ALL);
588                         if ((res.getState() != CmsResource.STATE_DELETED) && res.isFolder()) {
589                             count += getCms().countLockedResources(resName);
590                         }
591                     }
592                     return (count > 0);
593                 }
594             } else {
595                 // publish project: check all project resources
596
int id = Integer.parseInt(getParamProjectid());
597                 return (getCms().countLockedResources(id) > 0);
598             }
599         } catch (CmsException e) {
600             LOG.error(Messages.get().getBundle().key(Messages.LOG_DISPLAY_UNLOCK_INF_FAILED_0), e);
601         }
602         return false;
603     }
604
605     /**
606      * Starts the publish thread for the project or a resource.<p>
607      *
608      * The type of publish thread is determined by the value of the "directpublish" parameter.<p>
609      */

610     private void startPublishThread() {
611
612         // create a publish thread from the current publish list
613
CmsPublishList publishList = getSettings().getPublishList();
614         CmsWorkplaceSettings settings = (CmsWorkplaceSettings)getJsp().getRequest().getSession().getAttribute(
615             CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS);
616         CmsPublishThread thread = new CmsPublishThread(getCms(), publishList, settings);
617
618         // set the new thread id and flag that no thread is following
619
setParamThread(thread.getUUID().toString());
620         setParamThreadHasNext(CmsStringUtil.FALSE);
621
622         // start the publish thread
623
thread.start();
624     }
625
626 }
627
Popular Tags