KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > commons > CmsCommentImages


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsCommentImages.java,v $
3  * Date : $Date: 2006/10/06 15:33:27 $
4  * Version: $Revision: 1.3 $
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.commons;
33
34 import com.alkacon.simapi.Simapi;
35
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.i18n.CmsEncoder;
42 import org.opencms.jsp.CmsJspActionElement;
43 import org.opencms.loader.CmsImageScaler;
44 import org.opencms.main.CmsException;
45 import org.opencms.main.CmsLog;
46 import org.opencms.main.OpenCms;
47 import org.opencms.security.CmsPermissionSet;
48 import org.opencms.util.CmsStringUtil;
49 import org.opencms.workplace.CmsDialog;
50 import org.opencms.workplace.CmsWorkplaceSettings;
51
52 import java.awt.Color JavaDoc;
53 import java.util.ArrayList JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.List JavaDoc;
56
57 import javax.servlet.http.HttpServletRequest JavaDoc;
58 import javax.servlet.http.HttpServletResponse JavaDoc;
59 import javax.servlet.jsp.JspException JavaDoc;
60 import javax.servlet.jsp.PageContext JavaDoc;
61
62 import org.apache.commons.logging.Log;
63
64 /**
65  * Provides methods for the comment images dialog on image gallery folders.<p>
66  *
67  * The following files use this class:
68  * <ul>
69  * <li>/commons/commentimages.jsp
70  * </ul>
71  * <p>
72  *
73  * @author Andreas Zahner
74  *
75  * @version $Revision: 1.3 $
76  *
77  * @since 6.1.3
78  */

