KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > content > CmsMergePages


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/CmsMergePages.java,v $
3  * Date : $Date: 2006/03/31 13:59:16 $
4  * Version: $Revision: 1.15 $
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.tools.content;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsResourceFilter;
38 import org.opencms.file.types.CmsResourceTypeXmlPage;
39 import org.opencms.i18n.CmsLocaleManager;
40 import org.opencms.i18n.CmsMessages;
41 import org.opencms.jsp.CmsJspActionElement;
42 import org.opencms.main.CmsException;
43 import org.opencms.report.I_CmsReport;
44 import org.opencms.util.CmsStringUtil;
45 import org.opencms.workplace.CmsReport;
46 import org.opencms.workplace.CmsWorkplaceSettings;
47 import org.opencms.xml.page.CmsXmlPage;
48 import org.opencms.xml.page.CmsXmlPageFactory;
49
50 import java.util.ArrayList JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Locale JavaDoc;
54
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56 import javax.servlet.http.HttpServletResponse JavaDoc;
57 import javax.servlet.jsp.JspException JavaDoc;
58 import javax.servlet.jsp.PageContext JavaDoc;
59
60 /**
61  * Provides methods for the merge pages dialog.<p>
62  *
63  * @author Michael Emmerich
64  *
65  * @version $Revision: 1.15 $
66  *
67  * @since 6.0.0
68  */

