001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/CmsChangeElementLocaleThread.java,v $
003: * Date : $Date: 2008-02-27 12:05:37 $
004: * Version: $Revision: 1.6 $
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.CmsPropertyDefinition;
037: import org.opencms.file.CmsResource;
038: import org.opencms.file.CmsResourceFilter;
039: import org.opencms.file.types.CmsResourceTypeXmlPage;
040: import org.opencms.i18n.CmsLocaleManager;
041: import org.opencms.lock.CmsLock;
042: import org.opencms.main.CmsException;
043: import org.opencms.report.A_CmsReportThread;
044: import org.opencms.report.I_CmsReport;
045: import org.opencms.util.CmsStringUtil;
046: import org.opencms.xml.page.CmsXmlPage;
047: import org.opencms.xml.page.CmsXmlPageFactory;
048:
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Locale;
052:
053: /**
054: * Changes the element Locales of resources using the corresponding settings object.<p>
055: *
056: * @author Andreas Zahner
057: *
058: * @version $Revision: 1.6 $
059: *
060: * @since 6.0.1
061: */
062: public class CmsChangeElementLocaleThread extends A_CmsReportThread {
063:
064: private CmsElementChangeLocaleSettings m_settings;
065:
066: /**
067: * Creates a change element Locale Thread.<p>
068: *
069: * @param cms the current cms context
070: * @param settings the settings needed to perform the operation
071: */
072: public CmsChangeElementLocaleThread(CmsObject cms,
073: CmsElementChangeLocaleSettings settings) {
074:
075: super (cms, Messages.get().getBundle().key(
076: Messages.GUI_CHANGEELEMENTLOCALE_THREAD_NAME_0));
077: initHtmlReport(cms.getRequestContext().getLocale());
078: m_settings = settings;
079: }
080:
081: /**
082: * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
083: */
084: public String getReportUpdate() {
085:
086: return getReport().getReportUpdate();
087: }
088:
089: /**
090: * @see java.lang.Runnable#run()
091: */
092: public void run() {
093:
094: getReport().println(
095: Messages.get().container(
096: Messages.RPT_CHANGEELEMENTLOCALE_BEGIN_2,
097: m_settings.getOldLocale(),
098: m_settings.getNewLocale()),
099: I_CmsReport.FORMAT_HEADLINE);
100: try {
101: // change the element locales
102: changeElementLocales();
103: } catch (CmsException e) {
104: getReport().println(e);
105: }
106:
107: // append runtime statistics to report
108: getReport().print(
109: org.opencms.report.Messages.get().container(
110: org.opencms.report.Messages.RPT_STAT_0));
111: getReport()
112: .println(
113: org.opencms.report.Messages
114: .get()
115: .container(
116: org.opencms.report.Messages.RPT_STAT_DURATION_1,
117: getReport().formatRuntime()));
118: getReport().println(
119: Messages.get().container(
120: Messages.RPT_CHANGEELEMENTLOCALE_END_0),
121: I_CmsReport.FORMAT_HEADLINE);
122: }
123:
124: /**
125: * Performs the changing of the element Locales.<p>
126: *
127: * @throws CmsException if reading the list of resources to change fails
128: */
129: private void changeElementLocales() throws CmsException {
130:
131: // create Locale objects to work with
132: Locale oldLocale = CmsLocaleManager.getLocale(m_settings
133: .getOldLocale());
134: Locale newLocale = CmsLocaleManager.getLocale(m_settings
135: .getNewLocale());
136: boolean checkTemplate = CmsStringUtil.isNotEmpty(m_settings
137: .getTemplate());
138:
139: // set the resource filter to filter xml pages
140: CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION
141: .addRequireType(CmsResourceTypeXmlPage
142: .getStaticTypeId());
143: String path = CmsResource.getFolderPath(m_settings
144: .getVfsFolder());
145: // get the list of resources to change
146: List resources = getCms().readResources(path, filter,
147: m_settings.isIncludeSubFolders());
148:
149: // set the report counters
150: int count = 0;
151: int resSize = resources.size();
152:
153: // iterate the resources
154: Iterator i = resources.iterator();
155: while (i.hasNext()) {
156:
157: count++;
158: CmsResource res = (CmsResource) i.next();
159:
160: // generate report output
161: getReport()
162: .print(
163: org.opencms.report.Messages
164: .get()
165: .container(
166: org.opencms.report.Messages.RPT_SUCCESSION_2,
167: String.valueOf(count),
168: String.valueOf(resSize)),
169: I_CmsReport.FORMAT_NOTE);
170: getReport().print(
171: Messages.get().container(
172: Messages.RPT_PROCESSING_PAGE_0),
173: I_CmsReport.FORMAT_NOTE);
174: getReport().print(
175: org.opencms.report.Messages.get().container(
176: org.opencms.report.Messages.RPT_ARGUMENT_1,
177: getCms().getSitePath(res)));
178: getReport().print(
179: org.opencms.report.Messages.get().container(
180: org.opencms.report.Messages.RPT_DOTS_0));
181:
182: try {
183:
184: if (checkTemplate) {
185: // check the template property
186: String template = getCms().readPropertyObject(res,
187: CmsPropertyDefinition.PROPERTY_TEMPLATE,
188: true).getValue("");
189: if (!m_settings.getTemplate().equals(template)) {
190: // template property does not match, report and continue with next resource
191: getReport()
192: .println(
193: Messages
194: .get()
195: .container(
196: Messages.RPT_CHANGEELEMENTLOCALE_TEMPLATE_0),
197: I_CmsReport.FORMAT_NOTE);
198: continue;
199: }
200: }
201:
202: // get the file contents
203: CmsFile file = getCms().readFile(res);
204: // get the page object
205: CmsXmlPage page = CmsXmlPageFactory.unmarshal(getCms(),
206: file);
207: // write the report output
208:
209: if (!page.hasLocale(oldLocale)) {
210: // old Locale not present, report and continue with next resource
211: getReport()
212: .println(
213: Messages
214: .get()
215: .container(
216: Messages.RPT_CHANGEELEMENTLOCALE_OLDLOCALE_1,
217: m_settings
218: .getOldLocale()),
219: I_CmsReport.FORMAT_NOTE);
220: continue;
221: }
222:
223: if (page.hasLocale(newLocale)) {
224: // target Locale present, report and continue with next resource
225: getReport()
226: .println(
227: Messages
228: .get()
229: .container(
230: Messages.RPT_CHANGEELEMENTLOCALE_NEWLOCALE_1,
231: m_settings
232: .getNewLocale()),
233: I_CmsReport.FORMAT_NOTE);
234: continue;
235: }
236:
237: // change the Locale of the elements
238: page.moveLocale(oldLocale, newLocale);
239:
240: // set the file contents
241: file.setContents(page.marshal());
242:
243: // check the lock state of the file to write
244: CmsLock lock = getCms().getLock(res);
245: boolean isLocked = false;
246: boolean canWrite = false;
247: if (lock.isNullLock()) {
248: // file not locked, lock it
249: getCms().lockResource(getCms().getSitePath(res));
250: isLocked = true;
251: canWrite = true;
252: } else if (lock.isOwnedBy(getCms().getRequestContext()
253: .currentUser())) {
254: // file locked by current user
255: canWrite = true;
256: }
257:
258: if (canWrite) {
259: // write the file contents
260: getCms().writeFile(file);
261: } else {
262: // no write operation possible
263: getReport()
264: .println(
265: Messages
266: .get()
267: .container(
268: Messages.RPT_CHANGEELEMENTLOCALE_NOTLOCKED_0),
269: I_CmsReport.FORMAT_NOTE);
270: }
271:
272: if (isLocked) {
273: // unlock previously locked resource
274: getCms().unlockResource(getCms().getSitePath(res));
275: }
276:
277: if (canWrite) {
278: // successfully changed, report it
279: getReport()
280: .println(
281: org.opencms.report.Messages
282: .get()
283: .container(
284: org.opencms.report.Messages.RPT_OK_0),
285: I_CmsReport.FORMAT_OK);
286: }
287:
288: } catch (CmsException e) {
289: // an error occurred, show exception on report output
290: getReport().println(e);
291: }
292: }
293: }
294: }
|