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.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.WebsiteURLException;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.model.User;
028: import com.liferay.portal.model.Website;
029: import com.liferay.portal.model.impl.ListTypeImpl;
030: import com.liferay.portal.service.base.WebsiteLocalServiceBaseImpl;
031: import com.liferay.portal.util.PortalUtil;
032:
033: import java.net.MalformedURLException;
034: import java.net.URL;
035:
036: import java.rmi.RemoteException;
037:
038: import java.util.Date;
039: import java.util.Iterator;
040: import java.util.List;
041:
042: /**
043: * <a href="WebsiteLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Brian Wing Shun Chan
046: *
047: */
048: public class WebsiteLocalServiceImpl extends
049: WebsiteLocalServiceBaseImpl {
050:
051: public Website addWebsite(long userId, String className,
052: long classPK, String url, int typeId, boolean primary)
053: throws PortalException, SystemException {
054:
055: User user = userPersistence.findByPrimaryKey(userId);
056: long classNameId = PortalUtil.getClassNameId(className);
057: Date now = new Date();
058:
059: validate(0, user.getCompanyId(), classNameId, classPK, url,
060: typeId, primary);
061:
062: long websiteId = counterLocalService.increment();
063:
064: Website website = websitePersistence.create(websiteId);
065:
066: website.setCompanyId(user.getCompanyId());
067: website.setUserId(user.getUserId());
068: website.setUserName(user.getFullName());
069: website.setCreateDate(now);
070: website.setModifiedDate(now);
071: website.setClassNameId(classNameId);
072: website.setClassPK(classPK);
073: website.setUrl(url);
074: website.setTypeId(typeId);
075: website.setPrimary(primary);
076:
077: websitePersistence.update(website);
078:
079: return website;
080: }
081:
082: public void deleteWebsite(long websiteId) throws PortalException,
083: SystemException {
084:
085: websitePersistence.remove(websiteId);
086: }
087:
088: public void deleteWebsites(long companyId, String className,
089: long classPK) throws SystemException {
090:
091: long classNameId = PortalUtil.getClassNameId(className);
092:
093: websitePersistence.removeByC_C_C(companyId, classNameId,
094: classPK);
095: }
096:
097: public Website getWebsite(long websiteId) throws PortalException,
098: SystemException {
099:
100: return websitePersistence.findByPrimaryKey(websiteId);
101: }
102:
103: public List getWebsites() throws SystemException {
104: return websitePersistence.findAll();
105: }
106:
107: public List getWebsites(long companyId, String className,
108: long classPK) throws SystemException {
109:
110: long classNameId = PortalUtil.getClassNameId(className);
111:
112: return websitePersistence.findByC_C_C(companyId, classNameId,
113: classPK);
114: }
115:
116: public Website updateWebsite(long websiteId, String url,
117: int typeId, boolean primary) throws PortalException,
118: SystemException {
119:
120: validate(websiteId, 0, 0, 0, url, typeId, primary);
121:
122: Website website = websitePersistence
123: .findByPrimaryKey(websiteId);
124:
125: website.setModifiedDate(new Date());
126: website.setUrl(url);
127: website.setTypeId(typeId);
128: website.setPrimary(primary);
129:
130: websitePersistence.update(website);
131:
132: return website;
133: }
134:
135: protected void validate(long websiteId, long companyId,
136: long classNameId, long classPK, String url, int typeId,
137: boolean primary) throws PortalException, SystemException {
138:
139: if (Validator.isNull(url)) {
140: throw new WebsiteURLException();
141: } else {
142: try {
143: new URL(url);
144: } catch (MalformedURLException murle) {
145: throw new WebsiteURLException();
146: }
147: }
148:
149: if (websiteId > 0) {
150: Website website = websitePersistence
151: .findByPrimaryKey(websiteId);
152:
153: companyId = website.getCompanyId();
154: classNameId = website.getClassNameId();
155: classPK = website.getClassPK();
156: }
157:
158: try {
159: listTypeService.validate(typeId, classNameId,
160: ListTypeImpl.WEBSITE);
161: } catch (RemoteException re) {
162: throw new SystemException(re);
163: }
164:
165: validate(websiteId, companyId, classNameId, classPK, primary);
166: }
167:
168: protected void validate(long websiteId, long companyId,
169: long classNameId, long classPK, boolean primary)
170: throws PortalException, SystemException {
171:
172: // Check to make sure there isn't another website with the same company
173: // id, class name, and class pk that also has primary set to true
174:
175: if (primary) {
176: Iterator itr = websitePersistence.findByC_C_C_P(companyId,
177: classNameId, classPK, primary).iterator();
178:
179: while (itr.hasNext()) {
180: Website website = (Website) itr.next();
181:
182: if ((websiteId <= 0)
183: || (website.getWebsiteId() != websiteId)) {
184:
185: website.setPrimary(false);
186:
187: websitePersistence.update(website);
188: }
189: }
190: }
191: }
192:
193: }
|