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.portlet.softwarecatalog.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portlet.softwarecatalog.LicenseNameException;
021    import com.liferay.portlet.softwarecatalog.model.SCLicense;
022    import com.liferay.portlet.softwarecatalog.service.base.SCLicenseLocalServiceBaseImpl;
023    
024    import java.util.List;
025    
026    /**
027     * @author Jorge Ferrer
028     * @author Brian Wing Shun Chan
029     */
030    public class SCLicenseLocalServiceImpl extends SCLicenseLocalServiceBaseImpl {
031    
032            public SCLicense addLicense(
033                            String name, String url, boolean openSource, boolean active,
034                            boolean recommended)
035                    throws PortalException, SystemException {
036    
037                    validate(name);
038    
039                    long licenseId = counterLocalService.increment();
040    
041                    SCLicense license = scLicensePersistence.create(licenseId);
042    
043                    license.setName(name);
044                    license.setUrl(url);
045                    license.setOpenSource(openSource);
046                    license.setActive(active);
047                    license.setRecommended(recommended);
048    
049                    scLicensePersistence.update(license, false);
050    
051                    return license;
052            }
053    
054            public void deleteLicense(long licenseId)
055                    throws PortalException, SystemException {
056    
057                    scLicensePersistence.remove(licenseId);
058            }
059    
060            public SCLicense getLicense(long licenseId)
061                    throws PortalException, SystemException {
062    
063                    return scLicensePersistence.findByPrimaryKey(licenseId);
064            }
065    
066            public List<SCLicense> getLicenses() throws SystemException {
067                    return scLicensePersistence.findAll();
068            }
069    
070            public List<SCLicense> getLicenses(boolean active, boolean recommended)
071                    throws SystemException {
072    
073                    return scLicensePersistence.findByA_R(active, recommended);
074            }
075    
076            public List<SCLicense> getLicenses(
077                            boolean active, boolean recommended, int start, int end)
078                    throws SystemException {
079    
080                    return scLicensePersistence.findByA_R(active, recommended, start, end);
081            }
082    
083            public List<SCLicense> getLicenses(int start, int end)
084                    throws SystemException {
085    
086                    return scLicensePersistence.findAll(start, end);
087            }
088    
089            public int getLicensesCount() throws SystemException {
090                    return scLicensePersistence.countAll();
091            }
092    
093            public int getLicensesCount(boolean active, boolean recommended)
094                    throws SystemException {
095    
096                    return scLicensePersistence.countByA_R(active, recommended);
097            }
098    
099            public List<SCLicense> getProductEntryLicenses(long productEntryId)
100                    throws SystemException {
101    
102                    return scProductEntryPersistence.getSCLicenses(productEntryId);
103            }
104    
105            public SCLicense updateLicense(
106                            long licenseId, String name, String url, boolean openSource,
107                            boolean active, boolean recommended)
108                    throws PortalException, SystemException {
109    
110                    validate(name);
111    
112                    SCLicense license = scLicensePersistence.findByPrimaryKey(licenseId);
113    
114                    license.setName(name);
115                    license.setUrl(url);
116                    license.setOpenSource(openSource);
117                    license.setActive(active);
118                    license.setRecommended(recommended);
119    
120                    scLicensePersistence.update(license, false);
121    
122                    return license;
123            }
124    
125            protected void validate(String name) throws PortalException {
126                    if (Validator.isNull(name)) {
127                            throw new LicenseNameException();
128                    }
129            }
130    
131    }