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