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);
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 getWebsites() throws SystemException {
106         return websitePersistence.findAll();
107     }
108 
109     public List getWebsites(long companyId, String className, long classPK)
110         throws SystemException {
111 
112         long classNameId = PortalUtil.getClassNameId(className);
113 
114         return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
115     }
116 
117     public Website updateWebsite(
118             long websiteId, String url, int typeId, boolean primary)
119         throws PortalException, SystemException {
120 
121         validate(websiteId, 0, 0, 0, url, typeId, primary);
122 
123         Website website = websitePersistence.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(
136             long websiteId, long companyId, long classNameId, long classPK,
137             String url, int typeId, boolean primary)
138         throws PortalException, SystemException {
139 
140         if (Validator.isNull(url)) {
141             throw new WebsiteURLException();
142         }
143         else {
144             try {
145                 new URL(url);
146             }
147             catch (MalformedURLException murle) {
148                 throw new WebsiteURLException();
149             }
150         }
151 
152         if (websiteId > 0) {
153             Website website = websitePersistence.findByPrimaryKey(websiteId);
154 
155             companyId = website.getCompanyId();
156             classNameId = website.getClassNameId();
157             classPK = website.getClassPK();
158         }
159 
160         try {
161             listTypeService.validate(typeId, classNameId, ListTypeImpl.WEBSITE);
162         }
163         catch (RemoteException re) {
164             throw new SystemException(re);
165         }
166 
167         validate(websiteId, companyId, classNameId, classPK, primary);
168     }
169 
170     protected void validate(
171             long websiteId, long companyId, long classNameId, long classPK,
172             boolean primary)
173         throws PortalException, SystemException {
174 
175         // Check to make sure there isn't another website with the same company
176         // id, class name, and class pk that also has primary set to true
177 
178         if (primary) {
179             Iterator itr = websitePersistence.findByC_C_C_P(
180                 companyId, classNameId, classPK, primary).iterator();
181 
182             while (itr.hasNext()) {
183                 Website website = (Website)itr.next();
184 
185                 if ((websiteId <= 0) ||
186                     (website.getWebsiteId() != websiteId)) {
187 
188                     website.setPrimary(false);
189 
190                     websitePersistence.update(website);
191                 }
192             }
193         }
194     }
195 
196 }