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