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