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