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