79 public class CmsCommentImages extends CmsDialog {
80
81     /** Value for the action: comment images. */
82     public static final int ACTION_COMMENTIMAGES = 100;
83
84     /** The dialog type. */
85     public static final String JavaDoc DIALOG_TYPE = "commentimages";
86
87     /** The input field prefix for description property fields. */
88     public static final String JavaDoc PREFIX_DESCRIPTION = "desc_";
89
90     /** The input field prefix for title property fields. */
91     public static final String JavaDoc PREFIX_TITLE = "title_";
92
93     /** The height of the dialog thumbnails. */
94     public static final int THUMB_HEIGHT = 150;
95
96     /** The width of the dialog thumbnails. */
97     public static final int THUMB_WIDTH = 200;
98
99     /** The log object for this class. */
100     private static final Log LOG = CmsLog.getLog(CmsCommentImages.class);
101
102     /** The image scaler object used in the dialog input form. */
103     private CmsImageScaler m_imageScaler;
104
105     /**
106      * Public constructor with JSP action element.<p>
107      *
108      * @param jsp an initialized JSP action element
109      */

110     public CmsCommentImages(CmsJspActionElement jsp) {
111
112         super(jsp);
113     }
114
115     /**
116      * Public constructor with JSP variables.<p>
117      *
118      * @param context the JSP page context
119      * @param req the JSP request
120      * @param res the JSP response
121      */

122     public CmsCommentImages(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
123
124         this(new CmsJspActionElement(context, req, res));
125     }
126
127     /**
128      * Performs the comment images action, will be called by the JSP page.<p>
129      *
130      * @throws JspException if problems including sub-elements occur
131      */

132     public void actionCommentImages() throws JspException JavaDoc {
133
134         // save initialized instance of this class in request attribute for included sub-elements
135
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
136         try {
137             performDialogOperation();
138             // if no exception is caused comment operation was successful
139
actionCloseDialog();
140         } catch (Throwable JavaDoc e) {
141             // error during rename images, show error dialog
142
includeErrorpage(this, e);
143         }
144     }
145
146     /**
147      * Returns the HTML for the dialog input form to comment the images.<p>
148      *
149      * @return the HTML for the dialog input form to comment the images
150      */

151     public String JavaDoc buildDialogForm() {
152
153         StringBuffer JavaDoc result = new StringBuffer JavaDoc(16384);
154         Iterator JavaDoc i = getImages().iterator();
155
156         result.append("<div style=\"height: 450px; padding: 4px; overflow: auto;\">");
157
158         while (i.hasNext()) {
159             CmsResource res = (CmsResource)i.next();
160             String JavaDoc imageName = res.getName();
161             String JavaDoc propertySuffix = "" + imageName.hashCode();
162             result.append(dialogBlockStart(imageName));
163             result.append("<table border=\"0\">\n");
164             result.append("<tr>\n\t<td style=\"vertical-align: top;\">");
165             // create image tag
166
result.append("<img SRC=\"");
167             StringBuffer JavaDoc link = new StringBuffer JavaDoc(256);
168             link.append(getCms().getSitePath(res));
169             link.append(getImageScaler().toRequestParam());
170             result.append(getJsp().link(link.toString()));
171             result.append("\" border=\"0\" alt=\"\" width=\"");
172             result.append(getImageScaler().getWidth());
173             result.append("\" height=\"");
174             result.append(getImageScaler().getHeight());
175             result.append("\">");
176
177             result.append("</td>\n");
178             result.append("\t<td class=\"maxwidth\" style=\"vertical-align: top;\">\n");
179
180             result.append("\t\t<table border=\"0\">\n");
181
182             // build title property input row
183
String JavaDoc title = "";
184             try {
185                 title = getCms().readPropertyObject(res, CmsPropertyDefinition.PROPERTY_TITLE, false).getValue();
186             } catch (CmsException e) {
187                 // log, should never happen
188
if (LOG.isErrorEnabled()) {
189                     LOG.error(e.getLocalizedMessage(getLocale()));
190                 }
191             }
192             result.append("\t\t<tr>\n\t\t\t<td style=\"white-space: nowrap;\" unselectable=\"on\">");
193             result.append(key(Messages.GUI_LABEL_TITLE_0));
194             result.append(":</td>\n\t\t\t<td class=\"maxwidth\">");
195             result.append("<input type=\"text\" class=\"maxwidth\" name=\"");
196             result.append(PREFIX_TITLE);
197             result.append(propertySuffix);
198             result.append("\" value=\"");
199             if (CmsStringUtil.isNotEmpty(title)) {
200                 result.append(CmsEncoder.escapeXml(title));
201             }
202             result.append("\">");
203             result.append("</td>\n\t\t</tr>\n");
204
205             // build description property input row
206
String JavaDoc description = "";
207             try {
208                 description = getCms().readPropertyObject(res, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false).getValue();
209             } catch (CmsException e) {
210                 // log, should never happen
211
if (LOG.isErrorEnabled()) {
212                     LOG.error(e.getLocalizedMessage(getLocale()));
213                 }
214             }
215             result.append("\t\t<tr>\n\t\t\t<td style=\"white-space: nowrap; vertical-align: top;\" unselectable=\"on\">");
216             result.append(key(Messages.GUI_LABEL_DESCRIPTION_0));
217             result.append(":</td>\n\t\t\t<td style=\"vertical-align: top; height: 110px;\">");
218             result.append("<textarea rows=\"8\" class=\"maxwidth\" style=\"overflow: auto;\" name=\"");
219             result.append(PREFIX_DESCRIPTION);
220             result.append(propertySuffix);
221             result.append("\">");
222             if (CmsStringUtil.isNotEmpty(description)) {
223                 result.append(CmsEncoder.escapeXml(description));
224             }
225             result.append("</textarea>");
226             result.append("</td>\n\t\t</tr>\n");
227
228             result.append("\t\t</table>\n");
229
230             result.append("</td>\n</tr>\n");
231             result.append("</table>\n");
232             result.append(dialogBlockEnd());
233
234             if (i.hasNext()) {
235                 // append spacer if another entry follows
236
result.append(dialogSpacer());
237             }
238         }
239
240         result.append("</div>");
241
242         return result.toString();
243     }
244
245     /**
246      * Returns the image resources of the gallery folder which are edited in the dialog form.<p>
247      *
248      * @return the images of the gallery folder which are edited in the dialog form
249      */

250     protected List JavaDoc getImages() {
251
252         // get all image resources of the folder
253
CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(CmsResourceTypeImage.getStaticTypeId());
254         try {
255             return getCms().readResources(getParamResource(), filter, false);
256         } catch (CmsException e) {
257             // log, should never happen
258
if (LOG.isErrorEnabled()) {
259                 LOG.error(e.getLocalizedMessage(getLocale()));
260             }
261             return new ArrayList JavaDoc(0);
262         }
263     }
264
265     /**
266      * Returns the initialized image scaler object used to generate thumbnails for the dialog form.<p>
267      *
268      * @return the initialized image scaler object used to generate thumbnails for the dialog form
269      */

270     protected CmsImageScaler getImageScaler() {
271
272         if (m_imageScaler == null) {
273             // not initialized, create image scaler with default settings
274
m_imageScaler = new CmsImageScaler();
275             m_imageScaler.setWidth(THUMB_WIDTH);
276             m_imageScaler.setHeight(THUMB_HEIGHT);
277             m_imageScaler.setRenderMode(Simapi.RENDER_SPEED);
278             m_imageScaler.setColor(new Color JavaDoc(0, 0, 0));
279             m_imageScaler.setType(1);
280         }
281         return m_imageScaler;
282     }
283
284     /**
285      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
286      */

287     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
288
289         // fill the parameter values in the get/set methods
290
fillParamValues(request);
291
292         // check the required permissions to rename the resource
293
if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
294             // no write permissions for the resource, set cancel action to close dialog
295
setParamAction(DIALOG_CANCEL);
296         }
297
298         // set the dialog type
299
setParamDialogtype(DIALOG_TYPE);
300         // set the action for the JSP switch
301
if (DIALOG_TYPE.equals(getParamAction())) {
302             setAction(ACTION_COMMENTIMAGES);
303         } else if (DIALOG_CANCEL.equals(getParamAction())) {
304             setAction(ACTION_CANCEL);
305         } else {
306             setAction(ACTION_DEFAULT);
307             // build title for comment images dialog
308
Object JavaDoc[] args = new Object JavaDoc[] {getParamResource()};
309             setParamTitle(key(Messages.GUI_COMMENTIMAGES_TITLE_1, args));
310         }
311     }
312
313     /**
314      * Performs the comment images operation.<p>
315      *
316      * @return true, if the resources were successfully processed, otherwise false
317      * @throws CmsException if commenting is not successful
318      */

