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