001: package org.claros.intouch.contacts.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005:
006: import javax.servlet.ServletException;
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.claros.intouch.common.services.BaseService;
013: import org.claros.intouch.common.utility.Constants;
014: import org.claros.intouch.contacts.controllers.ContactsController;
015: import org.claros.intouch.contacts.models.Contact;
016:
017: public class SaveContactService extends BaseService {
018:
019: /**
020: *
021: */
022: private static final long serialVersionUID = -5047398836515996933L;
023: private static Log log = LogFactory
024: .getLog(GetContactDetailsService.class);
025: private static String charset = Constants.charset;
026:
027: /**
028: * The doPost method of the servlet. <br>
029: *
030: * This method is called when a form has its tag value method equals to post.
031: *
032: * @param request the request send by the client to the server
033: * @param response the response send by the server to the client
034: * @throws ServletException if an error occurred
035: * @throws IOException if an error occurred
036: */
037: public void doPost(HttpServletRequest request,
038: HttpServletResponse response) throws ServletException,
039: IOException {
040:
041: response.setHeader("Expires", "-1");
042: response.setHeader("Pragma", "no-cache");
043: response.setHeader("Cache-control", "no-cache");
044: response.setHeader("Content-Type", "text/html; charset=utf-8");
045: PrintWriter out = response.getWriter();
046:
047: try {
048: boolean doConversion = false;
049: String id = getValue(request, "contId", doConversion);
050:
051: String firstName = getValue(request, "contFirstName",
052: doConversion);
053: String middleName = getValue(request, "contMiddleName",
054: doConversion);
055: String lastName = getValue(request, "contLastName",
056: doConversion);
057: String title = getValue(request, "contTitle", doConversion);
058: String sex = getValue(request, "contSex", doConversion);
059: String emailPrimary = getValue(request, "contEmailPrimary",
060: doConversion);
061: String emailAlternative = getValue(request,
062: "contEmailAlternative", doConversion);
063: String gsmNoPrimary = getValue(request, "contGsmNoPrimary",
064: doConversion);
065: String gsmNoAlternative = getValue(request,
066: "contGsmNoAlternative", doConversion);
067: String webPage = getValue(request, "contWebPage",
068: doConversion);
069: String nickName = getValue(request, "contNickName",
070: doConversion);
071: String spouseName = getValue(request, "contSpouseName",
072: doConversion);
073: String personalNote = getValue(request, "contPersonalNote",
074: doConversion);
075: String homeAddress = getValue(request, "contHomeAddress",
076: doConversion);
077: String homeCity = getValue(request, "contHomeCity",
078: doConversion);
079: String homeProvince = getValue(request, "contHomeProvince",
080: doConversion);
081: String homeZip = getValue(request, "contHomeZip",
082: doConversion);
083: String homeCountry = getValue(request, "contHomeCountry",
084: doConversion);
085: String homePhone = getValue(request, "contHomePhone",
086: doConversion);
087: String homeFaks = getValue(request, "contHomeFaks",
088: doConversion);
089: String workCompany = getValue(request, "contWorkCompany",
090: doConversion);
091: String workJobTitle = getValue(request, "contWorkJobTitle",
092: doConversion);
093: String workOffice = getValue(request, "contWorkOffice",
094: doConversion);
095: String workDepartment = getValue(request,
096: "contWorkDepartment", doConversion);
097: String workProfession = getValue(request,
098: "contWorkProfession", doConversion);
099: String workManagerName = getValue(request,
100: "contWorkManagerName", doConversion);
101: String workAssistantName = getValue(request,
102: "contWorkAssistantName", doConversion);
103: String workAddress = getValue(request, "contWorkAddress",
104: doConversion);
105: String workCity = getValue(request, "contWorkCity",
106: doConversion);
107: String workProvince = getValue(request, "contWorkProvince",
108: doConversion);
109: String workZip = getValue(request, "contWorkZip",
110: doConversion);
111: String workCountry = getValue(request, "contWorkCountry",
112: doConversion);
113: String workPhone = getValue(request, "contWorkPhone",
114: doConversion);
115: String workFaks = getValue(request, "contWorkFaks",
116: doConversion);
117: String birthDay = getValue(request, "contBirthDay",
118: doConversion);
119: String birthMonth = getValue(request, "contBirthMonth",
120: doConversion);
121: String anniversaryDay = getValue(request,
122: "contAnniversaryDay", doConversion);
123: String anniversaryMonth = getValue(request,
124: "contAnniversaryMonth", doConversion);
125:
126: if (firstName == null || firstName.trim().equals("")) {
127: firstName = emailPrimary.substring(0, emailPrimary
128: .indexOf("@"));
129: }
130:
131: Contact contact = new Contact();
132:
133: if (id != null && id.trim().length() > 0) {
134: // it is an update
135: try {
136: contact.setId(new Long(Long.parseLong(id)));
137: } catch (NumberFormatException e) {
138: e.printStackTrace();
139: }
140: }
141:
142: contact.setBirthDay(birthDay);
143: contact.setBirthMonth(birthMonth);
144: contact.setAnniversaryDay(anniversaryDay);
145: contact.setAnniversaryMonth(anniversaryMonth);
146: contact.setEmailAlternate(emailAlternative);
147: contact.setEmailPrimary(emailPrimary);
148: contact.setFirstName(firstName);
149: contact.setGsmNoAlternate(gsmNoAlternative);
150: contact.setGsmNoPrimary(gsmNoPrimary);
151: contact.setHomeAddress(homeAddress);
152: contact.setHomeCity(homeCity);
153: contact.setHomeCountry(homeCountry);
154: contact.setHomeFaks(homeFaks);
155: contact.setHomePhone(homePhone);
156: contact.setHomeProvince(homeProvince);
157: contact.setHomeZip(homeZip);
158: contact.setLastName(lastName);
159: contact.setMiddleName(middleName);
160: contact.setNickName(nickName);
161: contact.setPersonalNote(personalNote);
162: contact.setSex(sex);
163: contact.setSpouseName(spouseName);
164: contact.setTitle(title);
165: contact.setUsername(getAuthProfile(request).getUsername());
166: contact.setWebPage(webPage);
167: contact.setWorkAddress(workAddress);
168: contact.setWorkAssistantName(workAssistantName);
169: contact.setWorkCity(workCity);
170: contact.setWorkCompany(workCompany);
171: contact.setWorkCountry(workCountry);
172: contact.setWorkDepartment(workDepartment);
173: contact.setWorkFaks(workFaks);
174: contact.setWorkJobTitle(workJobTitle);
175: contact.setWorkManagerName(workManagerName);
176: contact.setWorkOffice(workOffice);
177: contact.setWorkPhone(workPhone);
178: contact.setWorkProfession(workProfession);
179: contact.setWorkProvince(workProvince);
180: contact.setWorkZip(workZip);
181:
182: try {
183: ContactsController.saveContact(getAuthProfile(request),
184: contact);
185: out.print("ok");
186: } catch (Exception e) {
187: log.error("error while saving contact", e);
188: out.print("fail");
189: }
190: } catch (Exception e) {
191: log
192: .error(
193: "error while saving contact. (getting params)",
194: e);
195: out.print("fail");
196: }
197: }
198:
199: /**
200: *
201: * @param request
202: * @param param
203: * @param doConversion
204: * @return
205: * @throws Exception
206: */
207: private static String getValue(HttpServletRequest request,
208: String param, boolean doConversion) throws Exception {
209: if (doConversion) {
210: return new String(request.getParameter(param).getBytes(
211: charset), "utf-8");
212: } else {
213: return request.getParameter(param);
214: }
215: }
216: }
|