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