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