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.kernel.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
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.WebKeys;
029    import com.liferay.portlet.PortletURLImpl;
030    import com.liferay.portlet.dynamicdatamapping.NoSuchStructureException;
031    import com.liferay.portlet.dynamicdatamapping.StructureNameException;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
034    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
035    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
036    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
037    
038    import java.util.Locale;
039    import java.util.Map;
040    
041    import javax.portlet.ActionRequest;
042    import javax.portlet.ActionResponse;
043    import javax.portlet.PortletConfig;
044    import javax.portlet.PortletRequest;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Marcellus Tavares
054     */
055    public class CopyStructureAction extends PortletAction {
056    
057            @Override
058            public void processAction(
059                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
060                            ActionRequest actionRequest, ActionResponse actionResponse)
061                    throws Exception {
062    
063                    try {
064                            DDMStructure structure = copyStructure(actionRequest);
065    
066                            String redirect = getSaveAndContinueRedirect(
067                                    portletConfig, actionRequest, structure);
068    
069                            String closeRedirect = ParamUtil.getString(
070                                    actionRequest, "closeRedirect");
071    
072                            if (Validator.isNotNull(closeRedirect)) {
073                                    LiferayPortletConfig liferayPortletConfig =
074                                            (LiferayPortletConfig)portletConfig;
075    
076                                    SessionMessages.add(
077                                            actionRequest,
078                                            liferayPortletConfig.getPortletId() +
079                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
080                                            closeRedirect);
081                            }
082    
083                            sendRedirect(actionRequest, actionResponse, redirect);
084                    }
085                    catch (Exception e) {
086                            if (e instanceof NoSuchStructureException ||
087                                    e instanceof PrincipalException) {
088    
089                                    SessionErrors.add(actionRequest, e.getClass());
090    
091                                    setForward(actionRequest, "portlet.dynamic_data_mapping.error");
092                            }
093                            else if (e instanceof StructureNameException) {
094                                    SessionErrors.add(actionRequest, e.getClass(), e);
095                            }
096                            else {
097                                    throw e;
098                            }
099                    }
100            }
101    
102            @Override
103            public ActionForward render(
104                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105                            RenderRequest renderRequest, RenderResponse renderResponse)
106                    throws Exception {
107    
108                    try {
109                            ActionUtil.getStructure(renderRequest);
110                    }
111                    catch (NoSuchStructureException nsse) {
112    
113                            // Let this slide because the user can manually input a structure
114                            // key for a new structure that does not yet exist
115    
116                    }
117                    catch (Exception e) {
118                            if (e instanceof PrincipalException) {
119                                    SessionErrors.add(renderRequest, e.getClass());
120    
121                                    return mapping.findForward(
122                                            "portlet.dynamic_data_mapping.error");
123                            }
124                            else {
125                                    throw e;
126                            }
127                    }
128    
129                    return mapping.findForward(
130                            getForward(
131                                    renderRequest, "portlet.dynamic_data_mapping.copy_structure"));
132            }
133    
134            protected DDMStructure copyStructure(ActionRequest actionRequest)
135                    throws Exception {
136    
137                    long structureId = ParamUtil.getLong(actionRequest, "structureId");
138    
139                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
140                            actionRequest, "name");
141    
142                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
143                            DDMStructure.class.getName(), actionRequest);
144    
145                    DDMStructure structure = DDMStructureServiceUtil.copyStructure(
146                            structureId, nameMap, null, serviceContext);
147    
148                    copyTemplates(actionRequest, structureId, structure.getStructureId());
149    
150                    return structure;
151            }
152    
153            protected void copyTemplates(
154                            ActionRequest actionRequest, long structureId, long newStructureId)
155                    throws Exception {
156    
157                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
158                            DDMTemplate.class.getName(), actionRequest);
159    
160                    boolean copyDetailTemplates = ParamUtil.getBoolean(
161                            actionRequest, "copyDetailTemplates");
162    
163                    if (copyDetailTemplates) {
164                            DDMTemplateServiceUtil.copyTemplates(
165                                    structureId, newStructureId,
166                                    DDMTemplateConstants.TEMPLATE_TYPE_DETAIL, serviceContext);
167                    }
168    
169                    boolean copyListTemplates = ParamUtil.getBoolean(
170                            actionRequest, "copyListTemplates");
171    
172                    if (copyListTemplates) {
173                            DDMTemplateServiceUtil.copyTemplates(
174                                    structureId, newStructureId,
175                                    DDMTemplateConstants.TEMPLATE_TYPE_LIST, serviceContext);
176                    }
177            }
178    
179            protected String getSaveAndContinueRedirect(
180                            PortletConfig portletConfig, ActionRequest actionRequest,
181                            DDMStructure structure)
182                    throws Exception {
183    
184                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
185                            WebKeys.THEME_DISPLAY);
186    
187                    PortletURLImpl portletURL = new PortletURLImpl(
188                            actionRequest, portletConfig.getPortletName(),
189                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
190    
191                    portletURL.setWindowState(actionRequest.getWindowState());
192    
193                    portletURL.setParameter(
194                            "struts_action", "/dynamic_data_mapping/copy_structure");
195                    portletURL.setParameter(
196                            "structureId", String.valueOf(structure.getStructureId()), false);
197                    portletURL.setParameter(
198                            "copyDetailTemplates",
199                            ParamUtil.getString(actionRequest, "copyDetailTemplates"), false);
200                    portletURL.setParameter(
201                            "copyListTemplates",
202                            ParamUtil.getString(actionRequest, "copyListTemplates"), false);
203    
204                    return portletURL.toString();
205            }
206    
207    }