69 public class CmsMergePages extends CmsReport {
70
71     /** A constant representing the select option all templates. */
72     public static final String JavaDoc ALL = "ALL";
73
74     /** Key for pages found in folder 1 exclusivly. */
75     public static final int FOLDER1_EXCLUSIVE = 0;
76
77     /** Key for pages found in folder 2 exclusivly. */
78     public static final int FOLDER2_EXCLUSIVE = 1;
79
80     /** Key for pages found in both folders but as different types. */
81     public static final int FOLDERS_DIFFERENTTYPES = 4;
82
83     /** Key for pages found in both folders as individual resources. */
84     public static final int FOLDERS_EQUALNAMES = 3;
85
86     /** Key for pages found in both folders as siblings. */
87     public static final int FOLDERS_SIBLING = 2;
88     
89     /** The dialog type. */
90     public static final String JavaDoc DIALOG_TYPE = "mergepages";
91
92     /** Request parameter name for the first folder to merge. */
93     public static final String JavaDoc PARAM_FOLDER1 = "folder1";
94     
95     /** Request parameter name for the second folder to merge. */
96     public static final String JavaDoc PARAM_FOLDER2 = "folder2";
97
98     /** the cms object. */
99     private CmsObject m_cms;
100     
101     /** the error message. */
102     private String JavaDoc m_errorMessage;
103
104     /** List of pages found in folder 1 exclusivly. */
105     private List JavaDoc m_folder1Exclusive;
106
107     /** List of pages found in folder 2 exclusivly. */
108     private List JavaDoc m_folder2Exclusive;
109
110     /** List of pages found in both folders but as different types. */
111     private List JavaDoc m_foldersDifferenttypes;
112
113     /** List of pages found in both folders as individual resources. */
114     private List JavaDoc m_foldersEqualnames;
115
116     /** List of pages found in both folders as siblings. */
117     private List JavaDoc m_foldersSibling;
118
119     /** The first folder to merge. */
120     private String JavaDoc m_paramFolder1;
121     
122     /** The second folder to merge. */
123     private String JavaDoc m_paramFolder2;
124     
125     /** the report for the output. */
126     private I_CmsReport m_report;
127
128     /**
129      * Public constructor with JSP action element.<p>
130      *
131      * @param jsp an initialized JSP action element
132      */

133     public CmsMergePages(CmsJspActionElement jsp) {
134
135         super(jsp);
136         m_folder1Exclusive = new ArrayList JavaDoc();
137         m_folder2Exclusive = new ArrayList JavaDoc();
138         m_foldersSibling = new ArrayList JavaDoc();
139         m_foldersEqualnames = new ArrayList JavaDoc();
140         m_foldersDifferenttypes = new ArrayList JavaDoc();
141     }
142
143     /**
144      * Public constructor with JSP variables.<p>
145      *
146      * @param cms the current CmsObject
147      * @param context the JSP page context
148      * @param req the JSP request
149      * @param res the JSP response
150      */

151     public CmsMergePages(CmsObject cms, PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
152
153         this(new CmsJspActionElement(context, req, res));
154         m_cms = cms;
155     }
156
157     /**
158      * Merges the specified resources.<p>
159      *
160      * @param report the cms report
161      */

162     public void actionMerge(I_CmsReport report) {
163
164         m_report = report;
165         m_report.println(Messages.get().container(Messages.RPT_MERGE_PAGES_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
166
167         try {
168             // collect all pages and sort them depending on their state
169
collectResources();
170             // merge all pages that can be merged
171
mergePages();
172             // cleanup
173
cleanup();
174         } catch (CmsException e) {
175             m_report.println(e);
176         }
177
178     }
179
180     /**
181      * Performs the move report, will be called by the JSP page.<p>
182      *
183      * @throws JspException if problems including sub-elements occur
184      */

185     public void actionReport() throws JspException JavaDoc {
186
187         // save initialized instance of this class in request attribute for included sub-elements
188
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
189         switch (getAction()) {
190             case ACTION_REPORT_END:
191                 actionCloseDialog();
192                 break;
193             case ACTION_REPORT_UPDATE:
194                 setParamAction(REPORT_UPDATE);
195                 getJsp().include(FILE_REPORT_OUTPUT);
196                 break;
197             case ACTION_REPORT_BEGIN:
198             case ACTION_CONFIRMED:
199             default:
200                 CmsMergePagesThread thread = new CmsMergePagesThread(getCms(), this);
201                 setParamAction(REPORT_BEGIN);
202                 setParamThread(thread.getUUID().toString());
203                 getJsp().include(FILE_REPORT_OUTPUT);
204                 break;
205         }
206     }
207
208     /**
209      * @see org.opencms.workplace.CmsWorkplace#getCms()
210      */

211     public CmsObject getCms() {
212
213         if (m_cms == null) {
214             return super.getCms();
215         }
216
217         return m_cms;
218     }
219
220     /**
221      * Returns the errorMessage.<p>
222      *
223      * @return the errorMessage
224      */

225     public String JavaDoc getErrorMessage() {
226
227         if (CmsStringUtil.isEmpty(m_errorMessage)) {
228             return "";
229         }
230
231         return m_errorMessage;
232     }
233
234     /**
235      * Returns the first folder.<p>
236      *
237      * @return the folder
238      */

239     public String JavaDoc getParamFolder1() {
240
241         return m_paramFolder1;
242     }
243
244     /**
245      * Returns the second folder.<p>
246      *
247      * @return the folder
248      */

249     public String JavaDoc getParamFolder2() {
250
251         return m_paramFolder2;
252     }
253
254     /**
255      * Sets the errorMessage.<p>
256      *
257      * @param errorMessage the errorMessage to set
258      */

259     public void setErrorMessage(String JavaDoc errorMessage) {
260
261         m_errorMessage = errorMessage;
262     }
263
264     /**
265      * Sets the first folder to merge.<p>
266      *
267      * @param folder1 the first folder name to set
268      */

269     public void setParamFolder1(String JavaDoc folder1) {
270
271         m_paramFolder1 = folder1;
272     }
273
274     /**
275      * Sets the second folder to merge.<p>
276      *
277      * @param folder2 the second folder name to set
278      */

279     public void setParamFolder2(String JavaDoc folder2) {
280
281         m_paramFolder2 = folder2;
282     }
283
284     /**
285      * Does validate the request parameters and returns a buffer with error messages.<p>
286      * @param cms the current cms object
287      * If there were no error messages, the buffer is empty.<p>
288      */

289     public void validateParameters(CmsObject cms) {
290
291         CmsMessages messages = Messages.get().getBundle(getLocale());
292         StringBuffer JavaDoc validationErrors = new StringBuffer JavaDoc();
293         if (CmsStringUtil.isEmpty(getParamFolder1())) {
294             validationErrors.append(messages.key(Messages.GUI_MERGE_PAGES_VALIDATE_FIRST_FOLDER_0)).append("<br>");
295         } else {
296             try {
297                 cms.readResource(getParamFolder1());
298             } catch (CmsException e) {
299                 validationErrors.append(
300                     messages.key(Messages.GUI_MERGE_PAGES_VALIDATE_FIRST_FOLDER_1, getParamFolder1())).append("<br>");
301             }
302         }
303         if (CmsStringUtil.isEmpty(getParamFolder2())) {
304             validationErrors.append(messages.key(Messages.GUI_MERGE_PAGES_VALIDATE_SECOND_FOLDER_0)).append("<br>");
305         } else {
306             try {
307                 cms.readResource(getParamFolder2());
308             } catch (CmsException e) {
309                 validationErrors.append(
310                     messages.key(Messages.GUI_MERGE_PAGES_VALIDATE_SECOND_FOLDER_1, getParamFolder2())).append("<br>");
311             }
312         }
313         if (getParamFolder1().equals(getParamFolder2())) {
314             validationErrors.append(messages.key(Messages.GUI_MERGE_PAGES_VALIDATE_SAME_FOLDER_0)).append("<br>");
315         }
316
317         setErrorMessage(validationErrors.toString());
318     }
319
320     /**
321      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
322      */

323     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
324
325         // fill the parameter values in the get/set methods
326
fillParamValues(request);
327         // set the dialog type
328
setParamDialogtype(DIALOG_TYPE);
329         // set the action for the JSP switch
330
// set the action for the JSP switch
331
if (DIALOG_CONFIRMED.equals(getParamAction())) {
332             setAction(ACTION_CONFIRMED);
333         } else if (DIALOG_OK.equals(getParamAction())) {
334             setAction(ACTION_OK);
335         } else if (DIALOG_CANCEL.equals(getParamAction())) {
336             setAction(ACTION_CANCEL);
337         } else if (REPORT_UPDATE.equals(getParamAction())) {
338             setAction(ACTION_REPORT_UPDATE);
339         } else if (REPORT_BEGIN.equals(getParamAction())) {
340             setAction(ACTION_REPORT_BEGIN);
341         } else if (REPORT_END.equals(getParamAction())) {
342             setAction(ACTION_REPORT_END);
343         } else {
344             setAction(ACTION_DEFAULT);
345             // add the title for the dialog
346
setParamTitle(Messages.get().getBundle(getLocale()).key(Messages.GUI_TITLE_MERGEPAGES_0));
347         }
348     }
349
350     /**
351      * Analyses a page in the source morge folder and tests if a resouce with the same name exists in the target merge folder.<p>
352      *
353      * The method then calcualtes a action for further processing of this page, possible values are:
354      * <ul>
355      * <li>C_FOLDER1_EXCLUSIVE: exclusivly found in folder 1</li>
356      * <li>C_FOLDER2_EXCLUSIVE: exclusivly found in folder 2</li>
357      * <li>C_FOLDERS_SIBLING: found in both folders as siblings of each other </li>
358      * <li>C_FOLDERS_EQUALNAMES: found in both folders as individual resources</li>
359      * <li>C_FOLDERS_DIFFERENTTYPES: found in both folders as different types</li>
360      * </ul>
361      * @param res the resource to test
362      * @param sourceMergeFolder the path to the source merge folder
363      * @param targetMergefolder the path to the target merge folder
364      * @param currentFolder integer value (1 or 2) showing if the source folder is folder 1 or folder 2
365      * @return value of the action to do with this page
366      */

367     private int analyse(CmsResource res, String JavaDoc sourceMergeFolder, String JavaDoc targetMergefolder, int currentFolder) {
368
369         int retValue = -1;
370         String JavaDoc resourcenameOther = getResourceNameInOtherFolder(
371             m_cms.getSitePath(res),
372             sourceMergeFolder,
373             targetMergefolder);
374         try {
375             CmsResource otherRes = m_cms.readResource(resourcenameOther, CmsResourceFilter.IGNORE_EXPIRATION);
376             // there was a resource with the same name in the other merge folder
377
// now check if it is already a sibling of the current resource
378
if (res.getResourceId().equals(otherRes.getResourceId())) {
379                 // it is a sibling, so set the action to "sibling already";
380
retValue = FOLDERS_SIBLING;
381             } else {
382                 // it is no sibling, now test if it has the same resource type than the oringinal resource
383
if (res.getTypeId() == otherRes.getTypeId()) {
384                     // both resources have the same type, so set the action to "same name". Only those resources can be merged
385
retValue = FOLDERS_EQUALNAMES;
386                 } else {
387                     // both resources have different types, so set the action to "different types"
388
retValue = FOLDERS_DIFFERENTTYPES;
389                 }
390             }
391         } catch (CmsException e) {
392             // the resource was not found, so set the action mode to "found only in the source folder"
393
if (currentFolder == 1) {
394                 retValue = FOLDER1_EXCLUSIVE;
395             } else {
396                 retValue = FOLDER2_EXCLUSIVE;
397             }
398         }
399
400         return retValue;
401     }
402
403     /**
404      * Cleanup all internal storages.<p> *
405      */

406     private void cleanup() {
407
408         m_folder1Exclusive = null;
409         m_folder2Exclusive = null;
410         m_foldersSibling = null;
411         m_foldersEqualnames = null;
412         m_foldersDifferenttypes = null;
413     }
414
415     /**
416      * Collect all pages in a folders and sort them depending on the required action to do.<p>
417      *
418      * @param sourceMergeFolder the source merge folder to collect all pages from
419      * @param targetMergefolder the target merge folder to compare to
420      * @param currentFolder integer value (1 or 2) showing if the source folder is folder 1 or folder 2
421      * @throws CmsException if something goes wrong
422      */

423     private void collectFolder(String JavaDoc sourceMergeFolder, String JavaDoc targetMergefolder, int currentFolder)
424     throws CmsException {
425
426         //get the list of all resources in the source merge folder
427
CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(CmsResourceTypeXmlPage.getStaticTypeId());
428         List JavaDoc folderResources = m_cms.readResources(sourceMergeFolder, filter, true);
429         Iterator JavaDoc i = folderResources.iterator();
430         int size = folderResources.size();
431         // now loop through all resources and check them against those in the target merge folder
432
m_report.println(Messages.get().container(
433             Messages.RPT_SCAN_PAGES_IN_FOLDER_BEGIN_2,
434             sourceMergeFolder,
435             new Integer JavaDoc(size)), I_CmsReport.FORMAT_HEADLINE);
436         int count = 1;
437         while (i.hasNext()) {
438             CmsResource res = (CmsResource)i.next();
439             String JavaDoc resName = m_cms.getSitePath(res);
440
441             m_report.print(org.opencms.report.Messages.get().container(
442                 org.opencms.report.Messages.RPT_SUCCESSION_2,
443                 String.valueOf(count++),
444                 String.valueOf(size)), I_CmsReport.FORMAT_NOTE);
445             m_report.println(Messages.get().container(Messages.RPT_PROCESS_1, resName), I_CmsReport.FORMAT_NOTE);
446
447             // now analyse the page and calculate the action to do
448
int action = analyse(res, sourceMergeFolder, targetMergefolder, currentFolder);
449             // add the name of the resource to the correct list
450
switch (action) {
451                 case FOLDER1_EXCLUSIVE:
452                     m_folder1Exclusive.add(resName);
453                     m_report.println(
454                         Messages.get().container(Messages.RPT_FOLDER1_EXCLUSIVE_0),
455                         I_CmsReport.FORMAT_OK);
456                     break;
457                 case FOLDER2_EXCLUSIVE:
458                     m_folder2Exclusive.add(resName);
459                     m_report.println(
460                         Messages.get().container(Messages.RPT_FOLDER2_EXCLUSIVE_0),
461                         I_CmsReport.FORMAT_OK);
462                     break;
463                 case FOLDERS_SIBLING:
464                     if (!m_foldersSibling.contains(getResourceNameInOtherFolder(
465                         resName,
466                         sourceMergeFolder,
467                         targetMergefolder))) {
468                         m_foldersSibling.add(resName);
469                     }
470                     m_report.println(Messages.get().container(Messages.RPT_FOLDERS_SIBLING_0), I_CmsReport.FORMAT_OK);
471                     break;
472                 case FOLDERS_EQUALNAMES:
473                     if (!m_foldersEqualnames.contains(getResourceNameInOtherFolder(
474                         resName,
475                         sourceMergeFolder,
476                         targetMergefolder))) {
477                         m_foldersEqualnames.add(resName);
478                     }
479                     m_report.println(
480                         Messages.get().container(Messages.RPT_FOLDERS_EQUALNAMES_0),
481                         I_CmsReport.FORMAT_OK);
482                     break;
483                 case FOLDERS_DIFFERENTTYPES:
484                     if (!m_foldersDifferenttypes.contains(getResourceNameInOtherFolder(
485                         resName,
486                         sourceMergeFolder,
487                         targetMergefolder))) {
488                         m_foldersDifferenttypes.add(resName);
489                     }
490                     m_report.println(
491                         Messages.get().container(Messages.RPT_FOLDERS_DIFFERENTTYPES_0),
492                         I_CmsReport.FORMAT_OK);
493                     break;
494                 default:
495                     break;
496             }
497             res = null;
498         }
499         folderResources = null;
500         m_report.println(
501             Messages.get().container(Messages.RPT_SCAN_PAGES_IN_FOLDER_END_0),
502             I_CmsReport.FORMAT_HEADLINE);
503
504     }
505
506     /**
507      * Collect all pages in the folders to merge and sort them depending on the required action to do.<p>
508      *
509      * The method will create several lists. Each list contains the resource names of pages
510      * and will be used in further steps of the merging process.
511      * <ul>
512      * <li>List m_folder1Exclusive: contains all pages which are exclusivly found in folder 1</li>
513      * <li>List m_folder2Exclusive: contains all pages which are exclusivly found in folder 2</li>
514      * <li>List m_foldersSibling: contains all pages which can be found in both folders and are siblings of each other </li>
515      * <li>List m_foldersEqualnames: contains all pages which can be found in both folders and are no siblings of each other</li>
516      * <li>List m_foldersDifferenttypes: contains all pages which can be found in both folders but are of different types</li>
517      * </ul>
518      *
519      * @throws CmsException if something goes wrong
520      */

521     private void collectResources() throws CmsException {
522
523         String JavaDoc defaultLocale = CmsLocaleManager.getDefaultLocale().toString();
524         String JavaDoc locale1 = m_cms.readPropertyObject(getParamFolder1(), "locale", true).getValue(defaultLocale);
525         String JavaDoc locale2 = m_cms.readPropertyObject(getParamFolder2(), "locale", true).getValue(defaultLocale);
526         m_report.println(
527             Messages.get().container(Messages.RPT_CREATE_EXTERNAL_LINK_0, getParamFolder1(), locale1),
528             I_CmsReport.FORMAT_NOTE);
529         m_report.println(
530             Messages.get().container(Messages.RPT_CREATE_EXTERNAL_LINK_0, getParamFolder2(), locale2),
531             I_CmsReport.FORMAT_NOTE);
532
533         // collect all resources in folder 1
534
collectFolder(getParamFolder1(), getParamFolder2(), 1);
535         // collect all resources in folder 2
536
collectFolder(getParamFolder2(), getParamFolder1(), 2);
537
538         // report the results of the collection
539
m_report.println(Messages.get().container(Messages.RPT_SCANNING_RESULTS_0), I_CmsReport.FORMAT_HEADLINE);
540
541         m_report.println(Messages.get().container(Messages.RPT_FOLDER1_EXCLUSIVE_0), I_CmsReport.FORMAT_HEADLINE);
542         reportList(m_folder1Exclusive, false);
543         m_report.println(Messages.get().container(Messages.RPT_FOLDER2_EXCLUSIVE_0), I_CmsReport.FORMAT_HEADLINE);
544         reportList(m_folder2Exclusive, false);
545         m_report.println(Messages.get().container(Messages.RPT_FOLDERS_SIBLING_0), I_CmsReport.FORMAT_HEADLINE);
546         reportList(m_foldersSibling, false);
547         m_report.println(Messages.get().container(Messages.RPT_FOLDERS_EQUALNAMES_0), I_CmsReport.FORMAT_HEADLINE);
548         reportList(m_foldersEqualnames, true);
549         m_report.println(Messages.get().container(Messages.RPT_FOLDERS_DIFFERENTTYPES_0), I_CmsReport.FORMAT_HEADLINE);
550         reportList(m_foldersDifferenttypes, false);
551
552     }
553
554     /**
555      * Gets the name of a resource in the other merge folder.<p>
556      *
557      * @param resName the complete path of a resource
558      * @param sourceMergeFolder the path to the source merge folder
559      * @param targetMergefolder the path to the target merge folder
560      * @return the name of a resource in the other merge folder
561      */

562     private String JavaDoc getResourceNameInOtherFolder(String JavaDoc resName, String JavaDoc sourceMergeFolder, String JavaDoc targetMergefolder) {
563
564         // get the resourcename of the resouce to test without the source merge folder
565
String JavaDoc resourcename = resName.substring(sourceMergeFolder.length());
566         // get the complete path of the resource in the other merge folder
567
return targetMergefolder + resourcename;
568     }
569
570     /**
571      * Merges those pages in the two merge folders that have the same name and are no siblings of each other yet.<p>
572      * @throws CmsException if something goes wrong
573      */

574     private void mergePages() throws CmsException {
575
576         int size = m_foldersEqualnames.size();
577         if (size > 0) {
578
579             m_report.println(
580                 Messages.get().container(Messages.RPT_MERGE_PAGES_BEGIN_1, String.valueOf(size)),
581                 I_CmsReport.FORMAT_HEADLINE);
582             String JavaDoc defaultLocale = CmsLocaleManager.getDefaultLocale().toString();
583             String JavaDoc locale2 = m_cms.readPropertyObject(getParamFolder2(), "locale", true).getValue(defaultLocale);
584
585             // lock the source and the target folder
586
m_report.print(Messages.get().container(Messages.RPT_LOCK_FOLDER_0), I_CmsReport.FORMAT_NOTE);
587             m_report.print(org.opencms.report.Messages.get().container(
588                 org.opencms.report.Messages.RPT_ARGUMENT_1,
589                 getParamFolder1()));
590             m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
591             m_cms.lockResource(getParamFolder1());
592             m_report.println(
593                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
594                 I_CmsReport.FORMAT_OK);
595
596             m_report.print(Messages.get().container(Messages.RPT_LOCK_FOLDER_0), I_CmsReport.FORMAT_NOTE);
597             m_report.print(org.opencms.report.Messages.get().container(
598                 org.opencms.report.Messages.RPT_ARGUMENT_1,
599                 getParamFolder2()));
600             m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
601             m_cms.lockResource(getParamFolder2());
602             m_report.println(
603                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
604                 I_CmsReport.FORMAT_OK);
605
606             // now loop through all collected resources
607
int count = 1;
608             Iterator JavaDoc i = m_foldersEqualnames.iterator();
609             while (i.hasNext()) {
610                 String JavaDoc resFolder1Name = (String JavaDoc)i.next();
611                 try {
612                     String JavaDoc resFolder2Name = getResourceNameInOtherFolder(
613                         resFolder1Name,
614                         getParamFolder1(),
615                         getParamFolder2());
616                     m_report.print(org.opencms.report.Messages.get().container(
617                         org.opencms.report.Messages.RPT_SUCCESSION_2,
618                         String.valueOf(count++),
619                         String.valueOf(size)), I_CmsReport.FORMAT_NOTE);
620                     m_report.print(Messages.get().container(Messages.RPT_PROCESS_0), I_CmsReport.FORMAT_NOTE);
621                     m_report.print(org.opencms.report.Messages.get().container(
622                         org.opencms.report.Messages.RPT_ARGUMENT_1,
623                         resFolder1Name));
624                     m_report.print(Messages.get().container(Messages.RPT_DOUBLE_ARROW_0), I_CmsReport.FORMAT_NOTE);
625                     m_report.print(org.opencms.report.Messages.get().container(
626                         org.opencms.report.Messages.RPT_ARGUMENT_1,
627                         resFolder2Name));
628                     m_report.println(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
629
630                     // get the content of the resource in folder1
631
String JavaDoc locale = m_cms.readPropertyObject(resFolder1Name, "locale", true).getValue(defaultLocale);
632                     m_report.print(
633                         Messages.get().container(Messages.RPT_READ_CONTENT_2, resFolder1Name, locale),
634                         I_CmsReport.FORMAT_NOTE);
635                     CmsResource resFolder1 = m_cms.readResource(resFolder1Name, CmsResourceFilter.IGNORE_EXPIRATION);
636                     CmsFile fileFolder1 = CmsFile.upgrade(resFolder1, m_cms);
637                     CmsXmlPage pageFolder1 = CmsXmlPageFactory.unmarshal(m_cms, fileFolder1);
638                     m_report.println(
639                         org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
640                         I_CmsReport.FORMAT_OK);
641
642                     // get the content of the resource in folder2
643
locale = m_cms.readPropertyObject(resFolder2Name, "locale", true).getValue(defaultLocale);
644                     m_report.print(
645                         Messages.get().container(Messages.RPT_READ_CONTENT_2, resFolder2Name, locale),
646                         I_CmsReport.FORMAT_NOTE);
647                     CmsResource resFolder2 = m_cms.readResource(resFolder2Name, CmsResourceFilter.IGNORE_EXPIRATION);
648                     CmsFile fileFolder2 = CmsFile.upgrade(resFolder2, m_cms);
649                     CmsXmlPage pageFolder2 = CmsXmlPageFactory.unmarshal(m_cms, fileFolder2);
650                     m_report.println(
651                         org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
652                         I_CmsReport.FORMAT_OK);
653
654                     // now get all the text elements from the resource in folder 2 which match the the locale of folder 2
655
Locale JavaDoc loc = new Locale JavaDoc(locale2);
656                     List JavaDoc textElements2 = pageFolder2.getNames(loc);
657                     Iterator JavaDoc j = textElements2.iterator();
658                     while (j.hasNext()) {
659                         String JavaDoc textElementName = (String JavaDoc)j.next();
660                         m_report.print(
661                             Messages.get().container(Messages.RPT_PROCESS_TEXT_ELEM_1, textElementName),
662                             I_CmsReport.FORMAT_NOTE);
663                         // get the text element from the resource in folder 2...
664
String JavaDoc textElement = pageFolder2.getValue(textElementName, loc).getStringValue(m_cms);
665                         // and set it in the resource in folder 1...
666
// WARNING: An existing content will be overwritten!
667
if (!pageFolder1.hasValue(textElementName, loc)) {
668                             pageFolder1.addValue(textElementName, loc);
669                         }
670                         pageFolder1.setStringValue(m_cms, textElementName, loc, textElement);
671                         m_report.println(org.opencms.report.Messages.get().container(
672                             org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK);
673                     }
674                     // the resource in folder 1 now has all text elements in both locales, so update it in the vfs
675

676                     m_report.print(
677                         Messages.get().container(Messages.RPT_WRITE_CONTENT_1, resFolder1Name),
678                         I_CmsReport.FORMAT_NOTE);
679                     fileFolder1.setContents(pageFolder1.marshal());
680                     m_cms.writeFile(fileFolder1);
681                     m_report.println(
682                         org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
683                         I_CmsReport.FORMAT_OK);
684
685                     // save all properties from the resource in folder2
686
m_report.print(
687                         Messages.get().container(Messages.RPT_READ_PROPERTIES_1, resFolder2Name),
688                         I_CmsReport.FORMAT_NOTE);
689                     List JavaDoc properties = m_cms.readPropertyObjects(resFolder2Name, false);
690                     m_report.println(
691                         org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
692                         I_CmsReport.FORMAT_OK);
693
694                     // the next thing to do is to delete the old resource in folder 2
695
m_report.print(
696                         Messages.get().container(Messages.RPT_DELETE_PAGE_1, resFolder2Name),
697                         I_CmsReport.FORMAT_NOTE);
698                     m_cms.deleteResource(resFolder2Name, CmsResource.DELETE_PRESERVE_SIBLINGS);
699                     m_report.println(
700                         org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
701                         I_CmsReport.FORMAT_OK);
702
703                     // copy a sibling of the resource from folder 1 to folder 2
704
m_report.print(
705                         Messages.get().container(Messages.RPT_COPY_2, resFolder1Name, resFolder2Name),
706                         I_CmsReport.FORMAT_NOTE);
707                     m_cms.copyResource(resFolder1Name, resFolder2Name, CmsResource.COPY_AS_SIBLING);
708                     m_report.println(
709                         org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
710                         I_CmsReport.FORMAT_OK);
711
712                     // restore the properties at the sibling in folder 2
713
m_report.print(
714                         Messages.get().container(Messages.RPT_RESORE_PROPERTIES_1, resFolder2Name),
715                         I_CmsReport.FORMAT_NOTE);
716                     m_cms.writePropertyObjects(resFolder2Name, properties);
717                     m_report.println(
718                         org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
719                         I_CmsReport.FORMAT_OK);
720
721                     resFolder1 = null;
722                     resFolder2 = null;
723                     fileFolder1 = null;
724                     fileFolder2 = null;
725                     pageFolder1 = null;
726                     pageFolder2 = null;
727
728                 } catch (CmsException e) {
729                     m_report.println(e);
730                 }
731
732             }
733             // lock the source and the target folder
734
m_report.print(
735                 Messages.get().container(Messages.RPT_UNLOCK_1, getParamFolder1()),
736                 I_CmsReport.FORMAT_NOTE);
737             m_cms.unlockResource(getParamFolder1());
738             m_report.println(
739                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
740                 I_CmsReport.FORMAT_OK);
741
742             m_report.print(
743                 Messages.get().container(Messages.RPT_UNLOCK_1, getParamFolder2()),
744                 I_CmsReport.FORMAT_NOTE);
745             m_cms.unlockResource(getParamFolder2());
746             m_report.println(
747                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
748                 I_CmsReport.FORMAT_OK);
749
750             m_report.println(Messages.get().container(Messages.RPT_MERGE_PAGES_END_0), I_CmsReport.FORMAT_HEADLINE);
751         }
752     }
753
754     /**
755      * Creates a report list of all resources in one of the collected lists.<p>
756      *
757      * @param collected the list to create the output from
758      * @param doReport flag to enable detailed report
759      */

760     private void reportList(List JavaDoc collected, boolean doReport) {
761
762         int size = collected.size();
763         // now loop through all collected resources
764
m_report.println(
765             Messages.get().container(Messages.RPT_NUM_PAGES_1, new Integer JavaDoc(size)),
766             I_CmsReport.FORMAT_HEADLINE);
767         if (doReport) {
768             int count = 1;
769
770             Iterator JavaDoc i = collected.iterator();
771             while (i.hasNext()) {
772                 String JavaDoc resName = (String JavaDoc)i.next();
773                 m_report.print(org.opencms.report.Messages.get().container(
774                     org.opencms.report.Messages.RPT_SUCCESSION_2,
775                     String.valueOf(count++),
776                     String.valueOf(size)), I_CmsReport.FORMAT_NOTE);
777                 m_report.println(Messages.get().container(Messages.RPT_PROCESS_1, resName), I_CmsReport.FORMAT_NOTE);
778             }
779         }
780         m_report.println(Messages.get().container(Messages.RPT_MERGE_PAGES_END_0), I_CmsReport.FORMAT_HEADLINE);
781     }
782 }
Popular Tags