PageRenderTime 58ms CodeModel.GetById 40ms app.highlight 15ms 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
 15package com.liferay.webform.action;
 16
 17import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
 18import com.liferay.portal.kernel.servlet.SessionErrors;
 19import com.liferay.portal.kernel.util.Constants;
 20import com.liferay.portal.kernel.util.GetterUtil;
 21import com.liferay.portal.kernel.util.LocaleUtil;
 22import com.liferay.portal.kernel.util.LocalizationUtil;
 23import com.liferay.portal.kernel.util.ParamUtil;
 24import com.liferay.portal.kernel.util.StringPool;
 25import com.liferay.portal.kernel.util.StringUtil;
 26import com.liferay.portal.kernel.util.Validator;
 27import com.liferay.portlet.PortletPreferencesFactoryUtil;
 28import com.liferay.portlet.expando.DuplicateColumnNameException;
 29import com.liferay.webform.util.WebFormUtil;
 30
 31import java.io.FileNotFoundException;
 32import java.io.FileOutputStream;
 33
 34import java.util.HashSet;
 35import java.util.Locale;
 36import java.util.Map;
 37import java.util.Set;
 38
 39import javax.portlet.ActionRequest;
 40import javax.portlet.ActionResponse;
 41import javax.portlet.PortletConfig;
 42import javax.portlet.PortletPreferences;
 43import javax.portlet.RenderRequest;
 44import javax.portlet.RenderResponse;
 45
 46/**
 47 * @author Jorge Ferrer
 48 * @author Alberto Montero
 49 * @author Julio Camarero
 50 * @author Brian Wing Shun Chan
 51 */
 52public class ConfigurationActionImpl extends DefaultConfigurationAction {
 53
 54	@Override
 55	public void processAction(
 56			PortletConfig portletConfig, ActionRequest actionRequest,
 57			ActionResponse actionResponse)
 58		throws Exception {
 59
 60		validateFields(actionRequest);
 61
 62		if (!SessionErrors.isEmpty(actionRequest)) {
 63			return;
 64		}
 65
 66		Locale defaultLocale = LocaleUtil.getDefault();
 67		String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
 68
 69		boolean updateFields = ParamUtil.getBoolean(
 70			actionRequest, "updateFields");
 71
 72		String portletResource = ParamUtil.getString(
 73			actionRequest, "portletResource");
 74
 75		PortletPreferences preferences =
 76			PortletPreferencesFactoryUtil.getPortletSetup(
 77				actionRequest, portletResource);
 78
 79		LocalizationUtil.setLocalizedPreferencesValues(
 80			actionRequest, preferences, "title");
 81		LocalizationUtil.setLocalizedPreferencesValues(
 82			actionRequest, preferences, "description");
 83
 84		if (updateFields) {
 85			int i = 1;
 86
 87			String databaseTableName = WebFormUtil.getNewDatabaseTableName(
 88				portletResource);
 89
 90			preferences.setValue("databaseTableName", databaseTableName);
 91
 92			int[] formFieldsIndexes = StringUtil.split(
 93				ParamUtil.getString(actionRequest, "formFieldsIndexes"), 0);
 94
 95			for (int formFieldsIndex : formFieldsIndexes) {
 96				Map<Locale, String> fieldLabelMap =
 97					LocalizationUtil.getLocalizationMap(
 98						actionRequest, "fieldLabel" + formFieldsIndex);
 99
100				if (Validator.isNull(fieldLabelMap.get(defaultLocale))) {
101					continue;
102				}
103
104				String fieldType = ParamUtil.getString(
105					actionRequest, "fieldType" + formFieldsIndex);
106				boolean fieldOptional = ParamUtil.getBoolean(
107					actionRequest, "fieldOptional" + formFieldsIndex);
108				Map<Locale, String> fieldOptionsMap =
109					LocalizationUtil.getLocalizationMap(
110						actionRequest, "fieldOptions" + formFieldsIndex);
111				String fieldValidationScript = ParamUtil.getString(
112					actionRequest, "fieldValidationScript" + formFieldsIndex);
113				String fieldValidationErrorMessage = ParamUtil.getString(
114					actionRequest,
115					"fieldValidationErrorMessage" + formFieldsIndex);
116
117				if ((Validator.isNotNull(fieldValidationScript) ^
118					(Validator.isNotNull(fieldValidationErrorMessage)))) {
119
120					SessionErrors.add(
121						actionRequest, "invalidValidationDefinition" + i);
122				}
123
124				for (Locale locale : fieldLabelMap.keySet()) {
125					String languageId = LocaleUtil.toLanguageId(locale);
126					String fieldLabelValue = fieldLabelMap.get(locale);
127					String fieldOptionsValue = fieldOptionsMap.get(locale);
128
129					if (Validator.isNotNull(fieldLabelValue)) {
130						LocalizationUtil.setPreferencesValue(
131							preferences, "fieldLabel" + i, languageId,
132							fieldLabelValue);
133					}
134
135					if (Validator.isNotNull(fieldOptionsValue)) {
136						LocalizationUtil.setPreferencesValue(
137							preferences, "fieldOptions" + i, languageId,
138							fieldOptionsValue);
139					}
140				}
141
142				preferences.setValue("fieldType" + i, fieldType);
143				preferences.setValue(
144					"fieldOptional" + i, String.valueOf(fieldOptional));
145				preferences.setValue(
146					"fieldValidationScript" + i, fieldValidationScript);
147				preferences.setValue(
148					"fieldValidationErrorMessage" + i,
149					fieldValidationErrorMessage);
150
151				i++;
152			}
153
154			if (!SessionErrors.isEmpty(actionRequest)) {
155				return;
156			}
157
158			// Clear previous preferences that are now blank
159
160			String fieldLabel = LocalizationUtil.getPreferencesValue(
161				preferences, "fieldLabel" + i, defaultLanguageId);
162
163			while (Validator.isNotNull(fieldLabel)) {
164				Map<Locale, String> fieldLabelMap =
165					LocalizationUtil.getLocalizationMap(
166						actionRequest, "fieldLabel" + i);
167
168				for (Locale locale : fieldLabelMap.keySet()) {
169					String languageId = LocaleUtil.toLanguageId(locale);
170
171					LocalizationUtil.setPreferencesValue(
172						preferences, "fieldLabel" + i, languageId,
173						StringPool.BLANK);
174
175					LocalizationUtil.setPreferencesValue(
176						preferences, "fieldOptions" + i, languageId,
177						StringPool.BLANK);
178				}
179
180				preferences.setValue("fieldType" + i, StringPool.BLANK);
181				preferences.setValue("fieldOptional" + i, StringPool.BLANK);
182				preferences.setValue(
183					"fieldValidationScript" + i, StringPool.BLANK);
184				preferences.setValue(
185					"fieldValidationErrorMessage" + i, StringPool.BLANK);
186
187				i++;
188
189				fieldLabel = LocalizationUtil.getPreferencesValue(
190					preferences, "fieldLabel" + i, defaultLanguageId);
191			}
192		}
193
194		if (SessionErrors.isEmpty(actionRequest)) {
195			preferences.store();
196		}
197
198		super.processAction(portletConfig, actionRequest, actionResponse);
199	}
200
201	@Override
202	public String render(
203			PortletConfig portletConfig, RenderRequest renderRequest,
204			RenderResponse renderResponse)
205		throws Exception {
206
207		String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
208
209		if (cmd.equals(Constants.ADD)) {
210			return "/edit_field.jsp";
211		}
212		else {
213			return "/configuration.jsp";
214		}
215	}
216
217	protected void validateFields(ActionRequest actionRequest)
218		throws Exception {
219
220		Locale defaultLocale = LocaleUtil.getDefault();
221		String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
222
223		String title = ParamUtil.getString(
224			actionRequest, "title" + StringPool.UNDERLINE + defaultLanguageId);
225
226		boolean sendAsEmail = GetterUtil.getBoolean(
227			getParameter(actionRequest, "sendAsEmail"));
228		String subject = getParameter(actionRequest, "subject");
229
230		boolean saveToDatabase = GetterUtil.getBoolean(
231			getParameter(actionRequest, "saveToDatabase"));
232
233		boolean saveToFile = GetterUtil.getBoolean(
234			getParameter(actionRequest, "saveToFile"));
235
236		if (Validator.isNull(title)) {
237			SessionErrors.add(actionRequest, "titleRequired");
238		}
239
240		if (Validator.isNull(subject)) {
241			SessionErrors.add(actionRequest, "subjectRequired");
242		}
243
244		if (!sendAsEmail && !saveToDatabase && !saveToFile) {
245			SessionErrors.add(actionRequest, "handlingRequired");
246		}
247
248		if (sendAsEmail) {
249			String[] emailAdresses = WebFormUtil.split(
250				getParameter(actionRequest, "emailAddress"));
251
252			for (String emailAdress : emailAdresses) {
253				emailAdress = emailAdress.trim();
254
255				if (Validator.isNull(emailAdress)) {
256					SessionErrors.add(actionRequest, "emailAddressRequired");
257				}
258				else if (!Validator.isEmailAddress(emailAdress)) {
259					SessionErrors.add(actionRequest, "emailAddressInvalid");
260				}
261			}
262		}
263
264		if (saveToFile) {
265			String fileName = getParameter(actionRequest, "fileName");
266
267			// Check if server can create a file as specified
268
269			try {
270				FileOutputStream fileOutputStream = new FileOutputStream(
271					fileName, true);
272
273				fileOutputStream.close();
274			}
275			catch (SecurityException es) {
276				SessionErrors.add(actionRequest, "fileNameInvalid");
277			}
278			catch (FileNotFoundException fnfe) {
279				SessionErrors.add(actionRequest, "fileNameInvalid");
280			}
281		}
282
283		if (!validateUniqueFieldNames(actionRequest)) {
284			SessionErrors.add(
285				actionRequest, DuplicateColumnNameException.class.getName());
286		}
287	}
288
289	protected boolean validateUniqueFieldNames(ActionRequest actionRequest) {
290		Locale defaultLocale = LocaleUtil.getDefault();
291
292		Set<String> localizedUniqueFieldNames = new HashSet<String>();
293
294		int[] formFieldsIndexes = StringUtil.split(
295			ParamUtil.getString(actionRequest, "formFieldsIndexes"), 0);
296
297		for (int formFieldsIndex : formFieldsIndexes) {
298			Map<Locale, String> fieldLabelMap =
299				LocalizationUtil.getLocalizationMap(
300					actionRequest, "fieldLabel" + formFieldsIndex);
301
302			if (Validator.isNull(fieldLabelMap.get(defaultLocale))) {
303				continue;
304			}
305
306			for (Locale locale : fieldLabelMap.keySet()) {
307				String fieldLabelValue = fieldLabelMap.get(locale);
308
309				if (Validator.isNull(fieldLabelValue)) {
310					continue;
311				}
312
313				String languageId = LocaleUtil.toLanguageId(locale);
314
315				if (localizedUniqueFieldNames.add(
316						languageId + "_" + fieldLabelValue)) {
317
318					return false;
319				}
320			}
321		}
322
323		return true;
324	}
325
326}