MultiStepMainService.java

00001 
00004 package org.openmobileis.services;
00005 
00006 import java.io.IOException;
00007 import java.util.HashMap;
00008 import java.util.Map;
00009 
00010 import javax.servlet.ServletException;
00011 import javax.servlet.http.HttpServletRequest;
00012 import javax.servlet.http.HttpServletResponse;
00013 import javax.servlet.http.HttpSession;
00014 
00015 import org.openmobileis.common.util.exception.ServiceException;
00016 import org.openmobileis.common.util.log.LogManager;
00017 import org.openmobileis.services.common.ServiceManager;
00018 
00019 import freemarker.template.TemplateModelRoot;
00020 
00026 public abstract class MultiStepMainService extends TemplateService implements EditService {
00027         protected HashMap serviceMap;
00031         public MultiStepMainService() {
00032                 serviceMap = new HashMap(10);
00033         }
00034 
00035         public String runTemplate (HttpServletRequest req, HttpServletResponse res , TemplateModelRoot templateData) throws ServletException, IOException {
00036                 try     {
00037                         String lasttag = req.getParameter("last");
00038                         if (lasttag != null && lasttag.length() == 0) lasttag = null;
00039                         String nexttag = req.getParameter("next");
00040                         if (nexttag != null && nexttag.length() == 0) nexttag = null;
00041                         String forcetag = req.getParameter("stepname");
00042                         if (forcetag != null && forcetag.length() == 0) forcetag = null;
00043                         String stepName = this.getReqStepName(req);
00044                         String redirect = req.getParameter("redirectURL");
00045                         HttpSession session = req.getSession(true);
00046 
00047                         //remove session data if first call
00048                         boolean firstcall = this.isFirstCall(req);
00049                         if (firstcall)  {
00050                                 this.removeSessionData(req);
00051                         }
00052                         Object sessionDatas = this.getSessionData(req);
00053                         if (sessionDatas == null)       { //first call for create.
00054                                 sessionDatas = this.createSessionObject(req);
00055                                 if (sessionDatas == null)       {
00056                                         StepEditService edit = (StepEditService) serviceMap.get(this.getReqStepName(req));
00057                                         return edit.getTemplateName(sessionDatas, req);
00058                                 }
00059                                 this.setSessionData(req, sessionDatas);
00060                         }
00061 
00062                         //manage redirect
00063                         if(redirect != null && !firstcall){
00064                                 //put the params request in the session and do redirect service
00065                         //      HttpSession session = req.getSession(true);
00066                         //      session.setAttribute(this.getSessionDataTempName(), req.getParameterMap());
00067 
00068                                 Object red = session.getAttribute(this.getSessionDatasName()+"redirect");
00069                                 if (red == null)        { // one redirect.
00070                                         // store step data before redirect.
00071                                         StepEditService edit = (StepEditService) serviceMap.get(stepName);
00072                                         if (edit != null)       {
00073                                                 edit.fillSessionObjectWithRequestData(sessionDatas, req);
00074                                         }
00075                                         session.setAttribute(this.getSessionDatasName()+"redirect", "true");
00076                                         ServiceManager.getManager().redirectToServiceURI(redirect, req, res);
00077                                         return null;
00078                                 }
00079                         }
00080                         session.removeAttribute(this.getSessionDatasName()+"redirect");
00081 
00082 /*                      StepEditService currentEdit = this.getCurrentStep(req);
00083                         if (currentEdit != null){
00084                                 currentEdit.fillSessionObjectWithRequestData(sessionDatas, req);
00085                                 if (!currentEdit.validateSessionStepData(sessionDatas, req, templateData))      { //go to next step
00086                                         currentEdit.fillTemplateWithSessionData(sessionDatas, templateData);
00087                                         return currentEdit.getTemplateName();
00088                                 }
00089                         } */
00090 
00091                         StepEditService edit = (StepEditService) serviceMap.get(stepName);
00092                         if (edit != null)       {
00093                                 edit.fillSessionObjectWithRequestData(sessionDatas, req);
00094                                 String nextStep = null;
00095                                 if (lasttag != null) nextStep = edit.getLastStepName(sessionDatas, req);
00096                                 else if (nexttag != null) nextStep = edit.getNextStepName(sessionDatas, req);
00097                                 else if (forcetag != null) nextStep = forcetag;
00098                                 else nextStep = edit.getStepName();
00099                                 if (nextStep != null)   {
00100                                         if (!nextStep.equals(edit.getStepName()))       { //validate before change step.
00101                                                 if (!edit.validateSessionStepData(sessionDatas, req, templateData))     { //go to next step
00102                                                         edit.fillTemplateWithSessionData(sessionDatas, req, templateData);
00103                                                         return edit.getTemplateName(sessionDatas, req);
00104                                                 }
00105                                                 edit = (StepEditService) serviceMap.get(nextStep);
00106                                         }
00107                                         edit.fillTemplateWithSessionData(sessionDatas, req, templateData);
00108                                         return edit.getTemplateName(sessionDatas, req);
00109                                 } else  { //validate if next step == null
00110                                         if (!edit.validateSessionStepData(sessionDatas, req, templateData))     { //validate current edit
00111                                                 edit.fillTemplateWithSessionData(sessionDatas, req, templateData);
00112                                                 return edit.getTemplateName(sessionDatas, req);
00113                                         }
00114                                         this.storeSessionObjectInDB(sessionDatas, req, templateData);
00115                                         this.removeSessionData(req);
00116                                         //remove next and redirect from request
00117                                         Map params = req.getParameterMap();
00118                                         params.remove("last");
00119                                         params.remove("next");
00120                                         params.remove("redirectURL");
00121                                         ServiceManager.getManager().redirectToServiceURI(this.getDisplayServiceURI(), req, res);
00122                                         return null;
00123                                 }
00124                         } else  { //end no step => validate
00125                                 this.storeSessionObjectInDB(sessionDatas, req, templateData);
00126                                 this.removeSessionData(req);
00127                                 ServiceManager.getManager().redirectToServiceURI(this.getDisplayServiceURI(), req, res);
00128                                 return null;
00129                         }
00130                 } catch (Exception ex) {
00131                         LogManager.traceError(0, ex);
00132                         this.removeSessionData(req);
00133                         throw new ServletException(ex);
00134                 }
00135         }
00136 
00137         public void addSequenceService(StepEditService service) {
00138                 serviceMap.put(service.getStepName(), service);
00139         }
00140 
00141         public boolean hasSessionData(HttpServletRequest req)   {
00142                 HttpSession session = req.getSession(true);
00143                 return session.getAttribute(this.getSessionDatasName()) != null;
00144 
00145         }
00146 
00147         protected Object getSessionData(HttpServletRequest req) {
00148                 HttpSession session = req.getSession(true);
00149                 return session.getAttribute(this.getSessionDatasName());
00150         }
00151 
00152         protected void setSessionData(HttpServletRequest req, Object sessionDatas)      {
00153                 HttpSession session = req.getSession(true);
00154                 session.setAttribute(this.getSessionDatasName(), sessionDatas);
00155         }
00156 
00157 
00158         public void removeSessionData(HttpServletRequest req)   {
00159                 HttpSession session = req.getSession(true);
00160                 session.removeAttribute(this.getSessionDatasName());
00161                 session.removeAttribute(this.getSessionDatasName()+"stepname");
00162                 session.removeAttribute(this.getSessionDatasName()+"redirect");
00163         }
00164 
00165 /*      protected StepEditService getCurrentStep(HttpServletRequest req)        {
00166                 HttpSession session = req.getSession(true);
00167                 return (StepEditService) session.getAttribute(this.getSessionDatasName()+"stepname");
00168         }
00169 
00170         protected void setCurrentStep(HttpServletRequest req, StepEditService step)     {
00171                 HttpSession session = req.getSession(true);
00172                 session.setAttribute(this.getSessionDatasName()+"stepname", step);
00173         } */
00174 
00175         private String getReqStepName(HttpServletRequest req)   {
00176                 String serviceURI = req.getRequestURI();
00177                 int index = this.getServiceUri().length();
00178                 String stepName = "";
00179                 if ((index+1)< serviceURI.length() )    {
00180                         stepName = serviceURI.substring(index+1, serviceURI.length());
00181                 }
00182                 index = stepName.indexOf("?");
00183                 if (index != -1)        {
00184                         stepName = stepName.substring(0, index);
00185                 }
00186                 return stepName;
00187         }
00188 
00196         protected abstract void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException;
00197 
00198         protected abstract String getSessionDatasName();
00199         protected abstract Object createSessionObject(HttpServletRequest req) throws ServiceException;
00200         protected abstract String getDisplayServiceURI();
00201         protected abstract boolean isFirstCall(HttpServletRequest req);
00202 
00203 }

Generated on Mon Jan 14 17:29:48 2008 for OpenMobileIS by  doxygen 1.5.4