1   /**
2    * Copyright (c) 2000-2004 Liferay, LLC. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.addressbook.ejb;
24  
25  import com.liferay.counter.ejb.CounterManagerUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.auth.PrincipalException;
29  import com.liferay.portal.ejb.PrincipalBean;
30  import com.liferay.portal.util.RecipientComparator;
31  import com.liferay.portlet.addressbook.ContactEmailAddressException;
32  import com.liferay.portlet.addressbook.ContactFirstNameException;
33  import com.liferay.portlet.addressbook.ContactLastNameException;
34  import com.liferay.portlet.addressbook.DuplicateContactException;
35  import com.liferay.portlet.addressbook.model.ABContact;
36  import com.liferay.portlet.addressbook.util.Importer;
37  import com.liferay.util.StringPool;
38  import com.liferay.util.Validator;
39  
40  import java.util.Collections;
41  import java.util.Date;
42  import java.util.List;
43  
44  /**
45   * <a href="ABContactManagerImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author  Brian Wing Shun Chan
48   * @version $Revision: 1.1 $
49   *
50   */
51  public class ABContactManagerImpl
52      extends PrincipalBean implements ABContactManager {
53  
54      // Business methods
55  
56      public ABContact addContact(
57              String firstName, String lastName, String emailAddress)
58          throws PortalException, SystemException {
59  
60          return addContact(
61              firstName, StringPool.BLANK, lastName, StringPool.BLANK,
62              emailAddress, StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
63              StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
64              StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
65              StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
66              StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
67              StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
68              StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
69              StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
70              StringPool.BLANK, StringPool.BLANK, StringPool.BLANK, null,
71              StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
72              StringPool.BLANK);
73      }
74  
75      public ABContact addContact(
76              String firstName, String middleName, String lastName,
77              String nickName, String emailAddress, String homeStreet,
78              String homeCity, String homeState, String homeZip,
79              String homeCountry, String homePhone, String homeFax,
80              String homeCell, String homePager, String homeTollFree,
81              String homeEmailAddress, String businessCompany,
82              String businessStreet, String businessCity, String businessState,
83              String businessZip, String businessCountry, String businessPhone,
84              String businessFax, String businessCell, String businessPager,
85              String businessTollFree, String businessEmailAddress,
86              String employeeNumber, String jobTitle, String jobClass,
87              String hoursOfOperation, Date birthday, String timeZoneId,
88              String instantMessenger, String website, String comments)
89          throws PortalException, SystemException {
90  
91          emailAddress = emailAddress.trim();
92  
93          _validate(firstName, lastName, emailAddress, true);
94  
95          String contactId = Long.toString(CounterManagerUtil.increment(
96              ABContact.class.getName()));
97  
98          ABContact contact = ABContactUtil.create(contactId);
99  
100         contact.setUserId(getUserId());
101         contact.setFirstName(firstName);
102         contact.setMiddleName(middleName);
103         contact.setLastName(lastName);
104         contact.setNickName(nickName);
105         contact.setEmailAddress(emailAddress);
106         contact.setHomeStreet(homeStreet);
107         contact.setHomeCity(homeCity);
108         contact.setHomeState(homeState);
109         contact.setHomeZip(homeZip);
110         contact.setHomeCountry(homeCountry);
111         contact.setHomePhone(homePhone);
112         contact.setHomeFax(homeFax);
113         contact.setHomeCell(homeCell);
114         contact.setHomePager(homePager);
115         contact.setHomeTollFree(homeTollFree);
116         contact.setHomeEmailAddress(homeEmailAddress);
117         contact.setBusinessCompany(businessCompany);
118         contact.setBusinessStreet(businessStreet);
119         contact.setBusinessCity(businessCity);
120         contact.setBusinessState(businessState);
121         contact.setBusinessZip(businessZip);
122         contact.setBusinessCountry(businessCountry);
123         contact.setBusinessPhone(businessPhone);
124         contact.setBusinessFax(businessFax);
125         contact.setBusinessCell(businessCell);
126         contact.setBusinessPager(businessPager);
127         contact.setBusinessTollFree(businessTollFree);
128         contact.setBusinessEmailAddress(businessEmailAddress);
129         contact.setEmployeeNumber(employeeNumber);
130         contact.setJobTitle(jobTitle);
131         contact.setJobClass(jobClass);
132         contact.setHoursOfOperation(hoursOfOperation);
133         contact.setBirthday(birthday);
134         contact.setTimeZoneId(timeZoneId);
135         contact.setInstantMessenger(instantMessenger);
136         contact.setWebsite(website);
137         contact.setComments(comments);
138 
139         ABContactUtil.update(contact);
140 
141         return contact;
142     }
143 
144     public void addContacts(Importer importer)
145         throws PortalException, SystemException {
146 
147         ABContact[] contacts = importer.getContacts();
148 
149         if (contacts == null || contacts.length == 0) {
150             return;
151         }
152 
153         for (int i = 0; i < contacts.length; i++) {
154             _validate(
155                 contacts[i].getFirstName(), contacts[i].getLastName(),
156                 contacts[i].getEmailAddress(), true);
157         }
158 
159         for (int i = 0; i < contacts.length; i++) {
160             addContact(
161                 contacts[i].getFirstName(), contacts[i].getMiddleName(),
162                 contacts[i].getLastName(), contacts[i].getNickName(),
163                 contacts[i].getEmailAddress(), contacts[i].getHomeStreet(),
164                 contacts[i].getHomeCity(), contacts[i].getHomeState(),
165                 contacts[i].getHomeZip(), contacts[i].getHomeCountry(),
166                 contacts[i].getHomePhone(), contacts[i].getHomeFax(),
167                 contacts[i].getHomeCell(), contacts[i].getHomePager(),
168                 contacts[i].getHomeTollFree(),
169                 contacts[i].getHomeEmailAddress(),
170                 contacts[i].getBusinessCompany(),
171                 contacts[i].getBusinessStreet(), contacts[i].getBusinessCity(),
172                 contacts[i].getBusinessState(), contacts[i].getBusinessZip(),
173                 contacts[i].getBusinessCountry(),
174                 contacts[i].getBusinessPhone(), contacts[i].getBusinessFax(),
175                 contacts[i].getBusinessCell(), contacts[i].getBusinessPager(),
176                 contacts[i].getBusinessTollFree(),
177                 contacts[i].getBusinessEmailAddress(),
178                 contacts[i].getEmployeeNumber(), contacts[i].getJobTitle(),
179                 contacts[i].getJobClass(), contacts[i].getHoursOfOperation(),
180                 contacts[i].getBirthday(), contacts[i].getTimeZoneId(),
181                 contacts[i].getInstantMessenger(), contacts[i].getWebsite(),
182                 contacts[i].getComments());
183         }
184     }
185 
186     public void deleteContact(String contactId)
187         throws PortalException, SystemException {
188 
189         ABContact contact = ABContactUtil.findByPrimaryKey(contactId);
190 
191         if (!contact.getUserId().equals(getUserId())) {
192             throw new PrincipalException();
193         }
194 
195         ABContactLocalManagerUtil.deleteContact(contactId);
196     }
197 
198     public ABContact getContact(String contactId)
199         throws PortalException, SystemException {
200 
201         ABContact contact = ABContactUtil.findByPrimaryKey(contactId);
202 
203         if (!contact.getUserId().equals(getUserId())) {
204             throw new PrincipalException();
205         }
206 
207         return contact;
208     }
209 
210     public List getContacts() throws PortalException, SystemException {
211         return ABContactUtil.findByUserId(getUserId());
212     }
213 
214     public int getContactsSize() throws PortalException, SystemException {
215         return ABContactUtil.countByUserId(getUserId());
216     }
217 
218     public List getLists(String contactId)
219         throws PortalException, SystemException {
220 
221         ABContact contact = ABContactUtil.findByPrimaryKey(contactId);
222 
223         if (!contact.getUserId().equals(getUserId())) {
224             throw new PrincipalException();
225         }
226 
227         List lists = ABContactUtil.getABLists(contactId);
228 
229         Collections.sort(lists, new RecipientComparator());
230 
231         return lists;
232     }
233 
234     public void setLists(String contactId, List lists)
235         throws PortalException, SystemException {
236 
237         ABContact contact = ABContactUtil.findByPrimaryKey(contactId);
238 
239         if (!contact.getUserId().equals(getUserId())) {
240             throw new PrincipalException();
241         }
242 
243         ABContactUtil.setABLists(contactId, lists);
244     }
245 
246     public ABContact updateContact(
247             String contactId, String firstName, String middleName,
248             String lastName, String nickName, String emailAddress,
249             String homeStreet, String homeCity, String homeState,
250             String homeZip, String homeCountry, String homePhone,
251             String homeFax, String homeCell, String homePager,
252             String homeTollFree, String homeEmailAddress,
253             String businessCompany, String businessStreet, String businessCity,
254             String businessState, String businessZip, String businessCountry,
255             String businessPhone, String businessFax, String businessCell,
256             String businessPager, String businessTollFree,
257             String businessEmailAddress, String employeeNumber, String jobTitle,
258             String jobClass, String hoursOfOperation, Date birthday,
259             String timeZoneId, String instantMessenger, String website,
260             String comments)
261         throws PortalException, SystemException {
262 
263         emailAddress = emailAddress.trim();
264 
265         _validate(firstName, lastName, emailAddress, false);
266 
267         ABContact contact = ABContactUtil.findByPrimaryKey(contactId);
268 
269         if (!contact.getUserId().equals(getUserId())) {
270             throw new PrincipalException();
271         }
272 
273         contact.setFirstName(firstName);
274         contact.setMiddleName(middleName);
275         contact.setLastName(lastName);
276         contact.setNickName(nickName);
277         contact.setEmailAddress(emailAddress);
278         contact.setHomeStreet(homeStreet);
279         contact.setHomeCity(homeCity);
280         contact.setHomeState(homeState);
281         contact.setHomeZip(homeZip);
282         contact.setHomeCountry(homeCountry);
283         contact.setHomePhone(homePhone);
284         contact.setHomeFax(homeFax);
285         contact.setHomeCell(homeCell);
286         contact.setHomePager(homePager);
287         contact.setHomeTollFree(homeTollFree);
288         contact.setHomeEmailAddress(homeEmailAddress);
289         contact.setBusinessCompany(businessCompany);
290         contact.setBusinessStreet(businessStreet);
291         contact.setBusinessCity(businessCity);
292         contact.setBusinessState(businessState);
293         contact.setBusinessZip(businessZip);
294         contact.setBusinessCountry(businessCountry);
295         contact.setBusinessPhone(businessPhone);
296         contact.setBusinessFax(businessFax);
297         contact.setBusinessCell(businessCell);
298         contact.setBusinessPager(businessPager);
299         contact.setBusinessTollFree(businessTollFree);
300         contact.setBusinessEmailAddress(businessEmailAddress);
301         contact.setEmployeeNumber(employeeNumber);
302         contact.setJobTitle(jobTitle);
303         contact.setJobClass(jobClass);
304         contact.setHoursOfOperation(hoursOfOperation);
305         contact.setBirthday(birthday);
306         contact.setTimeZoneId(timeZoneId);
307         contact.setInstantMessenger(instantMessenger);
308         contact.setWebsite(website);
309         contact.setComments(comments);
310 
311         ABContactUtil.update(contact);
312 
313         return contact;
314     }
315 
316     // Private methods
317 
318     private void _validate(
319             String firstName, String lastName, String emailAddress,
320             boolean addContact)
321         throws PortalException, SystemException {
322 
323         if (Validator.isNull(firstName)) {
324             throw new ContactFirstNameException();
325         }
326         else if (Validator.isNull(lastName)) {
327             throw new ContactLastNameException();
328         }
329         else if (!Validator.isEmailAddress(emailAddress)) {
330             throw new ContactEmailAddressException();
331         }
332 
333         if (addContact) {
334             List contacts = getContacts();
335             ABContact contact =
336                 new ABContact(firstName, lastName, emailAddress);
337 
338             int pos = Collections.binarySearch(contacts, contact);
339             if (pos >= 0) {
340                 throw new DuplicateContactException();
341             }
342         }
343     }
344 
345 }