001
014
015 package com.liferay.portlet.dynamicdatamapping.action;
016
017 import com.liferay.portal.LocaleException;
018 import com.liferay.portal.kernel.servlet.SessionErrors;
019 import com.liferay.portal.kernel.util.Constants;
020 import com.liferay.portal.kernel.util.LocalizationUtil;
021 import com.liferay.portal.kernel.util.ParamUtil;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.security.auth.PrincipalException;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portal.service.ServiceContextFactory;
026 import com.liferay.portal.struts.PortletAction;
027 import com.liferay.portal.theme.ThemeDisplay;
028 import com.liferay.portal.util.PortalUtil;
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.RequiredStructureException;
033 import com.liferay.portlet.dynamicdatamapping.StructureDuplicateElementException;
034 import com.liferay.portlet.dynamicdatamapping.StructureNameException;
035 import com.liferay.portlet.dynamicdatamapping.StructureXsdException;
036 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
037 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
038 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
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
059 public class EditStructureAction extends PortletAction {
060
061 @Override
062 public void processAction(
063 ActionMapping actionMapping, ActionForm actionForm,
064 PortletConfig portletConfig, ActionRequest actionRequest,
065 ActionResponse actionResponse)
066 throws Exception {
067
068 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
069
070 DDMStructure structure = null;
071
072 try {
073 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
074 structure = updateStructure(actionRequest);
075 }
076 else if (cmd.equals(Constants.DELETE)) {
077 deleteStructure(actionRequest);
078 }
079
080 if (Validator.isNotNull(cmd)) {
081 String redirect = ParamUtil.getString(
082 actionRequest, "redirect");
083
084 if (structure != null) {
085 boolean saveAndContinue = ParamUtil.getBoolean(
086 actionRequest, "saveAndContinue");
087
088 if (saveAndContinue) {
089 redirect = getSaveAndContinueRedirect(
090 portletConfig, actionRequest, structure, redirect);
091 }
092 }
093
094 sendRedirect(actionRequest, actionResponse, redirect);
095 }
096 }
097 catch (Exception e) {
098 if (e instanceof NoSuchStructureException ||
099 e instanceof PrincipalException) {
100
101 SessionErrors.add(actionRequest, e.getClass());
102
103 setForward(actionRequest, "portlet.dynamic_data_mapping.error");
104 }
105 else if (e instanceof LocaleException ||
106 e instanceof RequiredStructureException ||
107 e instanceof StructureDuplicateElementException ||
108 e instanceof StructureNameException ||
109 e instanceof StructureXsdException) {
110
111 SessionErrors.add(actionRequest, e.getClass(), e);
112
113 if (e instanceof RequiredStructureException) {
114 String redirect = PortalUtil.escapeRedirect(
115 ParamUtil.getString(actionRequest, "redirect"));
116
117 if (Validator.isNotNull(redirect)) {
118 actionResponse.sendRedirect(redirect);
119 }
120 }
121 }
122 else {
123 throw e;
124 }
125 }
126 }
127
128 @Override
129 public ActionForward render(
130 ActionMapping actionMapping, ActionForm actionForm,
131 PortletConfig portletConfig, RenderRequest renderRequest,
132 RenderResponse renderResponse)
133 throws Exception {
134
135 try {
136 String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
137
138 if (!cmd.equals(Constants.ADD)) {
139 ActionUtil.getStructure(renderRequest);
140 }
141 }
142 catch (NoSuchStructureException nsse) {
143
144
145
146
147 }
148 catch (Exception e) {
149 if (
150 e instanceof PrincipalException) {
151
152 SessionErrors.add(renderRequest, e.getClass());
153
154 return actionMapping.findForward(
155 "portlet.dynamic_data_mapping.error");
156 }
157 else {
158 throw e;
159 }
160 }
161
162 return actionMapping.findForward(
163 getForward(
164 renderRequest, "portlet.dynamic_data_mapping.edit_structure"));
165 }
166
167 protected void deleteStructure(ActionRequest actionRequest)
168 throws Exception {
169
170 long structureId = ParamUtil.getLong(actionRequest, "structureId");
171
172 DDMStructureServiceUtil.deleteStructure(structureId);
173 }
174
175 protected String getSaveAndContinueRedirect(
176 PortletConfig portletConfig, ActionRequest actionRequest,
177 DDMStructure structure, String redirect)
178 throws Exception {
179
180 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
181 WebKeys.THEME_DISPLAY);
182
183 String availableFields = ParamUtil.getString(
184 actionRequest, "availableFields");
185 String saveCallback = ParamUtil.getString(
186 actionRequest, "saveCallback");
187
188 PortletURLImpl portletURL = new PortletURLImpl(
189 actionRequest, portletConfig.getPortletName(),
190 themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
191
192 portletURL.setWindowState(actionRequest.getWindowState());
193
194 portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
195 portletURL.setParameter(
196 "struts_action", "/dynamic_data_mapping/edit_structure");
197 portletURL.setParameter("redirect", redirect, false);
198 portletURL.setParameter(
199 "groupId", String.valueOf(structure.getGroupId()), false);
200 portletURL.setParameter(
201 "structureId", String.valueOf(structure.getStructureId()), false);
202 portletURL.setParameter("availableFields", availableFields, false);
203 portletURL.setParameter("saveCallback", saveCallback, false);
204
205 return portletURL.toString();
206 }
207
208 protected DDMStructure updateStructure(ActionRequest actionRequest)
209 throws Exception {
210
211 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
212
213 long structureId = ParamUtil.getLong(actionRequest, "structureId");
214
215 long groupId = ParamUtil.getLong(actionRequest, "groupId");
216 long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
217 Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
218 actionRequest, "name");
219 Map<Locale, String> descriptionMap =
220 LocalizationUtil.getLocalizationMap(actionRequest, "description");
221 String xsd = ParamUtil.getString(actionRequest, "xsd");
222 String storageType = ParamUtil.getString(actionRequest, "storageType");
223
224 ServiceContext serviceContext = ServiceContextFactory.getInstance(
225 DDMStructure.class.getName(), actionRequest);
226
227 DDMStructure structure = null;
228
229 if (cmd.equals(Constants.ADD)) {
230 structure = DDMStructureServiceUtil.addStructure(
231 groupId, classNameId, null, nameMap, descriptionMap, xsd,
232 storageType, DDMStructureConstants.TYPE_DEFAULT,
233 serviceContext);
234 }
235 else if (cmd.equals(Constants.UPDATE)) {
236 structure = DDMStructureServiceUtil.updateStructure(
237 structureId, nameMap, descriptionMap, xsd, serviceContext);
238 }
239
240 return structure;
241 }
242
243 }