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