001    /**
002     * Copyright (c) 2000-present 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.systemevent.SystemEvent;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.ListTypeConstants;
022    import com.liferay.portal.model.SystemEventConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.model.Website;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.base.WebsiteLocalServiceBaseImpl;
027    
028    import java.util.Date;
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class WebsiteLocalServiceImpl extends WebsiteLocalServiceBaseImpl {
035    
036            /**
037             * @deprecated As of 6.2.0, replaced by {@link #addWebsite(long, String,
038             *             long, String, int, boolean, ServiceContext)}
039             */
040            @Deprecated
041            @Override
042            public Website addWebsite(
043                            long userId, String className, long classPK, String url, int typeId,
044                            boolean primary)
045                    throws PortalException {
046    
047                    return addWebsite(
048                            userId, className, classPK, url, typeId, primary,
049                            new ServiceContext());
050            }
051    
052            @Override
053            public Website addWebsite(
054                            long userId, String className, long classPK, String url, int typeId,
055                            boolean primary, ServiceContext serviceContext)
056                    throws PortalException {
057    
058                    User user = userPersistence.findByPrimaryKey(userId);
059                    long classNameId = classNameLocalService.getClassNameId(className);
060                    Date now = new Date();
061    
062                    validate(
063                            0, user.getCompanyId(), classNameId, classPK, url, typeId, primary);
064    
065                    long websiteId = counterLocalService.increment();
066    
067                    Website website = websitePersistence.create(websiteId);
068    
069                    website.setUuid(serviceContext.getUuid());
070                    website.setCompanyId(user.getCompanyId());
071                    website.setUserId(user.getUserId());
072                    website.setUserName(user.getFullName());
073                    website.setCreateDate(now);
074                    website.setCreateDate(serviceContext.getCreateDate(now));
075                    website.setModifiedDate(serviceContext.getModifiedDate(now));
076                    website.setClassNameId(classNameId);
077                    website.setClassPK(classPK);
078                    website.setUrl(url);
079                    website.setTypeId(typeId);
080                    website.setPrimary(primary);
081    
082                    websitePersistence.update(website);
083    
084                    return website;
085            }
086    
087            @Override
088            public Website deleteWebsite(long websiteId) throws PortalException {
089                    Website website = websitePersistence.findByPrimaryKey(websiteId);
090    
091                    return websiteLocalService.deleteWebsite(website);
092            }
093    
094            @Override
095            @SystemEvent(
096                    action = SystemEventConstants.ACTION_SKIP,
097                    type = SystemEventConstants.TYPE_DELETE)
098            public Website deleteWebsite(Website website) {
099                    websitePersistence.remove(website);
100    
101                    return website;
102            }
103    
104            @Override
105            public void deleteWebsites(long companyId, String className, long classPK) {
106                    long classNameId = classNameLocalService.getClassNameId(className);
107    
108                    List<Website> websites = websitePersistence.findByC_C_C(
109                            companyId, classNameId, classPK);
110    
111                    for (Website website : websites) {
112                            websiteLocalService.deleteWebsite(website);
113                    }
114            }
115    
116            @Override
117            public List<Website> getWebsites() {
118                    return websitePersistence.findAll();
119            }
120    
121            @Override
122            public List<Website> getWebsites(
123                    long companyId, String className, long classPK) {
124    
125                    long classNameId = classNameLocalService.getClassNameId(className);
126    
127                    return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
128            }
129    
130            @Override
131            public Website updateWebsite(
132                            long websiteId, String url, int typeId, boolean primary)
133                    throws PortalException {
134    
135                    validate(websiteId, 0, 0, 0, url, typeId, primary);
136    
137                    Website website = websitePersistence.findByPrimaryKey(websiteId);
138    
139                    website.setModifiedDate(new Date());
140                    website.setUrl(url);
141                    website.setTypeId(typeId);
142                    website.setPrimary(primary);
143    
144                    websitePersistence.update(website);
145    
146                    return website;
147            }
148    
149            protected void validate(
150                    long websiteId, long companyId, long classNameId, long classPK,
151                    boolean primary) {
152    
153                    // Check to make sure there isn't another website with the same company
154                    // id, class name, and class pk that also has primary set to true
155    
156                    if (primary) {
157                            List<Website> websites = websitePersistence.findByC_C_C_P(
158                                    companyId, classNameId, classPK, primary);
159    
160                            for (Website website : websites) {
161                                    if ((websiteId <= 0) || (website.getWebsiteId() != websiteId)) {
162                                            website.setPrimary(false);
163    
164                                            websitePersistence.update(website);
165                                    }
166                            }
167                    }
168            }
169    
170            protected void validate(
171                            long websiteId, long companyId, long classNameId, long classPK,
172                            String url, int typeId, boolean primary)
173                    throws PortalException {
174    
175                    if (!Validator.isUrl(url)) {
176                            throw new WebsiteURLException();
177                    }
178    
179                    if (websiteId > 0) {
180                            Website website = websitePersistence.findByPrimaryKey(websiteId);
181    
182                            companyId = website.getCompanyId();
183                            classNameId = website.getClassNameId();
184                            classPK = website.getClassPK();
185                    }
186    
187                    listTypeService.validate(
188                            typeId, classNameId, ListTypeConstants.WEBSITE);
189    
190                    validate(websiteId, companyId, classNameId, classPK, primary);
191            }
192    
193    }