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