1   /**
2    * Copyright (c) 2000-2009 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.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.WebsiteURLException;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.model.Website;
31  import com.liferay.portal.model.impl.ListTypeImpl;
32  import com.liferay.portal.service.base.WebsiteLocalServiceBaseImpl;
33  import com.liferay.portal.util.PortalUtil;
34  
35  import java.net.MalformedURLException;
36  import java.net.URL;
37  
38  import java.util.Date;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="WebsiteLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class WebsiteLocalServiceImpl extends WebsiteLocalServiceBaseImpl {
49  
50      public Website addWebsite(
51              long userId, String className, long classPK, String url, int typeId,
52              boolean primary)
53          throws PortalException, SystemException {
54  
55          User user = userPersistence.findByPrimaryKey(userId);
56          long classNameId = PortalUtil.getClassNameId(className);
57          Date now = new Date();
58  
59          validate(
60              0, user.getCompanyId(), classNameId, classPK, url, typeId,
61              primary);
62  
63          long websiteId = counterLocalService.increment();
64  
65          Website website = websitePersistence.create(websiteId);
66  
67          website.setCompanyId(user.getCompanyId());
68          website.setUserId(user.getUserId());
69          website.setUserName(user.getFullName());
70          website.setCreateDate(now);
71          website.setModifiedDate(now);
72          website.setClassNameId(classNameId);
73          website.setClassPK(classPK);
74          website.setUrl(url);
75          website.setTypeId(typeId);
76          website.setPrimary(primary);
77  
78          websitePersistence.update(website, false);
79  
80          return website;
81      }
82  
83      public void deleteWebsite(long websiteId)
84          throws PortalException, SystemException {
85  
86          websitePersistence.remove(websiteId);
87      }
88  
89      public void deleteWebsites(long companyId, String className, long classPK)
90          throws SystemException {
91  
92          long classNameId = PortalUtil.getClassNameId(className);
93  
94          websitePersistence.removeByC_C_C(companyId, classNameId, classPK);
95      }
96  
97      public Website getWebsite(long websiteId)
98          throws PortalException, SystemException {
99  
100         return websitePersistence.findByPrimaryKey(websiteId);
101     }
102 
103     public List<Website> getWebsites() throws SystemException {
104         return websitePersistence.findAll();
105     }
106 
107     public List<Website> getWebsites(
108             long companyId, String className, long classPK)
109         throws SystemException {
110 
111         long classNameId = PortalUtil.getClassNameId(className);
112 
113         return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
114     }
115 
116     public Website updateWebsite(
117             long websiteId, String url, int typeId, boolean primary)
118         throws PortalException, SystemException {
119 
120         validate(websiteId, 0, 0, 0, url, typeId, primary);
121 
122         Website website = websitePersistence.findByPrimaryKey(websiteId);
123 
124         website.setModifiedDate(new Date());
125         website.setUrl(url);
126         website.setTypeId(typeId);
127         website.setPrimary(primary);
128 
129         websitePersistence.update(website, false);
130 
131         return website;
132     }
133 
134     protected void validate(
135             long websiteId, long companyId, long classNameId, long classPK,
136             String url, int typeId, boolean primary)
137         throws PortalException, SystemException {
138 
139         if (Validator.isNull(url)) {
140             throw new WebsiteURLException();
141         }
142         else {
143             try {
144                 new URL(url);
145             }
146             catch (MalformedURLException murle) {
147                 throw new WebsiteURLException();
148             }
149         }
150 
151         if (websiteId > 0) {
152             Website website = websitePersistence.findByPrimaryKey(websiteId);
153 
154             companyId = website.getCompanyId();
155             classNameId = website.getClassNameId();
156             classPK = website.getClassPK();
157         }
158 
159         listTypeService.validate(typeId, classNameId, ListTypeImpl.WEBSITE);
160 
161         validate(websiteId, companyId, classNameId, classPK, primary);
162     }
163 
164     protected void validate(
165             long websiteId, long companyId, long classNameId, long classPK,
166             boolean primary)
167         throws SystemException {
168 
169         // Check to make sure there isn't another website with the same company
170         // id, class name, and class pk that also has primary set to true
171 
172         if (primary) {
173             Iterator<Website> itr = websitePersistence.findByC_C_C_P(
174                 companyId, classNameId, classPK, primary).iterator();
175 
176             while (itr.hasNext()) {
177                 Website website = itr.next();
178 
179                 if ((websiteId <= 0) ||
180                     (website.getWebsiteId() != websiteId)) {
181 
182                     website.setPrimary(false);
183 
184                     websitePersistence.update(website, false);
185                 }
186             }
187         }
188     }
189 
190 }