EditReportService.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.util.Calendar;
00028 
00029 import javax.servlet.http.HttpServletRequest;
00030 
00031 import org.openmobileis.common.context.SessionContext;
00032 import org.openmobileis.common.context.SessionContextManager;
00033 import org.openmobileis.common.intl.IntlResourceManager;
00034 import org.openmobileis.common.util.UniqueIdGenerator;
00035 import org.openmobileis.common.util.collection.Array;
00036 import org.openmobileis.common.util.exception.DatabaseException;
00037 import org.openmobileis.common.util.exception.ServiceException;
00038 import org.openmobileis.examples.mycrm.data.Account;
00039 import org.openmobileis.examples.mycrm.data.Contact;
00040 import org.openmobileis.examples.mycrm.data.Report;
00041 import org.openmobileis.examples.mycrm.data.fodb.AccountFactory;
00042 import org.openmobileis.modules.common.data.LabelManager;
00043 import org.openmobileis.services.SimpleEditService;
00044 import org.openmobileis.services.common.ServiceManager;
00045 import org.openmobileis.services.navigation.NavigationBarService;
00046 import org.openmobileis.services.util.TemplateUtils;
00047 
00048 import freemarker.template.SimpleHash;
00049 import freemarker.template.SimpleScalar;
00050 import freemarker.template.TemplateModelRoot;
00051 
00062 public final class EditReportService extends SimpleEditService  implements NavigationBarService {
00063 
00064   class ReportSessionData     {
00065     String accountid;
00066     String contactid;
00067     Report report;
00068   }
00072   public EditReportService() {
00073     super();
00074   }
00075 
00076   /* (non-Javadoc)
00077    * @see org.openmobileis.services.SimpleEditService#fillTemplateWithSessionData(java.lang.Object, freemarker.template.TemplateModelRoot)
00078    */
00079   protected void fillTemplateWithSessionData(Object sessionDatas, TemplateModelRoot templateData) throws ServiceException {
00080     ReportSessionData data = (ReportSessionData) sessionDatas;
00081    
00082     templateData.put("accountid", new SimpleScalar(data.accountid));
00083     templateData.put("contactid", new SimpleScalar(data.contactid));
00084     templateData.put("reportid", new SimpleScalar(data.report.getId()));
00085     
00086     templateData.put("description", new SimpleScalar(data.report.getDescription()));
00087     
00088     //for action
00089     Array array = LabelManager.getManager().getLabelListForCategorie(Report.REPORT_ACTION_LABEL_CATEGORY);
00090     templateData.put("actions", TemplateUtils.fillFormPopUpWithLabel(array, Integer.toString(data.report.getAction())));
00091 
00092     Calendar calendar = Calendar.getInstance();
00093     calendar.setTime(data.report.getReportDate());
00094     // Fill start date and end date
00095     SimpleHash startDate =
00096         TemplateUtils.getInputDate(
00097             calendar.get(java.util.Calendar.DAY_OF_MONTH),
00098             calendar.get(java.util.Calendar.MONTH),
00099             calendar.get(java.util.Calendar.YEAR),
00100             calendar.get(java.util.Calendar.HOUR_OF_DAY),
00101             0);
00102     templateData.put("startDate", startDate);
00103   }
00104 
00105   /* (non-Javadoc)
00106    * @see org.openmobileis.services.SimpleEditService#storeSessionObjectInDB(java.lang.Object, javax.servlet.http.HttpServletRequest, freemarker.template.TemplateModelRoot)
00107    */
00108   protected void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException {
00109     ReportSessionData data = (ReportSessionData)sessionDatas;
00110     
00111     //validate the edited object has an id.
00112  /*   if (accountid == null) {
00113       this.setInputRequestParameterError(req, "Error :No account id. Restart edit process.");
00114       return;
00115     } */
00116     
00117     try {
00118       Account account = AccountFactory.getManager().getAccount(data.accountid);
00119       Contact contact = account.getContactById(data.contactid);
00120       SessionContext context = SessionContextManager.getManager().getSessionContext();
00121       data.report.setTerminalUserId(context.getUserId());
00122       contact.addReport(data.report);
00123       AccountFactory.getManager().storeAccount(account);
00124     } catch (DatabaseException e) {
00125       throw new ServiceException(e);
00126     }
00127   }
00128 
00129   /* (non-Javadoc)
00130    * @see org.openmobileis.services.SimpleEditService#fillSessionObjectWithRequestData(java.lang.Object, javax.servlet.http.HttpServletRequest)
00131    */
00132   protected void fillSessionObjectWithRequestData(Object sessionDatas, HttpServletRequest req) throws ServiceException {
00133     Report data = ((ReportSessionData)sessionDatas).report;
00134       Calendar caldate = Calendar.getInstance();
00135       caldate.setTime(data.getReportDate());
00136       String strStartDay = req.getParameter("startDay");
00137         if (strStartDay != null && strStartDay.length() != 0) {
00138             try {
00139               caldate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(strStartDay));
00140             } catch (NumberFormatException ex) {
00141               this.setInputRequestParameterError(req, "Error :Bad day.");
00142               return;
00143             }
00144         }
00145         
00146         String strStartMonth = req.getParameter("startMonth");
00147         if (strStartMonth != null && strStartMonth.length() != 0) {
00148             try {
00149               caldate.set(Calendar.MONTH, Integer.parseInt(strStartMonth) - 1);
00150             } catch (NumberFormatException ex) {
00151               this.setInputRequestParameterError(req, "Error :Bad Month.");
00152               return;
00153             }
00154         }
00155         
00156         String strStartYear = req.getParameter("startYear");
00157         if (strStartYear != null && strStartYear.length() != 0) {
00158             try {
00159                  caldate.set(Calendar.YEAR, Integer.parseInt(strStartYear));
00160             } catch (NumberFormatException ex) {
00161               this.setInputRequestParameterError(req, "Error :Bad Year.");
00162               return;
00163             }
00164         }
00165         
00166         data.setReportDate(caldate.getTime());
00167 
00168     // the Id of the object is edited. Its better to generate an unique id if new ID is needed.
00169     // Avoid Db problem with same Id . See UniqueIdGenerator class to generate an Id
00170     String temp = (String)req.getParameter("description");
00171     if (temp != null) {
00172       data.setDescription(temp);
00173     }
00174     temp = (String)req.getParameter("action");
00175     if (temp != null) {
00176       try   {
00177         data.setAction(Integer.parseInt(temp));
00178       } catch (NumberFormatException ex)   {
00179         data.setAction(0);
00180       }
00181     }
00182   }
00183 
00184   /* (non-Javadoc)
00185    * @see org.openmobileis.services.SimpleEditService#getSessionDatasName()
00186    */
00187   protected String getSessionDatasName() {
00188     return "reportdata";
00189   }
00190 
00191   /* (non-Javadoc)
00192    * @see org.openmobileis.services.SimpleEditService#getTemplateName()
00193    */
00194   protected String getTemplateName() {
00195     return "crm/editreport.htm";
00196   }
00197 
00203   protected Object createSessionObject(HttpServletRequest req) throws ServiceException {
00204     // the Id of the object is edited. Its better to generate an unique id if new ID is needed.
00205     // Avoid Db problem with same Id . See UniqueIdGenerator class to generate an Id
00206     String accountid = req.getParameter("accountid");
00207     String contactid = req.getParameter("contactid");
00208     String reportid = req.getParameter("reportid");
00209     ReportSessionData data = new ReportSessionData();
00210     data.accountid = accountid;
00211     data.contactid = contactid;
00212     if (accountid != null && contactid != null && reportid != null) {
00213       try {
00214         Account account = AccountFactory.getManager().getAccount(accountid);
00215         Contact contact  = account.getContactById(contactid);
00216         data.report = contact.getReportById(reportid);
00217       } catch (DatabaseException ex)  {
00218         throw new ServiceException(ex);
00219       }
00220     }
00221     if (data.report == null) {
00222       data.report = new Report(UniqueIdGenerator.getManager().getNewStringID());
00223     }
00224     return data;
00225   }
00226 
00231   protected String getDisplayServiceURI() {
00232      return "/crm/displayreport";
00233   }
00234 
00235   public String getServiceUri() {
00236     return ServiceManager.getManager().getServiceBaseURI() + "/crm/editreport";
00237   }
00238 
00239   /* 
00240    * Navigation bar methods
00241    */
00242   public String getNavigationBarLabel(HttpServletRequest req) {
00243     return IntlResourceManager.getManager().getLocalizedProperty("mycrm.editservicenavbar", "Edit");
00244   }
00245 
00246   public boolean displayFormExitMessage() {
00247     return true;
00248   }
00249 
00250   public boolean displayRecursive() {
00251     return false;
00252   }
00253 
00254 }

Generated on Tue May 22 23:01:09 2007 for OpenMobileIS by  doxygen 1.5.1-p1