1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.softwarecatalog.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.portal.model.User;
21  import com.liferay.portal.util.PortalUtil;
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, long plid, String name, String url, boolean active,
41              int priority, boolean addCommunityPermissions,
42              boolean addGuestPermissions)
43          throws PortalException, SystemException {
44  
45          return addFrameworkVersion(
46              userId, plid, name, url, active, priority,
47              Boolean.valueOf(addCommunityPermissions),
48              Boolean.valueOf(addGuestPermissions), null, null);
49      }
50  
51      public SCFrameworkVersion addFrameworkVersion(
52              long userId, long plid, String name, String url, boolean active,
53              int priority, String[] communityPermissions,
54              String[] guestPermissions)
55          throws PortalException, SystemException {
56  
57          return addFrameworkVersion(
58              userId, plid, name, url, active, priority, null, null,
59              communityPermissions, guestPermissions);
60      }
61  
62      public SCFrameworkVersion addFrameworkVersion(
63              long userId, long plid, String name, String url, boolean active,
64              int priority, Boolean addCommunityPermissions,
65              Boolean addGuestPermissions, String[] communityPermissions,
66              String[] guestPermissions)
67          throws PortalException, SystemException {
68  
69          // Framework version
70  
71          User user = userPersistence.findByPrimaryKey(userId);
72          long groupId = PortalUtil.getScopeGroupId(plid);
73          Date now = new Date();
74  
75          validate(name);
76  
77          long frameworkVersionId = counterLocalService.increment();
78  
79          SCFrameworkVersion frameworkVersion =
80              scFrameworkVersionPersistence.create(
81                  frameworkVersionId);
82  
83          frameworkVersion.setGroupId(groupId);
84          frameworkVersion.setCompanyId(user.getCompanyId());
85          frameworkVersion.setUserId(user.getUserId());
86          frameworkVersion.setUserName(user.getFullName());
87          frameworkVersion.setCreateDate(now);
88          frameworkVersion.setModifiedDate(now);
89          frameworkVersion.setName(name);
90          frameworkVersion.setUrl(url);
91          frameworkVersion.setActive(active);
92          frameworkVersion.setPriority(priority);
93  
94          scFrameworkVersionPersistence.update(frameworkVersion, false);
95  
96          // Resources
97  
98          if ((addCommunityPermissions != null) &&
99              (addGuestPermissions != null)) {
100 
101             addFrameworkVersionResources(
102                 frameworkVersion, addCommunityPermissions.booleanValue(),
103                 addGuestPermissions.booleanValue());
104         }
105         else {
106             addFrameworkVersionResources(
107                 frameworkVersion, communityPermissions, guestPermissions);
108         }
109 
110         return frameworkVersion;
111     }
112 
113     public void addFrameworkVersionResources(
114             long frameworkVersionId, boolean addCommunityPermissions,
115             boolean addGuestPermissions)
116         throws PortalException, SystemException {
117 
118         SCFrameworkVersion frameworkVersion =
119             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
120 
121         addFrameworkVersionResources(
122             frameworkVersion, addCommunityPermissions, addGuestPermissions);
123     }
124 
125     public void addFrameworkVersionResources(
126             SCFrameworkVersion frameworkVersion,
127             boolean addCommunityPermissions, boolean addGuestPermissions)
128         throws PortalException, SystemException {
129 
130         resourceLocalService.addResources(
131             frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
132             frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
133             frameworkVersion.getFrameworkVersionId(), false,
134             addCommunityPermissions, addGuestPermissions);
135     }
136 
137     public void addFrameworkVersionResources(
138             long frameworkVersionId, String[] communityPermissions,
139             String[] guestPermissions)
140         throws PortalException, SystemException {
141 
142         SCFrameworkVersion frameworkVersion =
143             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
144 
145         addFrameworkVersionResources(
146             frameworkVersion, communityPermissions, guestPermissions);
147     }
148 
149     public void addFrameworkVersionResources(
150             SCFrameworkVersion frameworkVersion, String[] communityPermissions,
151             String[] guestPermissions)
152         throws PortalException, SystemException {
153 
154         resourceLocalService.addModelResources(
155             frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
156             frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
157             frameworkVersion.getFrameworkVersionId(), communityPermissions,
158             guestPermissions);
159     }
160 
161     public void deleteFrameworkVersion(long frameworkVersionId)
162         throws PortalException, SystemException {
163 
164         scFrameworkVersionPersistence.remove(frameworkVersionId);
165     }
166 
167     public void deleteFrameworkVersion(SCFrameworkVersion frameworkVersion)
168         throws SystemException {
169 
170         scFrameworkVersionPersistence.remove(frameworkVersion);
171     }
172 
173     public void deleteFrameworkVersions(long groupId) throws SystemException {
174         List<SCFrameworkVersion> frameworkVersions =
175             scFrameworkVersionPersistence.findByGroupId(groupId);
176 
177         for (SCFrameworkVersion frameworkVersion : frameworkVersions) {
178             deleteFrameworkVersion(frameworkVersion);
179         }
180     }
181 
182     public SCFrameworkVersion getFrameworkVersion(long frameworkVersionId)
183         throws PortalException, SystemException {
184 
185         return scFrameworkVersionPersistence.findByPrimaryKey(
186             frameworkVersionId);
187     }
188 
189     public List<SCFrameworkVersion> getFrameworkVersions(
190             long groupId, int start, int end)
191         throws SystemException {
192 
193         return scFrameworkVersionPersistence.findByGroupId(groupId, start, end);
194     }
195 
196     public List<SCFrameworkVersion> getFrameworkVersions(
197             long groupId, boolean active)
198         throws SystemException {
199 
200         return scFrameworkVersionPersistence.findByG_A(groupId, active);
201     }
202 
203     public List<SCFrameworkVersion> getFrameworkVersions(
204             long groupId, boolean active, int start, int end)
205         throws SystemException {
206 
207         return scFrameworkVersionPersistence.findByG_A(
208             groupId, active, start, end);
209     }
210 
211     public int getFrameworkVersionsCount(long groupId)
212         throws SystemException {
213 
214         return scFrameworkVersionPersistence.countByGroupId(groupId);
215     }
216 
217     public int getFrameworkVersionsCount(long groupId, boolean active)
218         throws SystemException {
219 
220         return scFrameworkVersionPersistence.countByG_A(groupId, active);
221     }
222 
223     public List<SCFrameworkVersion> getProductVersionFrameworkVersions(
224             long productVersionId)
225         throws SystemException {
226 
227         return scProductVersionPersistence.getSCFrameworkVersions(
228             productVersionId);
229     }
230 
231     public SCFrameworkVersion updateFrameworkVersion(
232             long frameworkVersionId, String name, String url, boolean active,
233             int priority)
234         throws PortalException, SystemException {
235 
236         validate(name);
237 
238         SCFrameworkVersion frameworkVersion =
239             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
240 
241         frameworkVersion.setName(name);
242         frameworkVersion.setUrl(url);
243         frameworkVersion.setActive(active);
244         frameworkVersion.setPriority(priority);
245 
246         scFrameworkVersionPersistence.update(frameworkVersion, false);
247 
248         return frameworkVersion;
249     }
250 
251     protected void validate(String name) throws PortalException {
252         if (Validator.isNull(name)) {
253             throw new FrameworkVersionNameException();
254         }
255     }
256 
257 }