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.util.Date;
34 import java.util.Iterator;
35 import java.util.List;
36
37
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
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 }