001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.portal.model.User;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portlet.softwarecatalog.FrameworkVersionNameException;
023    import com.liferay.portlet.softwarecatalog.model.SCFrameworkVersion;
024    import com.liferay.portlet.softwarecatalog.service.base.SCFrameworkVersionLocalServiceBaseImpl;
025    
026    import java.util.Date;
027    import java.util.List;
028    
029    /**
030     * @author Jorge Ferrer
031     * @author Brian Wing Shun Chan
032     */
033    public class SCFrameworkVersionLocalServiceImpl
034            extends SCFrameworkVersionLocalServiceBaseImpl {
035    
036            public SCFrameworkVersion addFrameworkVersion(
037                            long userId, String name, String url, boolean active, int priority,
038                            ServiceContext serviceContext)
039                    throws PortalException, SystemException {
040    
041                    // Framework version
042    
043                    User user = userPersistence.findByPrimaryKey(userId);
044                    long groupId = serviceContext.getScopeGroupId();
045                    Date now = new Date();
046    
047                    validate(name);
048    
049                    long frameworkVersionId = counterLocalService.increment();
050    
051                    SCFrameworkVersion frameworkVersion =
052                            scFrameworkVersionPersistence.create(
053                                    frameworkVersionId);
054    
055                    frameworkVersion.setGroupId(groupId);
056                    frameworkVersion.setCompanyId(user.getCompanyId());
057                    frameworkVersion.setUserId(user.getUserId());
058                    frameworkVersion.setUserName(user.getFullName());
059                    frameworkVersion.setCreateDate(now);
060                    frameworkVersion.setModifiedDate(now);
061                    frameworkVersion.setName(name);
062                    frameworkVersion.setUrl(url);
063                    frameworkVersion.setActive(active);
064                    frameworkVersion.setPriority(priority);
065    
066                    scFrameworkVersionPersistence.update(frameworkVersion, false);
067    
068                    // Resources
069    
070                    if (serviceContext.getAddCommunityPermissions() ||
071                            serviceContext.getAddGuestPermissions()) {
072    
073                            addFrameworkVersionResources(
074                                    frameworkVersion, serviceContext.getAddCommunityPermissions(),
075                                    serviceContext.getAddGuestPermissions());
076                    }
077                    else {
078                            addFrameworkVersionResources(
079                                    frameworkVersion, serviceContext.getCommunityPermissions(),
080                                    serviceContext.getGuestPermissions());
081                    }
082    
083                    return frameworkVersion;
084            }
085    
086            public void addFrameworkVersionResources(
087                            long frameworkVersionId, boolean addCommunityPermissions,
088                            boolean addGuestPermissions)
089                    throws PortalException, SystemException {
090    
091                    SCFrameworkVersion frameworkVersion =
092                            scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
093    
094                    addFrameworkVersionResources(
095                            frameworkVersion, addCommunityPermissions, addGuestPermissions);
096            }
097    
098            public void addFrameworkVersionResources(
099                            SCFrameworkVersion frameworkVersion,
100                            boolean addCommunityPermissions, boolean addGuestPermissions)
101                    throws PortalException, SystemException {
102    
103                    resourceLocalService.addResources(
104                            frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
105                            frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
106                            frameworkVersion.getFrameworkVersionId(), false,
107                            addCommunityPermissions, addGuestPermissions);
108            }
109    
110            public void addFrameworkVersionResources(
111                            long frameworkVersionId, String[] communityPermissions,
112                            String[] guestPermissions)
113                    throws PortalException, SystemException {
114    
115                    SCFrameworkVersion frameworkVersion =
116                            scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
117    
118                    addFrameworkVersionResources(
119                            frameworkVersion, communityPermissions, guestPermissions);
120            }
121    
122            public void addFrameworkVersionResources(
123                            SCFrameworkVersion frameworkVersion, String[] communityPermissions,
124                            String[] guestPermissions)
125                    throws PortalException, SystemException {
126    
127                    resourceLocalService.addModelResources(
128                            frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
129                            frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
130                            frameworkVersion.getFrameworkVersionId(), communityPermissions,
131                            guestPermissions);
132            }
133    
134            public void deleteFrameworkVersion(long frameworkVersionId)
135                    throws PortalException, SystemException {
136    
137                    scFrameworkVersionPersistence.remove(frameworkVersionId);
138            }
139    
140            public void deleteFrameworkVersion(SCFrameworkVersion frameworkVersion)
141                    throws SystemException {
142    
143                    scFrameworkVersionPersistence.remove(frameworkVersion);
144            }
145    
146            public void deleteFrameworkVersions(long groupId) throws SystemException {
147                    List<SCFrameworkVersion> frameworkVersions =
148                            scFrameworkVersionPersistence.findByGroupId(groupId);
149    
150                    for (SCFrameworkVersion frameworkVersion : frameworkVersions) {
151                            deleteFrameworkVersion(frameworkVersion);
152                    }
153            }
154    
155            public SCFrameworkVersion getFrameworkVersion(long frameworkVersionId)
156                    throws PortalException, SystemException {
157    
158                    return scFrameworkVersionPersistence.findByPrimaryKey(
159                            frameworkVersionId);
160            }
161    
162            public List<SCFrameworkVersion> getFrameworkVersions(
163                            long groupId, int start, int end)
164                    throws SystemException {
165    
166                    return scFrameworkVersionPersistence.findByGroupId(groupId, start, end);
167            }
168    
169            public List<SCFrameworkVersion> getFrameworkVersions(
170                            long groupId, boolean active)
171                    throws SystemException {
172    
173                    return scFrameworkVersionPersistence.findByG_A(groupId, active);
174            }
175    
176            public List<SCFrameworkVersion> getFrameworkVersions(
177                            long groupId, boolean active, int start, int end)
178                    throws SystemException {
179    
180                    return scFrameworkVersionPersistence.findByG_A(
181                            groupId, active, start, end);
182            }
183    
184            public int getFrameworkVersionsCount(long groupId)
185                    throws SystemException {
186    
187                    return scFrameworkVersionPersistence.countByGroupId(groupId);
188            }
189    
190            public int getFrameworkVersionsCount(long groupId, boolean active)
191                    throws SystemException {
192    
193                    return scFrameworkVersionPersistence.countByG_A(groupId, active);
194            }
195    
196            public List<SCFrameworkVersion> getProductVersionFrameworkVersions(
197                            long productVersionId)
198                    throws SystemException {
199    
200                    return scProductVersionPersistence.getSCFrameworkVersions(
201                            productVersionId);
202            }
203    
204            public SCFrameworkVersion updateFrameworkVersion(
205                            long frameworkVersionId, String name, String url, boolean active,
206                            int priority)
207                    throws PortalException, SystemException {
208    
209                    validate(name);
210    
211                    SCFrameworkVersion frameworkVersion =
212                            scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
213    
214                    frameworkVersion.setName(name);
215                    frameworkVersion.setUrl(url);
216                    frameworkVersion.setActive(active);
217                    frameworkVersion.setPriority(priority);
218    
219                    scFrameworkVersionPersistence.update(frameworkVersion, false);
220    
221                    return frameworkVersion;
222            }
223    
224            protected void validate(String name) throws PortalException {
225                    if (Validator.isNull(name)) {
226                            throw new FrameworkVersionNameException();
227                    }
228            }
229    
230    }