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