319     protected boolean performDialogOperation() throws CmsException {
320
321         // lock the image gallery folder
322
checkLock(getParamResource());
323
324         Iterator JavaDoc i = getImages().iterator();
325         // loop over all image resources to change the properties
326
while (i.hasNext()) {
327             CmsResource res = (CmsResource)i.next();
328             String JavaDoc imageName = res.getName();
329             String JavaDoc propertySuffix = "" + imageName.hashCode();
330
331             // update the title property
332
CmsProperty titleProperty = getCms().readPropertyObject(res, CmsPropertyDefinition.PROPERTY_TITLE, false);
333             String JavaDoc newValue = getJsp().getRequest().getParameter(PREFIX_TITLE + propertySuffix);
334             writeProperty(res, CmsPropertyDefinition.PROPERTY_TITLE, newValue, titleProperty);
335
336             // update the description property
337
CmsProperty descProperty = getCms().readPropertyObject(
338                 res,
339                 CmsPropertyDefinition.PROPERTY_DESCRIPTION,
340                 false);
341             newValue = getJsp().getRequest().getParameter(PREFIX_DESCRIPTION + propertySuffix);
342             writeProperty(res, CmsPropertyDefinition.PROPERTY_DESCRIPTION, newValue, descProperty);
343         }
344
345         return true;
346     }
347
348     /**
349      * Writes a property value for a resource, if the value was changed.<p>
350      *
351      * @param res the resource to write the property to
352      * @param propName the name of the property definition
353      * @param propValue the new value of the property
354      * @param currentProperty the old property object
355      * @throws CmsException if something goes wrong
356      */

357     protected void writeProperty(CmsResource res, String JavaDoc propName, String JavaDoc propValue, CmsProperty currentProperty)
358     throws CmsException {
359
360         // check if current property is not the null property
361
if (currentProperty.isNullProperty()) {
362             // create new property object
363
currentProperty = new CmsProperty();
364             currentProperty.setName(propName);
365         }
366
367         if (CmsStringUtil.isEmptyOrWhitespaceOnly(propValue)) {
368             // parameter is empty, determine the value to delete
369
boolean writeProperty = false;
370             if (currentProperty.getStructureValue() != null) {
371                 currentProperty.setStructureValue(CmsProperty.DELETE_VALUE);
372                 currentProperty.setResourceValue(null);
373                 writeProperty = true;
374             } else if (currentProperty.getResourceValue() != null) {
375                 currentProperty.setResourceValue(CmsProperty.DELETE_VALUE);
376                 currentProperty.setStructureValue(null);
377                 writeProperty = true;
378             }
379             if (writeProperty) {
380                 // write the updated property object
381
getCms().writePropertyObject(getCms().getSitePath(res), currentProperty);
382             }
383         } else {
384             // parameter is not empty, check if the value has changed
385
if (!propValue.equals(currentProperty.getValue())) {
386                 if (currentProperty.getStructureValue() == null && currentProperty.getResourceValue() == null) {
387                     // new property, determine setting from OpenCms workplace configuration
388
if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) {
389                         currentProperty.setStructureValue(propValue);
390                         currentProperty.setResourceValue(null);
391                     } else {
392                         currentProperty.setResourceValue(propValue);
393                         currentProperty.setStructureValue(null);
394                     }
395                 } else if (currentProperty.getStructureValue() != null) {
396                     // structure value has to be updated
397
currentProperty.setStructureValue(propValue);
398                     currentProperty.setResourceValue(null);
399                 } else {
400                     // resource value has to be updated
401
currentProperty.setResourceValue(propValue);
402                     currentProperty.setStructureValue(null);
403                 }
404                 // write the updated property object
405
getCms().writePropertyObject(getCms().getSitePath(res), currentProperty);
406             }
407         }
408     }
409
410 }
411
Popular Tags