001    /**
002     * Copyright (c) 2000-2012 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, 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            public void deleteWebsites(long companyId, String className, long classPK)
069                    throws SystemException {
070    
071                    long classNameId = PortalUtil.getClassNameId(className);
072    
073                    List<Website> websites = websitePersistence.findByC_C_C(
074                            companyId, classNameId, classPK);
075    
076                    for (Website website : websites) {
077                            deleteWebsite(website);
078                    }
079            }
080    
081            public List<Website> getWebsites() throws SystemException {
082                    return websitePersistence.findAll();
083            }
084    
085            public List<Website> getWebsites(
086                            long companyId, String className, long classPK)
087                    throws SystemException {
088    
089                    long classNameId = PortalUtil.getClassNameId(className);
090    
091                    return websitePersistence.findByC_C_C(companyId, classNameId, classPK);
092            }
093    
094            public Website updateWebsite(
095                            long websiteId, String url, int typeId, boolean primary)
096                    throws PortalException, SystemException {
097    
098                    validate(websiteId, 0, 0, 0, url, typeId, primary);
099    
100                    Website website = websitePersistence.findByPrimaryKey(websiteId);
101    
102                    website.setModifiedDate(new Date());
103                    website.setUrl(url);
104                    website.setTypeId(typeId);
105                    website.setPrimary(primary);
106    
107                    websitePersistence.update(website, false);
108    
109                    return website;
110            }
111    
112            protected void validate(
113                            long websiteId, long companyId, long classNameId, long classPK,
114                            boolean primary)
115                    throws SystemException {
116    
117                    // Check to make sure there isn't another website with the same company
118                    // id, class name, and class pk that also has primary set to true
119    
120                    if (primary) {
121                            Iterator<Website> itr = websitePersistence.findByC_C_C_P(
122                                    companyId, classNameId, classPK, primary).iterator();
123    
124                            while (itr.hasNext()) {
125                                    Website website = itr.next();
126    
127                                    if ((websiteId <= 0) || (website.getWebsiteId() != websiteId)) {
128                                            website.setPrimary(false);
129    
130                                            websitePersistence.update(website, false);
131                                    }
132                            }
133                    }
134            }
135    
136            protected void validate(
137                            long websiteId, long companyId, long classNameId, long classPK,
138                            String url, int typeId, boolean primary)
139                    throws PortalException, SystemException {
140    
141                    if (!Validator.isUrl(url)) {
142                            throw new WebsiteURLException();
143                    }
144    
145                    if (websiteId > 0) {
146                            Website website = websitePersistence.findByPrimaryKey(websiteId);
147    
148                            companyId = website.getCompanyId();
149                            classNameId = website.getClassNameId();
150                            classPK = website.getClassPK();
151                    }
152    
153                    listTypeService.validate(
154                            typeId, classNameId, ListTypeConstants.WEBSITE);
155    
156                    validate(websiteId, companyId, classNameId, classPK, primary);
157            }
158    
159    }