00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00080
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
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
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
00109
00110
00111 protected void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException {
00112 ReportSessionData data = (ReportSessionData)sessionDatas;
00113
00114
00115
00116
00117
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
00133
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
00172
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
00188
00189
00190 protected String getSessionDatasName() {
00191 return "reportdata";
00192 }
00193
00194
00195
00196
00197 protected String getTemplateName() {
00198 return "crm/editreport.htm";
00199 }
00200
00206 protected Object createSessionObject(HttpServletRequest req) throws ServiceException {
00207
00208
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
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 }