001
014
015 package com.liferay.portlet.dynamicdatamapping.action;
016
017 import com.liferay.portal.LocaleException;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.servlet.SessionErrors;
020 import com.liferay.portal.kernel.servlet.SessionMessages;
021 import com.liferay.portal.kernel.util.Constants;
022 import com.liferay.portal.kernel.util.HttpUtil;
023 import com.liferay.portal.kernel.util.LocalizationUtil;
024 import com.liferay.portal.kernel.util.ParamUtil;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.security.auth.PrincipalException;
028 import com.liferay.portal.service.ServiceContext;
029 import com.liferay.portal.service.ServiceContextFactory;
030 import com.liferay.portal.struts.PortletAction;
031 import com.liferay.portal.theme.ThemeDisplay;
032 import com.liferay.portal.util.PortalUtil;
033 import com.liferay.portal.util.WebKeys;
034 import com.liferay.portlet.PortletURLImpl;
035 import com.liferay.portlet.dynamicdatamapping.NoSuchStructureException;
036 import com.liferay.portlet.dynamicdatamapping.RequiredStructureException;
037 import com.liferay.portlet.dynamicdatamapping.StructureDefinitionException;
038 import com.liferay.portlet.dynamicdatamapping.StructureDuplicateElementException;
039 import com.liferay.portlet.dynamicdatamapping.StructureNameException;
040 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
041 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
042 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
043 import com.liferay.portlet.dynamicdatamapping.util.DDMXSDUtil;
044
045 import java.util.Locale;
046 import java.util.Map;
047
048 import javax.portlet.ActionRequest;
049 import javax.portlet.ActionResponse;
050 import javax.portlet.PortletConfig;
051 import javax.portlet.PortletRequest;
052 import javax.portlet.RenderRequest;
053 import javax.portlet.RenderResponse;
054
055 import org.apache.struts.action.ActionForm;
056 import org.apache.struts.action.ActionForward;
057 import org.apache.struts.action.ActionMapping;
058
059
064 public class EditStructureAction extends PortletAction {
065
066 @Override
067 public void processAction(
068 ActionMapping actionMapping, ActionForm actionForm,
069 PortletConfig portletConfig, ActionRequest actionRequest,
070 ActionResponse actionResponse)
071 throws Exception {
072
073 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
074
075 DDMStructure structure = null;
076
077 try {
078 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
079 structure = updateStructure(actionRequest);
080 }
081 else if (cmd.equals(Constants.DELETE)) {
082 deleteStructures(actionRequest);
083 }
084
085 if (Validator.isNotNull(cmd)) {
086 String redirect = ParamUtil.getString(
087 actionRequest, "redirect");
088 String closeRedirect = ParamUtil.getString(
089 actionRequest, "closeRedirect");
090
091 if (Validator.isNotNull(closeRedirect)) {
092 redirect = HttpUtil.setParameter(
093 redirect, "closeRedirect", closeRedirect);
094
095 SessionMessages.add(
096 actionRequest,
097 PortalUtil.getPortletId(actionRequest) +
098 SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
099 closeRedirect);
100 }
101
102 if (structure != null) {
103 boolean saveAndContinue = ParamUtil.getBoolean(
104 actionRequest, "saveAndContinue");
105
106 if (saveAndContinue) {
107 redirect = getSaveAndContinueRedirect(
108 portletConfig, actionRequest, structure, redirect);
109 }
110 }
111
112 sendRedirect(actionRequest, actionResponse, redirect);
113 }
114 }
115 catch (Exception e) {
116 if (e instanceof NoSuchStructureException ||
117 e instanceof PrincipalException) {
118
119 SessionErrors.add(actionRequest, e.getClass());
120
121 setForward(actionRequest, "portlet.dynamic_data_mapping.error");
122 }
123 else if (e instanceof LocaleException ||
124 e instanceof RequiredStructureException ||
125 e instanceof StructureDefinitionException ||
126 e instanceof StructureDuplicateElementException ||
127 e instanceof StructureNameException) {
128
129 SessionErrors.add(actionRequest, e.getClass(), e);
130
131 if (e instanceof RequiredStructureException) {
132 String redirect = PortalUtil.escapeRedirect(
133 ParamUtil.getString(actionRequest, "redirect"));
134
135 if (Validator.isNotNull(redirect)) {
136 actionResponse.sendRedirect(redirect);
137 }
138 }
139 }
140 else {
141 throw e;
142 }
143 }
144 }
145
146 @Override
147 public ActionForward render(
148 ActionMapping actionMapping, ActionForm actionForm,
149 PortletConfig portletConfig, RenderRequest renderRequest,
150 RenderResponse renderResponse)
151 throws Exception {
152
153 try {
154 String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
155
156 if (!cmd.equals(Constants.ADD)) {
157 ActionUtil.getStructure(renderRequest);
158 }
159 }
160 catch (NoSuchStructureException nsse) {
161
162
163
164
165 }
166 catch (Exception e) {
167 if (
168 e instanceof PrincipalException) {
169
170 SessionErrors.add(renderRequest, e.getClass());
171
172 return actionMapping.findForward(
173 "portlet.dynamic_data_mapping.error");
174 }
175 else {
176 throw e;
177 }
178 }
179
180 return actionMapping.findForward(
181 getForward(
182 renderRequest, "portlet.dynamic_data_mapping.edit_structure"));
183 }
184
185 protected void deleteStructures(ActionRequest actionRequest)
186 throws Exception {
187
188 long[] deleteStructureIds = null;
189
190 long structureId = ParamUtil.getLong(actionRequest, "classPK");
191
192 if (structureId > 0) {
193 deleteStructureIds = new long[] {structureId};
194 }
195 else {
196 deleteStructureIds = StringUtil.split(
197 ParamUtil.getString(actionRequest, "deleteStructureIds"), 0L);
198 }
199
200 for (long deleteStructureId : deleteStructureIds) {
201 DDMStructureServiceUtil.deleteStructure(deleteStructureId);
202 }
203 }
204
205 protected String getDefinition(ActionRequest actionRequest)
206 throws PortalException {
207
208 try {
209 String definition = ParamUtil.getString(
210 actionRequest, "definition");
211
212 return DDMXSDUtil.getXSD(definition);
213 }
214 catch (PortalException pe) {
215 throw new StructureDefinitionException(pe);
216 }
217 }
218
219 protected String getSaveAndContinueRedirect(
220 PortletConfig portletConfig, ActionRequest actionRequest,
221 DDMStructure structure, String redirect)
222 throws Exception {
223
224 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
225 WebKeys.THEME_DISPLAY);
226
227 String availableFields = ParamUtil.getString(
228 actionRequest, "availableFields");
229 String eventName = ParamUtil.getString(actionRequest, "eventName");
230
231 PortletURLImpl portletURL = new PortletURLImpl(
232 actionRequest, portletConfig.getPortletName(),
233 themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
234
235 portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
236 portletURL.setParameter(
237 "struts_action", "/dynamic_data_mapping/edit_structure");
238 portletURL.setParameter("redirect", redirect, false);
239 portletURL.setParameter(
240 "groupId", String.valueOf(structure.getGroupId()), false);
241
242 long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
243
244 portletURL.setParameter(
245 "classNameId", String.valueOf(classNameId), false);
246
247 portletURL.setParameter(
248 "classPK", String.valueOf(structure.getStructureId()), false);
249 portletURL.setParameter("availableFields", availableFields, false);
250 portletURL.setParameter("eventName", eventName, false);
251 portletURL.setWindowState(actionRequest.getWindowState());
252
253 return portletURL.toString();
254 }
255
256 protected DDMStructure updateStructure(ActionRequest actionRequest)
257 throws Exception {
258
259 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
260
261 long classPK = ParamUtil.getLong(actionRequest, "classPK");
262
263 long groupId = ParamUtil.getLong(actionRequest, "groupId");
264 long scopeClassNameId = ParamUtil.getLong(
265 actionRequest, "scopeClassNameId");
266 String structureKey = ParamUtil.getString(
267 actionRequest, "structureKey");
268 long parentStructureId = ParamUtil.getLong(
269 actionRequest, "parentStructureId",
270 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID);
271 Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
272 actionRequest, "name");
273 Map<Locale, String> descriptionMap =
274 LocalizationUtil.getLocalizationMap(actionRequest, "description");
275 String definition = getDefinition(actionRequest);
276 String storageType = ParamUtil.getString(actionRequest, "storageType");
277
278 ServiceContext serviceContext = ServiceContextFactory.getInstance(
279 DDMStructure.class.getName(), actionRequest);
280
281 DDMStructure structure = null;
282
283 if (cmd.equals(Constants.ADD)) {
284 structure = DDMStructureServiceUtil.addStructure(
285 groupId, parentStructureId, scopeClassNameId, structureKey,
286 nameMap, descriptionMap, definition, storageType,
287 DDMStructureConstants.TYPE_DEFAULT, serviceContext);
288 }
289 else if (cmd.equals(Constants.UPDATE)) {
290 structure = DDMStructureServiceUtil.updateStructure(
291 classPK, parentStructureId, nameMap, descriptionMap, definition,
292 serviceContext);
293 }
294
295 return structure;
296 }
297
298 }