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