1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. 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.portal.service.impl;
24  
25  import com.liferay.portal.PhoneNumberException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Phone;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.model.impl.ListTypeImpl;
32  import com.liferay.portal.service.base.PhoneLocalServiceBaseImpl;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.util.format.PhoneNumberUtil;
35  
36  import java.rmi.RemoteException;
37  
38  import java.util.Date;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="PhoneLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class PhoneLocalServiceImpl extends PhoneLocalServiceBaseImpl {
49  
50      public Phone addPhone(
51              long userId, String className, long classPK, String number,
52              String extension, int typeId, boolean primary)
53          throws PortalException, SystemException {
54  
55          User user = userPersistence.findByPrimaryKey(userId);
56          long classNameId = PortalUtil.getClassNameId(className);
57          Date now = new Date();
58  
59          number = PhoneNumberUtil.strip(number);
60          extension = PhoneNumberUtil.strip(extension);
61  
62          validate(
63              0, user.getCompanyId(), classNameId, classPK, number, typeId,
64              primary);
65  
66          long phoneId = counterLocalService.increment();
67  
68          Phone phone = phonePersistence.create(phoneId);
69  
70          phone.setCompanyId(user.getCompanyId());
71          phone.setUserId(user.getUserId());
72          phone.setUserName(user.getFullName());
73          phone.setCreateDate(now);
74          phone.setModifiedDate(now);
75          phone.setClassNameId(classNameId);
76          phone.setClassPK(classPK);
77          phone.setNumber(number);
78          phone.setExtension(extension);
79          phone.setTypeId(typeId);
80          phone.setPrimary(primary);
81  
82          phonePersistence.update(phone, false);
83  
84          return phone;
85      }
86  
87      public void deletePhone(long phoneId)
88          throws PortalException, SystemException {
89  
90          phonePersistence.remove(phoneId);
91      }
92  
93      public void deletePhones(long companyId, String className, long classPK)
94          throws SystemException {
95  
96          long classNameId = PortalUtil.getClassNameId(className);
97  
98          phonePersistence.removeByC_C_C(companyId, classNameId, classPK);
99      }
100 
101     public Phone getPhone(long phoneId)
102         throws PortalException, SystemException {
103 
104         return phonePersistence.findByPrimaryKey(phoneId);
105     }
106 
107     public List<Phone> getPhones() throws SystemException {
108         return phonePersistence.findAll();
109     }
110 
111     public List<Phone> getPhones(long companyId, String className, long classPK)
112         throws SystemException {
113 
114         long classNameId = PortalUtil.getClassNameId(className);
115 
116         return phonePersistence.findByC_C_C(companyId, classNameId, classPK);
117     }
118 
119     public Phone updatePhone(
120             long phoneId, String number, String extension, int typeId,
121             boolean primary)
122         throws PortalException, SystemException {
123 
124         number = PhoneNumberUtil.strip(number);
125         extension = PhoneNumberUtil.strip(extension);
126 
127         validate(phoneId, 0, 0, 0, number, typeId, primary);
128 
129         Phone phone = phonePersistence.findByPrimaryKey(phoneId);
130 
131         phone.setModifiedDate(new Date());
132         phone.setNumber(number);
133         phone.setExtension(extension);
134         phone.setTypeId(typeId);
135         phone.setPrimary(primary);
136 
137         phonePersistence.update(phone, false);
138 
139         return phone;
140     }
141 
142     protected void validate(
143             long phoneId, long companyId, long classNameId, long classPK,
144             String number, int typeId, boolean primary)
145         throws PortalException, SystemException {
146 
147         if (Validator.isNull(number)) {
148             throw new PhoneNumberException();
149         }
150 
151         if (phoneId > 0) {
152             Phone phone = phonePersistence.findByPrimaryKey(phoneId);
153 
154             companyId = phone.getCompanyId();
155             classNameId = phone.getClassNameId();
156             classPK = phone.getClassPK();
157         }
158 
159         try {
160             listTypeService.validate(typeId, classNameId, ListTypeImpl.PHONE);
161         }
162         catch (RemoteException re) {
163             throw new SystemException(re);
164         }
165 
166         validate(phoneId, companyId, classNameId, classPK, primary);
167     }
168 
169     protected void validate(
170             long phoneId, long companyId, long classNameId, long classPK,
171             boolean primary)
172         throws PortalException, SystemException {
173 
174         // Check to make sure there isn't another phone with the same company
175         // id, class name, and class pk that also has primary set to true
176 
177         if (primary) {
178             Iterator<Phone> itr = phonePersistence.findByC_C_C_P(
179                 companyId, classNameId, classPK, primary).iterator();
180 
181             while (itr.hasNext()) {
182                 Phone phone = itr.next();
183 
184                 if ((phoneId <= 0) ||
185                     (phone.getPhoneId() != phoneId)) {
186 
187                     phone.setPrimary(false);
188 
189                     phonePersistence.update(phone, false);
190                 }
191             }
192         }
193     }
194 
195 }