MultiStepMainService.java

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

Generated on Tue May 22 23:01:10 2007 for OpenMobileIS by  doxygen 1.5.1-p1