001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
036     * @author Brian Wing Shun Chan
037     */
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            @Override
078            public void deletePhone(long phoneId)
079                    throws PortalException, SystemException {
080    
081                    Phone phone = phonePersistence.findByPrimaryKey(phoneId);
082    
083                    deletePhone(phone);
084            }
085    
086            @Override
087            public void deletePhone(Phone phone) throws SystemException {
088                    phonePersistence.remove(phone);
089            }
090    
091            public void deletePhones(long companyId, String className, long classPK)
092                    throws SystemException {
093    
094                    long classNameId = PortalUtil.getClassNameId(className);
095    
096                    List<Phone> phones = phonePersistence.findByC_C_C(
097                            companyId, classNameId, classPK);
098    
099                    for (Phone phone : phones) {
100                            deletePhone(phone);
101                    }
102            }
103    
104            @Override
105            public Phone getPhone(long phoneId)
106                    throws PortalException, SystemException {
107    
108                    return phonePersistence.findByPrimaryKey(phoneId);
109            }
110    
111            public List<Phone> getPhones() throws SystemException {
112                    return phonePersistence.findAll();
113            }
114    
115            public List<Phone> getPhones(long companyId, String className, long classPK)
116                    throws SystemException {
117    
118                    long classNameId = PortalUtil.getClassNameId(className);
119    
120                    return phonePersistence.findByC_C_C(companyId, classNameId, classPK);
121            }
122    
123            public Phone updatePhone(
124                            long phoneId, String number, String extension, int typeId,
125                            boolean primary)
126                    throws PortalException, SystemException {
127    
128                    number = PhoneNumberUtil.strip(number);
129                    extension = PhoneNumberUtil.strip(extension);
130    
131                    validate(phoneId, 0, 0, 0, number, typeId, primary);
132    
133                    Phone phone = phonePersistence.findByPrimaryKey(phoneId);
134    
135                    phone.setModifiedDate(new Date());
136                    phone.setNumber(number);
137                    phone.setExtension(extension);
138                    phone.setTypeId(typeId);
139                    phone.setPrimary(primary);
140    
141                    phonePersistence.update(phone, false);
142    
143                    return phone;
144            }
145    
146            protected void validate(
147                            long phoneId, long companyId, long classNameId, long classPK,
148                            String number, int typeId, boolean primary)
149                    throws PortalException, SystemException {
150    
151                    if (Validator.isNull(number)) {
152                            throw new PhoneNumberException();
153                    }
154    
155                    if (phoneId > 0) {
156                            Phone phone = phonePersistence.findByPrimaryKey(phoneId);
157    
158                            companyId = phone.getCompanyId();
159                            classNameId = phone.getClassNameId();
160                            classPK = phone.getClassPK();
161                    }
162    
163                    if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
164                            (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
165                            (classNameId == PortalUtil.getClassNameId(Organization.class))) {
166    
167                            listTypeService.validate(
168                                    typeId, classNameId, ListTypeConstants.PHONE);
169                    }
170    
171                    validate(phoneId, companyId, classNameId, classPK, primary);
172            }
173    
174            protected void validate(
175                            long phoneId, long companyId, long classNameId, long classPK,
176                            boolean primary)
177                    throws SystemException {
178    
179                    // Check to make sure there isn't another phone with the same company
180                    // id, class name, and class pk that also has primary set to true
181    
182                    if (primary) {
183                            Iterator<Phone> itr = phonePersistence.findByC_C_C_P(
184                                    companyId, classNameId, classPK, primary).iterator();
185    
186                            while (itr.hasNext()) {
187                                    Phone phone = itr.next();
188    
189                                    if ((phoneId <= 0) || (phone.getPhoneId() != phoneId)) {
190                                            phone.setPrimary(false);
191    
192                                            phonePersistence.update(phone, false);
193                                    }
194                            }
195                    }
196            }
197    
198    }