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