EditContactService.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 EditContactService extends SimpleEditService  implements NavigationBarService {
00057 
00058   class ContactSessionData     {
00059     String accountid;
00060     Contact contact;
00061   }
00065   public EditContactService() {
00066     super();
00067   }
00068 
00069   /* (non-Javadoc)
00070    * @see org.openmobileis.services.SimpleEditService#fillTemplateWithSessionData(java.lang.Object, freemarker.template.TemplateModelRoot)
00071    */
00072   protected void fillTemplateWithSessionData(Object sessionDatas, TemplateModelRoot templateData) throws ServiceException {
00073     ContactSessionData data = (ContactSessionData) sessionDatas;
00074    
00075     templateData.put("accountid", new SimpleScalar(data.accountid));
00076     templateData.put("contactid", new SimpleScalar(data.contact.getId()));
00077     templateData.put("firstname", new SimpleScalar(data.contact.getFirstname()));
00078     templateData.put("lastname", new SimpleScalar(data.contact.getLastname()));
00079     
00080     //for action
00081     Array array = LabelManager.getManager().getLabelListForCategorie(Contact.CONTACT_FUNCTION_LABEL_CATEGORY);
00082     templateData.put("functions", TemplateUtils.fillFormPopUpWithLabel(array, Integer.toString(data.contact.getFunction())));
00083   }
00084 
00085   /* (non-Javadoc)
00086    * @see org.openmobileis.services.SimpleEditService#storeSessionObjectInDB(java.lang.Object, javax.servlet.http.HttpServletRequest, freemarker.template.TemplateModelRoot)
00087    */
00088   protected void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException {
00089     ContactSessionData data = (ContactSessionData)sessionDatas;
00090     
00091     
00092     //validate the edited object has an id.
00093     if (data.contact.getLastname() == null || data.contact.getLastname().trim().length() == 0) {
00094       this.setInputRequestParameterError(req, "Error :A contact lastname must be entered.");
00095       return;
00096     } 
00097     
00098     try {
00099       Account account = AccountFactory.getManager().getAccount(data.accountid);
00100       account.addContact(data.contact);
00101       AccountFactory.getManager().storeAccount(account);
00102     } catch (DatabaseException e) {
00103       throw new ServiceException(e);
00104     }
00105   }
00106 
00107   /* (non-Javadoc)
00108    * @see org.openmobileis.services.SimpleEditService#fillSessionObjectWithRequestData(java.lang.Object, javax.servlet.http.HttpServletRequest)
00109    */
00110   protected void fillSessionObjectWithRequestData(Object sessionDatas, HttpServletRequest req) throws ServiceException {
00111     Contact data = ((ContactSessionData)sessionDatas).contact;
00112     
00113     // the Id of the object is edited. Its better to generate an unique id if new ID is needed.
00114     // Avoid Db problem with same Id . See UniqueIdGenerator class to generate an Id
00115     String temp = (String)req.getParameter("firstname");
00116     if (temp != null) {
00117       data.setFirstname(temp);
00118     }
00119     temp = (String)req.getParameter("lastname");
00120     if (temp != null) {
00121       data.setLastname(temp);
00122     }
00123     
00124     temp = (String)req.getParameter("function");
00125     if (temp != null) {
00126       try   {
00127         data.setFunction(Integer.parseInt(temp));
00128       } catch (NumberFormatException ex)   {
00129         data.setFunction(0);
00130       }
00131     }
00132   }
00133 
00134   /* (non-Javadoc)
00135    * @see org.openmobileis.services.SimpleEditService#getSessionDatasName()
00136    */
00137   protected String getSessionDatasName() {
00138     return "contactdata";
00139   }
00140 
00141   /* (non-Javadoc)
00142    * @see org.openmobileis.services.SimpleEditService#getTemplateName()
00143    */
00144   protected String getTemplateName() {
00145     return "crm/editcontact.htm";
00146   }
00147 
00153   protected Object createSessionObject(HttpServletRequest req) throws ServiceException {
00154     // the Id of the object is edited. Its better to generate an unique id if new ID is needed.
00155     // Avoid Db problem with same Id . See UniqueIdGenerator class to generate an Id
00156     String accountid = req.getParameter("accountid");
00157     String contactid = req.getParameter("contactid");
00158     ContactSessionData data = new ContactSessionData();
00159     data.accountid = accountid;
00160     if (accountid != null && contactid != null) {
00161       try {
00162         Account account = AccountFactory.getManager().getAccount(accountid);
00163         data.contact = account.getContactById(contactid);
00164       } catch (DatabaseException ex)  {
00165         throw new ServiceException(ex);
00166       }
00167     }
00168     if (data.contact == null) {
00169       data.contact = new Contact(UniqueIdGenerator.getManager().getNewStringID());
00170     }
00171     return data;
00172   }
00173 
00178   protected String getDisplayServiceURI() {
00179      return "/crm/displaycontact";
00180   }
00181 
00182   public String getServiceUri() {
00183     return ServiceManager.getManager().getServiceBaseURI() + "/crm/editcontact";
00184   }
00185 
00186   /* 
00187    * Navigation bar methods
00188    */
00189   public String getNavigationBarLabel(HttpServletRequest req) {
00190     return IntlResourceManager.getManager().getLocalizedProperty("mycrm.editservicenavbar", "Edit");
00191   }
00192 
00193   public boolean displayFormExitMessage() {
00194     return true;
00195   }
00196 
00197   public boolean displayRecursive() {
00198     return false;
00199   }
00200 
00201 }

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