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.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  public class WebsiteLocalServiceImpl extends WebsiteLocalServiceBaseImpl {
50  
51      public Website addWebsite(
52              long userId, String className, long classPK, String url, int typeId,
53              boolean primary)
54          throws PortalException, SystemException {
55  
56          User user = userPersistence.findByPrimaryKey(userId);
57          long classNameId = PortalUtil.getClassNameId(className);
58          Date now = new Date();
59  
60          validate(
61              0, user.getCompanyId(), classNameId, classPK, url, typeId,
62              primary);
63  
64          long websiteId = counterLocalService.increment();
65  
66          Website website = websitePersistence.create(websiteId);
67  
68          website.setCompanyId(user.getCompanyId());
69          website.setUserId(user.getUserId());
70          website.setUserName(user.getFullName());
71          website.setCreateDate(now);
72          website.setModifiedDate(now);
73          website.setClassNameId(classNameId);
74          website.setClassPK(classPK);
75          website.setUrl(url);
76          website.setTypeId(typeId);
77          website.setPrimary(primary);
78  
79          websitePersistence.update(website, false);
80  
81          return website;
82      }
83  
84      public void deleteWebsite(long websiteId)
85          throws PortalException, SystemException {
86  
87          websitePersistence.remove(websiteId);
88      }
89  
90      public void deleteWebsites(long companyId, String className, long classPK)
91          throws SystemException {
92  
93          long classNameId = PortalUtil.getClassNameId(className);
94  
95          websitePersistence.removeByC_C_C(companyId, classNameId, classPK);
96      }
97  
98      public Website getWebsite(long websiteId)
99          throws PortalException, SystemException {
100 
101         return websitePersistence.findByPrimaryKey(websiteId);
102     }
103 
104     public List<Website> getWebsites() throws SystemException {
105         return websitePersistence.findAll();
106     }
107 
108     public List<Website> getWebsites(
109             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, false);
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 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<Website> itr = websitePersistence.findByC_C_C_P(
180                 companyId, classNameId, classPK, primary).iterator();
181 
182             while (itr.hasNext()) {
183                 Website website = itr.next();
184 
185                 if ((websiteId <= 0) ||
186                     (website.getWebsiteId() != websiteId)) {
187 
188                     website.setPrimary(false);
189 
190                     websitePersistence.update(website, false);
191                 }
192             }
193         }
194     }
195 
196 }