1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.WebsiteURLException;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.model.ListTypeConstants;
22  import com.liferay.portal.model.User;
23  import com.liferay.portal.model.Website;
24  import com.liferay.portal.service.base.WebsiteLocalServiceBaseImpl;
25  import com.liferay.portal.util.PortalUtil;
26  
27  import java.util.Date;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /**
32   * <a href="WebsiteLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class WebsiteLocalServiceImpl extends WebsiteLocalServiceBaseImpl {
37  
38      public Website addWebsite(
39              long userId, String className, long classPK, String url, int typeId,
40              boolean primary)
41          throws PortalException, SystemException {
42  
43          User user = userPersistence.findByPrimaryKey(userId);
44          long classNameId = PortalUtil.getClassNameId(className);
45          Date now = new Date();
46  
47          validate(
48              0, user.getCompanyId(), classNameId, classPK, url, typeId,
49              primary);
50  
51          long websiteId = counterLocalService.increment();
52  
53          Website website = websitePersistence.create(websiteId);
54  
55          website.setCompanyId(user.getCompanyId());
56          website.setUserId(user.getUserId());
57          website.setUserName(user.getFullName());
58          website.setCreateDate(now);
59          website.setModifiedDate(now);
60          website.setClassNameId(classNameId);
61          website.setClassPK(classPK);
62          website.setUrl(url);
63          website.setTypeId(typeId);
64          website.setPrimary(primary);
65  
66          websitePersistence.update(website, false);
67  
68          return website;
69      }
70  
71      public void deleteWebsite(long websiteId)
72          throws PortalException, SystemException {
73  
74          websitePersistence.remove(websiteId);
75      }
76  
77      public void deleteWebsites(long companyId, String className, long classPK)
78          throws SystemException {
79  
80          long classNameId = PortalUtil.getClassNameId(className);
81  
82          websitePersistence.removeByC_C_C(companyId, classNameId, classPK);
83      }
84  
85      public Website getWebsite(long websiteId)
86          throws PortalException, SystemException {
87  
88          return websitePersistence.findByPrimaryKey(websiteId);
89      }
90  
91      public List<Website> getWebsites() throws SystemException {
92          return websitePersistence.findAll();
93      }
94  
95      public List<Website> getWebsites(
96              long companyId, String className, long classPK)
97          throws SystemException {
98  
99          long classNameId = PortalUtil.getClassNameId(className);
100 
101         return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
102     }
103 
104     public Website updateWebsite(
105             long websiteId, String url, int typeId, boolean primary)
106         throws PortalException, SystemException {
107 
108         validate(websiteId, 0, 0, 0, url, typeId, primary);
109 
110         Website website = websitePersistence.findByPrimaryKey(websiteId);
111 
112         website.setModifiedDate(new Date());
113         website.setUrl(url);
114         website.setTypeId(typeId);
115         website.setPrimary(primary);
116 
117         websitePersistence.update(website, false);
118 
119         return website;
120     }
121 
122     protected void validate(
123             long websiteId, long companyId, long classNameId, long classPK,
124             String url, int typeId, boolean primary)
125         throws PortalException, SystemException {
126 
127         if (!Validator.isUrl(url)) {
128             throw new WebsiteURLException();
129         }
130 
131         if (websiteId > 0) {
132             Website website = websitePersistence.findByPrimaryKey(websiteId);
133 
134             companyId = website.getCompanyId();
135             classNameId = website.getClassNameId();
136             classPK = website.getClassPK();
137         }
138 
139         listTypeService.validate(
140             typeId, classNameId, ListTypeConstants.WEBSITE);
141 
142         validate(websiteId, companyId, classNameId, classPK, primary);
143     }
144 
145     protected void validate(
146             long websiteId, long companyId, long classNameId, long classPK,
147             boolean primary)
148         throws SystemException {
149 
150         // Check to make sure there isn't another website with the same company
151         // id, class name, and class pk that also has primary set to true
152 
153         if (primary) {
154             Iterator<Website> itr = websitePersistence.findByC_C_C_P(
155                 companyId, classNameId, classPK, primary).iterator();
156 
157             while (itr.hasNext()) {
158                 Website website = itr.next();
159 
160                 if ((websiteId <= 0) ||
161                     (website.getWebsiteId() != websiteId)) {
162 
163                     website.setPrimary(false);
164 
165                     websitePersistence.update(website, false);
166                 }
167             }
168         }
169     }
170 
171 }