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