1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.PhoneNumberException;
23  import com.liferay.portal.PortalException;
24  import com.liferay.portal.SystemException;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.Phone;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.model.impl.ListTypeImpl;
29  import com.liferay.portal.service.base.PhoneLocalServiceBaseImpl;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.util.format.PhoneNumberUtil;
32  
33  import java.util.Date;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  /**
38   * <a href="PhoneLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class PhoneLocalServiceImpl extends PhoneLocalServiceBaseImpl {
44  
45      public Phone addPhone(
46              long userId, String className, long classPK, String number,
47              String extension, int typeId, boolean primary)
48          throws PortalException, SystemException {
49  
50          User user = userPersistence.findByPrimaryKey(userId);
51          long classNameId = PortalUtil.getClassNameId(className);
52          Date now = new Date();
53  
54          number = PhoneNumberUtil.strip(number);
55          extension = PhoneNumberUtil.strip(extension);
56  
57          validate(
58              0, user.getCompanyId(), classNameId, classPK, number, typeId,
59              primary);
60  
61          long phoneId = counterLocalService.increment();
62  
63          Phone phone = phonePersistence.create(phoneId);
64  
65          phone.setCompanyId(user.getCompanyId());
66          phone.setUserId(user.getUserId());
67          phone.setUserName(user.getFullName());
68          phone.setCreateDate(now);
69          phone.setModifiedDate(now);
70          phone.setClassNameId(classNameId);
71          phone.setClassPK(classPK);
72          phone.setNumber(number);
73          phone.setExtension(extension);
74          phone.setTypeId(typeId);
75          phone.setPrimary(primary);
76  
77          phonePersistence.update(phone, false);
78  
79          return phone;
80      }
81  
82      public void deletePhone(long phoneId)
83          throws PortalException, SystemException {
84  
85          phonePersistence.remove(phoneId);
86      }
87  
88      public void deletePhones(long companyId, String className, long classPK)
89          throws SystemException {
90  
91          long classNameId = PortalUtil.getClassNameId(className);
92  
93          phonePersistence.removeByC_C_C(companyId, classNameId, classPK);
94      }
95  
96      public Phone getPhone(long phoneId)
97          throws PortalException, SystemException {
98  
99          return phonePersistence.findByPrimaryKey(phoneId);
100     }
101 
102     public List<Phone> getPhones() throws SystemException {
103         return phonePersistence.findAll();
104     }
105 
106     public List<Phone> getPhones(long companyId, String className, long classPK)
107         throws SystemException {
108 
109         long classNameId = PortalUtil.getClassNameId(className);
110 
111         return phonePersistence.findByC_C_C(companyId, classNameId, classPK);
112     }
113 
114     public Phone updatePhone(
115             long phoneId, String number, String extension, int typeId,
116             boolean primary)
117         throws PortalException, SystemException {
118 
119         number = PhoneNumberUtil.strip(number);
120         extension = PhoneNumberUtil.strip(extension);
121 
122         validate(phoneId, 0, 0, 0, number, typeId, primary);
123 
124         Phone phone = phonePersistence.findByPrimaryKey(phoneId);
125 
126         phone.setModifiedDate(new Date());
127         phone.setNumber(number);
128         phone.setExtension(extension);
129         phone.setTypeId(typeId);
130         phone.setPrimary(primary);
131 
132         phonePersistence.update(phone, false);
133 
134         return phone;
135     }
136 
137     protected void validate(
138             long phoneId, long companyId, long classNameId, long classPK,
139             String number, int typeId, boolean primary)
140         throws PortalException, SystemException {
141 
142         if (Validator.isNull(number)) {
143             throw new PhoneNumberException();
144         }
145 
146         if (phoneId > 0) {
147             Phone phone = phonePersistence.findByPrimaryKey(phoneId);
148 
149             companyId = phone.getCompanyId();
150             classNameId = phone.getClassNameId();
151             classPK = phone.getClassPK();
152         }
153 
154         listTypeService.validate(typeId, classNameId, ListTypeImpl.PHONE);
155 
156         validate(phoneId, companyId, classNameId, classPK, primary);
157     }
158 
159     protected void validate(
160             long phoneId, long companyId, long classNameId, long classPK,
161             boolean primary)
162         throws SystemException {
163 
164         // Check to make sure there isn't another phone with the same company
165         // id, class name, and class pk that also has primary set to true
166 
167         if (primary) {
168             Iterator<Phone> itr = phonePersistence.findByC_C_C_P(
169                 companyId, classNameId, classPK, primary).iterator();
170 
171             while (itr.hasNext()) {
172                 Phone phone = itr.next();
173 
174                 if ((phoneId <= 0) ||
175                     (phone.getPhoneId() != phoneId)) {
176 
177                     phone.setPrimary(false);
178 
179                     phonePersistence.update(phone, false);
180                 }
181             }
182         }
183     }
184 
185 }