1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.WebsiteURLException;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.model.Website;
32  import com.liferay.portal.model.impl.ListTypeImpl;
33  import com.liferay.portal.service.ListTypeServiceUtil;
34  import com.liferay.portal.service.base.WebsiteLocalServiceBaseImpl;
35  import com.liferay.portal.service.persistence.UserUtil;
36  import com.liferay.portal.service.persistence.WebsiteUtil;
37  import com.liferay.portal.util.PortalUtil;
38  
39  import java.net.MalformedURLException;
40  import java.net.URL;
41  
42  import java.rmi.RemoteException;
43  
44  import java.util.Date;
45  import java.util.Iterator;
46  import java.util.List;
47  
48  /**
49   * <a href="WebsiteLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class WebsiteLocalServiceImpl extends WebsiteLocalServiceBaseImpl {
55  
56      public Website addWebsite(
57              long userId, String className, long classPK, String url, int typeId,
58              boolean primary)
59          throws PortalException, SystemException {
60  
61          User user = UserUtil.findByPrimaryKey(userId);
62          long classNameId = PortalUtil.getClassNameId(className);
63          Date now = new Date();
64  
65          validate(
66              0, user.getCompanyId(), classNameId, classPK, url, typeId,
67              primary);
68  
69          long websiteId = CounterLocalServiceUtil.increment();
70  
71          Website website = WebsiteUtil.create(websiteId);
72  
73          website.setCompanyId(user.getCompanyId());
74          website.setUserId(user.getUserId());
75          website.setUserName(user.getFullName());
76          website.setCreateDate(now);
77          website.setModifiedDate(now);
78          website.setClassNameId(classNameId);
79          website.setClassPK(classPK);
80          website.setUrl(url);
81          website.setTypeId(typeId);
82          website.setPrimary(primary);
83  
84          WebsiteUtil.update(website);
85  
86          return website;
87      }
88  
89      public void deleteWebsite(long websiteId)
90          throws PortalException, SystemException {
91  
92          WebsiteUtil.remove(websiteId);
93      }
94  
95      public void deleteWebsites(long companyId, String className, long classPK)
96          throws SystemException {
97  
98          long classNameId = PortalUtil.getClassNameId(className);
99  
100         WebsiteUtil.removeByC_C_C(companyId, classNameId, classPK);
101     }
102 
103     public Website getWebsite(long websiteId)
104         throws PortalException, SystemException {
105 
106         return WebsiteUtil.findByPrimaryKey(websiteId);
107     }
108 
109     public List getWebsites() throws SystemException {
110         return WebsiteUtil.findAll();
111     }
112 
113     public List getWebsites(long companyId, String className, long classPK)
114         throws SystemException {
115 
116         long classNameId = PortalUtil.getClassNameId(className);
117 
118         return WebsiteUtil.findByC_C_C(companyId, classNameId, classPK);
119     }
120 
121     public Website updateWebsite(
122             long websiteId, String url, int typeId, boolean primary)
123         throws PortalException, SystemException {
124 
125         validate(websiteId, 0, 0, 0, url, typeId, primary);
126 
127         Website website = WebsiteUtil.findByPrimaryKey(websiteId);
128 
129         website.setModifiedDate(new Date());
130         website.setUrl(url);
131         website.setTypeId(typeId);
132         website.setPrimary(primary);
133 
134         WebsiteUtil.update(website);
135 
136         return website;
137     }
138 
139     protected void validate(
140             long websiteId, long companyId, long classNameId, long classPK,
141             String url, int typeId, boolean primary)
142         throws PortalException, SystemException {
143 
144         if (Validator.isNull(url)) {
145             throw new WebsiteURLException();
146         }
147         else {
148             try {
149                 new URL(url);
150             }
151             catch (MalformedURLException murle) {
152                 throw new WebsiteURLException();
153             }
154         }
155 
156         if (websiteId > 0) {
157             Website website = WebsiteUtil.findByPrimaryKey(websiteId);
158 
159             companyId = website.getCompanyId();
160             classNameId = website.getClassNameId();
161             classPK = website.getClassPK();
162         }
163 
164         try {
165             ListTypeServiceUtil.validate(
166                 typeId, classNameId, ListTypeImpl.WEBSITE);
167         }
168         catch (RemoteException re) {
169             throw new SystemException(re);
170         }
171 
172         validate(websiteId, companyId, classNameId, classPK, primary);
173     }
174 
175     protected void validate(
176             long websiteId, long companyId, long classNameId, long classPK,
177             boolean primary)
178         throws PortalException, SystemException {
179 
180         // Check to make sure there isn't another website with the same company
181         // id, class name, and class pk that also has primary set to true
182 
183         if (primary) {
184             Iterator itr = WebsiteUtil.findByC_C_C_P(
185                 companyId, classNameId, classPK, primary).iterator();
186 
187             while (itr.hasNext()) {
188                 Website website = (Website)itr.next();
189 
190                 if ((websiteId <= 0) ||
191                     (website.getWebsiteId() != websiteId)) {
192 
193                     website.setPrimary(false);
194 
195                     WebsiteUtil.update(website);
196                 }
197             }
198         }
199     }
200 
201 }