1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.softwarecatalog.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.portal.model.User;
21  import com.liferay.portal.service.ServiceContext;
22  import com.liferay.portlet.softwarecatalog.FrameworkVersionNameException;
23  import com.liferay.portlet.softwarecatalog.model.SCFrameworkVersion;
24  import com.liferay.portlet.softwarecatalog.service.base.SCFrameworkVersionLocalServiceBaseImpl;
25  
26  import java.util.Date;
27  import java.util.List;
28  
29  /**
30   * <a href="SCFrameworkVersionLocalServiceImpl.java.html"><b><i>View Source</i>
31   * </b></a>
32   *
33   * @author Jorge Ferrer
34   * @author Brian Wing Shun Chan
35   */
36  public class SCFrameworkVersionLocalServiceImpl
37      extends SCFrameworkVersionLocalServiceBaseImpl {
38  
39      public SCFrameworkVersion addFrameworkVersion(
40              long userId, String name, String url, boolean active, int priority,
41              ServiceContext serviceContext)
42          throws PortalException, SystemException {
43  
44          // Framework version
45  
46          User user = userPersistence.findByPrimaryKey(userId);
47          long groupId = serviceContext.getScopeGroupId();
48          Date now = new Date();
49  
50          validate(name);
51  
52          long frameworkVersionId = counterLocalService.increment();
53  
54          SCFrameworkVersion frameworkVersion =
55              scFrameworkVersionPersistence.create(
56                  frameworkVersionId);
57  
58          frameworkVersion.setGroupId(groupId);
59          frameworkVersion.setCompanyId(user.getCompanyId());
60          frameworkVersion.setUserId(user.getUserId());
61          frameworkVersion.setUserName(user.getFullName());
62          frameworkVersion.setCreateDate(now);
63          frameworkVersion.setModifiedDate(now);
64          frameworkVersion.setName(name);
65          frameworkVersion.setUrl(url);
66          frameworkVersion.setActive(active);
67          frameworkVersion.setPriority(priority);
68  
69          scFrameworkVersionPersistence.update(frameworkVersion, false);
70  
71          // Resources
72  
73          if (serviceContext.getAddCommunityPermissions() ||
74              serviceContext.getAddGuestPermissions()) {
75  
76              addFrameworkVersionResources(
77                  frameworkVersion, serviceContext.getAddCommunityPermissions(),
78                  serviceContext.getAddGuestPermissions());
79          }
80          else {
81              addFrameworkVersionResources(
82                  frameworkVersion, serviceContext.getCommunityPermissions(),
83                  serviceContext.getGuestPermissions());
84          }
85  
86          return frameworkVersion;
87      }
88  
89      public void addFrameworkVersionResources(
90              long frameworkVersionId, boolean addCommunityPermissions,
91              boolean addGuestPermissions)
92          throws PortalException, SystemException {
93  
94          SCFrameworkVersion frameworkVersion =
95              scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
96  
97          addFrameworkVersionResources(
98              frameworkVersion, addCommunityPermissions, addGuestPermissions);
99      }
100 
101     public void addFrameworkVersionResources(
102             SCFrameworkVersion frameworkVersion,
103             boolean addCommunityPermissions, boolean addGuestPermissions)
104         throws PortalException, SystemException {
105 
106         resourceLocalService.addResources(
107             frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
108             frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
109             frameworkVersion.getFrameworkVersionId(), false,
110             addCommunityPermissions, addGuestPermissions);
111     }
112 
113     public void addFrameworkVersionResources(
114             long frameworkVersionId, String[] communityPermissions,
115             String[] guestPermissions)
116         throws PortalException, SystemException {
117 
118         SCFrameworkVersion frameworkVersion =
119             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
120 
121         addFrameworkVersionResources(
122             frameworkVersion, communityPermissions, guestPermissions);
123     }
124 
125     public void addFrameworkVersionResources(
126             SCFrameworkVersion frameworkVersion, String[] communityPermissions,
127             String[] guestPermissions)
128         throws PortalException, SystemException {
129 
130         resourceLocalService.addModelResources(
131             frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
132             frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
133             frameworkVersion.getFrameworkVersionId(), communityPermissions,
134             guestPermissions);
135     }
136 
137     public void deleteFrameworkVersion(long frameworkVersionId)
138         throws PortalException, SystemException {
139 
140         scFrameworkVersionPersistence.remove(frameworkVersionId);
141     }
142 
143     public void deleteFrameworkVersion(SCFrameworkVersion frameworkVersion)
144         throws SystemException {
145 
146         scFrameworkVersionPersistence.remove(frameworkVersion);
147     }
148 
149     public void deleteFrameworkVersions(long groupId) throws SystemException {
150         List<SCFrameworkVersion> frameworkVersions =
151             scFrameworkVersionPersistence.findByGroupId(groupId);
152 
153         for (SCFrameworkVersion frameworkVersion : frameworkVersions) {
154             deleteFrameworkVersion(frameworkVersion);
155         }
156     }
157 
158     public SCFrameworkVersion getFrameworkVersion(long frameworkVersionId)
159         throws PortalException, SystemException {
160 
161         return scFrameworkVersionPersistence.findByPrimaryKey(
162             frameworkVersionId);
163     }
164 
165     public List<SCFrameworkVersion> getFrameworkVersions(
166             long groupId, int start, int end)
167         throws SystemException {
168 
169         return scFrameworkVersionPersistence.findByGroupId(groupId, start, end);
170     }
171 
172     public List<SCFrameworkVersion> getFrameworkVersions(
173             long groupId, boolean active)
174         throws SystemException {
175 
176         return scFrameworkVersionPersistence.findByG_A(groupId, active);
177     }
178 
179     public List<SCFrameworkVersion> getFrameworkVersions(
180             long groupId, boolean active, int start, int end)
181         throws SystemException {
182 
183         return scFrameworkVersionPersistence.findByG_A(
184             groupId, active, start, end);
185     }
186 
187     public int getFrameworkVersionsCount(long groupId)
188         throws SystemException {
189 
190         return scFrameworkVersionPersistence.countByGroupId(groupId);
191     }
192 
193     public int getFrameworkVersionsCount(long groupId, boolean active)
194         throws SystemException {
195 
196         return scFrameworkVersionPersistence.countByG_A(groupId, active);
197     }
198 
199     public List<SCFrameworkVersion> getProductVersionFrameworkVersions(
200             long productVersionId)
201         throws SystemException {
202 
203         return scProductVersionPersistence.getSCFrameworkVersions(
204             productVersionId);
205     }
206 
207     public SCFrameworkVersion updateFrameworkVersion(
208             long frameworkVersionId, String name, String url, boolean active,
209             int priority)
210         throws PortalException, SystemException {
211 
212         validate(name);
213 
214         SCFrameworkVersion frameworkVersion =
215             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
216 
217         frameworkVersion.setName(name);
218         frameworkVersion.setUrl(url);
219         frameworkVersion.setActive(active);
220         frameworkVersion.setPriority(priority);
221 
222         scFrameworkVersionPersistence.update(frameworkVersion, false);
223 
224         return frameworkVersion;
225     }
226 
227     protected void validate(String name) throws PortalException {
228         if (Validator.isNull(name)) {
229             throw new FrameworkVersionNameException();
230         }
231     }
232 
233 }