001    /**
002     * Copyright (c) 2000-present 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.kernel.exception.PhoneNumberException;
018    import com.liferay.portal.kernel.exception.PhoneNumberExtensionException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.format.PhoneNumberFormatUtil;
021    import com.liferay.portal.kernel.model.Account;
022    import com.liferay.portal.kernel.model.Contact;
023    import com.liferay.portal.kernel.model.ListTypeConstants;
024    import com.liferay.portal.kernel.model.Organization;
025    import com.liferay.portal.kernel.model.Phone;
026    import com.liferay.portal.kernel.model.SystemEventConstants;
027    import com.liferay.portal.kernel.model.User;
028    import com.liferay.portal.kernel.service.ServiceContext;
029    import com.liferay.portal.kernel.systemevent.SystemEvent;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.service.base.PhoneLocalServiceBaseImpl;
032    
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class PhoneLocalServiceImpl extends PhoneLocalServiceBaseImpl {
039    
040            @Override
041            public Phone addPhone(
042                            long userId, String className, long classPK, String number,
043                            String extension, long typeId, boolean primary,
044                            ServiceContext serviceContext)
045                    throws PortalException {
046    
047                    User user = userPersistence.findByPrimaryKey(userId);
048                    long classNameId = classNameLocalService.getClassNameId(className);
049    
050                    validate(
051                            0, user.getCompanyId(), classNameId, classPK, number, extension,
052                            typeId, primary);
053    
054                    long phoneId = counterLocalService.increment();
055    
056                    Phone phone = phonePersistence.create(phoneId);
057    
058                    phone.setUuid(serviceContext.getUuid());
059                    phone.setCompanyId(user.getCompanyId());
060                    phone.setUserId(user.getUserId());
061                    phone.setUserName(user.getFullName());
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);
070    
071                    return phone;
072            }
073    
074            @Override
075            public Phone deletePhone(long phoneId) throws PortalException {
076                    Phone phone = phonePersistence.findByPrimaryKey(phoneId);
077    
078                    return phoneLocalService.deletePhone(phone);
079            }
080    
081            @Override
082            @SystemEvent(
083                    action = SystemEventConstants.ACTION_SKIP,
084                    type = SystemEventConstants.TYPE_DELETE
085            )
086            public Phone deletePhone(Phone phone) {
087                    phonePersistence.remove(phone);
088    
089                    return phone;
090            }
091    
092            @Override
093            public void deletePhones(long companyId, String className, long classPK) {
094                    long classNameId = classNameLocalService.getClassNameId(className);
095    
096                    List<Phone> phones = phonePersistence.findByC_C_C(
097                            companyId, classNameId, classPK);
098    
099                    for (Phone phone : phones) {
100                            phoneLocalService.deletePhone(phone);
101                    }
102            }
103    
104            @Override
105            public List<Phone> getPhones() {
106                    return phonePersistence.findAll();
107            }
108    
109            @Override
110            public List<Phone> getPhones(
111                    long companyId, String className, long classPK) {
112    
113                    long classNameId = classNameLocalService.getClassNameId(className);
114    
115                    return phonePersistence.findByC_C_C(companyId, classNameId, classPK);
116            }
117    
118            @Override
119            public Phone updatePhone(
120                            long phoneId, String number, String extension, long typeId,
121                            boolean primary)
122                    throws PortalException {
123    
124                    validate(phoneId, 0, 0, 0, number, extension, typeId, primary);
125    
126                    Phone phone = phonePersistence.findByPrimaryKey(phoneId);
127    
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(
139                    long phoneId, long companyId, long classNameId, long classPK,
140                    boolean primary) {
141    
142                    // Check to make sure there isn't another phone with the same company
143                    // id, class name, and class pk that also has primary set to true
144    
145                    if (primary) {
146                            List<Phone> phones = phonePersistence.findByC_C_C_P(
147                                    companyId, classNameId, classPK, primary);
148    
149                            for (Phone phone : phones) {
150                                    if ((phoneId <= 0) || (phone.getPhoneId() != phoneId)) {
151                                            phone.setPrimary(false);
152    
153                                            phonePersistence.update(phone);
154                                    }
155                            }
156                    }
157            }
158    
159            protected void validate(
160                            long phoneId, long companyId, long classNameId, long classPK,
161                            String number, String extension, long typeId, boolean primary)
162                    throws PortalException {
163    
164                    if (!PhoneNumberFormatUtil.validate(number)) {
165                            throw new PhoneNumberException();
166                    }
167    
168                    if (Validator.isNotNull(extension)) {
169                            for (int i = 0; i < extension.length(); i++) {
170                                    if (!Character.isDigit(extension.charAt(i))) {
171                                            throw new PhoneNumberExtensionException();
172                                    }
173                            }
174                    }
175    
176                    if (phoneId > 0) {
177                            Phone phone = phonePersistence.findByPrimaryKey(phoneId);
178    
179                            companyId = phone.getCompanyId();
180                            classNameId = phone.getClassNameId();
181                            classPK = phone.getClassPK();
182                    }
183    
184                    if ((classNameId ==
185                                    classNameLocalService.getClassNameId(Account.class)) ||
186                            (classNameId ==
187                                    classNameLocalService.getClassNameId(Contact.class)) ||
188                            (classNameId ==
189                                    classNameLocalService.getClassNameId(Organization.class))) {
190    
191                            listTypeLocalService.validate(
192                                    typeId, classNameId, ListTypeConstants.PHONE);
193                    }
194    
195                    validate(phoneId, companyId, classNameId, classPK, primary);
196            }
197    
198    }