Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

SimpleEditService.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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.services.common.ServiceManager;
00039 
00040 import freemarker.template.SimpleScalar;
00041 import freemarker.template.TemplateModelRoot;
00042 
00050 public abstract class SimpleEditService extends TemplateService {
00051                 
00052         public String runTemplate (HttpServletRequest req, HttpServletResponse res , TemplateModelRoot templateData) throws ServletException, IOException {
00053                 try     {
00054                         Object sessionDatas = this.getSessionData(req);
00055                         boolean createmode = false;
00056                         if (sessionDatas == null)       { //first call for create.
00057                                 sessionDatas = this.createSessionObject(req);
00058                                 if (sessionDatas == null)       {
00059                                         this.addParametersErrorToTemplate(req, templateData);
00060                                         return this.getTemplateName();
00061                                 }
00062                                 HttpSession session = req.getSession(true);
00063                                 session.setAttribute(this.getSessionDatasName(), sessionDatas);                         
00064                                 createmode = true;
00065                         }
00066                         
00067                         this.fillSessionObjectWithRequestData(sessionDatas, req);
00068                         
00069                         if ((!createmode) &&(!this.hasParameterError(req)) && (this.isStoreMode(req)))  {
00070                                 this.storeSessionObjectInDB(sessionDatas, req, templateData);
00071                                 if (!this.hasParameterError(req))       {
00072                                         HttpSession session = req.getSession(true);
00073                                         session.removeAttribute(this.getSessionDatasName());
00074                                         session.removeAttribute(this.getErrorSessionAttributName());
00075           ServiceManager.getManager().redirectToServiceURI(this.getDisplayServiceURI(), req, res);
00076                                         return null;
00077                                 }
00078                         }
00079                         
00080                         this.fillTemplateWithSessionData(sessionDatas, templateData);
00081                                                 
00082                         this.addParametersErrorToTemplate(req, templateData);
00083                         return this.getTemplateName();
00084                 } catch (Exception ex) {
00085                         HttpSession session = req.getSession(true);
00086                         session.removeAttribute(this.getSessionDatasName());
00087                         session.removeAttribute(this.getErrorSessionAttributName());
00088                         throw new ServletException(ex);
00089                 }
00090         }
00091                 
00092         protected void setInputRequestParameterError(HttpServletRequest req, String message) {
00093                 HttpSession session = req.getSession(true);
00094                 StringBuffer errorMessage = (StringBuffer)session.getAttribute(this.getErrorSessionAttributName());
00095                 if (errorMessage == null)       {
00096                         errorMessage = new StringBuffer();
00097                 } else  {
00098                         errorMessage.append("<BR>");
00099                 }
00100                 errorMessage.append(message);
00101                 session.setAttribute(this.getErrorSessionAttributName(),errorMessage);
00102         }
00103         
00104         protected String getErrorSessionAttributName()  {
00105                 return this.getSessionDatasName()+"-errorMessage";
00106         }
00107         
00108         protected void deleteSessionData(HttpServletRequest req)        {
00109                 HttpSession session = req.getSession(true);
00110                 session.removeAttribute(this.getSessionDatasName());
00111                 session.removeAttribute(this.getErrorSessionAttributName());
00112         }
00113         
00114         private Object getSessionData(HttpServletRequest req)   {
00115                 HttpSession session = req.getSession(true);
00116                 return session.getAttribute(this.getSessionDatasName());
00117         }
00118         
00119         private boolean hasParameterError(HttpServletRequest req)       {
00120                 HttpSession session = req.getSession(true);
00121                 return (session.getAttribute(this.getErrorSessionAttributName()) != null);
00122         }
00123         
00124         private void addParametersErrorToTemplate(HttpServletRequest req, TemplateModelRoot templateData)       {
00125                 HttpSession session = req.getSession(true);
00126                 if (this.hasParameterError(req))        {
00127                         StringBuffer error = (StringBuffer)session.getAttribute(this.getErrorSessionAttributName());
00128                         templateData.put("hasErrorMessage", new SimpleScalar(true));
00129                         templateData.put("errorMessage", new SimpleScalar(error.toString()));
00130                         //remove for next call          
00131                 }
00132                 session.removeAttribute(this.getErrorSessionAttributName());
00133         }
00134         
00135         protected abstract void fillTemplateWithSessionData(Object sessionDatas, TemplateModelRoot templateData) throws ServiceException;       
00136         protected abstract void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException;    
00137         protected abstract void fillSessionObjectWithRequestData(Object sessionDatas, HttpServletRequest req) throws ServiceException;  
00138         protected abstract String getSessionDatasName();
00139         protected abstract String getTemplateName();
00140         protected abstract Object createSessionObject(HttpServletRequest req) throws ServiceException;
00141         protected abstract String getDisplayServiceURI();
00142         
00143         
00150         protected boolean isStoreMode(HttpServletRequest req)   {
00151                 return true;
00152         }
00153 
00154 }

Generated on Wed Dec 14 21:05:35 2005 for OpenMobileIS by  doxygen 1.4.4