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.template.TemplateConstants;
020 import com.liferay.portal.kernel.upload.UploadPortletRequest;
021 import com.liferay.portal.kernel.util.Constants;
022 import com.liferay.portal.kernel.util.ContentTypes;
023 import com.liferay.portal.kernel.util.FileUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.HttpUtil;
026 import com.liferay.portal.kernel.util.LocalizationUtil;
027 import com.liferay.portal.kernel.util.MimeTypesUtil;
028 import com.liferay.portal.kernel.util.ParamUtil;
029 import com.liferay.portal.kernel.util.StringUtil;
030 import com.liferay.portal.kernel.util.Validator;
031 import com.liferay.portal.security.auth.PrincipalException;
032 import com.liferay.portal.service.ServiceContext;
033 import com.liferay.portal.service.ServiceContextFactory;
034 import com.liferay.portal.struts.PortletAction;
035 import com.liferay.portal.theme.ThemeDisplay;
036 import com.liferay.portal.util.PortalUtil;
037 import com.liferay.portal.util.WebKeys;
038 import com.liferay.portlet.PortletURLImpl;
039 import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
040 import com.liferay.portlet.dynamicdatamapping.RequiredTemplateException;
041 import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
042 import com.liferay.portlet.dynamicdatamapping.TemplateScriptException;
043 import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageNameException;
044 import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageSizeException;
045 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
046 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
047 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
048
049 import java.io.File;
050
051 import java.util.Locale;
052 import java.util.Map;
053
054 import javax.portlet.ActionRequest;
055 import javax.portlet.ActionResponse;
056 import javax.portlet.PortletConfig;
057 import javax.portlet.PortletPreferences;
058 import javax.portlet.PortletRequest;
059 import javax.portlet.RenderRequest;
060 import javax.portlet.RenderResponse;
061
062 import org.apache.struts.action.ActionForm;
063 import org.apache.struts.action.ActionForward;
064 import org.apache.struts.action.ActionMapping;
065
066
069 public class EditTemplateAction extends PortletAction {
070
071 @Override
072 public void processAction(
073 ActionMapping actionMapping, ActionForm actionForm,
074 PortletConfig portletConfig, ActionRequest actionRequest,
075 ActionResponse actionResponse)
076 throws Exception {
077
078 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
079
080 DDMTemplate template = null;
081
082 try {
083 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
084 template = updateTemplate(actionRequest);
085 }
086 else if (cmd.equals(Constants.DELETE)) {
087 deleteTemplates(actionRequest);
088 }
089
090 if (Validator.isNotNull(cmd)) {
091 String redirect = ParamUtil.getString(
092 actionRequest, "redirect");
093 String closeRedirect = ParamUtil.getString(
094 actionRequest, "closeRedirect");
095
096 if (Validator.isNotNull(closeRedirect)) {
097 redirect = HttpUtil.setParameter(
098 redirect, "closeRedirect", closeRedirect);
099
100 SessionMessages.add(
101 actionRequest,
102 PortalUtil.getPortletId(actionRequest) +
103 SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
104 closeRedirect);
105 }
106
107 if (template != null) {
108 boolean saveAndContinue = ParamUtil.getBoolean(
109 actionRequest, "saveAndContinue");
110
111 if (saveAndContinue) {
112 redirect = getSaveAndContinueRedirect(
113 portletConfig, actionRequest, template, redirect);
114 }
115 }
116
117 sendRedirect(actionRequest, actionResponse, redirect);
118 }
119 }
120 catch (Exception e) {
121 if (e instanceof NoSuchTemplateException ||
122 e instanceof PrincipalException) {
123
124 SessionErrors.add(actionRequest, e.getClass());
125
126 setForward(actionRequest, "portlet.dynamic_data_mapping.error");
127 }
128 else if (e instanceof RequiredTemplateException ||
129 e instanceof TemplateNameException ||
130 e instanceof TemplateScriptException ||
131 e instanceof TemplateSmallImageNameException ||
132 e instanceof TemplateSmallImageSizeException) {
133
134 SessionErrors.add(actionRequest, e.getClass(), e);
135
136 if (e instanceof RequiredTemplateException) {
137 String redirect = PortalUtil.escapeRedirect(
138 ParamUtil.getString(actionRequest, "redirect"));
139
140 if (Validator.isNotNull(redirect)) {
141 actionResponse.sendRedirect(redirect);
142 }
143 }
144 }
145 else {
146 throw e;
147 }
148 }
149 }
150
151 @Override
152 public ActionForward render(
153 ActionMapping actionMapping, ActionForm actionForm,
154 PortletConfig portletConfig, RenderRequest renderRequest,
155 RenderResponse renderResponse)
156 throws Exception {
157
158 try {
159 ActionUtil.getStructure(renderRequest);
160 ActionUtil.getTemplate(renderRequest);
161 }
162 catch (Exception e) {
163 if (e instanceof NoSuchTemplateException ||
164 e instanceof PrincipalException) {
165
166 SessionErrors.add(renderRequest, e.getClass());
167
168 return actionMapping.findForward(
169 "portlet.dynamic_data_mapping.error");
170 }
171 else {
172 throw e;
173 }
174 }
175
176 return actionMapping.findForward(
177 getForward(
178 renderRequest, "portlet.dynamic_data_mapping.edit_template"));
179 }
180
181 protected void deleteTemplates(ActionRequest actionRequest)
182 throws Exception {
183
184 long[] deleteTemplateIds = null;
185
186 long templateId = ParamUtil.getLong(actionRequest, "templateId");
187
188 if (templateId > 0) {
189 deleteTemplateIds = new long[] {templateId};
190 }
191 else {
192 deleteTemplateIds = StringUtil.split(
193 ParamUtil.getString(actionRequest, "deleteTemplateIds"), 0L);
194 }
195
196 for (long deleteTemplateId : deleteTemplateIds) {
197 DDMTemplateServiceUtil.deleteTemplate(deleteTemplateId);
198 }
199 }
200
201 protected String getSaveAndContinueRedirect(
202 PortletConfig portletConfig, ActionRequest actionRequest,
203 DDMTemplate template, String redirect)
204 throws Exception {
205
206 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
207 WebKeys.THEME_DISPLAY);
208
209 long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
210 long classPK = ParamUtil.getLong(actionRequest, "classPK");
211 String structureAvailableFields = ParamUtil.getString(
212 actionRequest, "structureAvailableFields");
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_template");
221 portletURL.setParameter("redirect", redirect, false);
222 portletURL.setParameter(
223 "templateId", String.valueOf(template.getTemplateId()), false);
224 portletURL.setParameter(
225 "groupId", String.valueOf(template.getGroupId()), false);
226 portletURL.setParameter(
227 "classNameId", String.valueOf(classNameId), false);
228 portletURL.setParameter("classPK", String.valueOf(classPK), false);
229 portletURL.setParameter("type", template.getType(), false);
230 portletURL.setParameter(
231 "structureAvailableFields", structureAvailableFields, false);
232 portletURL.setWindowState(actionRequest.getWindowState());
233
234 return portletURL.toString();
235 }
236
237 protected String getScript(UploadPortletRequest uploadPortletRequest)
238 throws Exception {
239
240 String scriptContent = ParamUtil.getString(
241 uploadPortletRequest, "scriptContent");
242
243 File file = uploadPortletRequest.getFile("script");
244
245 if (file == null) {
246 return scriptContent;
247 }
248
249 String script = FileUtil.read(file);
250
251 if (Validator.isNotNull(script) && !isValidFile(file)) {
252 throw new TemplateScriptException();
253 }
254
255 return GetterUtil.getString(script, scriptContent);
256 }
257
258 protected boolean isValidFile(File file) {
259 String contentType = MimeTypesUtil.getContentType(file);
260
261 if (contentType.equals(ContentTypes.APPLICATION_XSLT_XML) ||
262 contentType.startsWith(ContentTypes.TEXT)) {
263
264 return true;
265 }
266
267 return false;
268 }
269
270 protected DDMTemplate updateTemplate(ActionRequest actionRequest)
271 throws Exception {
272
273 UploadPortletRequest uploadPortletRequest =
274 PortalUtil.getUploadPortletRequest(actionRequest);
275
276 long templateId = ParamUtil.getLong(uploadPortletRequest, "templateId");
277
278 long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
279 long classNameId = ParamUtil.getLong(
280 uploadPortletRequest, "classNameId");
281 long classPK = ParamUtil.getLong(uploadPortletRequest, "classPK");
282 String templateKey = ParamUtil.getString(
283 uploadPortletRequest, "templateKey");
284 Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
285 uploadPortletRequest, "name");
286 Map<Locale, String> descriptionMap =
287 LocalizationUtil.getLocalizationMap(
288 uploadPortletRequest, "description");
289 String type = ParamUtil.getString(uploadPortletRequest, "type");
290 String mode = ParamUtil.getString(uploadPortletRequest, "mode");
291 String language = ParamUtil.getString(
292 uploadPortletRequest, "language", TemplateConstants.LANG_TYPE_VM);
293
294 String script = getScript(uploadPortletRequest);
295
296 boolean cacheable = ParamUtil.getBoolean(
297 uploadPortletRequest, "cacheable");
298 boolean smallImage = ParamUtil.getBoolean(
299 uploadPortletRequest, "smallImage");
300 String smallImageURL = ParamUtil.getString(
301 uploadPortletRequest, "smallImageURL");
302 File smallImageFile = uploadPortletRequest.getFile("smallImageFile");
303
304 ServiceContext serviceContext = ServiceContextFactory.getInstance(
305 DDMTemplate.class.getName(), uploadPortletRequest);
306
307 DDMTemplate template = null;
308
309 if (templateId <= 0) {
310 template = DDMTemplateServiceUtil.addTemplate(
311 groupId, classNameId, classPK, templateKey, nameMap,
312 descriptionMap, type, mode, language, script, cacheable,
313 smallImage, smallImageURL, smallImageFile, serviceContext);
314 }
315 else {
316 template = DDMTemplateServiceUtil.updateTemplate(
317 templateId, classPK, nameMap, descriptionMap, type, mode,
318 language, script, cacheable, smallImage, smallImageURL,
319 smallImageFile, serviceContext);
320 }
321
322 PortletPreferences portletPreferences = getStrictPortletSetup(
323 actionRequest);
324
325 if (portletPreferences != null) {
326 if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY)) {
327 portletPreferences.setValue(
328 "displayDDMTemplateId",
329 String.valueOf(template.getTemplateId()));
330 }
331 else {
332 portletPreferences.setValue(
333 "formDDMTemplateId",
334 String.valueOf(template.getTemplateId()));
335 }
336
337 portletPreferences.store();
338 }
339
340 return template;
341 }
342
343 }