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