001: package org.claros.intouch.contacts.controllers;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.claros.commons.auth.models.AuthProfile;
007: import org.claros.commons.utility.Utility;
008: import org.claros.intouch.contacts.models.Contact;
009:
010: /**
011: * @author Umut Gokbayrak
012: */
013: public class ImportExportController {
014:
015: public static int importContacts(AuthProfile auth,
016: ArrayList columns, ArrayList rows) {
017: Contact tmp = null;
018: String col = null;
019: ArrayList row = null;
020: String val = null;
021: int success = 0;
022: for (int i = 0; i < rows.size(); i++) {
023: try {
024: tmp = new Contact();
025: row = (ArrayList) rows.get(i);
026: for (int j = 0; j < columns.size(); j++) {
027: col = (String) columns.get(j);
028: try {
029: val = (String) row.get(j);
030: } catch (Exception m) {
031: val = " ";
032: }
033:
034: if (col.equals("FIRST_NAME")) {
035: tmp.setFirstName(val);
036: } else if (col.equals("MIDDLE_NAME")) {
037: tmp.setMiddleName(val);
038: } else if (col.equals("LAST_NAME")) {
039: tmp.setLastName(val);
040: } else if (col.equals("TITLE")) {
041: tmp.setTitle(val);
042: } else if (col.equals("SEX")) {
043: tmp.setSex(val);
044: } else if (col.equals("GSM_NO_PRIMARY")) {
045: tmp.setGsmNoPrimary(val);
046: } else if (col.equals("GSM_NO_ALTERNATE")) {
047: tmp.setGsmNoAlternate(val);
048: } else if (col.equals("EMAIL_PRIMARY")) {
049: tmp.setEmailPrimary(val);
050: } else if (col.equals("EMAIL_ALTERNATE")) {
051: tmp.setEmailAlternate(val);
052: } else if (col.equals("WEB_PAGE")) {
053: tmp.setWebPage(val);
054: } else if (col.equals("PERSONAL_NOTE")) {
055: tmp.setPersonalNote(val);
056: } else if (col.equals("SPOUSE_NAME")) {
057: tmp.setSpouseName(val);
058: } else if (col.equals("NICKNAME")) {
059: tmp.setNickName(val);
060: } else if (col.equals("BIRTHDAY")) {
061: tmp.setBirthDay(val);
062: } else if (col.equals("BIRTHMONTH")) {
063: tmp.setBirthMonth(val);
064: } else if (col.equals("ANNIVERSARYDAY")) {
065: tmp.setAnniversaryDay(val);
066: } else if (col.equals("ANNIVERSARYMONTH")) {
067: tmp.setAnniversaryMonth(val);
068: } else if (col.equals("HOME_ADDRESS")) {
069: tmp.setHomeAddress(val);
070: } else if (col.equals("HOME_CITY")) {
071: tmp.setHomeCity(val);
072: } else if (col.equals("HOME_PROVINCE")) {
073: tmp.setHomeProvince(val);
074: } else if (col.equals("HOME_ZIP")) {
075: tmp.setHomeZip(val);
076: } else if (col.equals("HOME_COUNTRY")) {
077: tmp.setHomeCountry(val);
078: } else if (col.equals("HOME_PHONE")) {
079: tmp.setHomePhone(val);
080: } else if (col.equals("HOME_FAKS")) {
081: tmp.setHomeFaks(val);
082: } else if (col.equals("WORK_COMPANY")) {
083: tmp.setWorkCompany(val);
084: } else if (col.equals("WORK_JOB_TITLE")) {
085: tmp.setWorkJobTitle(val);
086: } else if (col.equals("WORK_DEPARTMENT")) {
087: tmp.setWorkDepartment(val);
088: } else if (col.equals("WORK_OFFICE")) {
089: tmp.setWorkOffice(val);
090: } else if (col.equals("WORK_PROFESSION")) {
091: tmp.setWorkProfession(val);
092: } else if (col.equals("WORK_MANAGER_NAME")) {
093: tmp.setWorkManagerName(val);
094: } else if (col.equals("WORK_ASSISTANT_NAME")) {
095: tmp.setWorkAssistantName(val);
096: } else if (col.equals("WORK_ADDRESS")) {
097: tmp.setWorkAddress(val);
098: } else if (col.equals("WORK_CITY")) {
099: tmp.setWorkCity(val);
100: } else if (col.equals("WORK_PROVINCE")) {
101: tmp.setWorkProvince(val);
102: } else if (col.equals("WORK_ZIP")) {
103: tmp.setWorkZip(val);
104: } else if (col.equals("WORK_COUNTRY")) {
105: tmp.setWorkCountry(val);
106: } else if (col.equals("WORK_PHONE")) {
107: tmp.setWorkPhone(val);
108: } else if (col.equals("WORK_FAKS")) {
109: tmp.setWorkFaks(val);
110: }
111: }
112: tmp.setUsername(auth.getUsername());
113: ContactsController.saveContact(auth, tmp);
114: success++;
115: } catch (Exception e) {
116: // do nothing sier
117: }
118: }
119: return success;
120: }
121:
122: public static ArrayList exportContacts(AuthProfile auth) {
123: ArrayList out = new ArrayList();
124:
125: out
126: .add("FIRST NAME;MIDDLE NAME;LAST NAME;TITLE;SEX;GSM PRIMARY;GSM ALTERNATE;"
127: + "EMAIL PRIMARY;EMAIL ALTERNATE;WEB PAGE;PERSONAL NOTE;SPOUSE NAME;NICKNAME;"
128: + "BIRTHDAY;BIRTHMONTH;ANNIVERSARYDAY;ANNIVERSARYMONTH;HOME ADDRESS;HOME CITY;HOME PROVINCE;HOME ZIP;HOME COUNTRY;"
129: + "HOME PHONE;HOME FAX;COMPANY;JOB TITLE;DEPARTMENT;OFFICE;PROFESSION;MANAGER NAME;"
130: + "ASSISTANT NAME;WORK ADDRESS;WORK CITY;WORK PROVINCE;WORK ZIP;WORK COUNTRY;WORK PHONE;WORK FAX");
131: try {
132: List contacts = ContactsController.getContactsByNamePrefix(
133: auth, "ALL");
134: if (contacts != null) {
135: Contact tmp = null;
136: for (int i = 0; i < contacts.size(); i++) {
137: String line = "";
138: tmp = (Contact) contacts.get(i);
139: line += tmp.getFirstName() + ";";
140: line += tmp.getMiddleName() + ";";
141: line += tmp.getLastName() + ";";
142: line += tmp.getTitle() + ";";
143: line += tmp.getSex() + ";";
144: line += tmp.getGsmNoPrimary() + ";";
145: line += tmp.getGsmNoAlternate() + ";";
146: line += tmp.getEmailPrimary() + ";";
147: line += tmp.getEmailAlternate() + ";";
148: line += tmp.getWebPage() + ";";
149: line += Utility.replaceAllOccurances(tmp
150: .getPersonalNote(), "\n", " ")
151: + ";";
152: line += tmp.getSpouseName() + ";";
153: line += tmp.getNickName() + ";";
154: line += getInfo(tmp.getBirthDay()) + ";";
155: line += getInfo(tmp.getBirthMonth()) + ";";
156: line += getInfo(tmp.getAnniversaryDay()) + ";";
157: line += getInfo(tmp.getAnniversaryMonth()) + ";";
158: line += Utility.replaceAllOccurances(tmp
159: .getHomeAddress(), "\n", " ")
160: + ";";
161: line += tmp.getHomeCity() + ";";
162: line += tmp.getHomeProvince() + ";";
163: line += tmp.getHomeZip() + ";";
164: line += tmp.getHomeCountry() + ";";
165: line += tmp.getHomePhone() + ";";
166: line += tmp.getHomeFaks() + ";";
167: line += tmp.getWorkCompany() + ";";
168: line += tmp.getWorkJobTitle() + ";";
169: line += tmp.getWorkDepartment() + ";";
170: line += tmp.getWorkOffice() + ";";
171: line += tmp.getWorkProfession() + ";";
172: line += tmp.getWorkManagerName() + ";";
173: line += tmp.getWorkAssistantName() + ";";
174: line += Utility.replaceAllOccurances(tmp
175: .getWorkAddress(), "\n", " ")
176: + ";";
177: line += tmp.getWorkCity() + ";";
178: line += tmp.getWorkProvince() + ";";
179: line += tmp.getWorkZip() + ";";
180: line += tmp.getWorkCountry() + ";";
181: line += tmp.getWorkPhone() + ";";
182: line += tmp.getWorkFaks() + ";";
183: out.add(line);
184: }
185: }
186: } catch (Exception e) {
187: out.add("An Error Occured.");
188: }
189: return out;
190: }
191:
192: /**
193: *
194: * @param str
195: * @return
196: */
197: private static String getInfo(String str) {
198: if (str == null)
199: return " ";
200: if (str.equals(""))
201: return " ";
202: return str;
203: }
204: }
|