001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.PhoneNumberException;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.format.PhoneNumberFormatUtil;
020 import com.liferay.portal.kernel.systemevent.SystemEvent;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.model.Account;
023 import com.liferay.portal.model.Contact;
024 import com.liferay.portal.model.ListTypeConstants;
025 import com.liferay.portal.model.Organization;
026 import com.liferay.portal.model.Phone;
027 import com.liferay.portal.model.SystemEventConstants;
028 import com.liferay.portal.model.User;
029 import com.liferay.portal.service.ServiceContext;
030 import com.liferay.portal.service.base.PhoneLocalServiceBaseImpl;
031
032 import java.util.Date;
033 import java.util.List;
034
035
038 public class PhoneLocalServiceImpl extends PhoneLocalServiceBaseImpl {
039
040
044 @Deprecated
045 @Override
046 public Phone addPhone(
047 long userId, String className, long classPK, String number,
048 String extension, int typeId, boolean primary)
049 throws PortalException {
050
051 return addPhone(
052 userId, className, classPK, number, extension, typeId, primary,
053 new ServiceContext());
054 }
055
056 @Override
057 public Phone addPhone(
058 long userId, String className, long classPK, String number,
059 String extension, int typeId, boolean primary,
060 ServiceContext serviceContext)
061 throws PortalException {
062
063 User user = userPersistence.findByPrimaryKey(userId);
064 long classNameId = classNameLocalService.getClassNameId(className);
065 Date now = new Date();
066
067 validate(
068 0, user.getCompanyId(), classNameId, classPK, number, extension,
069 typeId, primary);
070
071 long phoneId = counterLocalService.increment();
072
073 Phone phone = phonePersistence.create(phoneId);
074
075 phone.setUuid(serviceContext.getUuid());
076 phone.setCompanyId(user.getCompanyId());
077 phone.setUserId(user.getUserId());
078 phone.setUserName(user.getFullName());
079 phone.setCreateDate(serviceContext.getCreateDate(now));
080 phone.setModifiedDate(serviceContext.getModifiedDate(now));
081 phone.setClassNameId(classNameId);
082 phone.setClassPK(classPK);
083 phone.setNumber(number);
084 phone.setExtension(extension);
085 phone.setTypeId(typeId);
086 phone.setPrimary(primary);
087
088 phonePersistence.update(phone);
089
090 return phone;
091 }
092
093 @Override
094 public Phone deletePhone(long phoneId) throws PortalException {
095 Phone phone = phonePersistence.findByPrimaryKey(phoneId);
096
097 return phoneLocalService.deletePhone(phone);
098 }
099
100 @Override
101 @SystemEvent(
102 action = SystemEventConstants.ACTION_SKIP,
103 type = SystemEventConstants.TYPE_DELETE)
104 public Phone deletePhone(Phone phone) {
105 phonePersistence.remove(phone);
106
107 return phone;
108 }
109
110 @Override
111 public void deletePhones(long companyId, String className, long classPK) {
112 long classNameId = classNameLocalService.getClassNameId(className);
113
114 List<Phone> phones = phonePersistence.findByC_C_C(
115 companyId, classNameId, classPK);
116
117 for (Phone phone : phones) {
118 phoneLocalService.deletePhone(phone);
119 }
120 }
121
122 @Override
123 public List<Phone> getPhones() {
124 return phonePersistence.findAll();
125 }
126
127 @Override
128 public List<Phone> getPhones(
129 long companyId, String className, long classPK) {
130
131 long classNameId = classNameLocalService.getClassNameId(className);
132
133 return phonePersistence.findByC_C_C(companyId, classNameId, classPK);
134 }
135
136 @Override
137 public Phone updatePhone(
138 long phoneId, String number, String extension, int typeId,
139 boolean primary)
140 throws PortalException {
141
142 validate(phoneId, 0, 0, 0, number, extension, typeId, primary);
143
144 Phone phone = phonePersistence.findByPrimaryKey(phoneId);
145
146 phone.setModifiedDate(new Date());
147 phone.setNumber(number);
148 phone.setExtension(extension);
149 phone.setTypeId(typeId);
150 phone.setPrimary(primary);
151
152 phonePersistence.update(phone);
153
154 return phone;
155 }
156
157 protected void validate(
158 long phoneId, long companyId, long classNameId, long classPK,
159 boolean primary) {
160
161
162
163
164 if (primary) {
165 List<Phone> phones = phonePersistence.findByC_C_C_P(
166 companyId, classNameId, classPK, primary);
167
168 for (Phone phone : phones) {
169 if ((phoneId <= 0) || (phone.getPhoneId() != phoneId)) {
170 phone.setPrimary(false);
171
172 phonePersistence.update(phone);
173 }
174 }
175 }
176 }
177
178 protected void validate(
179 long phoneId, long companyId, long classNameId, long classPK,
180 String number, String extension, int typeId, boolean primary)
181 throws PortalException {
182
183 if (!PhoneNumberFormatUtil.validate(number)) {
184 throw new PhoneNumberException();
185 }
186
187 if (Validator.isNotNull(extension)) {
188 for (int i = 0; i < extension.length(); i++) {
189 if (!Character.isDigit(extension.charAt(i))) {
190 throw new PhoneNumberException();
191 }
192 }
193 }
194
195 if (phoneId > 0) {
196 Phone phone = phonePersistence.findByPrimaryKey(phoneId);
197
198 companyId = phone.getCompanyId();
199 classNameId = phone.getClassNameId();
200 classPK = phone.getClassPK();
201 }
202
203 if ((classNameId ==
204 classNameLocalService.getClassNameId(Account.class)) ||
205 (classNameId ==
206 classNameLocalService.getClassNameId(Contact.class)) ||
207 (classNameId ==
208 classNameLocalService.getClassNameId(Organization.class))) {
209
210 listTypeService.validate(
211 typeId, classNameId, ListTypeConstants.PHONE);
212 }
213
214 validate(phoneId, companyId, classNameId, classPK, primary);
215 }
216
217 }