001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.WebsiteURLException;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.model.ListTypeConstants;
022 import com.liferay.portal.model.User;
023 import com.liferay.portal.model.Website;
024 import com.liferay.portal.service.base.WebsiteLocalServiceBaseImpl;
025 import com.liferay.portal.util.PortalUtil;
026
027 import java.util.Date;
028 import java.util.Iterator;
029 import java.util.List;
030
031
034 public class WebsiteLocalServiceImpl extends WebsiteLocalServiceBaseImpl {
035
036 @Override
037 public Website addWebsite(
038 long userId, String className, long classPK, String url, int typeId,
039 boolean primary)
040 throws PortalException, SystemException {
041
042 User user = userPersistence.findByPrimaryKey(userId);
043 long classNameId = PortalUtil.getClassNameId(className);
044 Date now = new Date();
045
046 validate(
047 0, user.getCompanyId(), classNameId, classPK, url, typeId, primary);
048
049 long websiteId = counterLocalService.increment();
050
051 Website website = websitePersistence.create(websiteId);
052
053 website.setCompanyId(user.getCompanyId());
054 website.setUserId(user.getUserId());
055 website.setUserName(user.getFullName());
056 website.setCreateDate(now);
057 website.setModifiedDate(now);
058 website.setClassNameId(classNameId);
059 website.setClassPK(classPK);
060 website.setUrl(url);
061 website.setTypeId(typeId);
062 website.setPrimary(primary);
063
064 websitePersistence.update(website, false);
065
066 return website;
067 }
068
069 @Override
070 public void deleteWebsites(long companyId, String className, long classPK)
071 throws SystemException {
072
073 long classNameId = PortalUtil.getClassNameId(className);
074
075 List<Website> websites = websitePersistence.findByC_C_C(
076 companyId, classNameId, classPK);
077
078 for (Website website : websites) {
079 deleteWebsite(website);
080 }
081 }
082
083 @Override
084 public List<Website> getWebsites() throws SystemException {
085 return websitePersistence.findAll();
086 }
087
088 @Override
089 public List<Website> getWebsites(
090 long companyId, String className, long classPK)
091 throws SystemException {
092
093 long classNameId = PortalUtil.getClassNameId(className);
094
095 return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
096 }
097
098 @Override
099 public Website updateWebsite(
100 long websiteId, String url, int typeId, boolean primary)
101 throws PortalException, SystemException {
102
103 validate(websiteId, 0, 0, 0, url, typeId, primary);
104
105 Website website = websitePersistence.findByPrimaryKey(websiteId);
106
107 website.setModifiedDate(new Date());
108 website.setUrl(url);
109 website.setTypeId(typeId);
110 website.setPrimary(primary);
111
112 websitePersistence.update(website, false);
113
114 return website;
115 }
116
117 protected void validate(
118 long websiteId, long companyId, long classNameId, long classPK,
119 boolean primary)
120 throws SystemException {
121
122
123
124
125 if (primary) {
126 Iterator<Website> itr = websitePersistence.findByC_C_C_P(
127 companyId, classNameId, classPK, primary).iterator();
128
129 while (itr.hasNext()) {
130 Website website = itr.next();
131
132 if ((websiteId <= 0) || (website.getWebsiteId() != websiteId)) {
133 website.setPrimary(false);
134
135 websitePersistence.update(website, false);
136 }
137 }
138 }
139 }
140
141 protected void validate(
142 long websiteId, long companyId, long classNameId, long classPK,
143 String url, int typeId, boolean primary)
144 throws PortalException, SystemException {
145
146 if (!Validator.isUrl(url)) {
147 throw new WebsiteURLException();
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(
159 typeId, classNameId, ListTypeConstants.WEBSITE);
160
161 validate(websiteId, companyId, classNameId, classPK, primary);
162 }
163
164 }