00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 package org.openmobileis.services;
00029
00030 import java.io.IOException;
00031
00032 import javax.servlet.ServletException;
00033 import javax.servlet.http.HttpServletRequest;
00034 import javax.servlet.http.HttpServletResponse;
00035 import javax.servlet.http.HttpSession;
00036
00037 import org.openmobileis.common.util.exception.ServiceException;
00038 import org.openmobileis.services.common.ServiceManager;
00039
00040 import freemarker.template.SimpleScalar;
00041 import freemarker.template.TemplateModelRoot;
00042
00050 public abstract class SimpleEditService extends TemplateService {
00051
00052 public String runTemplate (HttpServletRequest req, HttpServletResponse res , TemplateModelRoot templateData) throws ServletException, IOException {
00053 try {
00054 Object sessionDatas = this.getSessionData(req);
00055 boolean createmode = false;
00056 if (sessionDatas == null) {
00057 sessionDatas = this.createSessionObject(req);
00058 if (sessionDatas == null) {
00059 this.addParametersErrorToTemplate(req, templateData);
00060 return this.getTemplateName();
00061 }
00062 HttpSession session = req.getSession(true);
00063 session.setAttribute(this.getSessionDatasName(), sessionDatas);
00064 createmode = true;
00065 }
00066
00067 this.fillSessionObjectWithRequestData(sessionDatas, req);
00068
00069 if ((!createmode) &&(!this.hasParameterError(req)) && (this.isStoreMode(req))) {
00070 this.storeSessionObjectInDB(sessionDatas, req, templateData);
00071 if (!this.hasParameterError(req)) {
00072 HttpSession session = req.getSession(true);
00073 session.removeAttribute(this.getSessionDatasName());
00074 session.removeAttribute(this.getErrorSessionAttributName());
00075 ServiceManager.getManager().redirectToServiceURI(this.getDisplayServiceURI(), req, res);
00076 return null;
00077 }
00078 }
00079
00080 this.fillTemplateWithSessionData(sessionDatas, templateData);
00081
00082 this.addParametersErrorToTemplate(req, templateData);
00083 return this.getTemplateName();
00084 } catch (Exception ex) {
00085 HttpSession session = req.getSession(true);
00086 session.removeAttribute(this.getSessionDatasName());
00087 session.removeAttribute(this.getErrorSessionAttributName());
00088 throw new ServletException(ex);
00089 }
00090 }
00091
00092 protected void setInputRequestParameterError(HttpServletRequest req, String message) {
00093 HttpSession session = req.getSession(true);
00094 StringBuffer errorMessage = (StringBuffer)session.getAttribute(this.getErrorSessionAttributName());
00095 if (errorMessage == null) {
00096 errorMessage = new StringBuffer();
00097 } else {
00098 errorMessage.append("<BR>");
00099 }
00100 errorMessage.append(message);
00101 session.setAttribute(this.getErrorSessionAttributName(),errorMessage);
00102 }
00103
00104 protected String getErrorSessionAttributName() {
00105 return this.getSessionDatasName()+"-errorMessage";
00106 }
00107
00108 protected void deleteSessionData(HttpServletRequest req) {
00109 HttpSession session = req.getSession(true);
00110 session.removeAttribute(this.getSessionDatasName());
00111 session.removeAttribute(this.getErrorSessionAttributName());
00112 }
00113
00114 private Object getSessionData(HttpServletRequest req) {
00115 HttpSession session = req.getSession(true);
00116 return session.getAttribute(this.getSessionDatasName());
00117 }
00118
00119 private boolean hasParameterError(HttpServletRequest req) {
00120 HttpSession session = req.getSession(true);
00121 return (session.getAttribute(this.getErrorSessionAttributName()) != null);
00122 }
00123
00124 private void addParametersErrorToTemplate(HttpServletRequest req, TemplateModelRoot templateData) {
00125 HttpSession session = req.getSession(true);
00126 if (this.hasParameterError(req)) {
00127 StringBuffer error = (StringBuffer)session.getAttribute(this.getErrorSessionAttributName());
00128 templateData.put("hasErrorMessage", new SimpleScalar(true));
00129 templateData.put("errorMessage", new SimpleScalar(error.toString()));
00130
00131 }
00132 session.removeAttribute(this.getErrorSessionAttributName());
00133 }
00134
00135 protected abstract void fillTemplateWithSessionData(Object sessionDatas, TemplateModelRoot templateData) throws ServiceException;
00136 protected abstract void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException;
00137 protected abstract void fillSessionObjectWithRequestData(Object sessionDatas, HttpServletRequest req) throws ServiceException;
00138 protected abstract String getSessionDatasName();
00139 protected abstract String getTemplateName();
00140 protected abstract Object createSessionObject(HttpServletRequest req) throws ServiceException;
00141 protected abstract String getDisplayServiceURI();
00142
00143
00150 protected boolean isStoreMode(HttpServletRequest req) {
00151 return true;
00152 }
00153
00154 }