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