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