Java tutorial
/* * SurveyPanel * Copyright (C) 2009 Serge Tan Panza * All rights reserved. * License: GNU/GPL License v3 , see LICENSE.txt * SurveyPanel is free software. This version may have been modified pursuant * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. * See COPYRIGHT.txt for copyright notices and details. * */ package com.surveypanel.web.admin; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; import com.surveypanel.form.Form; import com.surveypanel.form.FormFactory; import com.surveypanel.js.JSManager; import com.surveypanel.js.Result; import com.surveypanel.utils.StringUtils; /** * @author stanpanza * */ @ParentPackage("json-default") @org.apache.struts2.convention.annotation.Result(type = "json", name = "success", params = { "contentType", "text/plain", "includeProperties", "display,logs,i18nKeysRendered" }) public class ShellAction extends ActionSupport implements SessionAware { public static String SESSION_FORMID = "formId"; private String source; private String display; private long surveyId; private long clientId; private String currentLocale; private boolean devMode; private Map session; private JSManager jsManager; private FormFactory formFactory; private Result result; private Map<String, String> i18nKeysRendered; private String logs; public String execute() throws Exception { try { Form form = null; if (devMode) { formFactory.init(surveyId); session.remove(SESSION_FORMID); } if (!session.containsKey(SESSION_FORMID)) { form = formFactory.create(surveyId, devMode); session.put(SESSION_FORMID, form.getId()); } else { String formId = (String) session.get(SESSION_FORMID); form = formFactory.load((String) session.get(SESSION_FORMID), surveyId, devMode); setValues(form); } form.setBackEnable(true); form.setLocale(StringUtils.toLocale(currentLocale)); form.setDevMode(devMode); if (org.apache.commons.lang.StringUtils.isEmpty(source)) { source = "flow.next();save();"; } result = (Result) jsManager.execute(form, source); display = result.getDisplay(); logs = result.getLogs(); i18nKeysRendered = form.getRenderedKeys(); session.put(SESSION_FORMID, form.getId()); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } private void setValues(Form form) { HttpServletRequest request = ServletActionContext.getRequest(); HashMap<String, String> values = new HashMap<String, String>(); Map<String, Object> formValues = form.getFieldValues(); Enumeration fieldNames = request.getParameterNames(); while (fieldNames.hasMoreElements()) { String fieldName = (String) fieldNames.nextElement(); if (formValues.containsKey(fieldName)) { values.put(fieldName, request.getParameter(fieldName)); } } form.setValues(values); } public void setSession(Map mappedSession) { this.session = mappedSession; } public Result getResult() { return result; } public void setSource(String source) { this.source = source; } public void setSurveyId(long surveyId) { this.surveyId = surveyId; } public void setCurrentLocale(String currentLocale) { this.currentLocale = currentLocale; } public void setDevMode(boolean devMode) { this.devMode = devMode; } public void setJsManager(JSManager jsManager) { this.jsManager = jsManager; } public void setFormFactory(FormFactory formFactory) { this.formFactory = formFactory; } public long getClientId() { return clientId; } public void setClientId(long clientId) { this.clientId = clientId; } public long getSurveyId() { return surveyId; } public Map<String, String> getI18nKeysRendered() { return i18nKeysRendered; } public String getDisplay() { return display; } public String getLogs() { return logs; } }