KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > scheduler > jobs > CmsCreateImageSizeJob


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/scheduler/jobs/CmsCreateImageSizeJob.java,v $
3  * Date : $Date: 2006/03/27 14:52:42 $
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.scheduler.jobs;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsProperty;
37 import org.opencms.file.CmsPropertyDefinition;
38 import org.opencms.file.CmsResource;
39 import org.opencms.file.CmsResourceFilter;
40 import org.opencms.file.types.CmsResourceTypeImage;
41 import org.opencms.loader.CmsImageLoader;
42 import org.opencms.loader.CmsImageScaler;
43 import org.opencms.lock.CmsLock;
44 import org.opencms.main.CmsException;
45 import org.opencms.report.CmsLogReport;
46 import org.opencms.report.I_CmsReport;
47 import org.opencms.scheduler.I_CmsScheduledJob;
48
49 import java.util.Collections JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52
53 /**
54  * A schedulable OpenCms job to calculate image size information.<p>
55  *
56  * Image size information is stored in the <code>{@link CmsPropertyDefinition#PROPERTY_IMAGE_SIZE}</code> property
57  * of an image file must have the format "h:x,w:y" with x and y being positive Integer vaulues.<p>
58  *
59  * This job does not have any parameters.<p>
60  *
61  * @author Michael Emmerich
62  *
63  * @version $Revision: 1.2 $
64  *
65  * @since 6.0.2
66  */

67 public class CmsCreateImageSizeJob implements I_CmsScheduledJob {
68
69     /**
70      * @see org.opencms.scheduler.I_CmsScheduledJob#launch(CmsObject, Map)
71      */

72     public String JavaDoc launch(CmsObject cms, Map JavaDoc parameters) throws Exception JavaDoc {
73
74         if (!CmsImageLoader.isEnabled()) {
75             // scaling functions are not available
76
return Messages.get().getBundle().key(Messages.LOG_IMAGE_SCALING_DISABLED_0);
77         }
78
79         I_CmsReport report = new CmsLogReport(cms.getRequestContext().getLocale(), CmsCreateImageSizeJob.class);
80         report.println(Messages.get().container(Messages.RPT_IMAGE_SIZE_START_0), I_CmsReport.FORMAT_HEADLINE);
81
82         List JavaDoc resources = Collections.EMPTY_LIST;
83         try {
84             // get all image resources
85
resources = cms.readResources(
86                 "/",
87                 CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(CmsResourceTypeImage.getStaticTypeId()));
88         } catch (CmsException e) {
89             report.println(e);
90         }
91
92         int count = 0;
93         // now iterate through all resources
94
for (int i = 0; i < resources.size(); i++) {
95
96             try {
97
98                 CmsResource res = (CmsResource)resources.get(i);
99                 report.print(Messages.get().container(
100                     Messages.RPT_IMAGE_SIZE_PROCESS_3,
101                     String.valueOf(i + 1),
102                     String.valueOf(resources.size()),
103                     res.getRootPath()), I_CmsReport.FORMAT_HEADLINE);
104
105                 report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
106
107                 // check if the resource is locked by another user
108
// we cannot process resources that are locked by someone else
109
CmsLock lock = cms.getLock(res);
110                 if (lock.isNullLock() || lock.getUserId().equals(cms.getRequestContext().currentUser().getId())) {
111
112                     // get the size info property
113
CmsProperty prop = cms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_IMAGE_SIZE, false);
114                     if (prop == null) {
115                         prop = CmsProperty.getNullProperty();
116                     }
117                     // read the file content
118
CmsFile file = CmsFile.upgrade(res, cms);
119                     // get the image size information
120
CmsImageScaler scaler = new CmsImageScaler(file.getContents(), file.getRootPath());
121
122                     if (scaler.isValid()) {
123                         // update the property if it does not exist or it is different than the newly calculated one
124
if (prop.isNullProperty() || !prop.getValue().equals(scaler.toString())) {
125
126                             boolean unlockFlag = false;
127                             // lock the resource if not locked so far
128
if (lock.isNullLock()) {
129                                 cms.lockResource(res.getRootPath());
130                                 unlockFlag = true;
131                             }
132                             // set the shared value of the property or create a new one if required
133
if (prop.isNullProperty()) {
134                                 prop = new CmsProperty(
135                                     CmsPropertyDefinition.PROPERTY_IMAGE_SIZE,
136                                     null,
137                                     scaler.toString());
138                             } else {
139                                 // delete any individual proprety value (just in case)
140
prop.setStructureValue(CmsProperty.DELETE_VALUE);
141                                 // set the calculated value as shared property
142
prop.setResourceValue(scaler.toString());
143                             }
144                             // write the property
145
cms.writePropertyObject(res.getRootPath(), prop);
146                             // unlock the resource if it was not locked before
147
if (unlockFlag) {
148                                 cms.unlockResource(res.getRootPath());
149                             }
150                             // increase conter
151
count++;
152                             // write report information
153
report.println(
154                                 Messages.get().container(Messages.RPT_IMAGE_SIZE_UPDATE_1, scaler.toString()),
155                                 I_CmsReport.FORMAT_DEFAULT);
156
157                         } else {
158                             report.println(
159                                 Messages.get().container(Messages.RPT_IMAGE_SIZE_SKIP_1, scaler.toString()),
160                                 I_CmsReport.FORMAT_DEFAULT);
161                         }
162                     } else {
163                         report.println(
164                             Messages.get().container(Messages.RPT_IMAGE_SIZE_UNABLE_TO_CALCULATE_0),
165                             I_CmsReport.FORMAT_DEFAULT);
166                     }
167
168                 } else {
169                     // the resource is locked by someone else
170
report.println(
171                         Messages.get().container(Messages.RPT_IMAGE_SIZE_LOCKED_0),
172                         I_CmsReport.FORMAT_DEFAULT);
173                 }
174
175             } catch (CmsException e) {
176                 report.println(e);
177             }
178         }
179
180         report.println(Messages.get().container(Messages.RPT_IMAGE_SIZE_END_0), I_CmsReport.FORMAT_HEADLINE);
181
182         return Messages.get().getBundle().key(Messages.LOG_IMAGE_SIZE_UPDATE_COUNT_1, new Integer JavaDoc(count));
183     }
184 }
Popular Tags