001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @Override
070            public void deleteWebsite(long websiteId)
071                    throws PortalException, SystemException {
072    
073                    Website website = websitePersistence.findByPrimaryKey(websiteId);
074    
075                    deleteWebsite(website);
076            }
077    
078            @Override
079            public void deleteWebsite(Website website) throws SystemException {
080                    websitePersistence.remove(website);
081            }
082    
083            public void deleteWebsites(long companyId, String className, long classPK)
084                    throws SystemException {
085    
086                    long classNameId = PortalUtil.getClassNameId(className);
087    
088                    List<Website> websites = websitePersistence.findByC_C_C(
089                            companyId, classNameId, classPK);
090    
091                    for (Website website : websites) {
092                            deleteWebsite(website);
093                    }
094            }
095    
096            @Override
097            public Website getWebsite(long websiteId)
098                    throws PortalException, SystemException {
099    
100                    return websitePersistence.findByPrimaryKey(websiteId);
101            }
102    
103            public List<Website> getWebsites() throws SystemException {
104                    return websitePersistence.findAll();
105            }
106    
107            public List<Website> getWebsites(
108                            long companyId, String className, long classPK)
109                    throws SystemException {
110    
111                    long classNameId = PortalUtil.getClassNameId(className);
112    
113                    return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
114            }
115    
116            public Website updateWebsite(
117                            long websiteId, String url, int typeId, boolean primary)
118                    throws PortalException, SystemException {
119    
120                    validate(websiteId, 0, 0, 0, url, typeId, primary);
121    
122                    Website website = websitePersistence.findByPrimaryKey(websiteId);
123    
124                    website.setModifiedDate(new Date());
125                    website.setUrl(url);
126                    website.setTypeId(typeId);
127                    website.setPrimary(primary);
128    
129                    websitePersistence.update(website, false);
130    
131                    return website;
132            }
133    
134            protected void validate(
135                            long websiteId, long companyId, long classNameId, long classPK,
136                            String url, int typeId, boolean primary)
137                    throws PortalException, SystemException {
138    
139                    if (!Validator.isUrl(url)) {
140                            throw new WebsiteURLException();
141                    }
142    
143                    if (websiteId > 0) {
144                            Website website = websitePersistence.findByPrimaryKey(websiteId);
145    
146                            companyId = website.getCompanyId();
147                            classNameId = website.getClassNameId();
148                            classPK = website.getClassPK();
149                    }
150    
151                    listTypeService.validate(
152                            typeId, classNameId, ListTypeConstants.WEBSITE);
153    
154                    validate(websiteId, companyId, classNameId, classPK, primary);
155            }
156    
157            protected void validate(
158                            long websiteId, long companyId, long classNameId, long classPK,
159                            boolean primary)
160                    throws SystemException {
161    
162                    // Check to make sure there isn't another website with the same company
163                    // id, class name, and class pk that also has primary set to true
164    
165                    if (primary) {
166                            Iterator<Website> itr = websitePersistence.findByC_C_C_P(
167                                    companyId, classNameId, classPK, primary).iterator();
168    
169                            while (itr.hasNext()) {
170                                    Website website = itr.next();
171    
172                                    if ((websiteId <= 0) ||
173                                            (website.getWebsiteId() != websiteId)) {
174    
175                                            website.setPrimary(false);
176    
177                                            websitePersistence.update(website, false);
178                                    }
179                            }
180                    }
181            }
182    
183    }