001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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, 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.ade.postupload;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.ade.postupload.shared.CmsPostUploadDialogBean;
032import org.opencms.ade.postupload.shared.CmsPostUploadDialogPanelBean;
033import org.opencms.ade.postupload.shared.I_CmsDialogConstants;
034import org.opencms.ade.postupload.shared.rpc.I_CmsPostUploadDialogService;
035import org.opencms.file.CmsProperty;
036import org.opencms.file.CmsPropertyDefinition;
037import org.opencms.file.CmsResource;
038import org.opencms.file.types.I_CmsResourceType;
039import org.opencms.flex.CmsFlexController;
040import org.opencms.gwt.CmsGwtService;
041import org.opencms.gwt.CmsPropertyEditorHelper;
042import org.opencms.gwt.CmsRpcException;
043import org.opencms.gwt.CmsVfsService;
044import org.opencms.gwt.shared.CmsListInfoBean;
045import org.opencms.gwt.shared.property.CmsClientProperty;
046import org.opencms.gwt.shared.property.CmsPropertyModification;
047import org.opencms.main.CmsException;
048import org.opencms.main.OpenCms;
049import org.opencms.util.CmsMacroResolver;
050import org.opencms.util.CmsStringUtil;
051import org.opencms.util.CmsUUID;
052import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
053import org.opencms.xml.content.CmsXmlContentProperty;
054import org.opencms.xml.content.CmsXmlContentPropertyHelper;
055
056import java.util.HashSet;
057import java.util.LinkedHashMap;
058import java.util.List;
059import java.util.Map;
060import java.util.Set;
061
062import javax.servlet.http.HttpServletRequest;
063
064/**
065 * The service implementation for the org.opencms.ade.postupload module.<p>
066 */
067public class CmsPostUploadDialogService extends CmsGwtService implements I_CmsPostUploadDialogService {
068
069    /** Serial version id. */
070    private static final long serialVersionUID = 1L;
071
072    /**
073     * Creates a new instance.<p>
074     */
075    public CmsPostUploadDialogService() {
076
077        super();
078    }
079
080    /**
081     * Fetches the dialog data.<p>
082     *
083     * @param request the servlet request
084     *
085     * @return the dialog data
086     * @throws CmsRpcException if something goes wrong
087     */
088    public static CmsPostUploadDialogBean prefetch(HttpServletRequest request) throws CmsRpcException {
089
090        CmsPostUploadDialogService srv = new CmsPostUploadDialogService();
091        srv.setCms(CmsFlexController.getCmsObject(request));
092        srv.setRequest(request);
093        CmsPostUploadDialogBean result = null;
094        try {
095            result = srv.prefetch();
096        } finally {
097            srv.clearThreadStorage();
098        }
099        return result;
100    }
101
102    /**
103     * @see org.opencms.ade.postupload.shared.rpc.I_CmsPostUploadDialogService#load(org.opencms.util.CmsUUID, boolean,boolean)
104     */
105    public CmsPostUploadDialogPanelBean load(CmsUUID id, boolean useConfiguration, boolean addBasicProperties)
106    throws CmsRpcException {
107
108        try {
109            CmsResource res = getCmsObject().readResource(id);
110            List<CmsProperty> properties = getCmsObject().readPropertyObjects(res, false);
111            String title = CmsProperty.get(CmsPropertyDefinition.PROPERTY_TITLE, properties).getValue();
112            if (title == null) {
113                title = res.getName();
114            }
115            String description = CmsProperty.get(CmsPropertyDefinition.PROPERTY_DESCRIPTION, properties).getValue();
116            if (description == null) {
117                description = getCmsObject().getSitePath(res);
118            }
119            CmsListInfoBean listInfo = CmsVfsService.getPageInfo(getCmsObject(), res);
120
121            CmsPostUploadDialogPanelBean result = new CmsPostUploadDialogPanelBean(id, listInfo);
122
123            I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(res.getTypeId());
124            String typeName = type.getTypeName();
125            listInfo.setResourceType(typeName);
126
127            CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(typeName);
128
129            List<String> defaultProperties = settings.getProperties();
130            while (properties.isEmpty() && !CmsStringUtil.isEmptyOrWhitespaceOnly(settings.getReference())) {
131                settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(settings.getReference());
132                defaultProperties = settings.getProperties();
133            }
134
135            Map<String, CmsXmlContentProperty> propertyDefinitions = new LinkedHashMap<String, CmsXmlContentProperty>();
136            Map<String, CmsClientProperty> clientProperties = new LinkedHashMap<String, CmsClientProperty>();
137
138            // add the file name to the list of properties to allow renaming the uploaded file
139            CmsXmlContentProperty fileNamePropDef = new CmsXmlContentProperty(
140                CmsPropertyModification.FILE_NAME_PROPERTY,
141                "string",
142                "string",
143                "",
144                "",
145                "",
146                "",
147                Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(getCmsObject())).key(
148                    Messages.GUI_UPLOAD_FILE_NAME_0),
149                "",
150                "",
151                "false");
152            propertyDefinitions.put(CmsPropertyModification.FILE_NAME_PROPERTY, fileNamePropDef);
153            clientProperties.put(
154                CmsPropertyModification.FILE_NAME_PROPERTY,
155                new CmsClientProperty(CmsPropertyModification.FILE_NAME_PROPERTY, res.getName(), res.getName()));
156
157            CmsADEConfigData configData = OpenCms.getADEManager().lookupConfiguration(
158                getCmsObject(),
159                res.getRootPath());
160            Map<String, CmsXmlContentProperty> propertyConfiguration = configData.getPropertyConfigurationAsMap();
161
162            Set<String> propertiesToShow = new HashSet<String>();
163            propertiesToShow.addAll(defaultProperties);
164            if (addBasicProperties) {
165                propertiesToShow.addAll(propertyConfiguration.keySet());
166            }
167            for (String propertyName : propertiesToShow) {
168                CmsXmlContentProperty propDef = null;
169                if (useConfiguration) {
170                    propDef = propertyConfiguration.get(propertyName);
171                }
172                if (propDef == null) {
173                    propDef = new CmsXmlContentProperty(
174                        propertyName,
175                        "string",
176                        "string",
177                        "",
178                        "",
179                        "",
180                        "",
181                        null,
182                        "",
183                        "",
184                        "false");
185                }
186                propertyDefinitions.put(propertyName, propDef);
187                CmsProperty property = CmsProperty.get(propertyName, properties);
188                if (property != null) {
189                    CmsClientProperty clientProperty = new CmsClientProperty(
190                        propertyName,
191                        property.getStructureValue(),
192                        property.getResourceValue());
193                    clientProperties.put(clientProperty.getName(), clientProperty);
194                }
195            }
196
197            propertyDefinitions = CmsXmlContentPropertyHelper.resolveMacrosInProperties(
198                propertyDefinitions,
199                CmsMacroResolver.newWorkplaceLocaleResolver(getCmsObject()));
200
201            CmsPropertyEditorHelper.updateWysiwygConfig(propertyDefinitions, getCmsObject(), res);
202
203            result.setPropertyDefinitions(propertyDefinitions);
204            result.setProperties(clientProperties);
205            return result;
206        } catch (CmsException e) {
207            error(e);
208            return null; // will never be reached
209        }
210    }
211
212    /**
213     * @see org.opencms.ade.postupload.shared.rpc.I_CmsPostUploadDialogService#prefetch()
214     */
215    public CmsPostUploadDialogBean prefetch() throws CmsRpcException {
216
217        try {
218
219            Map<CmsUUID, String> uuids = new LinkedHashMap<CmsUUID, String>();
220
221            if ((CmsStringUtil.isNotEmptyOrWhitespaceOnly(
222                getRequest().getParameter(I_CmsDialogConstants.PARAM_RESOURCES)))) {
223                // if the request parameter resources exists and contains a list of UUIDs
224                // this dialog is used as upload hook
225                String resourcesParam = getRequest().getParameter(I_CmsDialogConstants.PARAM_RESOURCES);
226                List<String> resourceUUIDs = CmsStringUtil.splitAsList(resourcesParam, ",");
227                for (String uuidAsString : resourceUUIDs) {
228                    CmsUUID uuid = new CmsUUID(uuidAsString);
229                    CmsResource res = getCmsObject().readResource(uuid);
230                    String resPath = getCmsObject().getRequestContext().removeSiteRoot(res.getRootPath());
231                    uuids.put(uuid, resPath);
232                }
233            } else if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getRequest().getParameter("resource"))) {
234                // if there was no parameter "resources" set as request parameter
235                // this dialog is not used as upload hook try to read the resource parameter
236                String resourceParam = getRequest().getParameter("resource");
237                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(resourceParam)) {
238                    CmsResource res = getCmsObject().readResource(resourceParam);
239                    String resPath = getCmsObject().getRequestContext().removeSiteRoot(res.getRootPath());
240                    uuids.put(res.getStructureId(), resPath);
241                }
242            }
243
244            return new CmsPostUploadDialogBean(uuids);
245        } catch (CmsException e) {
246            error(e);
247            return null; // will never be reached
248        }
249    }
250
251}