1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.util.Date;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="WebsiteLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
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         listTypeService.validate(typeId, classNameId, ListTypeImpl.WEBSITE);
159 
160         validate(websiteId, companyId, classNameId, classPK, primary);
161     }
162 
163     protected void validate(
164             long websiteId, long companyId, long classNameId, long classPK,
165             boolean primary)
166         throws SystemException {
167 
168         // Check to make sure there isn't another website with the same company
169         // id, class name, and class pk that also has primary set to true
170 
171         if (primary) {
172             Iterator<Website> itr = websitePersistence.findByC_C_C_P(
173                 companyId, classNameId, classPK, primary).iterator();
174 
175             while (itr.hasNext()) {
176                 Website website = itr.next();
177 
178                 if ((websiteId <= 0) ||
179                     (website.getWebsiteId() != websiteId)) {
180 
181                     website.setPrimary(false);
182 
183                     websitePersistence.update(website, false);
184                 }
185             }
186         }
187     }
188 
189 }