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.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    /**
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                    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            @Override
075            public void deletePhone(long phoneId)
076                    throws PortalException, SystemException {
077    
078                    Phone phone = phonePersistence.findByPrimaryKey(phoneId);
079    
080                    deletePhone(phone);
081            }
082    
083            @Override
084            public void deletePhone(Phone phone) throws SystemException {
085                    phonePersistence.remove(phone);
086            }
087    
088            public void deletePhones(long companyId, String className, long classPK)
089                    throws SystemException {
090    
091                    long classNameId = PortalUtil.getClassNameId(className);
092    
093                    List<Phone> phones = phonePersistence.findByC_C_C(
094                            companyId, classNameId, classPK);
095    
096                    for (Phone phone : phones) {
097                            deletePhone(phone);
098                    }
099            }
100    
101            @Override
102            public Phone getPhone(long phoneId)
103                    throws PortalException, SystemException {
104    
105                    return phonePersistence.findByPrimaryKey(phoneId);
106            }
107    
108            public List<Phone> getPhones() throws SystemException {
109                    return phonePersistence.findAll();
110            }
111    
112            public List<Phone> getPhones(long companyId, String className, long classPK)
113                    throws SystemException {
114    
115                    long classNameId = PortalUtil.getClassNameId(className);
116    
117                    return phonePersistence.findByC_C_C(companyId, classNameId, classPK);
118            }
119    
120            public Phone updatePhone(
121                            long phoneId, String number, String extension, int typeId,
122                            boolean primary)
123                    throws PortalException, SystemException {
124    
125                    validate(phoneId, 0, 0, 0, number, extension, typeId, primary);
126    
127                    Phone phone = phonePersistence.findByPrimaryKey(phoneId);
128    
129                    phone.setModifiedDate(new Date());
130                    phone.setNumber(number);
131                    phone.setExtension(extension);
132                    phone.setTypeId(typeId);
133                    phone.setPrimary(primary);
134    
135                    phonePersistence.update(phone, false);
136    
137                    return phone;
138            }
139    
140            protected void validate(
141                            long phoneId, long companyId, long classNameId, long classPK,
142                            boolean primary)
143                    throws SystemException {
144    
145                    // Check to make sure there isn't another phone with the same company
146                    // id, class name, and class pk that also has primary set to true
147    
148                    if (primary) {
149                            Iterator<Phone> itr = phonePersistence.findByC_C_C_P(
150                                    companyId, classNameId, classPK, primary).iterator();
151    
152                            while (itr.hasNext()) {
153                                    Phone phone = itr.next();
154    
155                                    if ((phoneId <= 0) || (phone.getPhoneId() != phoneId)) {
156                                            phone.setPrimary(false);
157    
158                                            phonePersistence.update(phone, false);
159                                    }
160                            }
161                    }
162            }
163    
164            protected void validate(
165                            long phoneId, long companyId, long classNameId, long classPK,
166                            String number, String extension, int typeId, boolean primary)
167                    throws PortalException, SystemException {
168    
169                    if (!PhoneNumberFormatUtil.validate(number)) {
170                            throw new PhoneNumberException();
171                    }
172    
173                    if (Validator.isNotNull(extension)) {
174                            for (int i = 0;i < extension.length();i++) {
175                                    if (!Character.isDigit(extension.charAt(i))) {
176                                            throw new PhoneNumberException();
177                                    }
178                            }
179                    }
180    
181                    if (phoneId > 0) {
182                            Phone phone = phonePersistence.findByPrimaryKey(phoneId);
183    
184                            companyId = phone.getCompanyId();
185                            classNameId = phone.getClassNameId();
186                            classPK = phone.getClassPK();
187                    }
188    
189                    if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
190                            (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
191                            (classNameId == PortalUtil.getClassNameId(Organization.class))) {
192    
193                            listTypeService.validate(
194                                    typeId, classNameId, ListTypeConstants.PHONE);
195                    }
196    
197                    validate(phoneId, companyId, classNameId, classPK, primary);
198            }
199    
200    }