EditBaseDataService.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  */
00025 package org.openmobileis.examples.simpleappli.terminal.services;
00026 
00027 import javax.servlet.http.HttpServletRequest;
00028 
00029 import org.openmobileis.common.util.exception.DatabaseException;
00030 import org.openmobileis.common.util.exception.ServiceException;
00031 import org.openmobileis.examples.simpleappli.data.BaseData;
00032 import org.openmobileis.examples.simpleappli.data.FODB.FODBBaseDataFactory;
00033 import org.openmobileis.services.SimpleEditService;
00034 import org.openmobileis.services.common.ServiceManager;
00035 import org.openmobileis.services.navigation.NavigationBarService;
00036 
00037 import freemarker.template.SimpleScalar;
00038 import freemarker.template.TemplateModelRoot;
00039 
00050 public final class EditBaseDataService extends SimpleEditService  implements NavigationBarService {
00051 
00055   public EditBaseDataService() {
00056     super();
00057   }
00058 
00059   /* (non-Javadoc)
00060    * @see org.openmobileis.services.SimpleEditService#fillTemplateWithSessionData(java.lang.Object, freemarker.template.TemplateModelRoot)
00061    */
00062   protected void fillTemplateWithSessionData(Object sessionDatas, TemplateModelRoot templateData) throws ServiceException {
00063     BaseData data = (BaseData) sessionDatas;
00064    
00065     templateData.put("id", new SimpleScalar(data.getId()));
00066     templateData.put("data",new SimpleScalar(data.getData()));
00067   }
00068 
00069   /* (non-Javadoc)
00070    * @see org.openmobileis.services.SimpleEditService#storeSessionObjectInDB(java.lang.Object, javax.servlet.http.HttpServletRequest, freemarker.template.TemplateModelRoot)
00071    */
00072   protected void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException {
00073     BaseData data = (BaseData) sessionDatas;
00074     
00075     //validate the edited object has an id.
00076     if ((data.getId() == null) || (data.getId().trim().length() == 0)) {
00077       this.setInputRequestParameterError(req, "Error :Data Id must be defined.");
00078       return;
00079     }
00080     
00081     try {
00082       FODBBaseDataFactory.getManager().storeBaseData(data);
00083     } catch (DatabaseException e) {
00084       throw new ServiceException(e);
00085     }
00086   }
00087 
00088   /* (non-Javadoc)
00089    * @see org.openmobileis.services.SimpleEditService#fillSessionObjectWithRequestData(java.lang.Object, javax.servlet.http.HttpServletRequest)
00090    */
00091   protected void fillSessionObjectWithRequestData(Object sessionDatas, HttpServletRequest req) throws ServiceException {
00092     BaseData data = (BaseData)sessionDatas;
00093     
00094     // the Id of the object is edited. Its better to generate an unique id if new ID is needed.
00095     // Avoid Db problem with same Id . See UniqueIdGenerator class to generate an Id
00096     String temp = (String)req.getParameter("id");
00097     if (temp != null) {
00098       data.setId(temp);
00099     }
00100     temp = (String)req.getParameter("data");
00101     if (temp != null) {
00102       data.setData(temp);
00103     }
00104   }
00105 
00106   /* (non-Javadoc)
00107    * @see org.openmobileis.services.SimpleEditService#getSessionDatasName()
00108    */
00109   protected String getSessionDatasName() {
00110     return "basedata";
00111   }
00112 
00113   /* (non-Javadoc)
00114    * @see org.openmobileis.services.SimpleEditService#getTemplateName()
00115    */
00116   protected String getTemplateName() {
00117     return "appli/editdata.htm";
00118   }
00119 
00125   protected Object createSessionObject(HttpServletRequest req) throws ServiceException {
00126     // the Id of the object is edited. Its better to generate an unique id if new ID is needed.
00127     // Avoid Db problem with same Id . See UniqueIdGenerator class to generate an Id
00128    String idData = (String)req.getParameter("id");
00129     BaseData data = null;
00130     if (idData !=null)  {
00131       try {
00132         data = FODBBaseDataFactory.getManager().getBaseData(idData);
00133       } catch (DatabaseException ex)  {
00134         throw new ServiceException(ex);
00135       }
00136     }
00137     if (data == null) {
00138       data = new BaseData();
00139     }
00140     return data;
00141   }
00142 
00147   protected String getDisplayServiceURI() {
00148      return "/basedata/showall";
00149   }
00150 
00151   public String getServiceUri() {
00152     return ServiceManager.getManager().getServiceBaseURI() + "/basedata/edit";
00153   }
00154 
00155   /* 
00156    * Navigation bar methods
00157    */
00158   public String getNavigationBarLabel(HttpServletRequest req) {
00159     return "Edit";
00160   }
00161 
00162   public boolean displayFormExitMessage() {
00163     return true;
00164   }
00165 
00166   public boolean displayRecursive() {
00167     return false;
00168   }
00169 
00170 }

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