001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.template.TemplateConstants;
021    import com.liferay.portal.kernel.upload.UploadPortletRequest;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.ContentTypes;
024    import com.liferay.portal.kernel.util.FileUtil;
025    import com.liferay.portal.kernel.util.HttpUtil;
026    import com.liferay.portal.kernel.util.LocalizationUtil;
027    import com.liferay.portal.kernel.util.MimeTypesUtil;
028    import com.liferay.portal.kernel.util.ParamUtil;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.security.auth.PrincipalException;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.ServiceContextFactory;
034    import com.liferay.portal.struts.PortletAction;
035    import com.liferay.portal.theme.ThemeDisplay;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portal.util.WebKeys;
038    import com.liferay.portlet.PortletURLImpl;
039    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
040    import com.liferay.portlet.dynamicdatamapping.RequiredTemplateException;
041    import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
042    import com.liferay.portlet.dynamicdatamapping.TemplateScriptException;
043    import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageNameException;
044    import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageSizeException;
045    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
046    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
047    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
048    import com.liferay.portlet.dynamicdatamapping.util.DDMXSDUtil;
049    
050    import java.io.File;
051    
052    import java.util.Locale;
053    import java.util.Map;
054    
055    import javax.portlet.ActionRequest;
056    import javax.portlet.ActionResponse;
057    import javax.portlet.PortletConfig;
058    import javax.portlet.PortletPreferences;
059    import javax.portlet.PortletRequest;
060    import javax.portlet.RenderRequest;
061    import javax.portlet.RenderResponse;
062    
063    import org.apache.struts.action.ActionForm;
064    import org.apache.struts.action.ActionForward;
065    import org.apache.struts.action.ActionMapping;
066    
067    /**
068     * @author Eduardo Lundgren
069     */
070    public class EditTemplateAction extends PortletAction {
071    
072            @Override
073            public void processAction(
074                            ActionMapping actionMapping, ActionForm actionForm,
075                            PortletConfig portletConfig, ActionRequest actionRequest,
076                            ActionResponse actionResponse)
077                    throws Exception {
078    
079                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
080    
081                    DDMTemplate template = null;
082    
083                    try {
084                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
085                                    template = updateTemplate(actionRequest);
086                            }
087                            else if (cmd.equals(Constants.DELETE)) {
088                                    deleteTemplates(actionRequest);
089                            }
090    
091                            if (Validator.isNotNull(cmd)) {
092                                    String redirect = ParamUtil.getString(
093                                            actionRequest, "redirect");
094                                    String closeRedirect = ParamUtil.getString(
095                                            actionRequest, "closeRedirect");
096    
097                                    if (Validator.isNotNull(closeRedirect)) {
098                                            redirect = HttpUtil.setParameter(
099                                                    redirect, "closeRedirect", closeRedirect);
100    
101                                            SessionMessages.add(
102                                                    actionRequest,
103                                                    PortalUtil.getPortletId(actionRequest) +
104                                                            SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
105                                                    closeRedirect);
106                                    }
107    
108                                    if (template != null) {
109                                            boolean saveAndContinue = ParamUtil.getBoolean(
110                                                    actionRequest, "saveAndContinue");
111    
112                                            if (saveAndContinue) {
113                                                    redirect = getSaveAndContinueRedirect(
114                                                            portletConfig, actionRequest, template, redirect);
115                                            }
116                                    }
117    
118                                    sendRedirect(actionRequest, actionResponse, redirect);
119                            }
120                    }
121                    catch (Exception e) {
122                            if (e instanceof NoSuchTemplateException ||
123                                    e instanceof PrincipalException) {
124    
125                                    SessionErrors.add(actionRequest, e.getClass());
126    
127                                    setForward(actionRequest, "portlet.dynamic_data_mapping.error");
128                            }
129                            else if (e instanceof RequiredTemplateException ||
130                                             e instanceof TemplateNameException ||
131                                             e instanceof TemplateScriptException ||
132                                             e instanceof TemplateSmallImageNameException ||
133                                             e instanceof TemplateSmallImageSizeException) {
134    
135                                    SessionErrors.add(actionRequest, e.getClass(), e);
136    
137                                    if (e instanceof RequiredTemplateException) {
138                                            String redirect = PortalUtil.escapeRedirect(
139                                                    ParamUtil.getString(actionRequest, "redirect"));
140    
141                                            if (Validator.isNotNull(redirect)) {
142                                                    actionResponse.sendRedirect(redirect);
143                                            }
144                                    }
145                            }
146                            else {
147                                    throw e;
148                            }
149                    }
150            }
151    
152            @Override
153            public ActionForward render(
154                            ActionMapping actionMapping, ActionForm actionForm,
155                            PortletConfig portletConfig, RenderRequest renderRequest,
156                            RenderResponse renderResponse)
157                    throws Exception {
158    
159                    try {
160                            ActionUtil.getStructure(renderRequest);
161                            ActionUtil.getTemplate(renderRequest);
162                    }
163                    catch (Exception e) {
164                            if (e instanceof NoSuchTemplateException ||
165                                    e instanceof PrincipalException) {
166    
167                                    SessionErrors.add(renderRequest, e.getClass());
168    
169                                    return actionMapping.findForward(
170                                            "portlet.dynamic_data_mapping.error");
171                            }
172                            else {
173                                    throw e;
174                            }
175                    }
176    
177                    return actionMapping.findForward(
178                            getForward(
179                                    renderRequest, "portlet.dynamic_data_mapping.edit_template"));
180            }
181    
182            protected void deleteTemplates(ActionRequest actionRequest)
183                    throws Exception {
184    
185                    long[] deleteTemplateIds = null;
186    
187                    long templateId = ParamUtil.getLong(actionRequest, "templateId");
188    
189                    if (templateId > 0) {
190                            deleteTemplateIds = new long[] {templateId};
191                    }
192                    else {
193                            deleteTemplateIds = StringUtil.split(
194                                    ParamUtil.getString(actionRequest, "deleteTemplateIds"), 0L);
195                    }
196    
197                    for (long deleteTemplateId : deleteTemplateIds) {
198                            DDMTemplateServiceUtil.deleteTemplate(deleteTemplateId);
199                    }
200            }
201    
202            protected String getFileScriptContent(
203                            UploadPortletRequest uploadPortletRequest)
204                    throws Exception {
205    
206                    File file = uploadPortletRequest.getFile("script");
207    
208                    if (file == null) {
209                            return null;
210                    }
211    
212                    String fileScriptContent = FileUtil.read(file);
213    
214                    if (Validator.isNotNull(fileScriptContent) && !isValidFile(file)) {
215                            throw new TemplateScriptException();
216                    }
217    
218                    return fileScriptContent;
219            }
220    
221            protected String getSaveAndContinueRedirect(
222                            PortletConfig portletConfig, ActionRequest actionRequest,
223                            DDMTemplate template, String redirect)
224                    throws Exception {
225    
226                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
227                            WebKeys.THEME_DISPLAY);
228    
229                    String portletResourceNamespace = ParamUtil.getString(
230                            actionRequest, "portletResourceNamespace");
231                    long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
232                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
233                    String structureAvailableFields = ParamUtil.getString(
234                            actionRequest, "structureAvailableFields");
235    
236                    PortletURLImpl portletURL = new PortletURLImpl(
237                            actionRequest, portletConfig.getPortletName(),
238                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
239    
240                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
241                    portletURL.setParameter(
242                            "struts_action", "/dynamic_data_mapping/edit_template");
243                    portletURL.setParameter("redirect", redirect, false);
244                    portletURL.setParameter(
245                            "portletResourceNamespace", portletResourceNamespace, false);
246                    portletURL.setParameter(
247                            "templateId", String.valueOf(template.getTemplateId()), false);
248                    portletURL.setParameter(
249                            "groupId", String.valueOf(template.getGroupId()), false);
250                    portletURL.setParameter(
251                            "classNameId", String.valueOf(classNameId), false);
252                    portletURL.setParameter("classPK", String.valueOf(classPK), false);
253                    portletURL.setParameter("type", template.getType(), false);
254                    portletURL.setParameter(
255                            "structureAvailableFields", structureAvailableFields, false);
256                    portletURL.setWindowState(actionRequest.getWindowState());
257    
258                    return portletURL.toString();
259            }
260    
261            protected String getScript(UploadPortletRequest uploadPortletRequest)
262                    throws Exception {
263    
264                    String fileScriptContent = getFileScriptContent(uploadPortletRequest);
265    
266                    if (Validator.isNotNull(fileScriptContent)) {
267                            return fileScriptContent;
268                    }
269    
270                    String scriptContent = ParamUtil.getString(
271                            uploadPortletRequest, "scriptContent");
272    
273                    String type = ParamUtil.getString(uploadPortletRequest, "type");
274    
275                    if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_FORM)) {
276                            try {
277                                    scriptContent = DDMXSDUtil.getXSD(scriptContent);
278                            }
279                            catch (PortalException pe) {
280                                    throw new TemplateScriptException();
281                            }
282                    }
283    
284                    return scriptContent;
285            }
286    
287            protected boolean isValidFile(File file) {
288                    String contentType = MimeTypesUtil.getContentType(file);
289    
290                    if (contentType.equals(ContentTypes.APPLICATION_XSLT_XML) ||
291                            contentType.startsWith(ContentTypes.TEXT)) {
292    
293                            return true;
294                    }
295    
296                    return false;
297            }
298    
299            protected DDMTemplate updateTemplate(ActionRequest actionRequest)
300                    throws Exception {
301    
302                    UploadPortletRequest uploadPortletRequest =
303                            PortalUtil.getUploadPortletRequest(actionRequest);
304    
305                    long templateId = ParamUtil.getLong(uploadPortletRequest, "templateId");
306    
307                    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
308                    long classNameId = ParamUtil.getLong(
309                            uploadPortletRequest, "classNameId");
310                    long classPK = ParamUtil.getLong(uploadPortletRequest, "classPK");
311                    String templateKey = ParamUtil.getString(
312                            uploadPortletRequest, "templateKey");
313                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
314                            uploadPortletRequest, "name");
315                    Map<Locale, String> descriptionMap =
316                            LocalizationUtil.getLocalizationMap(
317                                    uploadPortletRequest, "description");
318                    String type = ParamUtil.getString(uploadPortletRequest, "type");
319                    String mode = ParamUtil.getString(uploadPortletRequest, "mode");
320                    String language = ParamUtil.getString(
321                            uploadPortletRequest, "language", TemplateConstants.LANG_TYPE_VM);
322    
323                    String script = getScript(uploadPortletRequest);
324    
325                    boolean cacheable = ParamUtil.getBoolean(
326                            uploadPortletRequest, "cacheable");
327                    boolean smallImage = ParamUtil.getBoolean(
328                            uploadPortletRequest, "smallImage");
329                    String smallImageURL = ParamUtil.getString(
330                            uploadPortletRequest, "smallImageURL");
331                    File smallImageFile = uploadPortletRequest.getFile("smallImageFile");
332    
333                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
334                            DDMTemplate.class.getName(), uploadPortletRequest);
335    
336                    DDMTemplate template = null;
337    
338                    if (templateId <= 0) {
339                            template = DDMTemplateServiceUtil.addTemplate(
340                                    groupId, classNameId, classPK, templateKey, nameMap,
341                                    descriptionMap, type, mode, language, script, cacheable,
342                                    smallImage, smallImageURL, smallImageFile, serviceContext);
343                    }
344                    else {
345                            template = DDMTemplateServiceUtil.updateTemplate(
346                                    templateId, classPK, nameMap, descriptionMap, type, mode,
347                                    language, script, cacheable, smallImage, smallImageURL,
348                                    smallImageFile, serviceContext);
349                    }
350    
351                    PortletPreferences portletPreferences = getStrictPortletSetup(
352                            actionRequest);
353    
354                    if (portletPreferences != null) {
355                            if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY)) {
356                                    portletPreferences.setValue(
357                                            "displayDDMTemplateId",
358                                            String.valueOf(template.getTemplateId()));
359                            }
360                            else {
361                                    portletPreferences.setValue(
362                                            "formDDMTemplateId",
363                                            String.valueOf(template.getTemplateId()));
364                            }
365    
366                            portletPreferences.store();
367                    }
368    
369                    return template;
370            }
371    
372    }