1
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.Phone;
30 import com.liferay.portal.model.User;
31 import com.liferay.portal.model.impl.ListTypeImpl;
32 import com.liferay.portal.service.base.PhoneLocalServiceBaseImpl;
33 import com.liferay.portal.util.PortalUtil;
34 import com.liferay.util.format.PhoneNumberUtil;
35
36 import java.util.Date;
37 import java.util.Iterator;
38 import java.util.List;
39
40
46 public class PhoneLocalServiceImpl extends PhoneLocalServiceBaseImpl {
47
48 public Phone addPhone(
49 long userId, String className, long classPK, String number,
50 String extension, int typeId, boolean primary)
51 throws PortalException, SystemException {
52
53 User user = userPersistence.findByPrimaryKey(userId);
54 long classNameId = PortalUtil.getClassNameId(className);
55 Date now = new Date();
56
57 number = PhoneNumberUtil.strip(number);
58 extension = PhoneNumberUtil.strip(extension);
59
60 validate(
61 0, user.getCompanyId(), classNameId, classPK, number, typeId,
62 primary);
63
64 long phoneId = counterLocalService.increment();
65
66 Phone phone = phonePersistence.create(phoneId);
67
68 phone.setCompanyId(user.getCompanyId());
69 phone.setUserId(user.getUserId());
70 phone.setUserName(user.getFullName());
71 phone.setCreateDate(now);
72 phone.setModifiedDate(now);
73 phone.setClassNameId(classNameId);
74 phone.setClassPK(classPK);
75 phone.setNumber(number);
76 phone.setExtension(extension);
77 phone.setTypeId(typeId);
78 phone.setPrimary(primary);
79
80 phonePersistence.update(phone, false);
81
82 return phone;
83 }
84
85 public void deletePhone(long phoneId)
86 throws PortalException, SystemException {
87
88 phonePersistence.remove(phoneId);
89 }
90
91 public void deletePhones(long companyId, String className, long classPK)
92 throws SystemException {
93
94 long classNameId = PortalUtil.getClassNameId(className);
95
96 phonePersistence.removeByC_C_C(companyId, classNameId, classPK);
97 }
98
99 public Phone getPhone(long phoneId)
100 throws PortalException, SystemException {
101
102 return phonePersistence.findByPrimaryKey(phoneId);
103 }
104
105 public List<Phone> getPhones() throws SystemException {
106 return phonePersistence.findAll();
107 }
108
109 public List<Phone> getPhones(long companyId, String className, long classPK)
110 throws SystemException {
111
112 long classNameId = PortalUtil.getClassNameId(className);
113
114 return phonePersistence.findByC_C_C(companyId, classNameId, classPK);
115 }
116
117 public Phone updatePhone(
118 long phoneId, String number, String extension, int typeId,
119 boolean primary)
120 throws PortalException, SystemException {
121
122 number = PhoneNumberUtil.strip(number);
123 extension = PhoneNumberUtil.strip(extension);
124
125 validate(phoneId, 0, 0, 0, number, typeId, primary);
126
127 Phone phone = phonePersistence.findByPrimaryKey(phoneId);
128
129 phone.setModifiedDate(new Date());
130 phone.setNumber(number);
131 phone.setExtension(extension);
132 phone.setTypeId(typeId);
133 phone.setPrimary(primary);
134
135 phonePersistence.update(phone, false);
136
137 return phone;
138 }
139
140 protected void validate(
141 long phoneId, long companyId, long classNameId, long classPK,
142 String number, int typeId, boolean primary)
143 throws PortalException, SystemException {
144
145 if (Validator.isNull(number)) {
146 throw new PhoneNumberException();
147 }
148
149 if (phoneId > 0) {
150 Phone phone = phonePersistence.findByPrimaryKey(phoneId);
151
152 companyId = phone.getCompanyId();
153 classNameId = phone.getClassNameId();
154 classPK = phone.getClassPK();
155 }
156
157 listTypeService.validate(typeId, classNameId, ListTypeImpl.PHONE);
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 }