001
014
015 package com.liferay.portlet.dynamicdatamapping.action;
016
017 import com.liferay.portal.kernel.servlet.SessionErrors;
018 import com.liferay.portal.kernel.upload.UploadPortletRequest;
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.PortletPreferencesFactoryUtil;
031 import com.liferay.portlet.PortletURLImpl;
032 import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
033 import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
034 import com.liferay.portlet.dynamicdatamapping.TemplateScriptException;
035 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
036 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
037 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
038 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
039 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
040 import com.liferay.util.JS;
041
042 import java.util.Locale;
043 import java.util.Map;
044
045 import javax.portlet.ActionRequest;
046 import javax.portlet.ActionResponse;
047 import javax.portlet.PortletConfig;
048 import javax.portlet.PortletPreferences;
049 import javax.portlet.PortletRequest;
050 import javax.portlet.RenderRequest;
051 import javax.portlet.RenderResponse;
052
053 import org.apache.struts.action.ActionForm;
054 import org.apache.struts.action.ActionForward;
055 import org.apache.struts.action.ActionMapping;
056
057
060 public class EditTemplateAction extends PortletAction {
061
062 @Override
063 public void processAction(
064 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
065 ActionRequest actionRequest, ActionResponse actionResponse)
066 throws Exception {
067
068 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
069
070 DDMTemplate template = null;
071
072 try {
073 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
074 template = updateTemplate(actionRequest);
075 }
076 else if (cmd.equals(Constants.DELETE)) {
077 deleteTemplate(actionRequest);
078 }
079
080 if (Validator.isNotNull(cmd)) {
081 String redirect = ParamUtil.getString(
082 actionRequest, "redirect");
083
084 if (template != null) {
085 boolean saveAndContinue = ParamUtil.getBoolean(
086 actionRequest, "saveAndContinue");
087
088 if (saveAndContinue) {
089 redirect = getSaveAndContinueRedirect(
090 portletConfig, actionRequest, template, redirect);
091 }
092 }
093
094 sendRedirect(actionRequest, actionResponse, redirect);
095 }
096 }
097 catch (Exception e) {
098 if (e instanceof NoSuchTemplateException ||
099 e instanceof PrincipalException) {
100
101 SessionErrors.add(actionRequest, e.getClass().getName());
102
103 setForward(actionRequest, "portlet.dynamic_data_mapping.error");
104 }
105 else if (e instanceof TemplateNameException ||
106 e instanceof TemplateScriptException) {
107
108 SessionErrors.add(actionRequest, e.getClass().getName(), e);
109 }
110 else {
111 throw e;
112 }
113 }
114 }
115
116 @Override
117 public ActionForward render(
118 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
119 RenderRequest renderRequest, RenderResponse renderResponse)
120 throws Exception {
121
122 try {
123 ActionUtil.getStructure(renderRequest);
124 ActionUtil.getTemplate(renderRequest);
125 }
126 catch (Exception e) {
127 if (e instanceof NoSuchTemplateException ||
128 e instanceof PrincipalException) {
129
130 SessionErrors.add(renderRequest, e.getClass().getName());
131
132 return mapping.findForward(
133 "portlet.dynamic_data_mapping.error");
134 }
135 else {
136 throw e;
137 }
138 }
139
140 return mapping.findForward(
141 getForward(
142 renderRequest, "portlet.dynamic_data_mapping.edit_template"));
143 }
144
145 protected void deleteTemplate(ActionRequest actionRequest)
146 throws Exception {
147
148 long templateId = ParamUtil.getLong(actionRequest, "templateId");
149
150 if (templateId > 0) {
151 DDMTemplateServiceUtil.deleteTemplate(templateId);
152 }
153 }
154
155 protected String getSaveAndContinueRedirect(
156 PortletConfig portletConfig, ActionRequest actionRequest,
157 DDMTemplate template, String redirect)
158 throws Exception {
159
160 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
161 WebKeys.THEME_DISPLAY);
162
163 long structureId = ParamUtil.getLong(actionRequest, "structureId");
164 String availableFields = ParamUtil.getString(
165 actionRequest, "availableFields");
166 String saveCallback = ParamUtil.getString(
167 actionRequest, "saveCallback");
168
169 PortletURLImpl portletURL = new PortletURLImpl(
170 actionRequest, portletConfig.getPortletName(),
171 themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
172
173 portletURL.setWindowState(actionRequest.getWindowState());
174
175 portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
176 portletURL.setParameter(
177 "struts_action", "/dynamic_data_mapping/edit_template");
178 portletURL.setParameter("redirect", redirect, false);
179 portletURL.setParameter(
180 "templateId", String.valueOf(template.getTemplateId()), false);
181 portletURL.setParameter(
182 "groupId", String.valueOf(template.getGroupId()), false);
183 portletURL.setParameter(
184 "structureId", String.valueOf(structureId), false);
185 portletURL.setParameter("type", template.getType(), false);
186 portletURL.setParameter("availableFields", availableFields, false);
187 portletURL.setParameter("saveCallback", saveCallback, false);
188
189 return portletURL.toString();
190 }
191
192 protected DDMTemplate updateTemplate(ActionRequest actionRequest)
193 throws Exception {
194
195 UploadPortletRequest uploadPortletRequest =
196 PortalUtil.getUploadPortletRequest(actionRequest);
197
198 long templateId = ParamUtil.getLong(uploadPortletRequest, "templateId");
199
200 long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
201 long structureId = ParamUtil.getLong(
202 uploadPortletRequest, "structureId");
203 Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
204 actionRequest, "name");
205 Map<Locale, String> descriptionMap =
206 LocalizationUtil.getLocalizationMap(actionRequest, "description");
207 String type = ParamUtil.getString(uploadPortletRequest, "type");
208 String mode = ParamUtil.getString(uploadPortletRequest, "mode");
209 String language = ParamUtil.getString(
210 uploadPortletRequest, "language",
211 DDMTemplateConstants.LANG_TYPE_VM);
212
213 String script = ParamUtil.getString(uploadPortletRequest, "script");
214 String scriptContent = JS.decodeURIComponent(
215 ParamUtil.getString(uploadPortletRequest, "scriptContent"));
216
217 if (Validator.isNull(script)) {
218 script = scriptContent;
219 }
220
221 ServiceContext serviceContext = ServiceContextFactory.getInstance(
222 DDMTemplate.class.getName(), actionRequest);
223
224 DDMTemplate template = null;
225
226 if (templateId <= 0) {
227 DDMStructure structure = DDMStructureLocalServiceUtil.getStructure(
228 structureId);
229
230 template = DDMTemplateServiceUtil.addTemplate(
231 groupId, structure.getStructureId(), nameMap, descriptionMap,
232 type, mode, language, script, serviceContext);
233 }
234 else {
235 template = DDMTemplateServiceUtil.updateTemplate(
236 templateId, nameMap, descriptionMap, type, mode, language,
237 script, serviceContext);
238 }
239
240 String portletResource = ParamUtil.getString(
241 actionRequest, "portletResource");
242
243 if (Validator.isNotNull(portletResource)) {
244 PortletPreferences preferences =
245 PortletPreferencesFactoryUtil.getPortletSetup(
246 actionRequest, portletResource);
247
248 if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_DETAIL)) {
249 preferences.setValue(
250 "detailDDMTemplateId",
251 String.valueOf(template.getTemplateId()));
252 }
253 else {
254 preferences.setValue(
255 "listDDMTemplateId",
256 String.valueOf(template.getTemplateId()));
257 }
258
259 preferences.store();
260 }
261
262 return template;
263 }
264
265 }