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