1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.EmailAddressException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.EmailAddress;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.model.impl.ListTypeImpl;
32  import com.liferay.portal.service.base.EmailAddressLocalServiceBaseImpl;
33  import com.liferay.portal.util.PortalUtil;
34  
35  import java.rmi.RemoteException;
36  
37  import java.util.Date;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * <a href="EmailAddressLocalServiceImpl.java.html"><b><i>View Source</i></b>
43   * </a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Alexander Chow
47   */
48  public class EmailAddressLocalServiceImpl
49      extends EmailAddressLocalServiceBaseImpl {
50  
51      public EmailAddress addEmailAddress(
52              long userId, String className, long classPK, String address,
53              int typeId, boolean primary)
54          throws PortalException, SystemException {
55  
56          User user = userPersistence.findByPrimaryKey(userId);
57          long classNameId = PortalUtil.getClassNameId(className);
58          Date now = new Date();
59  
60          validate(
61              0, user.getCompanyId(), classNameId, classPK, address, typeId,
62              primary);
63  
64          long emailAddressId = counterLocalService.increment();
65  
66          EmailAddress emailAddress = emailAddressPersistence.create(
67              emailAddressId);
68  
69          emailAddress.setCompanyId(user.getCompanyId());
70          emailAddress.setUserId(user.getUserId());
71          emailAddress.setUserName(user.getFullName());
72          emailAddress.setCreateDate(now);
73          emailAddress.setModifiedDate(now);
74          emailAddress.setClassNameId(classNameId);
75          emailAddress.setClassPK(classPK);
76          emailAddress.setAddress(address);
77          emailAddress.setTypeId(typeId);
78          emailAddress.setPrimary(primary);
79  
80          emailAddressPersistence.update(emailAddress, false);
81  
82          return emailAddress;
83      }
84  
85      public void deleteEmailAddress(long emailAddressId)
86          throws PortalException, SystemException {
87  
88          emailAddressPersistence.remove(emailAddressId);
89      }
90  
91      public void deleteEmailAddresses(
92              long companyId, String className, long classPK)
93          throws SystemException {
94  
95          long classNameId = PortalUtil.getClassNameId(className);
96  
97          emailAddressPersistence.removeByC_C_C(companyId, classNameId, classPK);
98      }
99  
100     public EmailAddress getEmailAddress(long emailAddressId)
101         throws PortalException, SystemException {
102 
103         return emailAddressPersistence.findByPrimaryKey(emailAddressId);
104     }
105 
106     public List<EmailAddress> getEmailAddresses() throws SystemException {
107         return emailAddressPersistence.findAll();
108     }
109 
110     public List<EmailAddress> getEmailAddresses(
111             long companyId, String className, long classPK)
112         throws SystemException {
113 
114         long classNameId = PortalUtil.getClassNameId(className);
115 
116         return emailAddressPersistence.findByC_C_C(
117             companyId, classNameId, classPK);
118     }
119 
120     public EmailAddress updateEmailAddress(
121             long emailAddressId, String address, int typeId, boolean primary)
122         throws PortalException, SystemException {
123 
124         validate(emailAddressId, 0, 0, 0, address, typeId, primary);
125 
126         EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
127             emailAddressId);
128 
129         emailAddress.setModifiedDate(new Date());
130         emailAddress.setAddress(address);
131         emailAddress.setTypeId(typeId);
132         emailAddress.setPrimary(primary);
133 
134         emailAddressPersistence.update(emailAddress, false);
135 
136         return emailAddress;
137     }
138 
139     protected void validate(
140             long emailAddressId, long companyId, long classNameId,
141             long classPK, String address, int typeId, boolean primary)
142         throws PortalException, SystemException {
143 
144         if (!Validator.isEmailAddress(address)) {
145             throw new EmailAddressException();
146         }
147 
148         if (emailAddressId > 0) {
149             EmailAddress emailAddress =
150                 emailAddressPersistence.findByPrimaryKey(
151                     emailAddressId);
152 
153             companyId = emailAddress.getCompanyId();
154             classNameId = emailAddress.getClassNameId();
155             classPK = emailAddress.getClassPK();
156         }
157 
158         try {
159             listTypeService.validate(
160                 typeId, classNameId, ListTypeImpl.EMAIL_ADDRESS);
161         }
162         catch (RemoteException re) {
163             throw new SystemException(re);
164         }
165 
166         validate(emailAddressId, companyId, classNameId, classPK, primary);
167     }
168 
169     protected void validate(
170             long emailAddressId, long companyId, long classNameId, long classPK,
171             boolean primary)
172         throws SystemException {
173 
174         // Check to make sure there isn't another emailAddress with the same
175         // company id, class name, and class pk that also has primary set to
176         // true
177 
178         if (primary) {
179             Iterator<EmailAddress> itr = emailAddressPersistence.findByC_C_C_P(
180                 companyId, classNameId, classPK, primary).iterator();
181 
182             while (itr.hasNext()) {
183                 EmailAddress emailAddress = itr.next();
184 
185                 if ((emailAddressId <= 0) ||
186                     (emailAddress.getEmailAddressId() != emailAddressId)) {
187 
188                     emailAddress.setPrimary(false);
189 
190                     emailAddressPersistence.update(emailAddress, false);
191                 }
192             }
193         }
194     }
195 
196 }