001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace.explorer;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProperty;
032import org.opencms.file.CmsPropertyDefinition;
033import org.opencms.jsp.CmsJspNavBuilder;
034import org.opencms.jsp.CmsJspNavElement;
035import org.opencms.main.CmsIllegalArgumentException;
036import org.opencms.main.CmsLog;
037import org.opencms.main.CmsRuntimeException;
038import org.opencms.main.OpenCms;
039import org.opencms.util.CmsStringUtil;
040
041import java.lang.reflect.Constructor;
042import java.util.ArrayList;
043import java.util.List;
044
045import javax.servlet.http.HttpServletRequest;
046import javax.servlet.http.HttpServletResponse;
047import javax.servlet.jsp.PageContext;
048
049import org.apache.commons.logging.Log;
050
051/**
052 * The new resource entry dialog which displays the possible "new actions" for the current user.<p>
053 *
054 * It handles the creation of "simple" resource types like plain or JSP resources.<p>
055 *
056 * The following files use this class:
057 * <ul>
058 * <li>/commons/newresource.jsp
059 * </ul>
060 * <p>
061 *
062 * @since 6.0.0
063 */
064public class CmsNewResource {
065
066    /** The value for the resource name form action. */
067    public static final int ACTION_NEWFORM = 100;
068
069    /** The value for the resource name form submission action. */
070    public static final int ACTION_SUBMITFORM = 110;
071
072    /** Constant for the "Next" button in the build button methods. */
073    public static final int BUTTON_NEXT = 20;
074
075    /** The default suffix. */
076    public static final String DEFAULT_SUFFIX = ".html";
077
078    /** Delimiter for property values, e.g. for available resource types or template sites. */
079    public static final char DELIM_PROPERTYVALUES = ',';
080
081    /** The name for the advanced resource form action. */
082    public static final String DIALOG_ADVANCED = "advanced";
083
084    /** The name for the resource form action. */
085    public static final String DIALOG_NEWFORM = "newform";
086
087    /** The name for the resource form submission action. */
088    public static final String DIALOG_SUBMITFORM = "submitform";
089
090    /** The dialog type. */
091    public static final String DIALOG_TYPE = "newresource";
092
093    /** List column id constant. */
094    public static final String LIST_COLUMN_URI = "nrcu";
095
096    /** Request parameter name for the append html suffix checkbox. */
097    public static final String PARAM_APPENDSUFFIXHTML = "appendsuffixhtml";
098
099    /** Request parameter name for the current folder name. */
100    public static final String PARAM_CURRENTFOLDER = "currentfolder";
101
102    /** Request parameter name for the new form uri. */
103    public static final String PARAM_NEWFORMURI = "newformuri";
104
105    /** Request parameter name for the new resource edit properties flag. */
106    public static final String PARAM_NEWRESOURCEEDITPROPS = "newresourceeditprops";
107
108    /** Request parameter name for the new resource type. */
109    public static final String PARAM_NEWRESOURCETYPE = "newresourcetype";
110
111    /** Request parameter name for the new resource uri. */
112    public static final String PARAM_NEWRESOURCEURI = "newresourceuri";
113
114    /** Session attribute to store advanced mode. */
115    public static final String SESSION_ATTR_ADVANCED = "ocms_newres_adv";
116
117    /** Session attribute to store current page. */
118    public static final String SESSION_ATTR_PAGE = "ocms_newres_page";
119
120    /** The property value for available resource to reset behaviour to default dialog. */
121    public static final String VALUE_DEFAULT = "default";
122
123    /** The log object for this class. */
124    private static final Log LOG = CmsLog.getLog(CmsNewResource.class);
125
126    /**
127     * Returns the value for the Title property from the given resource name.<p>
128     *
129     * @param name the name of the resource
130     *
131     * @return the value for the Title property from the given resource name
132     */
133    public static String computeNewTitleProperty(String name) {
134
135        String title = name;
136        int lastDot = title.lastIndexOf('.');
137        // check the mime type for the file extension
138        if ((lastDot > 0) && (lastDot < (title.length() - 1))) {
139            // remove suffix for Title and NavPos property
140            title = title.substring(0, lastDot);
141        }
142        return title;
143    }
144
145    /**
146     * A factory to return handlers to create new resources.<p>
147     *
148     * @param type the resource type name to get a new resource handler for, as specified in the explorer type settings
149     * @param defaultClassName a default handler class name, to be used if the handler class specified in the explorer type settings cannot be found
150     * @param context the JSP page context
151     * @param req the JSP request
152     * @param res the JSP response
153     * @return a new instance of the handler class
154     * @throws CmsRuntimeException if something goes wrong
155     */
156    public static Object getNewResourceHandler(
157        String type,
158        String defaultClassName,
159        PageContext context,
160        HttpServletRequest req,
161        HttpServletResponse res)
162    throws CmsRuntimeException {
163
164        if (CmsStringUtil.isEmpty(type)) {
165            // it's not possible to hardwire the resource type name on the JSP for Xml content types
166            type = req.getParameter(PARAM_NEWRESOURCETYPE);
167        }
168
169        String className = defaultClassName;
170        CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(type);
171
172        Class<?> clazz = null;
173        try {
174            clazz = Class.forName(className);
175        } catch (ClassNotFoundException e) {
176
177            if (LOG.isErrorEnabled()) {
178                LOG.error(Messages.get().getBundle().key(Messages.ERR_NEW_RES_HANDLER_CLASS_NOT_FOUND_1, className), e);
179            }
180            throw new CmsIllegalArgumentException(
181                Messages.get().container(Messages.ERR_NEW_RES_HANDLER_CLASS_NOT_FOUND_1, className));
182        }
183
184        Object handler = null;
185        try {
186            Constructor<?> constructor = clazz.getConstructor(
187                new Class[] {PageContext.class, HttpServletRequest.class, HttpServletResponse.class});
188            handler = constructor.newInstance(new Object[] {context, req, res});
189        } catch (Exception e) {
190
191            throw new CmsIllegalArgumentException(
192                Messages.get().container(Messages.ERR_NEW_RES_CONSTRUCTOR_NOT_FOUND_1, className));
193        }
194
195        return handler;
196    }
197
198    /**
199     * Creates a single property object and sets the value individual or shared depending on the OpenCms settings.<p>
200     *
201     * @param name the name of the property
202     * @param value the value to set
203     * @return an initialized property object
204     */
205    protected static CmsProperty createPropertyObject(String name, String value) {
206
207        CmsProperty prop = new CmsProperty();
208        prop.setAutoCreatePropertyDefinition(true);
209        prop.setName(name);
210        if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) {
211            prop.setValue(value, CmsProperty.TYPE_INDIVIDUAL);
212        } else {
213            prop.setValue(value, CmsProperty.TYPE_SHARED);
214        }
215        return prop;
216    }
217
218    /**
219     * Returns the properties to create automatically with the new VFS resource.<p>
220     *
221     * If configured, the Title and Navigation properties are set on resource creation.<p>
222     *
223     * @param cms the initialized CmsObject
224     * @param resourceName the full resource name
225     * @param resTypeName the name of the resource type
226     * @param title the Title String to use for the property values
227     * @return the List of initialized property objects
228     */
229    protected static List<CmsProperty> createResourceProperties(
230        CmsObject cms,
231        String resourceName,
232        String resTypeName,
233        String title) {
234
235        // create property values
236        List<CmsProperty> properties = new ArrayList<CmsProperty>(3);
237
238        // get explorer type settings for the resource type
239        CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(resTypeName);
240        if (settings.isAutoSetTitle()) {
241
242            // add the Title property
243            properties.add(createPropertyObject(CmsPropertyDefinition.PROPERTY_TITLE, title));
244        }
245        if (settings.isAutoSetNavigation()) {
246
247            // add the NavText property
248            properties.add(createPropertyObject(CmsPropertyDefinition.PROPERTY_NAVTEXT, title));
249
250            // calculate the new navigation position for the resource
251            List<CmsJspNavElement> navList = new CmsJspNavBuilder(cms).getNavigationForFolder(resourceName);
252            float navPos = 1;
253            if (navList.size() > 0) {
254                CmsJspNavElement nav = navList.get(navList.size() - 1);
255                navPos = nav.getNavPosition() + 1;
256            }
257            // add the NavPos property
258            properties.add(createPropertyObject(CmsPropertyDefinition.PROPERTY_NAVPOS, String.valueOf(navPos)));
259        }
260        return properties;
261    }
262}