001    /**
002     * Copyright (c) 2000-2010 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,
047                            primary);
048    
049                    long websiteId = counterLocalService.increment();
050    
051                    Website website = websitePersistence.create(websiteId);
052    
053                    website.setCompanyId(user.getCompanyId());
054                    website.setUserId(user.getUserId());
055                    website.setUserName(user.getFullName());
056                    website.setCreateDate(now);
057                    website.setModifiedDate(now);
058                    website.setClassNameId(classNameId);
059                    website.setClassPK(classPK);
060                    website.setUrl(url);
061                    website.setTypeId(typeId);
062                    website.setPrimary(primary);
063    
064                    websitePersistence.update(website, false);
065    
066                    return website;
067            }
068    
069            public void deleteWebsite(long websiteId)
070                    throws PortalException, SystemException {
071    
072                    websitePersistence.remove(websiteId);
073            }
074    
075            public void deleteWebsites(long companyId, String className, long classPK)
076                    throws SystemException {
077    
078                    long classNameId = PortalUtil.getClassNameId(className);
079    
080                    websitePersistence.removeByC_C_C(companyId, classNameId, classPK);
081            }
082    
083            public Website getWebsite(long websiteId)
084                    throws PortalException, SystemException {
085    
086                    return websitePersistence.findByPrimaryKey(websiteId);
087            }
088    
089            public List<Website> getWebsites() throws SystemException {
090                    return websitePersistence.findAll();
091            }
092    
093            public List<Website> getWebsites(
094                            long companyId, String className, long classPK)
095                    throws SystemException {
096    
097                    long classNameId = PortalUtil.getClassNameId(className);
098    
099                    return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
100            }
101    
102            public Website updateWebsite(
103                            long websiteId, String url, int typeId, boolean primary)
104                    throws PortalException, SystemException {
105    
106                    validate(websiteId, 0, 0, 0, url, typeId, primary);
107    
108                    Website website = websitePersistence.findByPrimaryKey(websiteId);
109    
110                    website.setModifiedDate(new Date());
111                    website.setUrl(url);
112                    website.setTypeId(typeId);
113                    website.setPrimary(primary);
114    
115                    websitePersistence.update(website, false);
116    
117                    return website;
118            }
119    
120            protected void validate(
121                            long websiteId, long companyId, long classNameId, long classPK,
122                            String url, int typeId, boolean primary)
123                    throws PortalException, SystemException {
124    
125                    if (!Validator.isUrl(url)) {
126                            throw new WebsiteURLException();
127                    }
128    
129                    if (websiteId > 0) {
130                            Website website = websitePersistence.findByPrimaryKey(websiteId);
131    
132                            companyId = website.getCompanyId();
133                            classNameId = website.getClassNameId();
134                            classPK = website.getClassPK();
135                    }
136    
137                    listTypeService.validate(
138                            typeId, classNameId, ListTypeConstants.WEBSITE);
139    
140                    validate(websiteId, companyId, classNameId, classPK, primary);
141            }
142    
143            protected void validate(
144                            long websiteId, long companyId, long classNameId, long classPK,
145                            boolean primary)
146                    throws SystemException {
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                            Iterator<Website> itr = websitePersistence.findByC_C_C_P(
153                                    companyId, classNameId, classPK, primary).iterator();
154    
155                            while (itr.hasNext()) {
156                                    Website website = itr.next();
157    
158                                    if ((websiteId <= 0) ||
159                                            (website.getWebsiteId() != websiteId)) {
160    
161                                            website.setPrimary(false);
162    
163                                            websitePersistence.update(website, false);
164                                    }
165                            }
166                    }
167            }
168    
169    }