SimpleEditService.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
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)       { //first call for create.
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         public boolean hasSessionData(HttpServletRequest req)   {
00096                 HttpSession session = req.getSession(true);
00097                 return session.getAttribute(this.getSessionDatasName()) != null;
00098                 
00099         }
00100         public void removeSessionData(HttpServletRequest req)   {
00101                 HttpSession session = req.getSession(true);
00102                 session.removeAttribute(this.getSessionDatasName());
00103                 session.removeAttribute(this.getErrorSessionAttributName());    
00104         }
00105 
00106                 
00107         protected void setInputRequestParameterError(HttpServletRequest req, String message) {
00108                 HttpSession session = req.getSession(true);
00109                 StringBuffer errorMessage = (StringBuffer)session.getAttribute(this.getErrorSessionAttributName());
00110                 if (errorMessage == null)       {
00111                         errorMessage = new StringBuffer();
00112                 } else  {
00113                         errorMessage.append("<BR>");
00114                 }
00115                 errorMessage.append(message);
00116                 session.setAttribute(this.getErrorSessionAttributName(),errorMessage);
00117         }
00118         
00119         protected String getErrorSessionAttributName()  {
00120                 return this.getSessionDatasName()+"-errorMessage";
00121         }
00122         
00123         protected void deleteSessionData(HttpServletRequest req)        {
00124                 HttpSession session = req.getSession(true);
00125                 session.removeAttribute(this.getSessionDatasName());
00126                 session.removeAttribute(this.getErrorSessionAttributName());
00127         }
00128         
00129         private Object getSessionData(HttpServletRequest req)   {
00130                 HttpSession session = req.getSession(true);
00131                 return session.getAttribute(this.getSessionDatasName());
00132         }
00133         
00134         private boolean hasParameterError(HttpServletRequest req)       {
00135                 HttpSession session = req.getSession(true);
00136                 return (session.getAttribute(this.getErrorSessionAttributName()) != null);
00137         }
00138         
00139         private void addParametersErrorToTemplate(HttpServletRequest req, TemplateModelRoot templateData)       {
00140                 HttpSession session = req.getSession(true);
00141                 if (this.hasParameterError(req))        {
00142                         StringBuffer error = (StringBuffer)session.getAttribute(this.getErrorSessionAttributName());
00143                         templateData.put("hasErrorMessage", new SimpleScalar(true));
00144                         templateData.put("errorMessage", new SimpleScalar(error.toString()));
00145                         //remove for next call          
00146                 }
00147                 session.removeAttribute(this.getErrorSessionAttributName());
00148         }
00149         
00150         protected abstract void fillTemplateWithSessionData(Object sessionDatas, TemplateModelRoot templateData) throws ServiceException;       
00151         protected abstract void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException;    
00152         protected abstract void fillSessionObjectWithRequestData(Object sessionDatas, HttpServletRequest req) throws ServiceException;  
00153         protected abstract String getSessionDatasName();
00154         protected abstract String getTemplateName();
00155         protected abstract Object createSessionObject(HttpServletRequest req) throws ServiceException;
00156         protected abstract String getDisplayServiceURI();
00157         
00158         
00165         protected boolean isStoreMode(HttpServletRequest req)   {
00166                 return true;
00167         }
00168         
00169         protected boolean isRemoveSession(HttpServletRequest req){
00170                 return true;
00171         }
00172 
00173 }

Generated on Mon Dec 4 11:03:30 2006 for OpenMobileIS by  doxygen 1.5.1-p1