EditAccountService.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.mycrm.terminal.services;
00026 
00027 import javax.servlet.http.HttpServletRequest;
00028 
00029 import org.openmobileis.common.intl.IntlResourceManager;
00030 import org.openmobileis.common.util.UniqueIdGenerator;
00031 import org.openmobileis.common.util.collection.Array;
00032 import org.openmobileis.common.util.exception.DatabaseException;
00033 import org.openmobileis.common.util.exception.ServiceException;
00034 import org.openmobileis.examples.mycrm.data.Account;
00035 import org.openmobileis.examples.mycrm.data.fodb.AccountFactory;
00036 import org.openmobileis.modules.common.data.LabelManager;
00037 import org.openmobileis.services.SimpleEditService;
00038 import org.openmobileis.services.common.ServiceManager;
00039 import org.openmobileis.services.navigation.NavigationBarService;
00040 import org.openmobileis.services.util.TemplateUtils;
00041 
00042 import freemarker.template.SimpleScalar;
00043 import freemarker.template.TemplateModelRoot;
00044 
00055 public final class EditAccountService extends SimpleEditService  implements NavigationBarService {
00059   public EditAccountService() {
00060     super();
00061   }
00062 
00063   /* (non-Javadoc)
00064    * @see org.openmobileis.services.SimpleEditService#fillTemplateWithSessionData(java.lang.Object, freemarker.template.TemplateModelRoot)
00065    */
00066   protected void fillTemplateWithSessionData(Object sessionDatas, TemplateModelRoot templateData) throws ServiceException {
00067     Account data = (Account) sessionDatas;
00068    
00069     templateData.put("accountid", new SimpleScalar(data.getId()));
00070     templateData.put("name", new SimpleScalar(data.getName()));
00071     templateData.put("address", new SimpleScalar(data.getAddress()));
00072     templateData.put("city", new SimpleScalar(data.getCity()));
00073     
00074     //for action
00075     Array array = LabelManager.getManager().getLabelListForCategorie(Account.ACCOUNT_ACTIVITY_LABEL_CATEGORY);
00076     templateData.put("activities", TemplateUtils.fillFormPopUpWithLabel(array, data.getActivity()));
00077   }
00078 
00079   /* (non-Javadoc)
00080    * @see org.openmobileis.services.SimpleEditService#storeSessionObjectInDB(java.lang.Object, javax.servlet.http.HttpServletRequest, freemarker.template.TemplateModelRoot)
00081    */
00082   protected void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException {
00083     Account data = (Account)sessionDatas;
00084     
00085     //validate the edited object has an id.
00086     if (data.getName() == null || data.getName().trim().length() == 0) {
00087       this.setInputRequestParameterError(req, "Error :A account name must be entered.");
00088       return;
00089     } 
00090     
00091     try {
00092       AccountFactory.getManager().storeAccount(data);
00093     } catch (DatabaseException e) {
00094       throw new ServiceException(e);
00095     }
00096   }
00097 
00098   /* (non-Javadoc)
00099    * @see org.openmobileis.services.SimpleEditService#fillSessionObjectWithRequestData(java.lang.Object, javax.servlet.http.HttpServletRequest)
00100    */
00101   protected void fillSessionObjectWithRequestData(Object sessionDatas, HttpServletRequest req) throws ServiceException {
00102     Account data = (Account)sessionDatas;
00103     
00104     // the Id of the object is edited. Its better to generate an unique id if new ID is needed.
00105     // Avoid Db problem with same Id . See UniqueIdGenerator class to generate an Id
00106     String temp = (String)req.getParameter("name");
00107     if (temp != null) {
00108       data.setName(temp);
00109     }
00110     temp = (String)req.getParameter("address");
00111     if (temp != null) {
00112       data.setAddress(temp);
00113     }
00114     temp = (String)req.getParameter("city");
00115     if (temp != null) {
00116       data.setCity(temp);
00117     }
00118     temp = (String)req.getParameter("activity");
00119     if (temp != null) {
00120       data.setActivity(temp);
00121     }
00122   }
00123 
00124   /* (non-Javadoc)
00125    * @see org.openmobileis.services.SimpleEditService#getSessionDatasName()
00126    */
00127   protected String getSessionDatasName() {
00128     return "accountdata";
00129   }
00130 
00131   /* (non-Javadoc)
00132    * @see org.openmobileis.services.SimpleEditService#getTemplateName()
00133    */
00134   protected String getTemplateName() {
00135     return "crm/editaccount.htm";
00136   }
00137 
00143   protected Object createSessionObject(HttpServletRequest req) throws ServiceException {
00144     // the Id of the object is edited. Its better to generate an unique id if new ID is needed.
00145     // Avoid Db problem with same Id . See UniqueIdGenerator class to generate an Id
00146     String accountid = req.getParameter("accountid");
00147     Account account = null;
00148     if (accountid != null) {
00149       try {
00150         account = AccountFactory.getManager().getAccount(accountid);
00151       } catch (DatabaseException ex)  {
00152         throw new ServiceException(ex);
00153       }
00154     }
00155     if (account == null) {
00156       account = new Account(UniqueIdGenerator.getManager().getNewStringID());
00157     }
00158     return account;
00159   }
00160 
00165   protected String getDisplayServiceURI() {
00166      return "/crm/displayaccount";
00167   }
00168 
00169   public String getServiceUri() {
00170     return ServiceManager.getManager().getServiceBaseURI() + "/crm/editaccount";
00171   }
00172 
00173   /* 
00174    * Navigation bar methods
00175    */
00176   public String getNavigationBarLabel(HttpServletRequest req) {
00177     return IntlResourceManager.getManager().getLocalizedProperty("mycrm.editservicenavbar", "Edit");
00178   }
00179 
00180   public boolean displayFormExitMessage() {
00181     return true;
00182   }
00183 
00184   public boolean displayRecursive() {
00185     return false;
00186   }
00187 
00188 }

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