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
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
00077
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
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
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
00106
00107
00108 protected void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException {
00109 ReportSessionData data = (ReportSessionData)sessionDatas;
00110
00111
00112
00113
00114
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
00130
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
00169
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
00185
00186
00187 protected String getSessionDatasName() {
00188 return "reportdata";
00189 }
00190
00191
00192
00193
00194 protected String getTemplateName() {
00195 return "crm/editreport.htm";
00196 }
00197
00203 protected Object createSessionObject(HttpServletRequest req) throws ServiceException {
00204
00205
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
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 }