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

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