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