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