001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.PhoneNumberException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.model.Phone;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.model.impl.ListTypeImpl;
030: import com.liferay.portal.service.base.PhoneLocalServiceBaseImpl;
031: import com.liferay.portal.util.PortalUtil;
032: import com.liferay.util.format.PhoneNumberUtil;
033:
034: import java.rmi.RemoteException;
035:
036: import java.util.Date;
037: import java.util.Iterator;
038: import java.util.List;
039:
040: /**
041: * <a href="PhoneLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Brian Wing Shun Chan
044: *
045: */
046: public class PhoneLocalServiceImpl extends PhoneLocalServiceBaseImpl {
047:
048: public Phone addPhone(long userId, String className, long classPK,
049: String number, String extension, int typeId, boolean primary)
050: throws PortalException, SystemException {
051:
052: User user = userPersistence.findByPrimaryKey(userId);
053: long classNameId = PortalUtil.getClassNameId(className);
054: Date now = new Date();
055:
056: number = PhoneNumberUtil.strip(number);
057: extension = PhoneNumberUtil.strip(extension);
058:
059: validate(0, user.getCompanyId(), classNameId, classPK, number,
060: typeId, primary);
061:
062: long phoneId = counterLocalService.increment();
063:
064: Phone phone = phonePersistence.create(phoneId);
065:
066: phone.setCompanyId(user.getCompanyId());
067: phone.setUserId(user.getUserId());
068: phone.setUserName(user.getFullName());
069: phone.setCreateDate(now);
070: phone.setModifiedDate(now);
071: phone.setClassNameId(classNameId);
072: phone.setClassPK(classPK);
073: phone.setNumber(number);
074: phone.setExtension(extension);
075: phone.setTypeId(typeId);
076: phone.setPrimary(primary);
077:
078: phonePersistence.update(phone);
079:
080: return phone;
081: }
082:
083: public void deletePhone(long phoneId) throws PortalException,
084: SystemException {
085:
086: phonePersistence.remove(phoneId);
087: }
088:
089: public void deletePhones(long companyId, String className,
090: long classPK) throws SystemException {
091:
092: long classNameId = PortalUtil.getClassNameId(className);
093:
094: phonePersistence.removeByC_C_C(companyId, classNameId, classPK);
095: }
096:
097: public Phone getPhone(long phoneId) throws PortalException,
098: SystemException {
099:
100: return phonePersistence.findByPrimaryKey(phoneId);
101: }
102:
103: public List getPhones() throws SystemException {
104: return phonePersistence.findAll();
105: }
106:
107: public List getPhones(long companyId, String className, long classPK)
108: throws SystemException {
109:
110: long classNameId = PortalUtil.getClassNameId(className);
111:
112: return phonePersistence.findByC_C_C(companyId, classNameId,
113: classPK);
114: }
115:
116: public Phone updatePhone(long phoneId, String number,
117: String extension, int typeId, boolean primary)
118: throws PortalException, SystemException {
119:
120: number = PhoneNumberUtil.strip(number);
121: extension = PhoneNumberUtil.strip(extension);
122:
123: validate(phoneId, 0, 0, 0, number, typeId, primary);
124:
125: Phone phone = phonePersistence.findByPrimaryKey(phoneId);
126:
127: phone.setModifiedDate(new Date());
128: phone.setNumber(number);
129: phone.setExtension(extension);
130: phone.setTypeId(typeId);
131: phone.setPrimary(primary);
132:
133: phonePersistence.update(phone);
134:
135: return phone;
136: }
137:
138: protected void validate(long phoneId, long companyId,
139: long classNameId, long classPK, String number, int typeId,
140: boolean primary) throws PortalException, SystemException {
141:
142: if (Validator.isNull(number)) {
143: throw new PhoneNumberException();
144: }
145:
146: if (phoneId > 0) {
147: Phone phone = phonePersistence.findByPrimaryKey(phoneId);
148:
149: companyId = phone.getCompanyId();
150: classNameId = phone.getClassNameId();
151: classPK = phone.getClassPK();
152: }
153:
154: try {
155: listTypeService.validate(typeId, classNameId,
156: ListTypeImpl.PHONE);
157: } catch (RemoteException re) {
158: throw new SystemException(re);
159: }
160:
161: validate(phoneId, companyId, classNameId, classPK, primary);
162: }
163:
164: protected void validate(long phoneId, long companyId,
165: long classNameId, long classPK, boolean primary)
166: throws PortalException, SystemException {
167:
168: // Check to make sure there isn't another phone with the same company
169: // id, class name, and class pk that also has primary set to true
170:
171: if (primary) {
172: Iterator itr = phonePersistence.findByC_C_C_P(companyId,
173: classNameId, classPK, primary).iterator();
174:
175: while (itr.hasNext()) {
176: Phone phone = (Phone) itr.next();
177:
178: if ((phoneId <= 0) || (phone.getPhoneId() != phoneId)) {
179:
180: phone.setPrimary(false);
181:
182: phonePersistence.update(phone);
183: }
184: }
185: }
186: }
187:
188: }
|