001: package org.claros.intouch.contacts.services;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.IOException;
006: import java.io.InputStreamReader;
007: import java.util.ArrayList;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.List;
011: import java.util.Locale;
012: import java.util.StringTokenizer;
013:
014: import javax.servlet.ServletException;
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017:
018: import org.apache.commons.fileupload.FileItem;
019: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
020: import org.apache.commons.fileupload.servlet.ServletFileUpload;
021: import org.claros.commons.auth.models.AuthProfile;
022: import org.claros.intouch.common.exceptions.UnableToImportException;
023: import org.claros.intouch.common.services.BaseService;
024: import org.claros.intouch.common.utility.Constants;
025: import org.claros.intouch.contacts.controllers.ImportExportController;
026:
027: public class ImportContactsService extends BaseService {
028: private static int MAX_MEM_SIZE = 1024 * 1024;
029: private static int MAX_ATT_SIZE = 1024 * 1024 * Constants.maxAttSize;
030: private static HashMap fields = new HashMap();
031:
032: private static String tmpDir = Constants.tmpDir;
033:
034: /**
035: *
036: */
037: private static final long serialVersionUID = 56265867864257045L;
038:
039: /**
040: * The doPost method of the servlet. <br>
041: *
042: * This method is called when a form has its tag value method equals to post.
043: *
044: * @param request the request send by the client to the server
045: * @param response the response send by the server to the client
046: * @throws ServletException if an error occurred
047: * @throws IOException if an error occurred
048: */
049: public void doPost(HttpServletRequest request,
050: HttpServletResponse response) throws ServletException,
051: IOException {
052: response.setHeader("Expires", "-1");
053: response.setHeader("Pragma", "no-cache");
054: response.setHeader("Cache-control", "no-cache");
055: response.setHeader("Content-Type", "text/html; charset=utf-8");
056:
057: try {
058: // Create a factory for disk-based file items
059: DiskFileItemFactory factory = new DiskFileItemFactory();
060:
061: // Set factory constraints
062: factory.setSizeThreshold(MAX_MEM_SIZE);
063: factory.setRepository(new File(tmpDir));
064:
065: // Create a new file upload handler
066: ServletFileUpload upload = new ServletFileUpload(factory);
067:
068: // Set overall request size constraint
069: upload.setSizeMax(MAX_ATT_SIZE);
070:
071: // Parse the request
072: List items = upload.parseRequest(request);
073: // Process the uploaded items
074: Iterator iter = items.iterator();
075:
076: // we can parse the attachments now
077: iter = items.iterator();
078:
079: ArrayList columns = new ArrayList();
080: ArrayList data = new ArrayList();
081: int i = 0;
082:
083: while (iter.hasNext()) {
084: FileItem item = (FileItem) iter.next();
085:
086: if (!item.isFormField()) {
087: BufferedReader br = new BufferedReader(
088: new InputStreamReader(item.getInputStream()));
089: String line = "";
090: String tmp = null;
091: try {
092: String charset = Constants.charset;
093: while ((line = br.readLine()) != null) {
094: line = new String(line.getBytes(charset),
095: "utf-8");
096: try {
097: line = org.claros.commons.utility.Utility
098: .replaceAllOccurances(line,
099: ";;", "; ;");
100: line = org.claros.commons.utility.Utility
101: .replaceAllOccurances(line,
102: ";;", "; ;");
103: StringTokenizer token = new StringTokenizer(
104: line, ";");
105: if (i == 0) {
106: // the first line tells about what the fields stand for.
107: String fieldName = null;
108: while (token.hasMoreTokens()) {
109: tmp = token.nextToken()
110: .toUpperCase(
111: new Locale(
112: "en",
113: "US"));
114: fieldName = (String) fields
115: .get(tmp);
116: if (fieldName == null) {
117: throw new UnableToImportException(
118: "Field "
119: + tmp
120: + " is an invalid column name");
121: }
122: columns.add(fieldName);
123: }
124: } else {
125: // these are the data to be processed.
126: ArrayList row = new ArrayList();
127: while (token.hasMoreTokens()) {
128: tmp = token.nextToken();
129: row.add(tmp);
130: }
131: data.add(row);
132: }
133: } catch (UnableToImportException ue) {
134: throw ue;
135: } catch (Exception e) {
136: // do nothing sier
137: }
138: i++;
139: }
140: } catch (Exception e) {
141: response
142: .sendRedirect("../import_ok.jsp?result=1");
143: }
144: }
145: }
146:
147: AuthProfile auth = getAuthProfile(request);
148: int success = ImportExportController.importContacts(auth,
149: columns, data);
150: int fail = i - success - 1;
151:
152: response.sendRedirect("../import_ok.jsp?result=0&success="
153: + success + "&fail=" + fail);
154: } catch (Exception e) {
155: response.sendRedirect("../import_ok.jsp?result=1");
156: }
157: }
158:
159: static {
160: fields.put("FIRST NAME", "FIRST_NAME");
161: fields.put("MIDDLE NAME", "MIDDLE_NAME");
162: fields.put("LAST NAME", "LAST_NAME");
163: fields.put("FIRSTNAME", "FIRST_NAME");
164: fields.put("MIDDLENAME", "MIDDLE_NAME");
165: fields.put("LASTNAME", "LAST_NAME");
166: fields.put("TITLE", "TITLE");
167: fields.put("SEX", "SEX");
168: fields.put("GSM PRIMARY", "GSM_NO_PRIMARY");
169: fields.put("GSM ALTERNATE", "GSM_NO_ALTERNATE");
170: fields.put("PRIMARY GSM", "GSM_NO_PRIMARY");
171: fields.put("ALTERNATE GSM", "GSM_NO_ALTERNATE");
172: fields.put("EMAIL PRIMARY", "EMAIL_PRIMARY");
173: fields.put("EMAIL ALTERNATE", "EMAIL_ALTERNATE");
174: fields.put("PRIMARY EMAIL", "EMAIL_PRIMARY");
175: fields.put("ALTERNATE EMAIL", "EMAIL_ALTERNATE");
176: fields.put("WEB PAGE", "WEB_PAGE");
177: fields.put("URL", "WEB_PAGE");
178: fields.put("WEBSITE", "WEB_PAGE");
179: fields.put("WEBPAGE", "WEB_PAGE");
180: fields.put("PERSONAL NOTE", "PERSONAL_NOTE");
181: fields.put("NOTE", "PERSONAL_NOTE");
182: fields.put("SPOUSE NAME", "SPOUSE_NAME");
183: fields.put("NICKNAME", "NICKNAME");
184: fields.put("NICK NAME", "NICKNAME");
185: fields.put("BIRTHDAY", "BIRTHDAY");
186: fields.put("BIRTHMONTH", "BIRTHMONTH");
187: fields.put("ANNIVERSARYDAY", "ANNIVERSARYDAY");
188: fields.put("ANNIVERSARYMONTH", "ANNIVERSARYMONTH");
189: fields.put("HOME ADDRESS", "HOME_ADDRESS");
190: fields.put("HOME CITY", "HOME_CITY");
191: fields.put("HOME PROVINCE", "HOME_PROVINCE");
192: fields.put("HOME ZIP", "HOME_ZIP");
193: fields.put("HOME COUNTRY", "HOME_COUNTRY");
194: fields.put("HOME PHONE", "HOME_PHONE");
195: fields.put("HOME FAX", "HOME_FAKS");
196: fields.put("COMPANY", "WORK_COMPANY");
197: fields.put("JOB TITLE", "WORK_JOB_TITLE");
198: fields.put("DEPARTMENT", "WORK_DEPARTMENT");
199: fields.put("OFFICE", "WORK_OFFICE");
200: fields.put("PROFESSION", "WORK_PROFESSION");
201: fields.put("MANAGER NAME", "WORK_MANAGER_NAME");
202: fields.put("ASSISTANT NAME", "WORK_ASSISTANT_NAME");
203: fields.put("WORK ADDRESS", "WORK_ADDRESS");
204: fields.put("WORK CITY", "WORK_CITY");
205: fields.put("WORK PROVINCE", "WORK_PROVINCE");
206: fields.put("WORK ZIP", "WORK_ZIP");
207: fields.put("WORK COUNTRY", "WORK_COUNTRY");
208: fields.put("WORK PHONE", "WORK_PHONE");
209: fields.put("WORK FAX", "WORK_FAKS");
210: }
211:
212: }
|