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