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.journal.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.LocalizationUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.service.ServiceContextFactory;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.PortletURLImpl;
031    import com.liferay.portlet.journal.DuplicateStructureElementException;
032    import com.liferay.portlet.journal.DuplicateStructureIdException;
033    import com.liferay.portlet.journal.NoSuchStructureException;
034    import com.liferay.portlet.journal.RequiredStructureException;
035    import com.liferay.portlet.journal.StructureIdException;
036    import com.liferay.portlet.journal.StructureInheritanceException;
037    import com.liferay.portlet.journal.StructureNameException;
038    import com.liferay.portlet.journal.StructureXsdException;
039    import com.liferay.portlet.journal.model.JournalStructure;
040    import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
041    import com.liferay.portlet.journal.util.JournalUtil;
042    
043    import java.util.Locale;
044    import java.util.Map;
045    
046    import javax.portlet.ActionRequest;
047    import javax.portlet.ActionResponse;
048    import javax.portlet.PortletConfig;
049    import javax.portlet.PortletRequest;
050    import javax.portlet.RenderRequest;
051    import javax.portlet.RenderResponse;
052    
053    import org.apache.struts.action.ActionForm;
054    import org.apache.struts.action.ActionForward;
055    import org.apache.struts.action.ActionMapping;
056    
057    /**
058     * @author Brian Wing Shun Chan
059     * @author Raymond Augé
060     */
061    public class EditStructureAction extends PortletAction {
062    
063            @Override
064            public void processAction(
065                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
066                            ActionRequest actionRequest, ActionResponse actionResponse)
067                    throws Exception {
068    
069                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
070    
071                    JournalStructure structure = null;
072    
073                    try {
074                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
075                                    structure = updateStructure(actionRequest);
076                            }
077                            else if (cmd.equals(Constants.DELETE)) {
078                                    deleteStructures(actionRequest);
079                            }
080    
081                            if (Validator.isNotNull(cmd)) {
082                                    String redirect = ParamUtil.getString(
083                                            actionRequest, "redirect");
084    
085                                    if (structure != null) {
086                                            boolean saveAndContinue = ParamUtil.getBoolean(
087                                                    actionRequest, "saveAndContinue");
088    
089                                            if (saveAndContinue) {
090                                                    redirect = getSaveAndContinueRedirect(
091                                                            portletConfig, actionRequest, structure, redirect);
092                                            }
093                                    }
094    
095                                    sendRedirect(actionRequest, actionResponse, redirect);
096                            }
097                            else {
098                                    String xsd = ParamUtil.getString(actionRequest, "xsd");
099    
100                                    JournalUtil.validateXSD(xsd);
101                            }
102                    }
103                    catch (Exception e) {
104                            if (e instanceof NoSuchStructureException ||
105                                    e instanceof PrincipalException) {
106    
107                                    SessionErrors.add(actionRequest, e.getClass());
108    
109                                    setForward(actionRequest, "portlet.journal.error");
110                            }
111                            else if (e instanceof DuplicateStructureElementException ||
112                                             e instanceof DuplicateStructureIdException ||
113                                             e instanceof RequiredStructureException ||
114                                             e instanceof StructureIdException ||
115                                             e instanceof StructureInheritanceException ||
116                                             e instanceof StructureNameException ||
117                                             e instanceof StructureXsdException) {
118    
119                                    SessionErrors.add(actionRequest, e.getClass(), e);
120    
121                                    if (e instanceof RequiredStructureException) {
122                                            String redirect = PortalUtil.escapeRedirect(
123                                                    ParamUtil.getString(actionRequest, "redirect"));
124    
125                                            if (Validator.isNotNull(redirect)) {
126                                                    actionResponse.sendRedirect(redirect);
127                                            }
128                                    }
129                            }
130                            else {
131                                    throw e;
132                            }
133                    }
134            }
135    
136            @Override
137            public ActionForward render(
138                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
139                            RenderRequest renderRequest, RenderResponse renderResponse)
140                    throws Exception {
141    
142                    try {
143                            String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
144    
145                            if (!cmd.equals(Constants.ADD)) {
146                                    ActionUtil.getStructure(renderRequest);
147                            }
148                    }
149                    catch (NoSuchStructureException nsse) {
150    
151                            // Let this slide because the user can manually input a structure id
152                            // for a new structure that does not yet exist
153    
154                    }
155                    catch (Exception e) {
156                            if (//e instanceof NoSuchStructureException ||
157                                    e instanceof PrincipalException) {
158    
159                                    SessionErrors.add(renderRequest, e.getClass());
160    
161                                    return mapping.findForward("portlet.journal.error");
162                            }
163                            else {
164                                    throw e;
165                            }
166                    }
167    
168                    return mapping.findForward(
169                            getForward(renderRequest, "portlet.journal.edit_structure"));
170            }
171    
172            protected void deleteStructures(ActionRequest actionRequest)
173                    throws Exception {
174    
175                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
176    
177                    String[] deleteStructureIds = StringUtil.split(
178                            ParamUtil.getString(actionRequest, "deleteStructureIds"));
179    
180                    for (String deleteStructureId : deleteStructureIds) {
181                            JournalStructureServiceUtil.deleteStructure(
182                                    groupId, deleteStructureId);
183    
184                            JournalUtil.removeRecentStructure(actionRequest, deleteStructureId);
185                    }
186            }
187    
188            protected String getSaveAndContinueRedirect(
189                            PortletConfig portletConfig, ActionRequest actionRequest,
190                            JournalStructure structure, String redirect)
191                    throws Exception {
192    
193                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
194                            WebKeys.THEME_DISPLAY);
195    
196                    String originalRedirect = ParamUtil.getString(
197                            actionRequest, "originalRedirect");
198    
199                    PortletURLImpl portletURL = new PortletURLImpl(
200                            actionRequest, portletConfig.getPortletName(),
201                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
202    
203                    portletURL.setWindowState(actionRequest.getWindowState());
204    
205                    portletURL.setParameter("struts_action", "/journal/edit_structure");
206                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
207                    portletURL.setParameter("redirect", redirect, false);
208                    portletURL.setParameter("originalRedirect", originalRedirect, false);
209                    portletURL.setParameter(
210                            "groupId", String.valueOf(structure.getGroupId()), false);
211                    portletURL.setParameter(
212                            "structureId", structure.getStructureId(), false);
213    
214                    return portletURL.toString();
215            }
216    
217            protected JournalStructure updateStructure(ActionRequest actionRequest)
218                    throws Exception {
219    
220                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
221    
222                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
223    
224                    String structureId = ParamUtil.getString(actionRequest, "structureId");
225                    boolean autoStructureId = ParamUtil.getBoolean(
226                            actionRequest, "autoStructureId");
227    
228                    String parentStructureId = ParamUtil.getString(
229                            actionRequest, "parentStructureId");
230                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
231                            actionRequest, "name");
232                    Map<Locale, String> descriptionMap =
233                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
234                    String xsd = ParamUtil.getString(actionRequest, "xsd");
235    
236                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
237                            JournalStructure.class.getName(), actionRequest);
238    
239                    JournalStructure structure = null;
240    
241                    if (cmd.equals(Constants.ADD)) {
242    
243                            // Add structure
244    
245                            structure = JournalStructureServiceUtil.addStructure(
246                                    groupId, structureId, autoStructureId, parentStructureId,
247                                    nameMap, descriptionMap, xsd, serviceContext);
248                    }
249                    else {
250    
251                            // Update structure
252    
253                            structure = JournalStructureServiceUtil.updateStructure(
254                                    groupId, structureId, parentStructureId, nameMap,
255                                    descriptionMap, xsd, serviceContext);
256                    }
257    
258                    // Recent structures
259    
260                    JournalUtil.addRecentStructure(actionRequest, structure);
261    
262                    return structure;
263            }
264    
265    }