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