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