001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.EmailAddressException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.model.EmailAddress;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.model.impl.ListTypeImpl;
030: import com.liferay.portal.service.base.EmailAddressLocalServiceBaseImpl;
031: import com.liferay.portal.util.PortalUtil;
032:
033: import java.rmi.RemoteException;
034:
035: import java.util.Date;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: /**
040: * <a href="EmailAddressLocalServiceImpl.java.html"><b><i>View Source</i></b>
041: * </a>
042: *
043: * @author Brian Wing Shun Chan
044: * @author Alexander Chow
045: *
046: */
047: public class EmailAddressLocalServiceImpl extends
048: EmailAddressLocalServiceBaseImpl {
049:
050: public EmailAddress addEmailAddress(long userId, String className,
051: long classPK, String address, int typeId, boolean primary)
052: throws PortalException, SystemException {
053:
054: User user = userPersistence.findByPrimaryKey(userId);
055: long classNameId = PortalUtil.getClassNameId(className);
056: Date now = new Date();
057:
058: validate(0, user.getCompanyId(), classNameId, classPK, address,
059: typeId, primary);
060:
061: long emailAddressId = counterLocalService.increment();
062:
063: EmailAddress emailAddress = emailAddressPersistence
064: .create(emailAddressId);
065:
066: emailAddress.setCompanyId(user.getCompanyId());
067: emailAddress.setUserId(user.getUserId());
068: emailAddress.setUserName(user.getFullName());
069: emailAddress.setCreateDate(now);
070: emailAddress.setModifiedDate(now);
071: emailAddress.setClassNameId(classNameId);
072: emailAddress.setClassPK(classPK);
073: emailAddress.setAddress(address);
074: emailAddress.setTypeId(typeId);
075: emailAddress.setPrimary(primary);
076:
077: emailAddressPersistence.update(emailAddress);
078:
079: return emailAddress;
080: }
081:
082: public void deleteEmailAddress(long emailAddressId)
083: throws PortalException, SystemException {
084:
085: emailAddressPersistence.remove(emailAddressId);
086: }
087:
088: public void deleteEmailAddresses(long companyId, String className,
089: long classPK) throws SystemException {
090:
091: long classNameId = PortalUtil.getClassNameId(className);
092:
093: emailAddressPersistence.removeByC_C_C(companyId, classNameId,
094: classPK);
095: }
096:
097: public EmailAddress getEmailAddress(long emailAddressId)
098: throws PortalException, SystemException {
099:
100: return emailAddressPersistence.findByPrimaryKey(emailAddressId);
101: }
102:
103: public List getEmailAddresses() throws SystemException {
104: return emailAddressPersistence.findAll();
105: }
106:
107: public List getEmailAddresses(long companyId, String className,
108: long classPK) throws SystemException {
109:
110: long classNameId = PortalUtil.getClassNameId(className);
111:
112: return emailAddressPersistence.findByC_C_C(companyId,
113: classNameId, classPK);
114: }
115:
116: public EmailAddress updateEmailAddress(long emailAddressId,
117: String address, int typeId, boolean primary)
118: throws PortalException, SystemException {
119:
120: validate(emailAddressId, 0, 0, 0, address, typeId, primary);
121:
122: EmailAddress emailAddress = emailAddressPersistence
123: .findByPrimaryKey(emailAddressId);
124:
125: emailAddress.setModifiedDate(new Date());
126: emailAddress.setAddress(address);
127: emailAddress.setTypeId(typeId);
128: emailAddress.setPrimary(primary);
129:
130: emailAddressPersistence.update(emailAddress);
131:
132: return emailAddress;
133: }
134:
135: protected void validate(long emailAddressId, long companyId,
136: long classNameId, long classPK, String address, int typeId,
137: boolean primary) throws PortalException, SystemException {
138:
139: if (!Validator.isEmailAddress(address)) {
140: throw new EmailAddressException();
141: }
142:
143: if (emailAddressId > 0) {
144: EmailAddress emailAddress = emailAddressPersistence
145: .findByPrimaryKey(emailAddressId);
146:
147: companyId = emailAddress.getCompanyId();
148: classNameId = emailAddress.getClassNameId();
149: classPK = emailAddress.getClassPK();
150: }
151:
152: try {
153: listTypeService.validate(typeId, classNameId,
154: ListTypeImpl.EMAIL_ADDRESS);
155: } catch (RemoteException re) {
156: throw new SystemException(re);
157: }
158:
159: validate(emailAddressId, companyId, classNameId, classPK,
160: primary);
161: }
162:
163: protected void validate(long emailAddressId, long companyId,
164: long classNameId, long classPK, boolean primary)
165: throws PortalException, SystemException {
166:
167: // Check to make sure there isn't another emailAddress with the same
168: // company id, class name, and class pk that also has primary set to
169: // true
170:
171: if (primary) {
172: Iterator itr = emailAddressPersistence.findByC_C_C_P(
173: companyId, classNameId, classPK, primary)
174: .iterator();
175:
176: while (itr.hasNext()) {
177: EmailAddress emailAddress = (EmailAddress) itr.next();
178:
179: if ((emailAddressId <= 0)
180: || (emailAddress.getEmailAddressId() != emailAddressId)) {
181:
182: emailAddress.setPrimary(false);
183:
184: emailAddressPersistence.update(emailAddress);
185: }
186: }
187: }
188: }
189:
190: }
|