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