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.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.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class WebsiteLocalServiceImpl extends WebsiteLocalServiceBaseImpl {
034    
035            public Website addWebsite(
036                            long userId, String className, long classPK, String url, int typeId,
037                            boolean primary)
038                    throws PortalException, SystemException {
039    
040                    User user = userPersistence.findByPrimaryKey(userId);
041                    long classNameId = PortalUtil.getClassNameId(className);
042                    Date now = new Date();
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.setCompanyId(user.getCompanyId());
052                    website.setUserId(user.getUserId());
053                    website.setUserName(user.getFullName());
054                    website.setCreateDate(now);
055                    website.setModifiedDate(now);
056                    website.setClassNameId(classNameId);
057                    website.setClassPK(classPK);
058                    website.setUrl(url);
059                    website.setTypeId(typeId);
060                    website.setPrimary(primary);
061    
062                    websitePersistence.update(website);
063    
064                    return website;
065            }
066    
067            public void deleteWebsites(long companyId, String className, long classPK)
068                    throws SystemException {
069    
070                    long classNameId = PortalUtil.getClassNameId(className);
071    
072                    List<Website> websites = websitePersistence.findByC_C_C(
073                            companyId, classNameId, classPK);
074    
075                    for (Website website : websites) {
076                            deleteWebsite(website);
077                    }
078            }
079    
080            public List<Website> getWebsites() throws SystemException {
081                    return websitePersistence.findAll();
082            }
083    
084            public List<Website> getWebsites(
085                            long companyId, String className, long classPK)
086                    throws SystemException {
087    
088                    long classNameId = PortalUtil.getClassNameId(className);
089    
090                    return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
091            }
092    
093            public Website updateWebsite(
094                            long websiteId, String url, int typeId, boolean primary)
095                    throws PortalException, SystemException {
096    
097                    validate(websiteId, 0, 0, 0, url, typeId, primary);
098    
099                    Website website = websitePersistence.findByPrimaryKey(websiteId);
100    
101                    website.setModifiedDate(new Date());
102                    website.setUrl(url);
103                    website.setTypeId(typeId);
104                    website.setPrimary(primary);
105    
106                    websitePersistence.update(website);
107    
108                    return website;
109            }
110    
111            protected void validate(
112                            long websiteId, long companyId, long classNameId, long classPK,
113                            boolean primary)
114                    throws SystemException {
115    
116                    // Check to make sure there isn't another website with the same company
117                    // id, class name, and class pk that also has primary set to true
118    
119                    if (primary) {
120                            List<Website> websites = websitePersistence.findByC_C_C_P(
121                                    companyId, classNameId, classPK, primary);
122    
123                            for (Website website : websites) {
124                                    if ((websiteId <= 0) || (website.getWebsiteId() != websiteId)) {
125                                            website.setPrimary(false);
126    
127                                            websitePersistence.update(website);
128                                    }
129                            }
130                    }
131            }
132    
133            protected void validate(
134                            long websiteId, long companyId, long classNameId, long classPK,
135                            String url, int typeId, boolean primary)
136                    throws PortalException, SystemException {
137    
138                    if (!Validator.isUrl(url)) {
139                            throw new WebsiteURLException();
140                    }
141    
142                    if (websiteId > 0) {
143                            Website website = websitePersistence.findByPrimaryKey(websiteId);
144    
145                            companyId = website.getCompanyId();
146                            classNameId = website.getClassNameId();
147                            classPK = website.getClassPK();
148                    }
149    
150                    listTypeService.validate(
151                            typeId, classNameId, ListTypeConstants.WEBSITE);
152    
153                    validate(websiteId, companyId, classNameId, classPK, primary);
154            }
155    
156    }