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.ContactBirthdayException;
018    import com.liferay.portal.exception.ContactClassNameException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.search.Indexable;
021    import com.liferay.portal.kernel.search.IndexableType;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Contact;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.service.base.ContactLocalServiceBaseImpl;
027    import com.liferay.portal.util.PortalUtil;
028    
029    import java.util.Date;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class ContactLocalServiceImpl extends ContactLocalServiceBaseImpl {
036    
037            @Indexable(type = IndexableType.REINDEX)
038            @Override
039            public Contact addContact(
040                            long userId, String className, long classPK, String emailAddress,
041                            String firstName, String middleName, String lastName, long prefixId,
042                            long suffixId, boolean male, int birthdayMonth, int birthdayDay,
043                            int birthdayYear, String smsSn, String facebookSn, String jabberSn,
044                            String skypeSn, String twitterSn, String jobTitle)
045                    throws PortalException {
046    
047                    User user = userPersistence.findByPrimaryKey(userId);
048                    Date birthday = PortalUtil.getDate(
049                            birthdayMonth, birthdayDay, birthdayYear,
050                            ContactBirthdayException.class);
051    
052                    validate(className, classPK);
053    
054                    long contactId = counterLocalService.increment();
055    
056                    Contact contact = contactPersistence.create(contactId);
057    
058                    contact.setCompanyId(user.getCompanyId());
059                    contact.setUserId(user.getUserId());
060                    contact.setUserName(user.getFullName());
061                    contact.setClassName(className);
062                    contact.setClassPK(classPK);
063                    contact.setEmailAddress(emailAddress);
064                    contact.setFirstName(firstName);
065                    contact.setMiddleName(middleName);
066                    contact.setLastName(lastName);
067                    contact.setPrefixId(prefixId);
068                    contact.setSuffixId(suffixId);
069                    contact.setMale(male);
070                    contact.setBirthday(birthday);
071                    contact.setSmsSn(smsSn);
072                    contact.setFacebookSn(facebookSn);
073                    contact.setJabberSn(jabberSn);
074                    contact.setSkypeSn(skypeSn);
075                    contact.setTwitterSn(twitterSn);
076                    contact.setJobTitle(jobTitle);
077    
078                    contactPersistence.update(contact);
079    
080                    return contact;
081            }
082    
083            @Indexable(type = IndexableType.DELETE)
084            @Override
085            public Contact deleteContact(Contact contact) {
086    
087                    // Contact
088    
089                    contactPersistence.remove(contact);
090    
091                    // Addresses
092    
093                    addressLocalService.deleteAddresses(
094                            contact.getCompanyId(), Contact.class.getName(),
095                            contact.getContactId());
096    
097                    // Email addresses
098    
099                    emailAddressLocalService.deleteEmailAddresses(
100                            contact.getCompanyId(), Contact.class.getName(),
101                            contact.getContactId());
102    
103                    // Phone
104    
105                    phoneLocalService.deletePhones(
106                            contact.getCompanyId(), Contact.class.getName(),
107                            contact.getContactId());
108    
109                    // Website
110    
111                    websiteLocalService.deleteWebsites(
112                            contact.getCompanyId(), Contact.class.getName(),
113                            contact.getContactId());
114    
115                    return contact;
116            }
117    
118            @Indexable(type = IndexableType.DELETE)
119            @Override
120            public Contact deleteContact(long contactId) {
121                    Contact contact = contactPersistence.fetchByPrimaryKey(contactId);
122    
123                    if (contact != null) {
124                            deleteContact(contact);
125                    }
126    
127                    return contact;
128            }
129    
130            @Override
131            public List<Contact> getContacts(
132                    long classNameId, long classPK, int start, int end,
133                    OrderByComparator<Contact> orderByComparator) {
134    
135                    return contactPersistence.findByC_C(
136                            classNameId, classPK, start, end, orderByComparator);
137            }
138    
139            @Override
140            public int getContactsCount(long classNameId, long classPK) {
141                    return contactPersistence.countByC_C(classNameId, classPK);
142            }
143    
144            @Indexable(type = IndexableType.REINDEX)
145            @Override
146            public Contact updateContact(
147                            long contactId, String emailAddress, String firstName,
148                            String middleName, String lastName, long prefixId, long suffixId,
149                            boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
150                            String smsSn, String facebookSn, String jabberSn, String skypeSn,
151                            String twitterSn, String jobTitle)
152                    throws PortalException {
153    
154                    Date birthday = PortalUtil.getDate(
155                            birthdayMonth, birthdayDay, birthdayYear,
156                            ContactBirthdayException.class);
157    
158                    Contact contact = contactPersistence.findByPrimaryKey(contactId);
159    
160                    contact.setEmailAddress(emailAddress);
161                    contact.setFirstName(firstName);
162                    contact.setMiddleName(middleName);
163                    contact.setLastName(lastName);
164                    contact.setPrefixId(prefixId);
165                    contact.setSuffixId(suffixId);
166                    contact.setMale(male);
167                    contact.setBirthday(birthday);
168                    contact.setSmsSn(smsSn);
169                    contact.setFacebookSn(facebookSn);
170                    contact.setJabberSn(jabberSn);
171                    contact.setSkypeSn(skypeSn);
172                    contact.setTwitterSn(twitterSn);
173                    contact.setJobTitle(jobTitle);
174    
175                    contactPersistence.update(contact);
176    
177                    return contact;
178            }
179    
180            protected void validate(String className, long classPK)
181                    throws PortalException {
182    
183                    if (Validator.isNull(className) ||
184                            className.equals(User.class.getName()) || (classPK <= 0)) {
185    
186                            throw new ContactClassNameException();
187                    }
188            }
189    
190    }