1
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.net.MalformedURLException;
28 import java.net.URL;
29
30 import java.util.Date;
31 import java.util.Iterator;
32 import java.util.List;
33
34
39 public class WebsiteLocalServiceImpl extends WebsiteLocalServiceBaseImpl {
40
41 public Website addWebsite(
42 long userId, String className, long classPK, String url, int typeId,
43 boolean primary)
44 throws PortalException, SystemException {
45
46 User user = userPersistence.findByPrimaryKey(userId);
47 long classNameId = PortalUtil.getClassNameId(className);
48 Date now = new Date();
49
50 validate(
51 0, user.getCompanyId(), classNameId, classPK, url, typeId,
52 primary);
53
54 long websiteId = counterLocalService.increment();
55
56 Website website = websitePersistence.create(websiteId);
57
58 website.setCompanyId(user.getCompanyId());
59 website.setUserId(user.getUserId());
60 website.setUserName(user.getFullName());
61 website.setCreateDate(now);
62 website.setModifiedDate(now);
63 website.setClassNameId(classNameId);
64 website.setClassPK(classPK);
65 website.setUrl(url);
66 website.setTypeId(typeId);
67 website.setPrimary(primary);
68
69 websitePersistence.update(website, false);
70
71 return website;
72 }
73
74 public void deleteWebsite(long websiteId)
75 throws PortalException, SystemException {
76
77 websitePersistence.remove(websiteId);
78 }
79
80 public void deleteWebsites(long companyId, String className, long classPK)
81 throws SystemException {
82
83 long classNameId = PortalUtil.getClassNameId(className);
84
85 websitePersistence.removeByC_C_C(companyId, classNameId, classPK);
86 }
87
88 public Website getWebsite(long websiteId)
89 throws PortalException, SystemException {
90
91 return websitePersistence.findByPrimaryKey(websiteId);
92 }
93
94 public List<Website> getWebsites() throws SystemException {
95 return websitePersistence.findAll();
96 }
97
98 public List<Website> getWebsites(
99 long companyId, String className, long classPK)
100 throws SystemException {
101
102 long classNameId = PortalUtil.getClassNameId(className);
103
104 return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
105 }
106
107 public Website updateWebsite(
108 long websiteId, String url, int typeId, boolean primary)
109 throws PortalException, SystemException {
110
111 validate(websiteId, 0, 0, 0, url, typeId, primary);
112
113 Website website = websitePersistence.findByPrimaryKey(websiteId);
114
115 website.setModifiedDate(new Date());
116 website.setUrl(url);
117 website.setTypeId(typeId);
118 website.setPrimary(primary);
119
120 websitePersistence.update(website, false);
121
122 return website;
123 }
124
125 protected void validate(
126 long websiteId, long companyId, long classNameId, long classPK,
127 String url, int typeId, boolean primary)
128 throws PortalException, SystemException {
129
130 if (Validator.isNull(url)) {
131 throw new WebsiteURLException();
132 }
133 else {
134 try {
135 new URL(url);
136 }
137 catch (MalformedURLException murle) {
138 throw new WebsiteURLException();
139 }
140 }
141
142 if (websiteId > 0) {
143 Website website = websitePersistence.findByPrimaryKey(websiteId);
144
145 companyId = website.getCompanyId();
146 classNameId = website.getClassNameId();
147 classPK = website.getClassPK();
148 }
149
150 listTypeService.validate(
151 typeId, classNameId, ListTypeConstants.WEBSITE);
152
153 validate(websiteId, companyId, classNameId, classPK, primary);
154 }
155
156 protected void validate(
157 long websiteId, long companyId, long classNameId, long classPK,
158 boolean primary)
159 throws SystemException {
160
161
164 if (primary) {
165 Iterator<Website> itr = websitePersistence.findByC_C_C_P(
166 companyId, classNameId, classPK, primary).iterator();
167
168 while (itr.hasNext()) {
169 Website website = itr.next();
170
171 if ((websiteId <= 0) ||
172 (website.getWebsiteId() != websiteId)) {
173
174 website.setPrimary(false);
175
176 websitePersistence.update(website, false);
177 }
178 }
179 }
180 }
181
182 }