PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/portlets/web-form-portlet/docroot/WEB-INF/src/com/liferay/webform/action/ConfigurationActionImpl.java

https://github.com/stevenjiancao/liferay-plugins
Java | 326 lines | 233 code | 72 blank | 21 comment | 31 complexity | 555cef5c2de0ded81faeeabf7cb09a36 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.webform.action;
  15. import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
  16. import com.liferay.portal.kernel.servlet.SessionErrors;
  17. import com.liferay.portal.kernel.util.Constants;
  18. import com.liferay.portal.kernel.util.GetterUtil;
  19. import com.liferay.portal.kernel.util.LocaleUtil;
  20. import com.liferay.portal.kernel.util.LocalizationUtil;
  21. import com.liferay.portal.kernel.util.ParamUtil;
  22. import com.liferay.portal.kernel.util.StringPool;
  23. import com.liferay.portal.kernel.util.StringUtil;
  24. import com.liferay.portal.kernel.util.Validator;
  25. import com.liferay.portlet.PortletPreferencesFactoryUtil;
  26. import com.liferay.portlet.expando.DuplicateColumnNameException;
  27. import com.liferay.webform.util.WebFormUtil;
  28. import java.io.FileNotFoundException;
  29. import java.io.FileOutputStream;
  30. import java.util.HashSet;
  31. import java.util.Locale;
  32. import java.util.Map;
  33. import java.util.Set;
  34. import javax.portlet.ActionRequest;
  35. import javax.portlet.ActionResponse;
  36. import javax.portlet.PortletConfig;
  37. import javax.portlet.PortletPreferences;
  38. import javax.portlet.RenderRequest;
  39. import javax.portlet.RenderResponse;
  40. /**
  41. * @author Jorge Ferrer
  42. * @author Alberto Montero
  43. * @author Julio Camarero
  44. * @author Brian Wing Shun Chan
  45. */
  46. public class ConfigurationActionImpl extends DefaultConfigurationAction {
  47. @Override
  48. public void processAction(
  49. PortletConfig portletConfig, ActionRequest actionRequest,
  50. ActionResponse actionResponse)
  51. throws Exception {
  52. validateFields(actionRequest);
  53. if (!SessionErrors.isEmpty(actionRequest)) {
  54. return;
  55. }
  56. Locale defaultLocale = LocaleUtil.getDefault();
  57. String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
  58. boolean updateFields = ParamUtil.getBoolean(
  59. actionRequest, "updateFields");
  60. String portletResource = ParamUtil.getString(
  61. actionRequest, "portletResource");
  62. PortletPreferences preferences =
  63. PortletPreferencesFactoryUtil.getPortletSetup(
  64. actionRequest, portletResource);
  65. LocalizationUtil.setLocalizedPreferencesValues(
  66. actionRequest, preferences, "title");
  67. LocalizationUtil.setLocalizedPreferencesValues(
  68. actionRequest, preferences, "description");
  69. if (updateFields) {
  70. int i = 1;
  71. String databaseTableName = WebFormUtil.getNewDatabaseTableName(
  72. portletResource);
  73. preferences.setValue("databaseTableName", databaseTableName);
  74. int[] formFieldsIndexes = StringUtil.split(
  75. ParamUtil.getString(actionRequest, "formFieldsIndexes"), 0);
  76. for (int formFieldsIndex : formFieldsIndexes) {
  77. Map<Locale, String> fieldLabelMap =
  78. LocalizationUtil.getLocalizationMap(
  79. actionRequest, "fieldLabel" + formFieldsIndex);
  80. if (Validator.isNull(fieldLabelMap.get(defaultLocale))) {
  81. continue;
  82. }
  83. String fieldType = ParamUtil.getString(
  84. actionRequest, "fieldType" + formFieldsIndex);
  85. boolean fieldOptional = ParamUtil.getBoolean(
  86. actionRequest, "fieldOptional" + formFieldsIndex);
  87. Map<Locale, String> fieldOptionsMap =
  88. LocalizationUtil.getLocalizationMap(
  89. actionRequest, "fieldOptions" + formFieldsIndex);
  90. String fieldValidationScript = ParamUtil.getString(
  91. actionRequest, "fieldValidationScript" + formFieldsIndex);
  92. String fieldValidationErrorMessage = ParamUtil.getString(
  93. actionRequest,
  94. "fieldValidationErrorMessage" + formFieldsIndex);
  95. if ((Validator.isNotNull(fieldValidationScript) ^
  96. (Validator.isNotNull(fieldValidationErrorMessage)))) {
  97. SessionErrors.add(
  98. actionRequest, "invalidValidationDefinition" + i);
  99. }
  100. for (Locale locale : fieldLabelMap.keySet()) {
  101. String languageId = LocaleUtil.toLanguageId(locale);
  102. String fieldLabelValue = fieldLabelMap.get(locale);
  103. String fieldOptionsValue = fieldOptionsMap.get(locale);
  104. if (Validator.isNotNull(fieldLabelValue)) {
  105. LocalizationUtil.setPreferencesValue(
  106. preferences, "fieldLabel" + i, languageId,
  107. fieldLabelValue);
  108. }
  109. if (Validator.isNotNull(fieldOptionsValue)) {
  110. LocalizationUtil.setPreferencesValue(
  111. preferences, "fieldOptions" + i, languageId,
  112. fieldOptionsValue);
  113. }
  114. }
  115. preferences.setValue("fieldType" + i, fieldType);
  116. preferences.setValue(
  117. "fieldOptional" + i, String.valueOf(fieldOptional));
  118. preferences.setValue(
  119. "fieldValidationScript" + i, fieldValidationScript);
  120. preferences.setValue(
  121. "fieldValidationErrorMessage" + i,
  122. fieldValidationErrorMessage);
  123. i++;
  124. }
  125. if (!SessionErrors.isEmpty(actionRequest)) {
  126. return;
  127. }
  128. // Clear previous preferences that are now blank
  129. String fieldLabel = LocalizationUtil.getPreferencesValue(
  130. preferences, "fieldLabel" + i, defaultLanguageId);
  131. while (Validator.isNotNull(fieldLabel)) {
  132. Map<Locale, String> fieldLabelMap =
  133. LocalizationUtil.getLocalizationMap(
  134. actionRequest, "fieldLabel" + i);
  135. for (Locale locale : fieldLabelMap.keySet()) {
  136. String languageId = LocaleUtil.toLanguageId(locale);
  137. LocalizationUtil.setPreferencesValue(
  138. preferences, "fieldLabel" + i, languageId,
  139. StringPool.BLANK);
  140. LocalizationUtil.setPreferencesValue(
  141. preferences, "fieldOptions" + i, languageId,
  142. StringPool.BLANK);
  143. }
  144. preferences.setValue("fieldType" + i, StringPool.BLANK);
  145. preferences.setValue("fieldOptional" + i, StringPool.BLANK);
  146. preferences.setValue(
  147. "fieldValidationScript" + i, StringPool.BLANK);
  148. preferences.setValue(
  149. "fieldValidationErrorMessage" + i, StringPool.BLANK);
  150. i++;
  151. fieldLabel = LocalizationUtil.getPreferencesValue(
  152. preferences, "fieldLabel" + i, defaultLanguageId);
  153. }
  154. }
  155. if (SessionErrors.isEmpty(actionRequest)) {
  156. preferences.store();
  157. }
  158. super.processAction(portletConfig, actionRequest, actionResponse);
  159. }
  160. @Override
  161. public String render(
  162. PortletConfig portletConfig, RenderRequest renderRequest,
  163. RenderResponse renderResponse)
  164. throws Exception {
  165. String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
  166. if (cmd.equals(Constants.ADD)) {
  167. return "/edit_field.jsp";
  168. }
  169. else {
  170. return "/configuration.jsp";
  171. }
  172. }
  173. protected void validateFields(ActionRequest actionRequest)
  174. throws Exception {
  175. Locale defaultLocale = LocaleUtil.getDefault();
  176. String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
  177. String title = ParamUtil.getString(
  178. actionRequest, "title" + StringPool.UNDERLINE + defaultLanguageId);
  179. boolean sendAsEmail = GetterUtil.getBoolean(
  180. getParameter(actionRequest, "sendAsEmail"));
  181. String subject = getParameter(actionRequest, "subject");
  182. boolean saveToDatabase = GetterUtil.getBoolean(
  183. getParameter(actionRequest, "saveToDatabase"));
  184. boolean saveToFile = GetterUtil.getBoolean(
  185. getParameter(actionRequest, "saveToFile"));
  186. if (Validator.isNull(title)) {
  187. SessionErrors.add(actionRequest, "titleRequired");
  188. }
  189. if (Validator.isNull(subject)) {
  190. SessionErrors.add(actionRequest, "subjectRequired");
  191. }
  192. if (!sendAsEmail && !saveToDatabase && !saveToFile) {
  193. SessionErrors.add(actionRequest, "handlingRequired");
  194. }
  195. if (sendAsEmail) {
  196. String[] emailAdresses = WebFormUtil.split(
  197. getParameter(actionRequest, "emailAddress"));
  198. for (String emailAdress : emailAdresses) {
  199. emailAdress = emailAdress.trim();
  200. if (Validator.isNull(emailAdress)) {
  201. SessionErrors.add(actionRequest, "emailAddressRequired");
  202. }
  203. else if (!Validator.isEmailAddress(emailAdress)) {
  204. SessionErrors.add(actionRequest, "emailAddressInvalid");
  205. }
  206. }
  207. }
  208. if (saveToFile) {
  209. String fileName = getParameter(actionRequest, "fileName");
  210. // Check if server can create a file as specified
  211. try {
  212. FileOutputStream fileOutputStream = new FileOutputStream(
  213. fileName, true);
  214. fileOutputStream.close();
  215. }
  216. catch (SecurityException es) {
  217. SessionErrors.add(actionRequest, "fileNameInvalid");
  218. }
  219. catch (FileNotFoundException fnfe) {
  220. SessionErrors.add(actionRequest, "fileNameInvalid");
  221. }
  222. }
  223. if (!validateUniqueFieldNames(actionRequest)) {
  224. SessionErrors.add(
  225. actionRequest, DuplicateColumnNameException.class.getName());
  226. }
  227. }
  228. protected boolean validateUniqueFieldNames(ActionRequest actionRequest) {
  229. Locale defaultLocale = LocaleUtil.getDefault();
  230. Set<String> localizedUniqueFieldNames = new HashSet<String>();
  231. int[] formFieldsIndexes = StringUtil.split(
  232. ParamUtil.getString(actionRequest, "formFieldsIndexes"), 0);
  233. for (int formFieldsIndex : formFieldsIndexes) {
  234. Map<Locale, String> fieldLabelMap =
  235. LocalizationUtil.getLocalizationMap(
  236. actionRequest, "fieldLabel" + formFieldsIndex);
  237. if (Validator.isNull(fieldLabelMap.get(defaultLocale))) {
  238. continue;
  239. }
  240. for (Locale locale : fieldLabelMap.keySet()) {
  241. String fieldLabelValue = fieldLabelMap.get(locale);
  242. if (Validator.isNull(fieldLabelValue)) {
  243. continue;
  244. }
  245. String languageId = LocaleUtil.toLanguageId(locale);
  246. if (localizedUniqueFieldNames.add(
  247. languageId + "_" + fieldLabelValue)) {
  248. return false;
  249. }
  250. }
  251. }
  252. return true;
  253. }
  254. }