001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.systemevent.SystemEvent;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.ListTypeConstants;
023    import com.liferay.portal.model.SystemEventConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.model.Website;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.base.WebsiteLocalServiceBaseImpl;
028    import com.liferay.portal.util.PortalUtil;
029    
030    import java.util.Date;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class WebsiteLocalServiceImpl extends WebsiteLocalServiceBaseImpl {
037    
038            /**
039             * @deprecated As of 6.2.0, replaced by {@link #addWebsite(long, String,
040             *             long, String, int, boolean, ServiceContext)}
041             */
042            @Override
043            public Website addWebsite(
044                            long userId, String className, long classPK, String url, int typeId,
045                            boolean primary)
046                    throws PortalException, SystemException {
047    
048                    return addWebsite(
049                            userId, className, classPK, url, typeId, primary,
050                            new ServiceContext());
051            }
052    
053            @Override
054            public Website addWebsite(
055                            long userId, String className, long classPK, String url, int typeId,
056                            boolean primary, ServiceContext serviceContext)
057                    throws PortalException, SystemException {
058    
059                    User user = userPersistence.findByPrimaryKey(userId);
060                    long classNameId = PortalUtil.getClassNameId(className);
061                    Date now = new Date();
062    
063                    validate(
064                            0, user.getCompanyId(), classNameId, classPK, url, typeId, primary);
065    
066                    long websiteId = counterLocalService.increment();
067    
068                    Website website = websitePersistence.create(websiteId);
069    
070                    website.setUuid(serviceContext.getUuid());
071                    website.setCompanyId(user.getCompanyId());
072                    website.setUserId(user.getUserId());
073                    website.setUserName(user.getFullName());
074                    website.setCreateDate(now);
075                    website.setCreateDate(serviceContext.getCreateDate(now));
076                    website.setModifiedDate(serviceContext.getModifiedDate(now));
077                    website.setClassNameId(classNameId);
078                    website.setClassPK(classPK);
079                    website.setUrl(url);
080                    website.setTypeId(typeId);
081                    website.setPrimary(primary);
082    
083                    websitePersistence.update(website);
084    
085                    return website;
086            }
087    
088            @Override
089            public Website deleteWebsite(long websiteId)
090                    throws PortalException, SystemException {
091    
092                    Website website = websitePersistence.findByPrimaryKey(websiteId);
093    
094                    return websiteLocalService.deleteWebsite(website);
095            }
096    
097            @Override
098            @SystemEvent(
099                    action = SystemEventConstants.ACTION_SKIP,
100                    type = SystemEventConstants.TYPE_DELETE)
101            public Website deleteWebsite(Website website) throws SystemException {
102                    websitePersistence.remove(website);
103    
104                    return website;
105            }
106    
107            @Override
108            public void deleteWebsites(long companyId, String className, long classPK)
109                    throws SystemException {
110    
111                    long classNameId = PortalUtil.getClassNameId(className);
112    
113                    List<Website> websites = websitePersistence.findByC_C_C(
114                            companyId, classNameId, classPK);
115    
116                    for (Website website : websites) {
117                            websiteLocalService.deleteWebsite(website);
118                    }
119            }
120    
121            @Override
122            public List<Website> getWebsites() throws SystemException {
123                    return websitePersistence.findAll();
124            }
125    
126            @Override
127            public List<Website> getWebsites(
128                            long companyId, String className, long classPK)
129                    throws SystemException {
130    
131                    long classNameId = PortalUtil.getClassNameId(className);
132    
133                    return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
134            }
135    
136            @Override
137            public Website updateWebsite(
138                            long websiteId, String url, int typeId, boolean primary)
139                    throws PortalException, SystemException {
140    
141                    validate(websiteId, 0, 0, 0, url, typeId, primary);
142    
143                    Website website = websitePersistence.findByPrimaryKey(websiteId);
144    
145                    website.setModifiedDate(new Date());
146                    website.setUrl(url);
147                    website.setTypeId(typeId);
148                    website.setPrimary(primary);
149    
150                    websitePersistence.update(website);
151    
152                    return website;
153            }
154    
155            protected void validate(
156                            long websiteId, long companyId, long classNameId, long classPK,
157                            boolean primary)
158                    throws SystemException {
159    
160                    // Check to make sure there isn't another website with the same company
161                    // id, class name, and class pk that also has primary set to true
162    
163                    if (primary) {
164                            List<Website> websites = websitePersistence.findByC_C_C_P(
165                                    companyId, classNameId, classPK, primary);
166    
167                            for (Website website : websites) {
168                                    if ((websiteId <= 0) || (website.getWebsiteId() != websiteId)) {
169                                            website.setPrimary(false);
170    
171                                            websitePersistence.update(website);
172                                    }
173                            }
174                    }
175            }
176    
177            protected void validate(
178                            long websiteId, long companyId, long classNameId, long classPK,
179                            String url, int typeId, boolean primary)
180                    throws PortalException, SystemException {
181    
182                    if (!Validator.isUrl(url)) {
183                            throw new WebsiteURLException();
184                    }
185    
186                    if (websiteId > 0) {
187                            Website website = websitePersistence.findByPrimaryKey(websiteId);
188    
189                            companyId = website.getCompanyId();
190                            classNameId = website.getClassNameId();
191                            classPK = website.getClassPK();
192                    }
193    
194                    listTypeService.validate(
195                            typeId, classNameId, ListTypeConstants.WEBSITE);
196    
197                    validate(websiteId, companyId, classNameId, classPK, primary);
198            }
199    
200    }