001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.action;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
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.GetterUtil;
026    import com.liferay.portal.kernel.util.HttpUtil;
027    import com.liferay.portal.kernel.util.LocalizationUtil;
028    import com.liferay.portal.kernel.util.MimeTypesUtil;
029    import com.liferay.portal.kernel.util.ParamUtil;
030    import com.liferay.portal.kernel.util.StringUtil;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.security.auth.PrincipalException;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.service.ServiceContextFactory;
035    import com.liferay.portal.struts.PortletAction;
036    import com.liferay.portal.theme.ThemeDisplay;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.WebKeys;
039    import com.liferay.portlet.PortletURLFactoryUtil;
040    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
041    import com.liferay.portlet.dynamicdatamapping.RequiredTemplateException;
042    import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
043    import com.liferay.portlet.dynamicdatamapping.TemplateScriptException;
044    import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageNameException;
045    import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageSizeException;
046    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
047    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
048    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
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 getSaveAndContinueRedirect(
203                            PortletConfig portletConfig, ActionRequest actionRequest,
204                            DDMTemplate template, String redirect)
205                    throws Exception {
206    
207                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
208                            WebKeys.THEME_DISPLAY);
209    
210                    String portletResourceNamespace = ParamUtil.getString(
211                            actionRequest, "portletResourceNamespace");
212                    long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
213                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
214                    String structureAvailableFields = ParamUtil.getString(
215                            actionRequest, "structureAvailableFields");
216    
217                    LiferayPortletURL portletURL = PortletURLFactoryUtil.create(
218                            actionRequest, portletConfig.getPortletName(),
219                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
220    
221                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
222                    portletURL.setParameter(
223                            "struts_action", "/dynamic_data_mapping/edit_template");
224                    portletURL.setParameter("redirect", redirect, false);
225                    portletURL.setParameter(
226                            "portletResourceNamespace", portletResourceNamespace, false);
227                    portletURL.setParameter(
228                            "templateId", String.valueOf(template.getTemplateId()), false);
229                    portletURL.setParameter(
230                            "groupId", String.valueOf(template.getGroupId()), false);
231                    portletURL.setParameter(
232                            "classNameId", String.valueOf(classNameId), false);
233                    portletURL.setParameter("classPK", String.valueOf(classPK), false);
234                    portletURL.setParameter("type", template.getType(), false);
235                    portletURL.setParameter(
236                            "structureAvailableFields", structureAvailableFields, false);
237                    portletURL.setWindowState(actionRequest.getWindowState());
238    
239                    return portletURL.toString();
240            }
241    
242            protected String getScript(UploadPortletRequest uploadPortletRequest)
243                    throws Exception {
244    
245                    String scriptContent = ParamUtil.getString(
246                            uploadPortletRequest, "scriptContent");
247    
248                    File file = uploadPortletRequest.getFile("script");
249    
250                    if (file == null) {
251                            return scriptContent;
252                    }
253    
254                    String script = FileUtil.read(file);
255    
256                    if (Validator.isNotNull(script) && !isValidFile(file)) {
257                            throw new TemplateScriptException();
258                    }
259    
260                    return GetterUtil.getString(script, scriptContent);
261            }
262    
263            protected boolean isValidFile(File file) {
264                    String contentType = MimeTypesUtil.getContentType(file);
265    
266                    if (contentType.equals(ContentTypes.APPLICATION_XSLT_XML) ||
267                            contentType.startsWith(ContentTypes.TEXT)) {
268    
269                            return true;
270                    }
271    
272                    return false;
273            }
274    
275            protected DDMTemplate updateTemplate(ActionRequest actionRequest)
276                    throws Exception {
277    
278                    UploadPortletRequest uploadPortletRequest =
279                            PortalUtil.getUploadPortletRequest(actionRequest);
280    
281                    long templateId = ParamUtil.getLong(uploadPortletRequest, "templateId");
282    
283                    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
284                    long classNameId = ParamUtil.getLong(
285                            uploadPortletRequest, "classNameId");
286                    long classPK = ParamUtil.getLong(uploadPortletRequest, "classPK");
287                    String templateKey = ParamUtil.getString(
288                            uploadPortletRequest, "templateKey");
289                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
290                            uploadPortletRequest, "name");
291                    Map<Locale, String> descriptionMap =
292                            LocalizationUtil.getLocalizationMap(
293                                    uploadPortletRequest, "description");
294                    String type = ParamUtil.getString(uploadPortletRequest, "type");
295                    String mode = ParamUtil.getString(uploadPortletRequest, "mode");
296                    String language = ParamUtil.getString(
297                            uploadPortletRequest, "language", TemplateConstants.LANG_TYPE_VM);
298    
299                    String script = getScript(uploadPortletRequest);
300    
301                    boolean cacheable = ParamUtil.getBoolean(
302                            uploadPortletRequest, "cacheable");
303                    boolean smallImage = ParamUtil.getBoolean(
304                            uploadPortletRequest, "smallImage");
305                    String smallImageURL = ParamUtil.getString(
306                            uploadPortletRequest, "smallImageURL");
307                    File smallImageFile = uploadPortletRequest.getFile("smallImageFile");
308    
309                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
310                            DDMTemplate.class.getName(), uploadPortletRequest);
311    
312                    DDMTemplate template = null;
313    
314                    if (templateId <= 0) {
315                            template = DDMTemplateServiceUtil.addTemplate(
316                                    groupId, classNameId, classPK, templateKey, nameMap,
317                                    descriptionMap, type, mode, language, script, cacheable,
318                                    smallImage, smallImageURL, smallImageFile, serviceContext);
319                    }
320                    else {
321                            template = DDMTemplateServiceUtil.updateTemplate(
322                                    templateId, classPK, nameMap, descriptionMap, type, mode,
323                                    language, script, cacheable, smallImage, smallImageURL,
324                                    smallImageFile, serviceContext);
325                    }
326    
327                    PortletPreferences portletPreferences = getStrictPortletSetup(
328                            actionRequest);
329    
330                    if (portletPreferences != null) {
331                            if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY)) {
332                                    portletPreferences.setValue(
333                                            "displayDDMTemplateId",
334                                            String.valueOf(template.getTemplateId()));
335                            }
336                            else if (mode.equals(DDMTemplateConstants.TEMPLATE_MODE_CREATE)) {
337                                    portletPreferences.setValue(
338                                            "formDDMTemplateId",
339                                            String.valueOf(template.getTemplateId()));
340                            }
341    
342                            portletPreferences.store();
343                    }
344    
345                    return template;
346            }
347    
348    }