001    /**
002     * Copyright (c) 2000-2010 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.EmailAddressException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.EmailAddress;
022    import com.liferay.portal.model.ListTypeConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.base.EmailAddressLocalServiceBaseImpl;
025    import com.liferay.portal.util.PortalUtil;
026    
027    import java.util.Date;
028    import java.util.Iterator;
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Alexander Chow
034     */
035    public class EmailAddressLocalServiceImpl
036            extends EmailAddressLocalServiceBaseImpl {
037    
038            public EmailAddress addEmailAddress(
039                            long userId, String className, long classPK, String address,
040                            int typeId, boolean primary)
041                    throws PortalException, SystemException {
042    
043                    User user = userPersistence.findByPrimaryKey(userId);
044                    long classNameId = PortalUtil.getClassNameId(className);
045                    Date now = new Date();
046    
047                    validate(
048                            0, user.getCompanyId(), classNameId, classPK, address, typeId,
049                            primary);
050    
051                    long emailAddressId = counterLocalService.increment();
052    
053                    EmailAddress emailAddress = emailAddressPersistence.create(
054                            emailAddressId);
055    
056                    emailAddress.setCompanyId(user.getCompanyId());
057                    emailAddress.setUserId(user.getUserId());
058                    emailAddress.setUserName(user.getFullName());
059                    emailAddress.setCreateDate(now);
060                    emailAddress.setModifiedDate(now);
061                    emailAddress.setClassNameId(classNameId);
062                    emailAddress.setClassPK(classPK);
063                    emailAddress.setAddress(address);
064                    emailAddress.setTypeId(typeId);
065                    emailAddress.setPrimary(primary);
066    
067                    emailAddressPersistence.update(emailAddress, false);
068    
069                    return emailAddress;
070            }
071    
072            public void deleteEmailAddress(long emailAddressId)
073                    throws PortalException, SystemException {
074    
075                    emailAddressPersistence.remove(emailAddressId);
076            }
077    
078            public void deleteEmailAddresses(
079                            long companyId, String className, long classPK)
080                    throws SystemException {
081    
082                    long classNameId = PortalUtil.getClassNameId(className);
083    
084                    emailAddressPersistence.removeByC_C_C(companyId, classNameId, classPK);
085            }
086    
087            public EmailAddress getEmailAddress(long emailAddressId)
088                    throws PortalException, SystemException {
089    
090                    return emailAddressPersistence.findByPrimaryKey(emailAddressId);
091            }
092    
093            public List<EmailAddress> getEmailAddresses() throws SystemException {
094                    return emailAddressPersistence.findAll();
095            }
096    
097            public List<EmailAddress> getEmailAddresses(
098                            long companyId, String className, long classPK)
099                    throws SystemException {
100    
101                    long classNameId = PortalUtil.getClassNameId(className);
102    
103                    return emailAddressPersistence.findByC_C_C(
104                            companyId, classNameId, classPK);
105            }
106    
107            public EmailAddress updateEmailAddress(
108                            long emailAddressId, String address, int typeId, boolean primary)
109                    throws PortalException, SystemException {
110    
111                    validate(emailAddressId, 0, 0, 0, address, typeId, primary);
112    
113                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
114                            emailAddressId);
115    
116                    emailAddress.setModifiedDate(new Date());
117                    emailAddress.setAddress(address);
118                    emailAddress.setTypeId(typeId);
119                    emailAddress.setPrimary(primary);
120    
121                    emailAddressPersistence.update(emailAddress, false);
122    
123                    return emailAddress;
124            }
125    
126            protected void validate(
127                            long emailAddressId, long companyId, long classNameId,
128                            long classPK, String address, int typeId, boolean primary)
129                    throws PortalException, SystemException {
130    
131                    if (!Validator.isEmailAddress(address)) {
132                            throw new EmailAddressException();
133                    }
134    
135                    if (emailAddressId > 0) {
136                            EmailAddress emailAddress =
137                                    emailAddressPersistence.findByPrimaryKey(
138                                            emailAddressId);
139    
140                            companyId = emailAddress.getCompanyId();
141                            classNameId = emailAddress.getClassNameId();
142                            classPK = emailAddress.getClassPK();
143                    }
144    
145                    listTypeService.validate(
146                            typeId, classNameId, ListTypeConstants.EMAIL_ADDRESS);
147    
148                    validate(emailAddressId, companyId, classNameId, classPK, primary);
149            }
150    
151            protected void validate(
152                            long emailAddressId, long companyId, long classNameId, long classPK,
153                            boolean primary)
154                    throws SystemException {
155    
156                    // Check to make sure there isn't another emailAddress with the same
157                    // company id, class name, and class pk that also has primary set to
158                    // true
159    
160                    if (primary) {
161                            Iterator<EmailAddress> itr = emailAddressPersistence.findByC_C_C_P(
162                                    companyId, classNameId, classPK, primary).iterator();
163    
164                            while (itr.hasNext()) {
165                                    EmailAddress emailAddress = itr.next();
166    
167                                    if ((emailAddressId <= 0) ||
168                                            (emailAddress.getEmailAddressId() != emailAddressId)) {
169    
170                                            emailAddress.setPrimary(false);
171    
172                                            emailAddressPersistence.update(emailAddress, false);
173                                    }
174                            }
175                    }
176            }
177    
178    }