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