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.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                    SCLicense license = scLicensePersistence.findByPrimaryKey(licenseId);
058    
059                    deleteLicense(license);
060            }
061    
062            public void deleteLicense(SCLicense license) throws SystemException {
063                    scLicensePersistence.remove(license);
064            }
065    
066            public SCLicense getLicense(long licenseId)
067                    throws PortalException, SystemException {
068    
069                    return scLicensePersistence.findByPrimaryKey(licenseId);
070            }
071    
072            public List<SCLicense> getLicenses() throws SystemException {
073                    return scLicensePersistence.findAll();
074            }
075    
076            public List<SCLicense> getLicenses(boolean active, boolean recommended)
077                    throws SystemException {
078    
079                    return scLicensePersistence.findByA_R(active, recommended);
080            }
081    
082            public List<SCLicense> getLicenses(
083                            boolean active, boolean recommended, int start, int end)
084                    throws SystemException {
085    
086                    return scLicensePersistence.findByA_R(active, recommended, start, end);
087            }
088    
089            public List<SCLicense> getLicenses(int start, int end)
090                    throws SystemException {
091    
092                    return scLicensePersistence.findAll(start, end);
093            }
094    
095            public int getLicensesCount() throws SystemException {
096                    return scLicensePersistence.countAll();
097            }
098    
099            public int getLicensesCount(boolean active, boolean recommended)
100                    throws SystemException {
101    
102                    return scLicensePersistence.countByA_R(active, recommended);
103            }
104    
105            public List<SCLicense> getProductEntryLicenses(long productEntryId)
106                    throws SystemException {
107    
108                    return scProductEntryPersistence.getSCLicenses(productEntryId);
109            }
110    
111            public SCLicense updateLicense(
112                            long licenseId, String name, String url, boolean openSource,
113                            boolean active, boolean recommended)
114                    throws PortalException, SystemException {
115    
116                    validate(name);
117    
118                    SCLicense license = scLicensePersistence.findByPrimaryKey(licenseId);
119    
120                    license.setName(name);
121                    license.setUrl(url);
122                    license.setOpenSource(openSource);
123                    license.setActive(active);
124                    license.setRecommended(recommended);
125    
126                    scLicensePersistence.update(license, false);
127    
128                    return license;
129            }
130    
131            protected void validate(String name) throws PortalException {
132                    if (Validator.isNull(name)) {
133                            throw new LicenseNameException();
134                    }
135            }
136    
137    }