001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsChnav.java,v $
003: * Date : $Date: 2008-02-27 12:05:25 $
004: * Version: $Revision: 1.28 $
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.commons;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.file.CmsProperty;
036: import org.opencms.file.CmsPropertyDefinition;
037: import org.opencms.file.CmsResource;
038: import org.opencms.i18n.CmsEncoder;
039: import org.opencms.i18n.CmsMessages;
040: import org.opencms.jsp.CmsJspActionElement;
041: import org.opencms.jsp.CmsJspNavBuilder;
042: import org.opencms.jsp.CmsJspNavElement;
043: import org.opencms.main.CmsException;
044: import org.opencms.main.CmsLog;
045: import org.opencms.main.OpenCms;
046: import org.opencms.security.CmsPermissionSet;
047: import org.opencms.util.CmsStringUtil;
048: import org.opencms.workplace.CmsDialog;
049: import org.opencms.workplace.CmsWorkplace;
050: import org.opencms.workplace.CmsWorkplaceSettings;
051:
052: import java.util.ArrayList;
053: import java.util.List;
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: import org.apache.commons.logging.Log;
061:
062: /**
063: * Provides methods for the change navigation dialog.<p>
064: *
065: * The following files use this class:
066: * <ul>
067: * <li>/commons/chnav.jsp
068: * </ul>
069: * <p>
070: *
071: * @author Andreas Zahner
072: *
073: * @version $Revision: 1.28 $
074: *
075: * @since 6.0.0
076: */
077: public class CmsChnav extends CmsDialog {
078:
079: /** Value for the action: change the navigation. */
080: public static final int ACTION_CHNAV = 100;
081:
082: /** The dialog type. */
083: public static final String DIALOG_TYPE = "chnav";
084:
085: /** Request parameter name for the navigation position. */
086: public static final String PARAM_NAVPOS = "navpos";
087:
088: /** Request parameter name for the navigation text. */
089: public static final String PARAM_NAVTEXT = "navtext";
090:
091: /** The log object for this class. */
092: private static final Log LOG = CmsLog.getLog(CmsChnav.class);
093:
094: private String m_paramNavpos;
095:
096: private String m_paramNavtext;
097:
098: /**
099: * Public constructor.<p>
100: *
101: * @param jsp an initialized JSP action element
102: */
103: public CmsChnav(CmsJspActionElement jsp) {
104:
105: super (jsp);
106: }
107:
108: /**
109: * Public constructor with JSP variables.<p>
110: *
111: * @param context the JSP page context
112: * @param req the JSP request
113: * @param res the JSP response
114: */
115: public CmsChnav(PageContext context, HttpServletRequest req,
116: HttpServletResponse res) {
117:
118: this (new CmsJspActionElement(context, req, res));
119: }
120:
121: /**
122: * Builds the HTML for the select box of the navigation position.<p>
123: *
124: * @param cms the CmsObject
125: * @param filename the current file
126: * @param attributes optional attributes for the <select> tag, do not add the "name" atribute!
127: * @param messages the localized workplace messages
128: *
129: * @return the HTML for a navigation position select box
130: */
131: public static String buildNavPosSelector(CmsObject cms,
132: String filename, String attributes, CmsMessages messages) {
133:
134: // get current file navigation element
135: CmsJspNavElement curNav = CmsJspNavBuilder
136: .getNavigationForResource(cms, filename);
137:
138: // get the parent folder of the current file
139: filename = CmsResource.getParentFolder(filename);
140:
141: // get navigation of the current folder
142: List navList = CmsJspNavBuilder.getNavigationForFolder(cms,
143: filename);
144: float maxValue = 0;
145: float nextPos = 0;
146:
147: // calculate value for the first navigation position
148: float firstValue = 1;
149: if (navList.size() > 0) {
150: try {
151: CmsJspNavElement ne = (CmsJspNavElement) navList.get(0);
152: maxValue = ne.getNavPosition();
153: } catch (Exception e) {
154: // should usually never happen
155: LOG.error(e.getLocalizedMessage());
156: }
157: }
158:
159: if (maxValue != 0) {
160: firstValue = maxValue / 2;
161: }
162:
163: List options = new ArrayList(navList.size() + 1);
164: List values = new ArrayList(navList.size() + 1);
165:
166: // add the first entry: before first element
167: options.add(messages.key(Messages.GUI_CHNAV_POS_FIRST_0));
168: values.add(firstValue + "");
169:
170: // show all present navigation elements in box
171: for (int i = 0; i < navList.size(); i++) {
172: CmsJspNavElement ne = (CmsJspNavElement) navList.get(i);
173: String navText = ne.getNavText();
174: float navPos = ne.getNavPosition();
175: // get position of next nav element
176: nextPos = navPos + 2;
177: if ((i + 1) < navList.size()) {
178: nextPos = ((CmsJspNavElement) navList.get(i + 1))
179: .getNavPosition();
180: }
181: // calculate new position of current nav element
182: float newPos;
183: if ((nextPos - navPos) > 1) {
184: newPos = navPos + 1;
185: } else {
186: newPos = (navPos + nextPos) / 2;
187: }
188:
189: // check new maxValue of positions and increase it
190: if (navPos > maxValue) {
191: maxValue = navPos;
192: }
193:
194: // if the element is the current file, mark it in selectbox
195: if (curNav.getNavText().equals(navText)
196: && (curNav.getNavPosition() == navPos)) {
197: options.add(CmsEncoder.escapeHtml(messages.key(
198: Messages.GUI_CHNAV_POS_CURRENT_1,
199: new Object[] { ne.getFileName() })));
200: values.add("-1");
201: } else {
202: options.add(CmsEncoder.escapeHtml(navText + " ["
203: + ne.getFileName() + "]"));
204: values.add(newPos + "");
205: }
206: }
207:
208: // add the entry: at the last position
209: options.add(messages.key(Messages.GUI_CHNAV_POS_LAST_0));
210: values.add((maxValue + 1) + "");
211:
212: // add the entry: no change
213: options.add(messages.key(Messages.GUI_CHNAV_NO_CHANGE_0));
214: if (curNav.getNavPosition() == Float.MAX_VALUE) {
215: // current resource has no valid position, use "last position"
216: values.add((maxValue + 1) + "");
217: } else {
218: // current resource has valid position, use "-1" for no change
219: values.add("-1");
220: }
221:
222: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(attributes)) {
223: attributes = " " + attributes;
224: } else {
225: attributes = "";
226: }
227: return CmsWorkplace.buildSelect("name=\"" + PARAM_NAVPOS + "\""
228: + attributes, options, values, values.size() - 1, true);
229: }
230:
231: /**
232: * Performs the navigation change.<p>
233: *
234: * @throws JspException if including a JSP subelement is not successful
235: */
236: public void actionChangeNav() throws JspException {
237:
238: // save initialized instance of this class in request attribute for included sub-elements
239: getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS,
240: this );
241:
242: // get request parameters
243: String filename = getParamResource();
244: // do not use #getParamNavText since it is decoded, see CmsWorkplace#fillParamValues(HttpServletRequest)
245: String newText = getJsp().getRequest().getParameter(
246: PARAM_NAVTEXT);
247: String selectedPosString = getParamNavpos();
248:
249: try {
250: // lock resource if autolock is enabled
251: checkLock(getParamResource());
252: // save the new NavText if not null
253: if (newText != null) {
254: CmsProperty newNavText = new CmsProperty();
255: newNavText
256: .setName(CmsPropertyDefinition.PROPERTY_NAVTEXT);
257: CmsProperty oldNavText = getCms().readPropertyObject(
258: filename,
259: CmsPropertyDefinition.PROPERTY_NAVTEXT, false);
260: if (oldNavText.isNullProperty()) {
261: // property value was not already set
262: if (OpenCms.getWorkplaceManager()
263: .isDefaultPropertiesOnStructure()) {
264: newNavText.setStructureValue(newText);
265: } else {
266: newNavText.setResourceValue(newText);
267: }
268: } else {
269: if (oldNavText.getStructureValue() != null) {
270: newNavText.setStructureValue(newText);
271: newNavText.setResourceValue(oldNavText
272: .getResourceValue());
273: } else {
274: newNavText.setResourceValue(newText);
275: }
276: }
277:
278: String oldStructureValue = oldNavText
279: .getStructureValue();
280: String newStructureValue = newNavText
281: .getStructureValue();
282: if (CmsStringUtil.isEmpty(oldStructureValue)) {
283: oldStructureValue = CmsProperty.DELETE_VALUE;
284: }
285: if (CmsStringUtil.isEmpty(newStructureValue)) {
286: newStructureValue = CmsProperty.DELETE_VALUE;
287: }
288:
289: String oldResourceValue = oldNavText.getResourceValue();
290: String newResourceValue = newNavText.getResourceValue();
291: if (CmsStringUtil.isEmpty(oldResourceValue)) {
292: oldResourceValue = CmsProperty.DELETE_VALUE;
293: }
294: if (CmsStringUtil.isEmpty(newResourceValue)) {
295: newResourceValue = CmsProperty.DELETE_VALUE;
296: }
297:
298: // change nav text only if it has been changed
299: if (!oldResourceValue.equals(newResourceValue)
300: || !oldStructureValue.equals(newStructureValue)) {
301: getCms().writePropertyObject(getParamResource(),
302: newNavText);
303: }
304: }
305:
306: // determine the selected position
307: float selectedPos = -1;
308: try {
309: selectedPos = Float.parseFloat(selectedPosString);
310: } catch (Exception e) {
311: // can usually be ignored
312: if (LOG.isInfoEnabled()) {
313: LOG.info(e.getLocalizedMessage());
314: }
315: }
316:
317: // only update the position if a change is requested
318: if (selectedPos != -1) {
319: CmsProperty newNavPos = new CmsProperty();
320: newNavPos
321: .setName(CmsPropertyDefinition.PROPERTY_NAVPOS);
322: CmsProperty oldNavPos = getCms().readPropertyObject(
323: filename,
324: CmsPropertyDefinition.PROPERTY_NAVPOS, false);
325: if (oldNavPos.isNullProperty()) {
326: // property value was not already set
327: if (OpenCms.getWorkplaceManager()
328: .isDefaultPropertiesOnStructure()) {
329: newNavPos.setStructureValue(selectedPosString);
330: } else {
331: newNavPos.setResourceValue(selectedPosString);
332: }
333: } else {
334: if (oldNavPos.getStructureValue() != null) {
335: newNavPos.setStructureValue(selectedPosString);
336: newNavPos.setResourceValue(oldNavPos
337: .getResourceValue());
338: } else {
339: newNavPos.setResourceValue(selectedPosString);
340: }
341: }
342: getCms().writePropertyObject(filename, newNavPos);
343: }
344: } catch (Throwable e) {
345: // error during chnav, show error dialog
346: includeErrorpage(this , e);
347: }
348: // chnav operation was successful, return to workplace
349: actionCloseDialog();
350: }
351:
352: /**
353: * Builds the HTML for the select box of the navigation position.<p>
354: *
355: * @return the HTML for a navigation position select box
356: */
357: public String buildNavPosSelector() {
358:
359: return buildNavPosSelector(getCms(), getParamResource(), null,
360: getMessages());
361: }
362:
363: /**
364: * Returns the escaped NavText property value of the current resource.<p>
365: *
366: * @return the NavText property value of the current resource
367: */
368: public String getCurrentNavText() {
369:
370: try {
371: String navText = getCms().readPropertyObject(
372: getParamResource(),
373: CmsPropertyDefinition.PROPERTY_NAVTEXT, false)
374: .getValue();
375: if (navText == null) {
376: navText = "";
377: }
378: return CmsEncoder.escapeXml(navText);
379: } catch (CmsException e) {
380: // can usually be ignored
381: if (LOG.isInfoEnabled()) {
382: LOG.info(e.getLocalizedMessage());
383: }
384: return "";
385: }
386:
387: }
388:
389: /**
390: * Returns the value of the navigation position parameter,
391: * or null if this parameter was not provided.<p>
392: *
393: * The navigation position parameter defines the new value for
394: * the NavPos property.<p>
395: *
396: * @return the value of the target parameter
397: */
398: public String getParamNavpos() {
399:
400: return m_paramNavpos;
401: }
402:
403: /**
404: * Returns the value of the navigation text parameter,
405: * or null if this parameter was not provided.<p>
406: *
407: * The navigation text parameter defines the new value for
408: * the NavText property.<p>
409: *
410: * @return the value of the target parameter
411: */
412: public String getParamNavtext() {
413:
414: return m_paramNavtext;
415: }
416:
417: /**
418: * Sets the value of the navigation position parameter.<p>
419: *
420: * @param value the value to set
421: */
422: public void setParamNavpos(String value) {
423:
424: m_paramNavpos = value;
425: }
426:
427: /**
428: * Sets the value of the navigation text parameter.<p>
429: *
430: * @param value the value to set
431: */
432: public void setParamNavtext(String value) {
433:
434: m_paramNavtext = value;
435: }
436:
437: /**
438: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
439: */
440: protected void initWorkplaceRequestValues(
441: CmsWorkplaceSettings settings, HttpServletRequest request) {
442:
443: // fill the parameter values in the get/set methods
444: fillParamValues(request);
445:
446: // check the required permissions to change navigation of the resource
447: if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE,
448: false)) {
449: // no write permissions for the resource, set cancel action to close dialog
450: setParamAction(DIALOG_CANCEL);
451: }
452:
453: // set the dialog type
454: setParamDialogtype(DIALOG_TYPE);
455: // set the action for the JSP switch
456: if (DIALOG_TYPE.equals(getParamAction())) {
457: setAction(ACTION_CHNAV);
458: } else if (DIALOG_LOCKS_CONFIRMED.equals(getParamAction())) {
459: setAction(ACTION_LOCKS_CONFIRMED);
460: } else if (DIALOG_CANCEL.equals(getParamAction())) {
461: setAction(ACTION_CANCEL);
462: } else {
463: setAction(ACTION_DEFAULT);
464: // build title for chnav dialog
465: setParamTitle(key(Messages.GUI_CHNAV_1,
466: new Object[] { CmsResource
467: .getName(getParamResource()) }));
468: }
469: }
470:
471: }
|