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.common.util.log.LogManager;
00039 import org.openmobileis.services.common.ServiceManager;
00040
00041 import freemarker.template.SimpleScalar;
00042 import freemarker.template.TemplateModelRoot;
00043
00051 public abstract class SimpleEditService extends TemplateService implements EditService {
00052
00053 public String runTemplate (HttpServletRequest req, HttpServletResponse res , TemplateModelRoot templateData) throws ServletException, IOException {
00054 try {
00055 Object sessionDatas = this.getSessionData(req);
00056 boolean createmode = false;
00057 if (sessionDatas == null) {
00058 sessionDatas = this.createSessionObject(req);
00059 if (sessionDatas == null) {
00060 this.addParametersErrorToTemplate(req, templateData);
00061 return this.getTemplateName();
00062 }
00063 HttpSession session = req.getSession(true);
00064 session.setAttribute(this.getSessionDatasName(), sessionDatas);
00065 createmode = true;
00066 }
00067
00068 this.fillSessionObjectWithRequestData(sessionDatas, req);
00069
00070 if ((!createmode) &&(!this.hasParameterError(req)) && (this.isStoreMode(req))) {
00071 this.storeSessionObjectInDB(sessionDatas, req, templateData);
00072 if (!this.hasParameterError(req)) {
00073 if(isRemoveSession(req)){
00074 HttpSession session = req.getSession(true);
00075 session.removeAttribute(this.getSessionDatasName());
00076 session.removeAttribute(this.getErrorSessionAttributName());
00077 }
00078 ServiceManager.getManager().redirectToServiceURI(this.getDisplayServiceURI(), req, res);
00079 return null;
00080 }
00081 }
00082
00083 this.fillTemplateWithSessionData(sessionDatas, templateData);
00084
00085 this.addParametersErrorToTemplate(req, templateData);
00086 return this.getTemplateName();
00087 } catch (Exception ex) {
00088 LogManager.traceError(0, ex);
00089 HttpSession session = req.getSession(true);
00090 session.removeAttribute(this.getSessionDatasName());
00091 session.removeAttribute(this.getErrorSessionAttributName());
00092 throw new ServletException(ex);
00093 }
00094 }
00095
00096 public boolean hasSessionData(HttpServletRequest req) {
00097 HttpSession session = req.getSession(true);
00098 return session.getAttribute(this.getSessionDatasName()) != null;
00099
00100 }
00101 public void removeSessionData(HttpServletRequest req) {
00102 HttpSession session = req.getSession(true);
00103 session.removeAttribute(this.getSessionDatasName());
00104 session.removeAttribute(this.getErrorSessionAttributName());
00105 }
00106
00107
00108 protected void setInputRequestParameterError(HttpServletRequest req, String message) {
00109 HttpSession session = req.getSession(true);
00110 StringBuffer errorMessage = (StringBuffer)session.getAttribute(this.getErrorSessionAttributName());
00111 if (errorMessage == null) {
00112 errorMessage = new StringBuffer();
00113 } else {
00114 errorMessage.append("<BR>");
00115 }
00116 errorMessage.append(message);
00117 session.setAttribute(this.getErrorSessionAttributName(),errorMessage);
00118 }
00119
00120 protected String getErrorSessionAttributName() {
00121 return this.getSessionDatasName()+"-errorMessage";
00122 }
00123
00124 protected void deleteSessionData(HttpServletRequest req) {
00125 HttpSession session = req.getSession(true);
00126 session.removeAttribute(this.getSessionDatasName());
00127 session.removeAttribute(this.getErrorSessionAttributName());
00128 }
00129
00130 private Object getSessionData(HttpServletRequest req) {
00131 HttpSession session = req.getSession(true);
00132 return session.getAttribute(this.getSessionDatasName());
00133 }
00134
00135 private boolean hasParameterError(HttpServletRequest req) {
00136 HttpSession session = req.getSession(true);
00137 return (session.getAttribute(this.getErrorSessionAttributName()) != null);
00138 }
00139
00140 private void addParametersErrorToTemplate(HttpServletRequest req, TemplateModelRoot templateData) {
00141 HttpSession session = req.getSession(true);
00142 if (this.hasParameterError(req)) {
00143 StringBuffer error = (StringBuffer)session.getAttribute(this.getErrorSessionAttributName());
00144 templateData.put("hasErrorMessage", new SimpleScalar(true));
00145 templateData.put("errorMessage", new SimpleScalar(error.toString()));
00146
00147 }
00148 session.removeAttribute(this.getErrorSessionAttributName());
00149 }
00150
00151 protected abstract void fillTemplateWithSessionData(Object sessionDatas, TemplateModelRoot templateData) throws ServiceException;
00152 protected abstract void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException;
00153 protected abstract void fillSessionObjectWithRequestData(Object sessionDatas, HttpServletRequest req) throws ServiceException;
00154 protected abstract String getSessionDatasName();
00155 protected abstract String getTemplateName();
00156 protected abstract Object createSessionObject(HttpServletRequest req) throws ServiceException;
00157 protected abstract String getDisplayServiceURI();
00158
00159
00166 protected boolean isStoreMode(HttpServletRequest req) {
00167 return true;
00168 }
00169
00170 protected boolean isRemoveSession(HttpServletRequest req){
00171 return true;
00172 }
00173
00174 }