001    /**
002     * Copyright (c) 2000-2013 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
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.FileUtil;
024    import com.liferay.portal.kernel.util.LocalizationUtil;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.StreamUtil;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.service.ServiceContextFactory;
032    import com.liferay.portal.struts.PortletAction;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.PortletPreferencesFactoryUtil;
037    import com.liferay.portlet.PortletURLImpl;
038    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
039    import com.liferay.portlet.dynamicdatamapping.RequiredTemplateException;
040    import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
041    import com.liferay.portlet.dynamicdatamapping.TemplateScriptException;
042    import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageNameException;
043    import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageSizeException;
044    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
045    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
046    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
047    
048    import java.io.File;
049    import java.io.IOException;
050    import java.io.InputStream;
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 mapping, ActionForm form, PortletConfig portletConfig,
075                            ActionRequest actionRequest, ActionResponse actionResponse)
076                    throws Exception {
077    
078                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
079    
080                    DDMTemplate template = null;
081    
082                    try {
083                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
084                                    template = updateTemplate(actionRequest);
085                            }
086                            else if (cmd.equals(Constants.DELETE)) {
087                                    deleteTemplates(actionRequest);
088                            }
089    
090                            if (Validator.isNotNull(cmd)) {
091                                    String redirect = ParamUtil.getString(
092                                            actionRequest, "redirect");
093    
094                                    if (template != null) {
095                                            boolean saveAndContinue = ParamUtil.getBoolean(
096                                                    actionRequest, "saveAndContinue");
097    
098                                            if (saveAndContinue) {
099                                                    redirect = getSaveAndContinueRedirect(
100                                                            portletConfig, actionRequest, template, redirect);
101                                            }
102                                    }
103    
104                                    sendRedirect(actionRequest, actionResponse, redirect);
105                            }
106                    }
107                    catch (Exception e) {
108                            if (e instanceof NoSuchTemplateException ||
109                                    e instanceof PrincipalException) {
110    
111                                    SessionErrors.add(actionRequest, e.getClass());
112    
113                                    setForward(actionRequest, "portlet.dynamic_data_mapping.error");
114                            }
115                            else if (e instanceof RequiredTemplateException ||
116                                             e instanceof TemplateNameException ||
117                                             e instanceof TemplateScriptException ||
118                                             e instanceof TemplateSmallImageNameException ||
119                                             e instanceof TemplateSmallImageSizeException) {
120    
121                                    SessionErrors.add(actionRequest, e.getClass(), e);
122    
123                                    if (e instanceof RequiredTemplateException) {
124                                            String redirect = PortalUtil.escapeRedirect(
125                                                    ParamUtil.getString(actionRequest, "redirect"));
126    
127                                            if (Validator.isNotNull(redirect)) {
128                                                    actionResponse.sendRedirect(redirect);
129                                            }
130                                    }
131                            }
132                            else {
133                                    throw e;
134                            }
135                    }
136            }
137    
138            @Override
139            public ActionForward render(
140                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
141                            RenderRequest renderRequest, RenderResponse renderResponse)
142                    throws Exception {
143    
144                    try {
145                            ActionUtil.getStructure(renderRequest);
146                            ActionUtil.getTemplate(renderRequest);
147                    }
148                    catch (Exception e) {
149                            if (e instanceof NoSuchTemplateException ||
150                                    e instanceof PrincipalException) {
151    
152                                    SessionErrors.add(renderRequest, e.getClass());
153    
154                                    return mapping.findForward(
155                                            "portlet.dynamic_data_mapping.error");
156                            }
157                            else {
158                                    throw e;
159                            }
160                    }
161    
162                    return mapping.findForward(
163                            getForward(
164                                    renderRequest, "portlet.dynamic_data_mapping.edit_template"));
165            }
166    
167            protected void deleteTemplates(ActionRequest actionRequest)
168                    throws Exception {
169    
170                    long[] deleteTemplateIds = null;
171    
172                    long templateId = ParamUtil.getLong(actionRequest, "templateId");
173    
174                    if (templateId > 0) {
175                            deleteTemplateIds = new long[] {templateId};
176                    }
177                    else {
178                            deleteTemplateIds = StringUtil.split(
179                                    ParamUtil.getString(actionRequest, "deleteTemplateIds"), 0L);
180                    }
181    
182                    for (long deleteTemplateId : deleteTemplateIds) {
183                            DDMTemplateServiceUtil.deleteTemplate(deleteTemplateId);
184                    }
185            }
186    
187            protected String getSaveAndContinueRedirect(
188                            PortletConfig portletConfig, ActionRequest actionRequest,
189                            DDMTemplate template, String redirect)
190                    throws Exception {
191    
192                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
193                            WebKeys.THEME_DISPLAY);
194    
195                    long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
196                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
197                    String availableFields = ParamUtil.getString(
198                            actionRequest, "availableFields");
199    
200                    PortletURLImpl portletURL = new PortletURLImpl(
201                            actionRequest, portletConfig.getPortletName(),
202                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
203    
204                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
205                    portletURL.setParameter(
206                            "struts_action", "/dynamic_data_mapping/edit_template");
207                    portletURL.setParameter("redirect", redirect, false);
208                    portletURL.setParameter(
209                            "templateId", String.valueOf(template.getTemplateId()), false);
210                    portletURL.setParameter(
211                            "groupId", String.valueOf(template.getGroupId()), false);
212                    portletURL.setParameter(
213                            "classNameId", String.valueOf(classNameId), false);
214                    portletURL.setParameter("classPK", String.valueOf(classPK), false);
215                    portletURL.setParameter("type", template.getType(), false);
216                    portletURL.setParameter("availableFields", availableFields, false);
217                    portletURL.setWindowState(actionRequest.getWindowState());
218    
219                    return portletURL.toString();
220            }
221    
222            protected String getScript(UploadPortletRequest uploadPortletRequest) {
223                    InputStream inputStream = null;
224    
225                    try {
226                            inputStream = uploadPortletRequest.getFileAsStream("script");
227    
228                            if (inputStream != null) {
229                                    return new String(FileUtil.getBytes(inputStream));
230                            }
231                    }
232                    catch (IOException ioe) {
233                            if (_log.isWarnEnabled()) {
234                                    _log.warn(ioe, ioe);
235                            }
236                    }
237                    finally {
238                            StreamUtil.cleanUp(inputStream);
239                    }
240    
241                    return null;
242            }
243    
244            protected DDMTemplate updateTemplate(ActionRequest actionRequest)
245                    throws Exception {
246    
247                    UploadPortletRequest uploadPortletRequest =
248                            PortalUtil.getUploadPortletRequest(actionRequest);
249    
250                    long templateId = ParamUtil.getLong(uploadPortletRequest, "templateId");
251    
252                    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
253                    long classNameId = ParamUtil.getLong(
254                            uploadPortletRequest, "classNameId");
255                    long classPK = ParamUtil.getLong(uploadPortletRequest, "classPK");
256                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
257                            actionRequest, "name");
258                    Map<Locale, String> descriptionMap =
259                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
260                    String type = ParamUtil.getString(uploadPortletRequest, "type");
261                    String mode = ParamUtil.getString(uploadPortletRequest, "mode");
262                    String language = ParamUtil.getString(
263                            uploadPortletRequest, "language", TemplateConstants.LANG_TYPE_VM);
264    
265                    String script = getScript(uploadPortletRequest);
266                    String scriptContent = ParamUtil.getString(
267                            uploadPortletRequest, "scriptContent");
268    
269                    if (Validator.isNull(script)) {
270                            script = scriptContent;
271                    }
272    
273                    boolean cacheable = ParamUtil.getBoolean(
274                            uploadPortletRequest, "cacheable");
275                    boolean smallImage = ParamUtil.getBoolean(
276                            uploadPortletRequest, "smallImage");
277                    String smallImageURL = ParamUtil.getString(
278                            uploadPortletRequest, "smallImageURL");
279                    File smallImageFile = uploadPortletRequest.getFile("smallImageFile");
280    
281                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
282                            DDMTemplate.class.getName(), actionRequest);
283    
284                    DDMTemplate template = null;
285    
286                    if (templateId <= 0) {
287                            template = DDMTemplateServiceUtil.addTemplate(
288                                    groupId, classNameId, classPK, null, nameMap, descriptionMap,
289                                    type, mode, language, script, cacheable, smallImage,
290                                    smallImageURL, smallImageFile, serviceContext);
291                    }
292                    else {
293                            template = DDMTemplateServiceUtil.updateTemplate(
294                                    templateId, nameMap, descriptionMap, type, mode, language,
295                                    script, cacheable, smallImage, smallImageURL, smallImageFile,
296                                    serviceContext);
297                    }
298    
299                    String portletResource = ParamUtil.getString(
300                            actionRequest, "portletResource");
301    
302                    if (Validator.isNotNull(portletResource)) {
303                            PortletPreferences preferences =
304                                    PortletPreferencesFactoryUtil.getPortletSetup(
305                                            actionRequest, portletResource);
306    
307                            if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY)) {
308                                    preferences.setValue(
309                                            "displayDDMTemplateId",
310                                            String.valueOf(template.getTemplateId()));
311                            }
312                            else {
313                                    preferences.setValue(
314                                            "formDDMTemplateId",
315                                            String.valueOf(template.getTemplateId()));
316                            }
317    
318                            preferences.store();
319                    }
320    
321                    return template;
322            }
323    
324            private static Log _log = LogFactoryUtil.getLog(EditTemplateAction.class);
325    
326    }