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

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