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.exception.SystemException;
020 import com.liferay.portal.kernel.format.PhoneNumberFormatUtil;
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.User;
028 import com.liferay.portal.service.base.PhoneLocalServiceBaseImpl;
029 import com.liferay.portal.util.PortalUtil;
030
031 import java.util.Date;
032 import java.util.Iterator;
033 import java.util.List;
034
035
038 public class PhoneLocalServiceImpl extends PhoneLocalServiceBaseImpl {
039
040 @Override
041 public Phone addPhone(
042 long userId, String className, long classPK, String number,
043 String extension, int typeId, boolean primary)
044 throws PortalException, SystemException {
045
046 User user = userPersistence.findByPrimaryKey(userId);
047 long classNameId = PortalUtil.getClassNameId(className);
048 Date now = new Date();
049
050 validate(
051 0, user.getCompanyId(), classNameId, classPK, number, extension,
052 typeId, primary);
053
054 long phoneId = counterLocalService.increment();
055
056 Phone phone = phonePersistence.create(phoneId);
057
058 phone.setCompanyId(user.getCompanyId());
059 phone.setUserId(user.getUserId());
060 phone.setUserName(user.getFullName());
061 phone.setCreateDate(now);
062 phone.setModifiedDate(now);
063 phone.setClassNameId(classNameId);
064 phone.setClassPK(classPK);
065 phone.setNumber(number);
066 phone.setExtension(extension);
067 phone.setTypeId(typeId);
068 phone.setPrimary(primary);
069
070 phonePersistence.update(phone, false);
071
072 return phone;
073 }
074
075 @Override
076 public void deletePhones(long companyId, String className, long classPK)
077 throws SystemException {
078
079 long classNameId = PortalUtil.getClassNameId(className);
080
081 List<Phone> phones = phonePersistence.findByC_C_C(
082 companyId, classNameId, classPK);
083
084 for (Phone phone : phones) {
085 deletePhone(phone);
086 }
087 }
088
089 @Override
090 public List<Phone> getPhones() throws SystemException {
091 return phonePersistence.findAll();
092 }
093
094 @Override
095 public List<Phone> getPhones(long companyId, String className, long classPK)
096 throws SystemException {
097
098 long classNameId = PortalUtil.getClassNameId(className);
099
100 return phonePersistence.findByC_C_C(companyId, classNameId, classPK);
101 }
102
103 @Override
104 public Phone updatePhone(
105 long phoneId, String number, String extension, int typeId,
106 boolean primary)
107 throws PortalException, SystemException {
108
109 validate(phoneId, 0, 0, 0, number, extension, typeId, primary);
110
111 Phone phone = phonePersistence.findByPrimaryKey(phoneId);
112
113 phone.setModifiedDate(new Date());
114 phone.setNumber(number);
115 phone.setExtension(extension);
116 phone.setTypeId(typeId);
117 phone.setPrimary(primary);
118
119 phonePersistence.update(phone, false);
120
121 return phone;
122 }
123
124 protected void validate(
125 long phoneId, long companyId, long classNameId, long classPK,
126 boolean primary)
127 throws SystemException {
128
129
130
131
132 if (primary) {
133 Iterator<Phone> itr = phonePersistence.findByC_C_C_P(
134 companyId, classNameId, classPK, primary).iterator();
135
136 while (itr.hasNext()) {
137 Phone phone = itr.next();
138
139 if ((phoneId <= 0) || (phone.getPhoneId() != phoneId)) {
140 phone.setPrimary(false);
141
142 phonePersistence.update(phone, false);
143 }
144 }
145 }
146 }
147
148 protected void validate(
149 long phoneId, long companyId, long classNameId, long classPK,
150 String number, String extension, int typeId, boolean primary)
151 throws PortalException, SystemException {
152
153 if (!PhoneNumberFormatUtil.validate(number)) {
154 throw new PhoneNumberException();
155 }
156
157 if (Validator.isNotNull(extension)) {
158 for (int i = 0; i < extension.length(); i++) {
159 if (!Character.isDigit(extension.charAt(i))) {
160 throw new PhoneNumberException();
161 }
162 }
163 }
164
165 if (phoneId > 0) {
166 Phone phone = phonePersistence.findByPrimaryKey(phoneId);
167
168 companyId = phone.getCompanyId();
169 classNameId = phone.getClassNameId();
170 classPK = phone.getClassPK();
171 }
172
173 if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
174 (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
175 (classNameId == PortalUtil.getClassNameId(Organization.class))) {
176
177 listTypeService.validate(
178 typeId, classNameId, ListTypeConstants.PHONE);
179 }
180
181 validate(phoneId, companyId, classNameId, classPK, primary);
182 }
183
184 }