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.exception.PhoneNumberException;
018    import com.liferay.portal.exception.PhoneNumberExtensionException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.format.PhoneNumberFormatUtil;
021    import com.liferay.portal.kernel.systemevent.SystemEvent;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Account;
024    import com.liferay.portal.model.Contact;
025    import com.liferay.portal.model.ListTypeConstants;
026    import com.liferay.portal.model.Organization;
027    import com.liferay.portal.model.Phone;
028    import com.liferay.portal.model.SystemEventConstants;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.service.ServiceContext;
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            /**
041             * @deprecated As of 6.2.0, replaced by {@link #addPhone(long, String, long,
042             *             String, String, int, boolean, ServiceContext)}
043             */
044            @Deprecated
045            @Override
046            public Phone addPhone(
047                            long userId, String className, long classPK, String number,
048                            String extension, long typeId, boolean primary)
049                    throws PortalException {
050    
051                    return addPhone(
052                            userId, className, classPK, number, extension, typeId, primary,
053                            new ServiceContext());
054            }
055    
056            @Override
057            public Phone addPhone(
058                            long userId, String className, long classPK, String number,
059                            String extension, long typeId, boolean primary,
060                            ServiceContext serviceContext)
061                    throws PortalException {
062    
063                    User user = userPersistence.findByPrimaryKey(userId);
064                    long classNameId = classNameLocalService.getClassNameId(className);
065    
066                    validate(
067                            0, user.getCompanyId(), classNameId, classPK, number, extension,
068                            typeId, primary);
069    
070                    long phoneId = counterLocalService.increment();
071    
072                    Phone phone = phonePersistence.create(phoneId);
073    
074                    phone.setUuid(serviceContext.getUuid());
075                    phone.setCompanyId(user.getCompanyId());
076                    phone.setUserId(user.getUserId());
077                    phone.setUserName(user.getFullName());
078                    phone.setClassNameId(classNameId);
079                    phone.setClassPK(classPK);
080                    phone.setNumber(number);
081                    phone.setExtension(extension);
082                    phone.setTypeId(typeId);
083                    phone.setPrimary(primary);
084    
085                    phonePersistence.update(phone);
086    
087                    return phone;
088            }
089    
090            @Override
091            public Phone deletePhone(long phoneId) throws PortalException {
092                    Phone phone = phonePersistence.findByPrimaryKey(phoneId);
093    
094                    return phoneLocalService.deletePhone(phone);
095            }
096    
097            @Override
098            @SystemEvent(
099                    action = SystemEventConstants.ACTION_SKIP,
100                    type = SystemEventConstants.TYPE_DELETE
101            )
102            public Phone deletePhone(Phone phone) {
103                    phonePersistence.remove(phone);
104    
105                    return phone;
106            }
107    
108            @Override
109            public void deletePhones(long companyId, String className, long classPK) {
110                    long classNameId = classNameLocalService.getClassNameId(className);
111    
112                    List<Phone> phones = phonePersistence.findByC_C_C(
113                            companyId, classNameId, classPK);
114    
115                    for (Phone phone : phones) {
116                            phoneLocalService.deletePhone(phone);
117                    }
118            }
119    
120            @Override
121            public List<Phone> getPhones() {
122                    return phonePersistence.findAll();
123            }
124    
125            @Override
126            public List<Phone> getPhones(
127                    long companyId, String className, long classPK) {
128    
129                    long classNameId = classNameLocalService.getClassNameId(className);
130    
131                    return phonePersistence.findByC_C_C(companyId, classNameId, classPK);
132            }
133    
134            @Override
135            public Phone updatePhone(
136                            long phoneId, String number, String extension, long typeId,
137                            boolean primary)
138                    throws PortalException {
139    
140                    validate(phoneId, 0, 0, 0, number, extension, typeId, primary);
141    
142                    Phone phone = phonePersistence.findByPrimaryKey(phoneId);
143    
144                    phone.setNumber(number);
145                    phone.setExtension(extension);
146                    phone.setTypeId(typeId);
147                    phone.setPrimary(primary);
148    
149                    phonePersistence.update(phone);
150    
151                    return phone;
152            }
153    
154            protected void validate(
155                    long phoneId, long companyId, long classNameId, long classPK,
156                    boolean primary) {
157    
158                    // Check to make sure there isn't another phone with the same company
159                    // id, class name, and class pk that also has primary set to true
160    
161                    if (primary) {
162                            List<Phone> phones = phonePersistence.findByC_C_C_P(
163                                    companyId, classNameId, classPK, primary);
164    
165                            for (Phone phone : phones) {
166                                    if ((phoneId <= 0) || (phone.getPhoneId() != phoneId)) {
167                                            phone.setPrimary(false);
168    
169                                            phonePersistence.update(phone);
170                                    }
171                            }
172                    }
173            }
174    
175            protected void validate(
176                            long phoneId, long companyId, long classNameId, long classPK,
177                            String number, String extension, long typeId, boolean primary)
178                    throws PortalException {
179    
180                    if (!PhoneNumberFormatUtil.validate(number)) {
181                            throw new PhoneNumberException();
182                    }
183    
184                    if (Validator.isNotNull(extension)) {
185                            for (int i = 0; i < extension.length(); i++) {
186                                    if (!Character.isDigit(extension.charAt(i))) {
187                                            throw new PhoneNumberExtensionException();
188                                    }
189                            }
190                    }
191    
192                    if (phoneId > 0) {
193                            Phone phone = phonePersistence.findByPrimaryKey(phoneId);
194    
195                            companyId = phone.getCompanyId();
196                            classNameId = phone.getClassNameId();
197                            classPK = phone.getClassPK();
198                    }
199    
200                    if ((classNameId ==
201                                    classNameLocalService.getClassNameId(Account.class)) ||
202                            (classNameId ==
203                                    classNameLocalService.getClassNameId(Contact.class)) ||
204                            (classNameId ==
205                                    classNameLocalService.getClassNameId(Organization.class))) {
206    
207                            listTypeLocalService.validate(
208                                    typeId, classNameId, ListTypeConstants.PHONE);
209                    }
210    
211                    validate(phoneId, companyId, classNameId, classPK, primary);
212            }
213    
214    }