DisplayAccountService.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 java.io.IOException;
00028 
00029 import javax.servlet.ServletException;
00030 import javax.servlet.http.HttpServletRequest;
00031 import javax.servlet.http.HttpServletResponse;
00032 
00033 import org.openmobileis.common.util.collection.Array;
00034 import org.openmobileis.common.util.exception.DatabaseException;
00035 import org.openmobileis.common.util.exception.ServiceException;
00036 import org.openmobileis.common.util.log.LogManager;
00037 import org.openmobileis.examples.mycrm.data.Account;
00038 import org.openmobileis.examples.mycrm.data.Contact;
00039 import org.openmobileis.examples.mycrm.data.Leads;
00040 import org.openmobileis.examples.mycrm.data.fodb.AccountFactory;
00041 import org.openmobileis.examples.mycrm.data.fodb.LeadsFactory;
00042 import org.openmobileis.modules.common.data.LabelManager;
00043 import org.openmobileis.modules.common.history.FODBHistoryManager;
00044 import org.openmobileis.modules.common.history.HistoryManager;
00045 import org.openmobileis.services.TemplateService;
00046 import org.openmobileis.services.common.ServiceManager;
00047 import org.openmobileis.services.navigation.NavigationBarService;
00048 import org.openmobileis.services.servlet.IndexServlet;
00049 import org.openmobileis.services.servlet.IndexServletContentListener;
00050 
00051 import freemarker.template.SimpleHash;
00052 import freemarker.template.SimpleList;
00053 import freemarker.template.SimpleScalar;
00054 import freemarker.template.TemplateModelRoot;
00055 
00064 public final class DisplayAccountService extends TemplateService  implements NavigationBarService, IndexServletContentListener {
00065   private HistoryManager histomanager;
00066 
00070   public DisplayAccountService() {
00071     super();
00072     histomanager = new FODBHistoryManager("account", 3);
00073     IndexServlet.getIndexServlet().addContentListener(this);
00074   }
00075 
00076   /*
00077    * (non-Javadoc)
00078    * 
00079    * @see org.openmobileis.services.TemplateService#runTemplate(javax.servlet.http.HttpServletRequest,
00080    *      javax.servlet.http.HttpServletResponse,
00081    *      freemarker.template.TemplateModelRoot)
00082    */
00083   public String runTemplate(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) throws ServletException, IOException {
00084     String accountid = req.getParameter("accountid");
00085     if (accountid != null) {
00086       try   {
00087         histomanager.addUID(accountid);
00088         Account account = AccountFactory.getManager().getAccount(accountid);
00089         templateData.put("name", new SimpleScalar(account.getName()));
00090         templateData.put("address", new SimpleScalar(account.getAddress()));
00091         templateData.put("city", new SimpleScalar(account.getCity()));
00092         templateData.put("activity", new SimpleScalar(LabelManager.getManager().getLabelWithIds(account.getActivity(), Account.ACCOUNT_ACTIVITY_LABEL_CATEGORY).getLabel()));     
00093         
00094         Array contactlist = account.getAllContacts();      
00095         // construct the SimpleList to fill HTML array (see the template).
00096         SimpleList showList = new SimpleList();
00097         SimpleHash dataStruct;
00098         for (int i = 0; i < contactlist.size(); i++) {
00099           dataStruct = new SimpleHash();
00100           Contact contact = (Contact)contactlist.get(i);
00101           dataStruct.put("id", contact.getId());
00102           dataStruct.put("firstname",  contact.getFirstname());
00103           dataStruct.put("lastname",  contact.getLastname());
00104           showList.add(dataStruct);
00105         }
00106         templateData.put("contactlist", showList);
00107 
00108         Array leadslist = LeadsFactory.getManager().getLeadsForAccount(accountid);      
00109         // construct the SimpleList to fill HTML array (see the template).
00110         java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat("MM/dd/yyyy", java.util.Locale.FRANCE);
00111         showList = new SimpleList();
00112         for (int i = 0; i < leadslist.size(); i++) {
00113           dataStruct = new SimpleHash();
00114           Leads lead = (Leads)leadslist.get(i);
00115           dataStruct.put("leadid", lead.getId());
00116           dataStruct.put("amount",  Integer.toString(lead.getAmount()));
00117           dataStruct.put("date",  formater.format(lead.getCloseDate()).toString());
00118           showList.add(dataStruct);
00119         }
00120         templateData.put("leadslist", showList);
00121         
00122         templateData.put("accountid", new SimpleScalar(accountid));
00123       } catch (DatabaseException ex) {
00124         throw new ServletException(ex);
00125       }
00126     }
00127     return "crm/displayaccount.htm";
00128   }
00129 
00130   /* (non-Javadoc)
00131    * @see org.openmobileis.services.servlet.IndexServletContentListener#addContent(freemarker.template.TemplateModelRoot)
00132    */
00133   public void addContent(TemplateModelRoot modelRoot) throws ServiceException {
00134     try {
00135     Array allids = histomanager.getAll();
00136     SimpleList showList = new SimpleList();
00137     SimpleHash dataStruct;
00138     for (int i = 0; i < allids.size(); i++) {
00139       dataStruct = new SimpleHash();
00140       String accountid = (String)allids.get(i);
00141       Account account = AccountFactory.getManager().getAccount(accountid);
00142       if (account != null)  {
00143         dataStruct.put("id", account.getId());
00144         dataStruct.put("name",  account.getName());
00145         dataStruct.put("city",  account.getCity());
00146         showList.add(dataStruct);
00147       } else  {
00148         histomanager.removeUID(accountid);
00149       }
00150     }
00151     modelRoot.put("accountList", showList);
00152     } catch (Throwable ex)  {
00153       throw new ServiceException(ex);
00154     }
00155   }
00156 
00157   /*
00158    * (non-Javadoc)
00159    * 
00160    * @see org.openmobileis.services.Service#getServiceUri()
00161    */
00162   public String getServiceUri() {
00163     return ServiceManager.getManager().getServiceBaseURI() + "/crm/displayaccount";
00164   }
00165 
00166   /* 
00167    * Navigation bar methods
00168    */
00169 
00170   public String getNavigationBarLabel(HttpServletRequest req) {
00171     String paramId = req.getParameter("accountid");
00172     if (paramId != null) {
00173       try   {
00174         Account account = AccountFactory.getManager().getAccount(paramId);
00175         return account.getName();
00176       } catch (Throwable ex)    {
00177         LogManager.traceError(0, ex);
00178       }
00179     }
00180     return "Account";
00181   }
00182 
00183   public boolean displayFormExitMessage() {
00184     return false;
00185   }
00186 
00187   public boolean displayRecursive() {
00188     return false;
00189   }
00190 
00191 }

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