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