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 javax.servlet.http.HttpServletRequest;
00028
00029 import org.openmobileis.common.intl.IntlResourceManager;
00030 import org.openmobileis.common.util.UniqueIdGenerator;
00031 import org.openmobileis.common.util.collection.Array;
00032 import org.openmobileis.common.util.exception.DatabaseException;
00033 import org.openmobileis.common.util.exception.ServiceException;
00034 import org.openmobileis.examples.mycrm.data.Account;
00035 import org.openmobileis.examples.mycrm.data.Contact;
00036 import org.openmobileis.examples.mycrm.data.fodb.AccountFactory;
00037 import org.openmobileis.modules.common.data.LabelManager;
00038 import org.openmobileis.services.SimpleEditService;
00039 import org.openmobileis.services.common.ServiceManager;
00040 import org.openmobileis.services.navigation.NavigationBarService;
00041 import org.openmobileis.services.util.TemplateUtils;
00042
00043 import freemarker.template.SimpleScalar;
00044 import freemarker.template.TemplateModelRoot;
00045
00056 public final class EditContactService extends SimpleEditService implements NavigationBarService {
00057
00058 class ContactSessionData {
00059 String accountid;
00060 Contact contact;
00061 }
00065 public EditContactService() {
00066 super();
00067 }
00068
00069
00070
00071
00072 protected void fillTemplateWithSessionData(Object sessionDatas, TemplateModelRoot templateData) throws ServiceException {
00073 ContactSessionData data = (ContactSessionData) sessionDatas;
00074
00075 templateData.put("accountid", new SimpleScalar(data.accountid));
00076 templateData.put("contactid", new SimpleScalar(data.contact.getId()));
00077 templateData.put("firstname", new SimpleScalar(data.contact.getFirstname()));
00078 templateData.put("lastname", new SimpleScalar(data.contact.getLastname()));
00079
00080
00081 Array array = LabelManager.getManager().getLabelListForCategorie(Contact.CONTACT_FUNCTION_LABEL_CATEGORY);
00082 templateData.put("functions", TemplateUtils.fillFormPopUpWithLabel(array, Integer.toString(data.contact.getFunction())));
00083 }
00084
00085
00086
00087
00088 protected void storeSessionObjectInDB(Object sessionDatas, HttpServletRequest req, TemplateModelRoot templateData) throws ServiceException {
00089 ContactSessionData data = (ContactSessionData)sessionDatas;
00090
00091
00092
00093 if (data.contact.getLastname() == null || data.contact.getLastname().trim().length() == 0) {
00094 this.setInputRequestParameterError(req, "Error :A contact lastname must be entered.");
00095 return;
00096 }
00097
00098 try {
00099 Account account = AccountFactory.getManager().getAccount(data.accountid);
00100 account.addContact(data.contact);
00101 AccountFactory.getManager().storeAccount(account);
00102 } catch (DatabaseException e) {
00103 throw new ServiceException(e);
00104 }
00105 }
00106
00107
00108
00109
00110 protected void fillSessionObjectWithRequestData(Object sessionDatas, HttpServletRequest req) throws ServiceException {
00111 Contact data = ((ContactSessionData)sessionDatas).contact;
00112
00113
00114
00115 String temp = (String)req.getParameter("firstname");
00116 if (temp != null) {
00117 data.setFirstname(temp);
00118 }
00119 temp = (String)req.getParameter("lastname");
00120 if (temp != null) {
00121 data.setLastname(temp);
00122 }
00123
00124 temp = (String)req.getParameter("function");
00125 if (temp != null) {
00126 try {
00127 data.setFunction(Integer.parseInt(temp));
00128 } catch (NumberFormatException ex) {
00129 data.setFunction(0);
00130 }
00131 }
00132 }
00133
00134
00135
00136
00137 protected String getSessionDatasName() {
00138 return "contactdata";
00139 }
00140
00141
00142
00143
00144 protected String getTemplateName() {
00145 return "crm/editcontact.htm";
00146 }
00147
00153 protected Object createSessionObject(HttpServletRequest req) throws ServiceException {
00154
00155
00156 String accountid = req.getParameter("accountid");
00157 String contactid = req.getParameter("contactid");
00158 ContactSessionData data = new ContactSessionData();
00159 data.accountid = accountid;
00160 if (accountid != null && contactid != null) {
00161 try {
00162 Account account = AccountFactory.getManager().getAccount(accountid);
00163 data.contact = account.getContactById(contactid);
00164 } catch (DatabaseException ex) {
00165 throw new ServiceException(ex);
00166 }
00167 }
00168 if (data.contact == null) {
00169 data.contact = new Contact(UniqueIdGenerator.getManager().getNewStringID());
00170 }
00171 return data;
00172 }
00173
00178 protected String getDisplayServiceURI() {
00179 return "/crm/displaycontact";
00180 }
00181
00182 public String getServiceUri() {
00183 return ServiceManager.getManager().getServiceBaseURI() + "/crm/editcontact";
00184 }
00185
00186
00187
00188
00189 public String getNavigationBarLabel(HttpServletRequest req) {
00190 return IntlResourceManager.getManager().getLocalizedProperty("mycrm.editservicenavbar", "Edit");
00191 }
00192
00193 public boolean displayFormExitMessage() {
00194 return true;
00195 }
00196
00197 public boolean displayRecursive() {
00198 return false;
00199 }
00200
00201 }