001    /**
002     * Copyright (c) 2000-present 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.util.Validator;
019    import com.liferay.portlet.softwarecatalog.LicenseNameException;
020    import com.liferay.portlet.softwarecatalog.RequiredLicenseException;
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            @Override
033            public SCLicense addLicense(
034                            String name, String url, boolean openSource, boolean active,
035                            boolean recommended)
036                    throws PortalException {
037    
038                    validate(name);
039    
040                    long licenseId = counterLocalService.increment();
041    
042                    SCLicense license = scLicensePersistence.create(licenseId);
043    
044                    license.setName(name);
045                    license.setUrl(url);
046                    license.setOpenSource(openSource);
047                    license.setActive(active);
048                    license.setRecommended(recommended);
049    
050                    scLicensePersistence.update(license);
051    
052                    return license;
053            }
054    
055            @Override
056            public void deleteLicense(long licenseId) throws PortalException {
057                    SCLicense license = scLicensePersistence.findByPrimaryKey(licenseId);
058    
059                    if (scLicensePersistence.getSCProductEntriesSize(licenseId) > 0) {
060                            throw new RequiredLicenseException();
061                    }
062    
063                    deleteLicense(license);
064            }
065    
066            @Override
067            public void deleteLicense(SCLicense license) {
068                    scLicensePersistence.remove(license);
069            }
070    
071            @Override
072            public SCLicense getLicense(long licenseId) throws PortalException {
073                    return scLicensePersistence.findByPrimaryKey(licenseId);
074            }
075    
076            @Override
077            public List<SCLicense> getLicenses() {
078                    return scLicensePersistence.findAll();
079            }
080    
081            @Override
082            public List<SCLicense> getLicenses(boolean active, boolean recommended) {
083                    return scLicensePersistence.findByA_R(active, recommended);
084            }
085    
086            @Override
087            public List<SCLicense> getLicenses(
088                    boolean active, boolean recommended, int start, int end) {
089    
090                    return scLicensePersistence.findByA_R(active, recommended, start, end);
091            }
092    
093            @Override
094            public List<SCLicense> getLicenses(int start, int end) {
095                    return scLicensePersistence.findAll(start, end);
096            }
097    
098            @Override
099            public int getLicensesCount() {
100                    return scLicensePersistence.countAll();
101            }
102    
103            @Override
104            public int getLicensesCount(boolean active, boolean recommended) {
105                    return scLicensePersistence.countByA_R(active, recommended);
106            }
107    
108            @Override
109            public List<SCLicense> getProductEntryLicenses(long productEntryId) {
110                    return scProductEntryPersistence.getSCLicenses(productEntryId);
111            }
112    
113            @Override
114            public SCLicense updateLicense(
115                            long licenseId, String name, String url, boolean openSource,
116                            boolean active, boolean recommended)
117                    throws PortalException {
118    
119                    validate(name);
120    
121                    SCLicense license = scLicensePersistence.findByPrimaryKey(licenseId);
122    
123                    license.setName(name);
124                    license.setUrl(url);
125                    license.setOpenSource(openSource);
126                    license.setActive(active);
127                    license.setRecommended(recommended);
128    
129                    scLicensePersistence.update(license);
130    
131                    return license;
132            }
133    
134            protected void validate(String name) throws PortalException {
135                    if (Validator.isNull(name)) {
136                            throw new LicenseNameException();
137                    }
138            }
139    
140    }