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.AddressCityException;
024: import com.liferay.portal.AddressStreetException;
025: import com.liferay.portal.AddressZipException;
026: import com.liferay.portal.PortalException;
027: import com.liferay.portal.SystemException;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.Address;
030: import com.liferay.portal.model.User;
031: import com.liferay.portal.model.impl.ListTypeImpl;
032: import com.liferay.portal.service.base.AddressLocalServiceBaseImpl;
033: import com.liferay.portal.util.PortalUtil;
034:
035: import java.rmi.RemoteException;
036:
037: import java.util.Date;
038: import java.util.Iterator;
039: import java.util.List;
040:
041: /**
042: * <a href="AddressLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Brian Wing Shun Chan
045: * @author Alexander Chow
046: *
047: */
048: public class AddressLocalServiceImpl extends
049: AddressLocalServiceBaseImpl {
050:
051: public Address addAddress(long userId, String className,
052: long classPK, String street1, String street2,
053: String street3, String city, String zip, long regionId,
054: long countryId, int typeId, boolean mailing, boolean primary)
055: throws PortalException, SystemException {
056:
057: User user = userPersistence.findByPrimaryKey(userId);
058: long classNameId = PortalUtil.getClassNameId(className);
059: Date now = new Date();
060:
061: validate(0, user.getCompanyId(), classNameId, classPK, street1,
062: city, zip, regionId, countryId, typeId, mailing,
063: primary);
064:
065: long addressId = counterLocalService.increment();
066:
067: Address address = addressPersistence.create(addressId);
068:
069: address.setCompanyId(user.getCompanyId());
070: address.setUserId(user.getUserId());
071: address.setUserName(user.getFullName());
072: address.setCreateDate(now);
073: address.setModifiedDate(now);
074: address.setClassNameId(classNameId);
075: address.setClassPK(classPK);
076: address.setStreet1(street1);
077: address.setStreet2(street2);
078: address.setStreet3(street3);
079: address.setCity(city);
080: address.setZip(zip);
081: address.setRegionId(regionId);
082: address.setCountryId(countryId);
083: address.setTypeId(typeId);
084: address.setMailing(mailing);
085: address.setPrimary(primary);
086:
087: addressPersistence.update(address);
088:
089: return address;
090: }
091:
092: public void deleteAddress(long addressId) throws PortalException,
093: SystemException {
094:
095: addressPersistence.remove(addressId);
096: }
097:
098: public void deleteAddresses(long companyId, String className,
099: long classPK) throws SystemException {
100:
101: long classNameId = PortalUtil.getClassNameId(className);
102:
103: addressPersistence.removeByC_C_C(companyId, classNameId,
104: classPK);
105: }
106:
107: public Address getAddress(long addressId) throws PortalException,
108: SystemException {
109:
110: return addressPersistence.findByPrimaryKey(addressId);
111: }
112:
113: public List getAddresses() throws SystemException {
114: return addressPersistence.findAll();
115: }
116:
117: public List getAddresses(long companyId, String className,
118: long classPK) throws SystemException {
119:
120: long classNameId = PortalUtil.getClassNameId(className);
121:
122: return addressPersistence.findByC_C_C(companyId, classNameId,
123: classPK);
124: }
125:
126: public Address updateAddress(long addressId, String street1,
127: String street2, String street3, String city, String zip,
128: long regionId, long countryId, int typeId, boolean mailing,
129: boolean primary) throws PortalException, SystemException {
130:
131: validate(addressId, 0, 0, 0, street1, city, zip, regionId,
132: countryId, typeId, mailing, primary);
133:
134: Address address = addressPersistence
135: .findByPrimaryKey(addressId);
136:
137: address.setModifiedDate(new Date());
138: address.setStreet1(street1);
139: address.setStreet2(street2);
140: address.setStreet3(street3);
141: address.setCity(city);
142: address.setZip(zip);
143: address.setRegionId(regionId);
144: address.setCountryId(countryId);
145: address.setTypeId(typeId);
146: address.setMailing(mailing);
147: address.setPrimary(primary);
148:
149: addressPersistence.update(address);
150:
151: return address;
152: }
153:
154: protected void validate(long addressId, long companyId,
155: long classNameId, long classPK, String street1,
156: String city, String zip, long regionId, long countryId,
157: int typeId, boolean mailing, boolean primary)
158: throws PortalException, SystemException {
159:
160: if (Validator.isNull(street1)) {
161: throw new AddressStreetException();
162: } else if (Validator.isNull(city)) {
163: throw new AddressCityException();
164: } else if (Validator.isNull(zip)) {
165: throw new AddressZipException();
166: }
167:
168: // Relax region and country requirement. See LEP-978.
169:
170: /*try {
171: RegionServiceUtil.getRegion(regionId);
172:
173: CountryServiceUtil.getCountry(countryId);
174: }
175: catch (RemoteException re) {
176: throw new SystemException(re);
177: }*/
178:
179: if (addressId > 0) {
180: Address address = addressPersistence
181: .findByPrimaryKey(addressId);
182:
183: companyId = address.getCompanyId();
184: classNameId = address.getClassNameId();
185: classPK = address.getClassPK();
186: }
187:
188: try {
189: listTypeService.validate(typeId, classNameId,
190: ListTypeImpl.ADDRESS);
191: } catch (RemoteException re) {
192: throw new SystemException(re);
193: }
194:
195: validate(addressId, companyId, classNameId, classPK, mailing,
196: primary);
197: }
198:
199: protected void validate(long addressId, long companyId,
200: long classNameId, long classPK, boolean mailing,
201: boolean primary) throws PortalException, SystemException {
202:
203: // Check to make sure there isn't another address with the same company
204: // id, class name, and class pk that also has mailing set to true
205:
206: if (mailing) {
207: Iterator itr = addressPersistence.findByC_C_C_M(companyId,
208: classNameId, classPK, mailing).iterator();
209:
210: while (itr.hasNext()) {
211: Address address = (Address) itr.next();
212:
213: if ((addressId <= 0)
214: || (address.getAddressId() != addressId)) {
215:
216: address.setMailing(false);
217:
218: addressPersistence.update(address);
219: }
220: }
221: }
222:
223: // Check to make sure there isn't another address with the same company
224: // id, class name, and class pk that also has primary set to true
225:
226: if (primary) {
227: Iterator itr = addressPersistence.findByC_C_C_P(companyId,
228: classNameId, classPK, primary).iterator();
229:
230: while (itr.hasNext()) {
231: Address address = (Address) itr.next();
232:
233: if ((addressId <= 0)
234: || (address.getAddressId() != addressId)) {
235:
236: address.setPrimary(false);
237:
238: addressPersistence.update(address);
239: }
240: }
241: }
242: }
243:
244: }
|