KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/CmsChangeElementLocaleThread.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.2 $
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.CmsPropertyDefinition;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsResourceFilter;
39 import org.opencms.file.types.CmsResourceTypeXmlPage;
40 import org.opencms.i18n.CmsLocaleManager;
41 import org.opencms.lock.CmsLock;
42 import org.opencms.main.CmsException;
43 import org.opencms.report.A_CmsReportThread;
44 import org.opencms.report.I_CmsReport;
45 import org.opencms.util.CmsStringUtil;
46 import org.opencms.xml.page.CmsXmlPage;
47 import org.opencms.xml.page.CmsXmlPageFactory;
48
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Locale JavaDoc;
52
53 /**
54  * Changes the element Locales of resources using the corresponding settings object.<p>
55  *
56  * @author Andreas Zahner
57  *
58  * @version $Revision: 1.2 $
59  *
60  * @since 6.0.1
61  */

62 public class CmsChangeElementLocaleThread extends A_CmsReportThread {
63
64     private Throwable JavaDoc m_error;
65     
66     private CmsElementChangeLocaleSettings m_settings;
67
68     /**
69      * Creates a change element Locale Thread.<p>
70      *
71      * @param cms the current cms context
72      * @param settings the settings needed to perform the operation
73      */

74     public CmsChangeElementLocaleThread(CmsObject cms, CmsElementChangeLocaleSettings settings) {
75
76         super(cms, Messages.get().getBundle().key(Messages.GUI_CHANGEELEMENTLOCALE_THREAD_NAME_0));
77         initHtmlReport(cms.getRequestContext().getLocale());
78         m_settings = settings;
79     }
80
81     /**
82      * @see org.opencms.report.A_CmsReportThread#getError()
83      */

84     public Throwable JavaDoc getError() {
85
86         return m_error;
87     }
88
89     /**
90      * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
91      */

92     public String JavaDoc getReportUpdate() {
93
94         return getReport().getReportUpdate();
95     }
96
97     /**
98      * @see java.lang.Runnable#run()
99      */

100     public void run() {
101
102         getReport().println(
103             Messages.get().container(
104                 Messages.RPT_CHANGEELEMENTLOCALE_BEGIN_2, m_settings.getOldLocale(), m_settings.getNewLocale()),
105             I_CmsReport.FORMAT_HEADLINE);
106         try {
107             // change the element locales
108
changeElementLocales();
109         } catch (CmsException e) {
110             getReport().println(e);
111         }
112
113         // append runtime statistics to report
114
getReport().print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_STAT_0));
115         getReport().println(
116             org.opencms.report.Messages.get().container(
117                 org.opencms.report.Messages.RPT_STAT_DURATION_1,
118                 getReport().formatRuntime()));
119         getReport().println(
120             Messages.get().container(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 JavaDoc oldLocale = CmsLocaleManager.getLocale(m_settings.getOldLocale());
133         Locale JavaDoc newLocale = CmsLocaleManager.getLocale(m_settings.getNewLocale());
134         boolean checkTemplate = CmsStringUtil.isNotEmpty(m_settings.getTemplate());
135
136         // set the resource filter to filter xml pages
137
CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(CmsResourceTypeXmlPage.getStaticTypeId());
138         String JavaDoc path = CmsResource.getFolderPath(m_settings.getVfsFolder());
139         // get the list of resources to change
140
List JavaDoc resources = getCms().readResources(path, filter, m_settings.isIncludeSubFolders());
141
142         // set the report counters
143
int count = 0;
144         int resSize = resources.size();
145
146         // iterate the resources
147
Iterator JavaDoc i = resources.iterator();
148         while (i.hasNext()) {
149
150             count++;
151             CmsResource res = (CmsResource)i.next();
152
153             // generate report output
154
getReport().print(
155                 org.opencms.report.Messages.get().container(
156                     org.opencms.report.Messages.RPT_SUCCESSION_2,
157                     String.valueOf(count),
158                     String.valueOf(resSize)),
159                 I_CmsReport.FORMAT_NOTE);
160             getReport().print(Messages.get().container(Messages.RPT_PROCESSING_PAGE_0), I_CmsReport.FORMAT_NOTE);
161             getReport().print(
162                 org.opencms.report.Messages.get().container(
163                     org.opencms.report.Messages.RPT_ARGUMENT_1,
164                     getCms().getSitePath(res)));
165             getReport().print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
166
167             try {
168
169                 if (checkTemplate) {
170                     // check the template property
171
String JavaDoc template = getCms().readPropertyObject(res, CmsPropertyDefinition.PROPERTY_TEMPLATE, true).getValue(
172                         "");
173                     if (!m_settings.getTemplate().equals(template)) {
174                         // template property does not match, report and continue with next resource
175
getReport().println(
176                             Messages.get().container(Messages.RPT_CHANGEELEMENTLOCALE_TEMPLATE_0),
177                             I_CmsReport.FORMAT_NOTE);
178                         continue;
179                     }
180                 }
181
182                 // get the file contents
183
CmsFile file = CmsFile.upgrade(res, getCms());
184                 // get the page object
185
CmsXmlPage page = CmsXmlPageFactory.unmarshal(getCms(), file);
186                 // write the report output
187

188                 if (!page.hasLocale(oldLocale)) {
189                     // old Locale not present, report and continue with next resource
190
getReport().println(
191                         Messages.get().container(
192                             Messages.RPT_CHANGEELEMENTLOCALE_OLDLOCALE_1,
193                             m_settings.getOldLocale()),
194                         I_CmsReport.FORMAT_NOTE);
195                     continue;
196                 }
197
198                 if (page.hasLocale(newLocale)) {
199                     // target Locale present, report and continue with next resource
200
getReport().println(
201                         Messages.get().container(
202                             Messages.RPT_CHANGEELEMENTLOCALE_NEWLOCALE_1,
203                             m_settings.getNewLocale()),
204                         I_CmsReport.FORMAT_NOTE);
205                     continue;
206                 }
207
208                 // change the Locale of the elements
209
page.moveLocale(oldLocale, newLocale);
210
211                 // set the file contents
212
file.setContents(page.marshal());
213
214                 // check the lock state of the file to write
215
CmsLock lock = getCms().getLock(res);
216                 boolean isLocked = false;
217                 boolean canWrite = false;
218                 if (lock.isNullLock()) {
219                     // file not locked, lock it
220
getCms().lockResource(getCms().getSitePath(res));
221                     isLocked = true;
222                     canWrite = true;
223                 } else if (lock.getUserId().equals(getCms().getRequestContext().currentUser().getId())) {
224                     // file locked by current user
225
canWrite = true;
226                 }
227
228                 if (canWrite) {
229                     // write the file contents
230
getCms().writeFile(file);
231                 } else {
232                     // no write operation possible
233
getReport().println(
234                         Messages.get().container(Messages.RPT_CHANGEELEMENTLOCALE_NOTLOCKED_0),
235                         I_CmsReport.FORMAT_NOTE);
236                 }
237
238                 if (isLocked) {
239                     // unlock previously locked resource
240
getCms().unlockResource(getCms().getSitePath(res));
241                 }
242
243                 if (canWrite) {
244                     // successfully changed, report it
245
getReport().println(
246                         org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
247                         I_CmsReport.FORMAT_OK);
248                 }
249
250             } catch (CmsException e) {
251                 // an error occurred, show exception on report output
252
getReport().println(e);
253             }
254         }
255     }
256 }
Popular Tags