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