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