001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/CmsMergePages.java,v $
003: * Date : $Date: 2008-02-27 12:05:37 $
004: * Version: $Revision: 1.19 $
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.workplace.tools.content;
033:
034: import org.opencms.file.CmsFile;
035: import org.opencms.file.CmsObject;
036: import org.opencms.file.CmsResource;
037: import org.opencms.file.CmsResourceFilter;
038: import org.opencms.file.types.CmsResourceTypeXmlPage;
039: import org.opencms.i18n.CmsLocaleManager;
040: import org.opencms.i18n.CmsMessages;
041: import org.opencms.jsp.CmsJspActionElement;
042: import org.opencms.main.CmsException;
043: import org.opencms.report.I_CmsReport;
044: import org.opencms.util.CmsStringUtil;
045: import org.opencms.workplace.CmsReport;
046: import org.opencms.workplace.CmsWorkplaceSettings;
047: import org.opencms.xml.page.CmsXmlPage;
048: import org.opencms.xml.page.CmsXmlPageFactory;
049:
050: import java.util.ArrayList;
051: import java.util.Iterator;
052: import java.util.List;
053: import java.util.Locale;
054:
055: import javax.servlet.http.HttpServletRequest;
056: import javax.servlet.http.HttpServletResponse;
057: import javax.servlet.jsp.JspException;
058: import javax.servlet.jsp.PageContext;
059:
060: /**
061: * Provides methods for the merge pages dialog.<p>
062: *
063: * @author Michael Emmerich
064: *
065: * @version $Revision: 1.19 $
066: *
067: * @since 6.0.0
068: */
069: public class CmsMergePages extends CmsReport {
070:
071: /** A constant representing the select option all templates. */
072: public static final String ALL = "ALL";
073:
074: /** Key for pages found in folder 1 exclusivly. */
075: public static final int FOLDER1_EXCLUSIVE = 0;
076:
077: /** Key for pages found in folder 2 exclusivly. */
078: public static final int FOLDER2_EXCLUSIVE = 1;
079:
080: /** Key for pages found in both folders but as different types. */
081: public static final int FOLDERS_DIFFERENTTYPES = 4;
082:
083: /** Key for pages found in both folders as individual resources. */
084: public static final int FOLDERS_EQUALNAMES = 3;
085:
086: /** Key for pages found in both folders as siblings. */
087: public static final int FOLDERS_SIBLING = 2;
088:
089: /** The dialog type. */
090: public static final String DIALOG_TYPE = "mergepages";
091:
092: /** Request parameter name for the first folder to merge. */
093: public static final String PARAM_FOLDER1 = "folder1";
094:
095: /** Request parameter name for the second folder to merge. */
096: public static final String PARAM_FOLDER2 = "folder2";
097:
098: /** the cms object. */
099: private CmsObject m_cms;
100:
101: /** the error message. */
102: private String m_errorMessage;
103:
104: /** List of pages found in folder 1 exclusivly. */
105: private List m_folder1Exclusive;
106:
107: /** List of pages found in folder 2 exclusivly. */
108: private List m_folder2Exclusive;
109:
110: /** List of pages found in both folders but as different types. */
111: private List m_foldersDifferenttypes;
112:
113: /** List of pages found in both folders as individual resources. */
114: private List m_foldersEqualnames;
115:
116: /** List of pages found in both folders as siblings. */
117: private List m_foldersSibling;
118:
119: /** The first folder to merge. */
120: private String m_paramFolder1;
121:
122: /** The second folder to merge. */
123: private String 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();
137: m_folder2Exclusive = new ArrayList();
138: m_foldersSibling = new ArrayList();
139: m_foldersEqualnames = new ArrayList();
140: m_foldersDifferenttypes = new ArrayList();
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 context,
152: HttpServletRequest req, HttpServletResponse res) {
153:
154: this (new CmsJspActionElement(context, req, res));
155: m_cms = cms;
156: }
157:
158: /**
159: * Merges the specified resources.<p>
160: *
161: * @param report the cms report
162: */
163: public void actionMerge(I_CmsReport report) {
164:
165: m_report = report;
166: m_report.println(Messages.get().container(
167: Messages.RPT_MERGE_PAGES_BEGIN_0),
168: I_CmsReport.FORMAT_HEADLINE);
169:
170: try {
171: // collect all pages and sort them depending on their state
172: collectResources();
173: // merge all pages that can be merged
174: mergePages();
175: // cleanup
176: cleanup();
177: } catch (CmsException e) {
178: m_report.println(e);
179: }
180:
181: }
182:
183: /**
184: * Performs the move report, will be called by the JSP page.<p>
185: *
186: * @throws JspException if problems including sub-elements occur
187: */
188: public void actionReport() throws JspException {
189:
190: // save initialized instance of this class in request attribute for included sub-elements
191: getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS,
192: this );
193: switch (getAction()) {
194: case ACTION_REPORT_END:
195: actionCloseDialog();
196: break;
197: case ACTION_REPORT_UPDATE:
198: setParamAction(REPORT_UPDATE);
199: getJsp().include(FILE_REPORT_OUTPUT);
200: break;
201: case ACTION_REPORT_BEGIN:
202: case ACTION_CONFIRMED:
203: default:
204: CmsMergePagesThread thread = new CmsMergePagesThread(
205: getCms(), this );
206: thread.start();
207: setParamAction(REPORT_BEGIN);
208: setParamThread(thread.getUUID().toString());
209: getJsp().include(FILE_REPORT_OUTPUT);
210: break;
211: }
212: }
213:
214: /**
215: * @see org.opencms.workplace.CmsWorkplace#getCms()
216: */
217: public CmsObject getCms() {
218:
219: if (m_cms == null) {
220: return super .getCms();
221: }
222:
223: return m_cms;
224: }
225:
226: /**
227: * Returns the errorMessage.<p>
228: *
229: * @return the errorMessage
230: */
231: public String getErrorMessage() {
232:
233: if (CmsStringUtil.isEmpty(m_errorMessage)) {
234: return "";
235: }
236:
237: return m_errorMessage;
238: }
239:
240: /**
241: * Returns the first folder.<p>
242: *
243: * @return the folder
244: */
245: public String getParamFolder1() {
246:
247: return m_paramFolder1;
248: }
249:
250: /**
251: * Returns the second folder.<p>
252: *
253: * @return the folder
254: */
255: public String getParamFolder2() {
256:
257: return m_paramFolder2;
258: }
259:
260: /**
261: * Sets the errorMessage.<p>
262: *
263: * @param errorMessage the errorMessage to set
264: */
265: public void setErrorMessage(String errorMessage) {
266:
267: m_errorMessage = errorMessage;
268: }
269:
270: /**
271: * Sets the first folder to merge.<p>
272: *
273: * @param folder1 the first folder name to set
274: */
275: public void setParamFolder1(String folder1) {
276:
277: m_paramFolder1 = folder1;
278: }
279:
280: /**
281: * Sets the second folder to merge.<p>
282: *
283: * @param folder2 the second folder name to set
284: */
285: public void setParamFolder2(String folder2) {
286:
287: m_paramFolder2 = folder2;
288: }
289:
290: /**
291: * Does validate the request parameters and returns a buffer with error messages.<p>
292: * @param cms the current cms object
293: * If there were no error messages, the buffer is empty.<p>
294: */
295: public void validateParameters(CmsObject cms) {
296:
297: CmsMessages messages = Messages.get().getBundle(getLocale());
298: StringBuffer validationErrors = new StringBuffer();
299: if (CmsStringUtil.isEmpty(getParamFolder1())) {
300: validationErrors
301: .append(
302: messages
303: .key(Messages.GUI_MERGE_PAGES_VALIDATE_FIRST_FOLDER_0))
304: .append("<br>");
305: } else {
306: try {
307: cms.readResource(getParamFolder1());
308: } catch (CmsException e) {
309: validationErrors
310: .append(
311: messages
312: .key(
313: Messages.GUI_MERGE_PAGES_VALIDATE_FIRST_FOLDER_1,
314: getParamFolder1()))
315: .append("<br>");
316: }
317: }
318: if (CmsStringUtil.isEmpty(getParamFolder2())) {
319: validationErrors
320: .append(
321: messages
322: .key(Messages.GUI_MERGE_PAGES_VALIDATE_SECOND_FOLDER_0))
323: .append("<br>");
324: } else {
325: try {
326: cms.readResource(getParamFolder2());
327: } catch (CmsException e) {
328: validationErrors
329: .append(
330: messages
331: .key(
332: Messages.GUI_MERGE_PAGES_VALIDATE_SECOND_FOLDER_1,
333: getParamFolder2()))
334: .append("<br>");
335: }
336: }
337: if (getParamFolder1().equals(getParamFolder2())) {
338: validationErrors
339: .append(
340: messages
341: .key(Messages.GUI_MERGE_PAGES_VALIDATE_SAME_FOLDER_0))
342: .append("<br>");
343: }
344:
345: setErrorMessage(validationErrors.toString());
346: }
347:
348: /**
349: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
350: */
351: protected void initWorkplaceRequestValues(
352: CmsWorkplaceSettings settings, HttpServletRequest request) {
353:
354: // fill the parameter values in the get/set methods
355: fillParamValues(request);
356: // set the dialog type
357: setParamDialogtype(DIALOG_TYPE);
358: // set the action for the JSP switch
359: // set the action for the JSP switch
360: if (DIALOG_CONFIRMED.equals(getParamAction())) {
361: setAction(ACTION_CONFIRMED);
362: } else if (DIALOG_OK.equals(getParamAction())) {
363: setAction(ACTION_OK);
364: } else if (DIALOG_CANCEL.equals(getParamAction())) {
365: setAction(ACTION_CANCEL);
366: } else if (REPORT_UPDATE.equals(getParamAction())) {
367: setAction(ACTION_REPORT_UPDATE);
368: } else if (REPORT_BEGIN.equals(getParamAction())) {
369: setAction(ACTION_REPORT_BEGIN);
370: } else if (REPORT_END.equals(getParamAction())) {
371: setAction(ACTION_REPORT_END);
372: } else {
373: setAction(ACTION_DEFAULT);
374: // add the title for the dialog
375: setParamTitle(Messages.get().getBundle(getLocale()).key(
376: Messages.GUI_TITLE_MERGEPAGES_0));
377: }
378: }
379:
380: /**
381: * Analyses a page in the source morge folder and tests if a resouce with the same name exists in the target merge folder.<p>
382: *
383: * The method then calcualtes a action for further processing of this page, possible values are:
384: * <ul>
385: * <li>C_FOLDER1_EXCLUSIVE: exclusivly found in folder 1</li>
386: * <li>C_FOLDER2_EXCLUSIVE: exclusivly found in folder 2</li>
387: * <li>C_FOLDERS_SIBLING: found in both folders as siblings of each other </li>
388: * <li>C_FOLDERS_EQUALNAMES: found in both folders as individual resources</li>
389: * <li>C_FOLDERS_DIFFERENTTYPES: found in both folders as different types</li>
390: * </ul>
391: * @param res the resource to test
392: * @param sourceMergeFolder the path to the source merge folder
393: * @param targetMergefolder the path to the target merge folder
394: * @param currentFolder integer value (1 or 2) showing if the source folder is folder 1 or folder 2
395: * @return value of the action to do with this page
396: */
397: private int analyse(CmsResource res, String sourceMergeFolder,
398: String targetMergefolder, int currentFolder) {
399:
400: int retValue = -1;
401: String resourcenameOther = getResourceNameInOtherFolder(m_cms
402: .getSitePath(res), sourceMergeFolder, targetMergefolder);
403: try {
404: CmsResource otherRes = m_cms.readResource(
405: resourcenameOther,
406: CmsResourceFilter.IGNORE_EXPIRATION);
407: // there was a resource with the same name in the other merge folder
408: // now check if it is already a sibling of the current resource
409: if (res.getResourceId().equals(otherRes.getResourceId())) {
410: // it is a sibling, so set the action to "sibling already";
411: retValue = FOLDERS_SIBLING;
412: } else {
413: // it is no sibling, now test if it has the same resource type than the oringinal resource
414: if (res.getTypeId() == otherRes.getTypeId()) {
415: // both resources have the same type, so set the action to "same name". Only those resources can be merged
416: retValue = FOLDERS_EQUALNAMES;
417: } else {
418: // both resources have different types, so set the action to "different types"
419: retValue = FOLDERS_DIFFERENTTYPES;
420: }
421: }
422: } catch (CmsException e) {
423: // the resource was not found, so set the action mode to "found only in the source folder"
424: if (currentFolder == 1) {
425: retValue = FOLDER1_EXCLUSIVE;
426: } else {
427: retValue = FOLDER2_EXCLUSIVE;
428: }
429: }
430:
431: return retValue;
432: }
433:
434: /**
435: * Cleanup all internal storages.<p> *
436: */
437: private void cleanup() {
438:
439: m_folder1Exclusive = null;
440: m_folder2Exclusive = null;
441: m_foldersSibling = null;
442: m_foldersEqualnames = null;
443: m_foldersDifferenttypes = null;
444: }
445:
446: /**
447: * Collect all pages in a folders and sort them depending on the required action to do.<p>
448: *
449: * @param sourceMergeFolder the source merge folder to collect all pages from
450: * @param targetMergefolder the target merge folder to compare to
451: * @param currentFolder integer value (1 or 2) showing if the source folder is folder 1 or folder 2
452: * @throws CmsException if something goes wrong
453: */
454: private void collectFolder(String sourceMergeFolder,
455: String targetMergefolder, int currentFolder)
456: throws CmsException {
457:
458: //get the list of all resources in the source merge folder
459: CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION
460: .addRequireType(CmsResourceTypeXmlPage
461: .getStaticTypeId());
462: List folderResources = m_cms.readResources(sourceMergeFolder,
463: filter, true);
464: Iterator i = folderResources.iterator();
465: int size = folderResources.size();
466: // now loop through all resources and check them against those in the target merge folder
467: m_report.println(Messages.get().container(
468: Messages.RPT_SCAN_PAGES_IN_FOLDER_BEGIN_2,
469: sourceMergeFolder, new Integer(size)),
470: I_CmsReport.FORMAT_HEADLINE);
471: int count = 1;
472: while (i.hasNext()) {
473: CmsResource res = (CmsResource) i.next();
474: String resName = m_cms.getSitePath(res);
475:
476: m_report.print(org.opencms.report.Messages.get().container(
477: org.opencms.report.Messages.RPT_SUCCESSION_2,
478: String.valueOf(count++), String.valueOf(size)),
479: I_CmsReport.FORMAT_NOTE);
480: m_report.println(Messages.get().container(
481: Messages.RPT_PROCESS_1, resName),
482: I_CmsReport.FORMAT_NOTE);
483:
484: // now analyse the page and calculate the action to do
485: int action = analyse(res, sourceMergeFolder,
486: targetMergefolder, currentFolder);
487: // add the name of the resource to the correct list
488: switch (action) {
489: case FOLDER1_EXCLUSIVE:
490: m_folder1Exclusive.add(resName);
491: m_report.println(Messages.get().container(
492: Messages.RPT_FOLDER1_EXCLUSIVE_0),
493: I_CmsReport.FORMAT_OK);
494: break;
495: case FOLDER2_EXCLUSIVE:
496: m_folder2Exclusive.add(resName);
497: m_report.println(Messages.get().container(
498: Messages.RPT_FOLDER2_EXCLUSIVE_0),
499: I_CmsReport.FORMAT_OK);
500: break;
501: case FOLDERS_SIBLING:
502: if (!m_foldersSibling
503: .contains(getResourceNameInOtherFolder(resName,
504: sourceMergeFolder, targetMergefolder))) {
505: m_foldersSibling.add(resName);
506: }
507: m_report.println(Messages.get().container(
508: Messages.RPT_FOLDERS_SIBLING_0),
509: I_CmsReport.FORMAT_OK);
510: break;
511: case FOLDERS_EQUALNAMES:
512: if (!m_foldersEqualnames
513: .contains(getResourceNameInOtherFolder(resName,
514: sourceMergeFolder, targetMergefolder))) {
515: m_foldersEqualnames.add(resName);
516: }
517: m_report.println(Messages.get().container(
518: Messages.RPT_FOLDERS_EQUALNAMES_0),
519: I_CmsReport.FORMAT_OK);
520: break;
521: case FOLDERS_DIFFERENTTYPES:
522: if (!m_foldersDifferenttypes
523: .contains(getResourceNameInOtherFolder(resName,
524: sourceMergeFolder, targetMergefolder))) {
525: m_foldersDifferenttypes.add(resName);
526: }
527: m_report.println(Messages.get().container(
528: Messages.RPT_FOLDERS_DIFFERENTTYPES_0),
529: I_CmsReport.FORMAT_OK);
530: break;
531: default:
532: break;
533: }
534: res = null;
535: }
536: folderResources = null;
537: m_report.println(Messages.get().container(
538: Messages.RPT_SCAN_PAGES_IN_FOLDER_END_0),
539: I_CmsReport.FORMAT_HEADLINE);
540:
541: }
542:
543: /**
544: * Collect all pages in the folders to merge and sort them depending on the required action to do.<p>
545: *
546: * The method will create several lists. Each list contains the resource names of pages
547: * and will be used in further steps of the merging process.
548: * <ul>
549: * <li>List m_folder1Exclusive: contains all pages which are exclusivly found in folder 1</li>
550: * <li>List m_folder2Exclusive: contains all pages which are exclusivly found in folder 2</li>
551: * <li>List m_foldersSibling: contains all pages which can be found in both folders and are siblings of each other </li>
552: * <li>List m_foldersEqualnames: contains all pages which can be found in both folders and are no siblings of each other</li>
553: * <li>List m_foldersDifferenttypes: contains all pages which can be found in both folders but are of different types</li>
554: * </ul>
555: *
556: * @throws CmsException if something goes wrong
557: */
558: private void collectResources() throws CmsException {
559:
560: String defaultLocale = CmsLocaleManager.getDefaultLocale()
561: .toString();
562: String locale1 = m_cms.readPropertyObject(getParamFolder1(),
563: "locale", true).getValue(defaultLocale);
564: String locale2 = m_cms.readPropertyObject(getParamFolder2(),
565: "locale", true).getValue(defaultLocale);
566: m_report.println(Messages.get().container(
567: Messages.RPT_CREATE_EXTERNAL_LINK_0, getParamFolder1(),
568: locale1), I_CmsReport.FORMAT_NOTE);
569: m_report.println(Messages.get().container(
570: Messages.RPT_CREATE_EXTERNAL_LINK_0, getParamFolder2(),
571: locale2), I_CmsReport.FORMAT_NOTE);
572:
573: // collect all resources in folder 1
574: collectFolder(getParamFolder1(), getParamFolder2(), 1);
575: // collect all resources in folder 2
576: collectFolder(getParamFolder2(), getParamFolder1(), 2);
577:
578: // report the results of the collection
579: m_report.println(Messages.get().container(
580: Messages.RPT_SCANNING_RESULTS_0),
581: I_CmsReport.FORMAT_HEADLINE);
582:
583: m_report.println(Messages.get().container(
584: Messages.RPT_FOLDER1_EXCLUSIVE_0),
585: I_CmsReport.FORMAT_HEADLINE);
586: reportList(m_folder1Exclusive, false);
587: m_report.println(Messages.get().container(
588: Messages.RPT_FOLDER2_EXCLUSIVE_0),
589: I_CmsReport.FORMAT_HEADLINE);
590: reportList(m_folder2Exclusive, false);
591: m_report.println(Messages.get().container(
592: Messages.RPT_FOLDERS_SIBLING_0),
593: I_CmsReport.FORMAT_HEADLINE);
594: reportList(m_foldersSibling, false);
595: m_report.println(Messages.get().container(
596: Messages.RPT_FOLDERS_EQUALNAMES_0),
597: I_CmsReport.FORMAT_HEADLINE);
598: reportList(m_foldersEqualnames, true);
599: m_report.println(Messages.get().container(
600: Messages.RPT_FOLDERS_DIFFERENTTYPES_0),
601: I_CmsReport.FORMAT_HEADLINE);
602: reportList(m_foldersDifferenttypes, false);
603:
604: }
605:
606: /**
607: * Gets the name of a resource in the other merge folder.<p>
608: *
609: * @param resName the complete path of a resource
610: * @param sourceMergeFolder the path to the source merge folder
611: * @param targetMergefolder the path to the target merge folder
612: * @return the name of a resource in the other merge folder
613: */
614: private String getResourceNameInOtherFolder(String resName,
615: String sourceMergeFolder, String targetMergefolder) {
616:
617: // get the resourcename of the resouce to test without the source merge folder
618: String resourcename = resName.substring(sourceMergeFolder
619: .length());
620: // get the complete path of the resource in the other merge folder
621: return targetMergefolder + resourcename;
622: }
623:
624: /**
625: * Merges those pages in the two merge folders that have the same name and are no siblings of each other yet.<p>
626: * @throws CmsException if something goes wrong
627: */
628: private void mergePages() throws CmsException {
629:
630: int size = m_foldersEqualnames.size();
631: if (size > 0) {
632:
633: m_report.println(Messages.get().container(
634: Messages.RPT_MERGE_PAGES_BEGIN_1,
635: String.valueOf(size)), I_CmsReport.FORMAT_HEADLINE);
636: String defaultLocale = CmsLocaleManager.getDefaultLocale()
637: .toString();
638: String locale2 = m_cms.readPropertyObject(
639: getParamFolder2(), "locale", true).getValue(
640: defaultLocale);
641:
642: // lock the source and the target folder
643: m_report.print(Messages.get().container(
644: Messages.RPT_LOCK_FOLDER_0),
645: I_CmsReport.FORMAT_NOTE);
646: m_report.print(org.opencms.report.Messages.get().container(
647: org.opencms.report.Messages.RPT_ARGUMENT_1,
648: getParamFolder1()));
649: m_report.print(org.opencms.report.Messages.get().container(
650: org.opencms.report.Messages.RPT_DOTS_0));
651: m_cms.lockResource(getParamFolder1());
652: m_report.println(org.opencms.report.Messages.get()
653: .container(org.opencms.report.Messages.RPT_OK_0),
654: I_CmsReport.FORMAT_OK);
655:
656: m_report.print(Messages.get().container(
657: Messages.RPT_LOCK_FOLDER_0),
658: I_CmsReport.FORMAT_NOTE);
659: m_report.print(org.opencms.report.Messages.get().container(
660: org.opencms.report.Messages.RPT_ARGUMENT_1,
661: getParamFolder2()));
662: m_report.print(org.opencms.report.Messages.get().container(
663: org.opencms.report.Messages.RPT_DOTS_0));
664: m_cms.lockResource(getParamFolder2());
665: m_report.println(org.opencms.report.Messages.get()
666: .container(org.opencms.report.Messages.RPT_OK_0),
667: I_CmsReport.FORMAT_OK);
668:
669: // now loop through all collected resources
670: int count = 1;
671: Iterator i = m_foldersEqualnames.iterator();
672: while (i.hasNext()) {
673: String resFolder1Name = (String) i.next();
674: try {
675: String resFolder2Name = getResourceNameInOtherFolder(
676: resFolder1Name, getParamFolder1(),
677: getParamFolder2());
678: m_report
679: .print(
680: org.opencms.report.Messages
681: .get()
682: .container(
683: org.opencms.report.Messages.RPT_SUCCESSION_2,
684: String
685: .valueOf(count++),
686: String
687: .valueOf(size)),
688: I_CmsReport.FORMAT_NOTE);
689: m_report.print(Messages.get().container(
690: Messages.RPT_PROCESS_0),
691: I_CmsReport.FORMAT_NOTE);
692: m_report
693: .print(org.opencms.report.Messages
694: .get()
695: .container(
696: org.opencms.report.Messages.RPT_ARGUMENT_1,
697: resFolder1Name));
698: m_report.print(Messages.get().container(
699: Messages.RPT_DOUBLE_ARROW_0),
700: I_CmsReport.FORMAT_NOTE);
701: m_report
702: .print(org.opencms.report.Messages
703: .get()
704: .container(
705: org.opencms.report.Messages.RPT_ARGUMENT_1,
706: resFolder2Name));
707: m_report
708: .println(org.opencms.report.Messages
709: .get()
710: .container(
711: org.opencms.report.Messages.RPT_DOTS_0));
712:
713: // get the content of the resource in folder1
714: String locale = m_cms.readPropertyObject(
715: resFolder1Name, "locale", true).getValue(
716: defaultLocale);
717: m_report.print(Messages.get().container(
718: Messages.RPT_READ_CONTENT_2,
719: resFolder1Name, locale),
720: I_CmsReport.FORMAT_NOTE);
721: CmsResource resFolder1 = m_cms.readResource(
722: resFolder1Name,
723: CmsResourceFilter.IGNORE_EXPIRATION);
724: CmsFile fileFolder1 = m_cms.readFile(resFolder1);
725: CmsXmlPage pageFolder1 = CmsXmlPageFactory
726: .unmarshal(m_cms, fileFolder1);
727: m_report
728: .println(
729: org.opencms.report.Messages
730: .get()
731: .container(
732: org.opencms.report.Messages.RPT_OK_0),
733: I_CmsReport.FORMAT_OK);
734:
735: // get the content of the resource in folder2
736: locale = m_cms.readPropertyObject(resFolder2Name,
737: "locale", true).getValue(defaultLocale);
738: m_report.print(Messages.get().container(
739: Messages.RPT_READ_CONTENT_2,
740: resFolder2Name, locale),
741: I_CmsReport.FORMAT_NOTE);
742: CmsResource resFolder2 = m_cms.readResource(
743: resFolder2Name,
744: CmsResourceFilter.IGNORE_EXPIRATION);
745: CmsFile fileFolder2 = m_cms.readFile(resFolder2);
746: CmsXmlPage pageFolder2 = CmsXmlPageFactory
747: .unmarshal(m_cms, fileFolder2);
748: m_report
749: .println(
750: org.opencms.report.Messages
751: .get()
752: .container(
753: org.opencms.report.Messages.RPT_OK_0),
754: I_CmsReport.FORMAT_OK);
755:
756: // now get all the text elements from the resource in folder 2 which match the the locale of folder 2
757: Locale loc = new Locale(locale2);
758: List textElements2 = pageFolder2.getNames(loc);
759: Iterator j = textElements2.iterator();
760: while (j.hasNext()) {
761: String textElementName = (String) j.next();
762: m_report.print(Messages.get().container(
763: Messages.RPT_PROCESS_TEXT_ELEM_1,
764: textElementName),
765: I_CmsReport.FORMAT_NOTE);
766: // get the text element from the resource in folder 2...
767: String textElement = pageFolder2.getValue(
768: textElementName, loc).getStringValue(
769: m_cms);
770: // and set it in the resource in folder 1...
771: // WARNING: An existing content will be overwritten!
772: if (!pageFolder1.hasValue(textElementName, loc)) {
773: pageFolder1.addValue(textElementName, loc);
774: }
775: pageFolder1.setStringValue(m_cms,
776: textElementName, loc, textElement);
777: m_report
778: .println(
779: org.opencms.report.Messages
780: .get()
781: .container(
782: org.opencms.report.Messages.RPT_OK_0),
783: I_CmsReport.FORMAT_OK);
784: }
785: // the resource in folder 1 now has all text elements in both locales, so update it in the vfs
786:
787: m_report.print(Messages.get().container(
788: Messages.RPT_WRITE_CONTENT_1,
789: resFolder1Name), I_CmsReport.FORMAT_NOTE);
790: fileFolder1.setContents(pageFolder1.marshal());
791: m_cms.writeFile(fileFolder1);
792: m_report
793: .println(
794: org.opencms.report.Messages
795: .get()
796: .container(
797: org.opencms.report.Messages.RPT_OK_0),
798: I_CmsReport.FORMAT_OK);
799:
800: // save all properties from the resource in folder2
801: m_report.print(Messages.get().container(
802: Messages.RPT_READ_PROPERTIES_1,
803: resFolder2Name), I_CmsReport.FORMAT_NOTE);
804: List properties = m_cms.readPropertyObjects(
805: resFolder2Name, false);
806: m_report
807: .println(
808: org.opencms.report.Messages
809: .get()
810: .container(
811: org.opencms.report.Messages.RPT_OK_0),
812: I_CmsReport.FORMAT_OK);
813:
814: // the next thing to do is to delete the old resource in folder 2
815: m_report.print(
816: Messages.get().container(
817: Messages.RPT_DELETE_PAGE_1,
818: resFolder2Name),
819: I_CmsReport.FORMAT_NOTE);
820: m_cms.deleteResource(resFolder2Name,
821: CmsResource.DELETE_PRESERVE_SIBLINGS);
822: m_report
823: .println(
824: org.opencms.report.Messages
825: .get()
826: .container(
827: org.opencms.report.Messages.RPT_OK_0),
828: I_CmsReport.FORMAT_OK);
829:
830: // copy a sibling of the resource from folder 1 to folder 2
831: m_report.print(Messages.get().container(
832: Messages.RPT_COPY_2, resFolder1Name,
833: resFolder2Name), I_CmsReport.FORMAT_NOTE);
834: m_cms.copyResource(resFolder1Name, resFolder2Name,
835: CmsResource.COPY_AS_SIBLING);
836: m_report
837: .println(
838: org.opencms.report.Messages
839: .get()
840: .container(
841: org.opencms.report.Messages.RPT_OK_0),
842: I_CmsReport.FORMAT_OK);
843:
844: // restore the properties at the sibling in folder 2
845: m_report.print(Messages.get().container(
846: Messages.RPT_RESORE_PROPERTIES_1,
847: resFolder2Name), I_CmsReport.FORMAT_NOTE);
848: m_cms.writePropertyObjects(resFolder2Name,
849: properties);
850: m_report
851: .println(
852: org.opencms.report.Messages
853: .get()
854: .container(
855: org.opencms.report.Messages.RPT_OK_0),
856: I_CmsReport.FORMAT_OK);
857:
858: resFolder1 = null;
859: resFolder2 = null;
860: fileFolder1 = null;
861: fileFolder2 = null;
862: pageFolder1 = null;
863: pageFolder2 = null;
864:
865: } catch (CmsException e) {
866: m_report.println(e);
867: }
868:
869: }
870: // lock the source and the target folder
871: m_report.print(Messages.get().container(
872: Messages.RPT_UNLOCK_1, getParamFolder1()),
873: I_CmsReport.FORMAT_NOTE);
874: m_cms.unlockResource(getParamFolder1());
875: m_report.println(org.opencms.report.Messages.get()
876: .container(org.opencms.report.Messages.RPT_OK_0),
877: I_CmsReport.FORMAT_OK);
878:
879: m_report.print(Messages.get().container(
880: Messages.RPT_UNLOCK_1, getParamFolder2()),
881: I_CmsReport.FORMAT_NOTE);
882: m_cms.unlockResource(getParamFolder2());
883: m_report.println(org.opencms.report.Messages.get()
884: .container(org.opencms.report.Messages.RPT_OK_0),
885: I_CmsReport.FORMAT_OK);
886:
887: m_report.println(Messages.get().container(
888: Messages.RPT_MERGE_PAGES_END_0),
889: I_CmsReport.FORMAT_HEADLINE);
890: }
891: }
892:
893: /**
894: * Creates a report list of all resources in one of the collected lists.<p>
895: *
896: * @param collected the list to create the output from
897: * @param doReport flag to enable detailed report
898: */
899: private void reportList(List collected, boolean doReport) {
900:
901: int size = collected.size();
902: // now loop through all collected resources
903: m_report.println(Messages.get().container(
904: Messages.RPT_NUM_PAGES_1, new Integer(size)),
905: I_CmsReport.FORMAT_HEADLINE);
906: if (doReport) {
907: int count = 1;
908:
909: Iterator i = collected.iterator();
910: while (i.hasNext()) {
911: String resName = (String) i.next();
912: m_report
913: .print(
914: org.opencms.report.Messages
915: .get()
916: .container(
917: org.opencms.report.Messages.RPT_SUCCESSION_2,
918: String.valueOf(count++),
919: String.valueOf(size)),
920: I_CmsReport.FORMAT_NOTE);
921: m_report.println(Messages.get().container(
922: Messages.RPT_PROCESS_1, resName),
923: I_CmsReport.FORMAT_NOTE);
924: }
925: }
926: m_report.println(Messages.get().container(
927: Messages.RPT_MERGE_PAGES_END_0),
928: I_CmsReport.FORMAT_HEADLINE);
929: }
930: }
|