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 
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                         //remove for next call
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 
00175         protected boolean isRemoveSession(HttpServletRequest req){
00176                 return true;
00177         }
00178 
00179 }

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