001    /**
002     * Copyright (c) 2000-2013 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.RequiredLicenseException;
022    import com.liferay.portlet.softwarecatalog.model.SCLicense;
023    import com.liferay.portlet.softwarecatalog.service.base.SCLicenseLocalServiceBaseImpl;
024    
025    import java.util.List;
026    
027    /**
028     * @author Jorge Ferrer
029     * @author Brian Wing Shun Chan
030     */
031    public class SCLicenseLocalServiceImpl extends SCLicenseLocalServiceBaseImpl {
032    
033            public SCLicense addLicense(
034                            String name, String url, boolean openSource, boolean active,
035                            boolean recommended)
036                    throws PortalException, SystemException {
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            public void deleteLicense(long licenseId)
056                    throws PortalException, SystemException {
057    
058                    SCLicense license = scLicensePersistence.findByPrimaryKey(licenseId);
059    
060                    if (scLicensePersistence.getSCProductEntriesSize(licenseId) > 0) {
061                            throw new RequiredLicenseException();
062                    }
063    
064                    deleteLicense(license);
065            }
066    
067            public void deleteLicense(SCLicense license) throws SystemException {
068                    scLicensePersistence.remove(license);
069            }
070    
071            public SCLicense getLicense(long licenseId)
072                    throws PortalException, SystemException {
073    
074                    return scLicensePersistence.findByPrimaryKey(licenseId);
075            }
076    
077            public List<SCLicense> getLicenses() throws SystemException {
078                    return scLicensePersistence.findAll();
079            }
080    
081            public List<SCLicense> getLicenses(boolean active, boolean recommended)
082                    throws SystemException {
083    
084                    return scLicensePersistence.findByA_R(active, recommended);
085            }
086    
087            public List<SCLicense> getLicenses(
088                            boolean active, boolean recommended, int start, int end)
089                    throws SystemException {
090    
091                    return scLicensePersistence.findByA_R(active, recommended, start, end);
092            }
093    
094            public List<SCLicense> getLicenses(int start, int end)
095                    throws SystemException {
096    
097                    return scLicensePersistence.findAll(start, end);
098            }
099    
100            public int getLicensesCount() throws SystemException {
101                    return scLicensePersistence.countAll();
102            }
103    
104            public int getLicensesCount(boolean active, boolean recommended)
105                    throws SystemException {
106    
107                    return scLicensePersistence.countByA_R(active, recommended);
108            }
109    
110            public List<SCLicense> getProductEntryLicenses(long productEntryId)
111                    throws SystemException {
112    
113                    return scProductEntryPersistence.getSCLicenses(productEntryId);
114            }
115    
116            public SCLicense updateLicense(
117                            long licenseId, String name, String url, boolean openSource,
118                            boolean active, boolean recommended)
119                    throws PortalException, SystemException {
120    
121                    validate(name);
122    
123                    SCLicense license = scLicensePersistence.findByPrimaryKey(licenseId);
124    
125                    license.setName(name);
126                    license.setUrl(url);
127                    license.setOpenSource(openSource);
128                    license.setActive(active);
129                    license.setRecommended(recommended);
130    
131                    scLicensePersistence.update(license);
132    
133                    return license;
134            }
135    
136            protected void validate(String name) throws PortalException {
137                    if (Validator.isNull(name)) {
138                            throw new LicenseNameException();
139                    }
140            }
141    
142    }