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