1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.softwarecatalog.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.softwarecatalog.FrameworkVersionNameException;
31  import com.liferay.portlet.softwarecatalog.model.SCFrameworkVersion;
32  import com.liferay.portlet.softwarecatalog.service.base.SCFrameworkVersionLocalServiceBaseImpl;
33  
34  import java.util.Date;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="SCFrameworkVersionLocalServiceImpl.java.html"><b><i>View Source</i>
40   * </b></a>
41   *
42   * @author Jorge Ferrer
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class SCFrameworkVersionLocalServiceImpl
47      extends SCFrameworkVersionLocalServiceBaseImpl {
48  
49      public SCFrameworkVersion addFrameworkVersion(
50              long userId, long plid, String name, String url, boolean active,
51              int priority, boolean addCommunityPermissions,
52              boolean addGuestPermissions)
53          throws PortalException, SystemException {
54  
55          return addFrameworkVersion(
56              userId, plid, name, url, active, priority,
57              Boolean.valueOf(addCommunityPermissions),
58              Boolean.valueOf(addGuestPermissions), null, null);
59      }
60  
61      public SCFrameworkVersion addFrameworkVersion(
62              long userId, long plid, String name, String url, boolean active,
63              int priority, String[] communityPermissions,
64              String[] guestPermissions)
65          throws PortalException, SystemException {
66  
67          return addFrameworkVersion(
68              userId, plid, name, url, active, priority, null, null,
69              communityPermissions, guestPermissions);
70      }
71  
72      public SCFrameworkVersion addFrameworkVersion(
73              long userId, long plid, String name, String url, boolean active,
74              int priority, Boolean addCommunityPermissions,
75              Boolean addGuestPermissions, String[] communityPermissions,
76              String[] guestPermissions)
77          throws PortalException, SystemException {
78  
79          // Framework version
80  
81          User user = userPersistence.findByPrimaryKey(userId);
82          long groupId = PortalUtil.getPortletGroupId(plid);
83          Date now = new Date();
84  
85          validate(name);
86  
87          long frameworkVersionId = counterLocalService.increment();
88  
89          SCFrameworkVersion frameworkVersion =
90              scFrameworkVersionPersistence.create(
91                  frameworkVersionId);
92  
93          frameworkVersion.setGroupId(groupId);
94          frameworkVersion.setCompanyId(user.getCompanyId());
95          frameworkVersion.setUserId(user.getUserId());
96          frameworkVersion.setUserName(user.getFullName());
97          frameworkVersion.setCreateDate(now);
98          frameworkVersion.setModifiedDate(now);
99          frameworkVersion.setName(name);
100         frameworkVersion.setUrl(url);
101         frameworkVersion.setActive(active);
102         frameworkVersion.setPriority(priority);
103 
104         scFrameworkVersionPersistence.update(frameworkVersion);
105 
106         // Resources
107 
108         if ((addCommunityPermissions != null) &&
109             (addGuestPermissions != null)) {
110 
111             addFrameworkVersionResources(
112                 frameworkVersion, addCommunityPermissions.booleanValue(),
113                 addGuestPermissions.booleanValue());
114         }
115         else {
116             addFrameworkVersionResources(
117                 frameworkVersion, communityPermissions, guestPermissions);
118         }
119 
120         return frameworkVersion;
121     }
122 
123     public void addFrameworkVersionResources(
124             long frameworkVersionId, boolean addCommunityPermissions,
125             boolean addGuestPermissions)
126         throws PortalException, SystemException {
127 
128         SCFrameworkVersion frameworkVersion =
129             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
130 
131         addFrameworkVersionResources(
132             frameworkVersion, addCommunityPermissions, addGuestPermissions);
133     }
134 
135     public void addFrameworkVersionResources(
136             SCFrameworkVersion frameworkVersion,
137             boolean addCommunityPermissions, boolean addGuestPermissions)
138         throws PortalException, SystemException {
139 
140         resourceLocalService.addResources(
141             frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
142             frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
143             frameworkVersion.getFrameworkVersionId(), false,
144             addCommunityPermissions, addGuestPermissions);
145     }
146 
147     public void addFrameworkVersionResources(
148             long frameworkVersionId, String[] communityPermissions,
149             String[] guestPermissions)
150         throws PortalException, SystemException {
151 
152         SCFrameworkVersion frameworkVersion =
153             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
154 
155         addFrameworkVersionResources(
156             frameworkVersion, communityPermissions, guestPermissions);
157     }
158 
159     public void addFrameworkVersionResources(
160             SCFrameworkVersion frameworkVersion, String[] communityPermissions,
161             String[] guestPermissions)
162         throws PortalException, SystemException {
163 
164         resourceLocalService.addModelResources(
165             frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
166             frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
167             frameworkVersion.getFrameworkVersionId(), communityPermissions,
168             guestPermissions);
169     }
170 
171     public void deleteFrameworkVersion(long frameworkVersionId)
172         throws PortalException, SystemException {
173 
174         scFrameworkVersionPersistence.remove(frameworkVersionId);
175     }
176 
177     public void deleteFrameworkVersion(SCFrameworkVersion frameworkVersion)
178         throws PortalException, SystemException {
179 
180         scFrameworkVersionPersistence.remove(frameworkVersion);
181     }
182 
183     public void deleteFrameworkVersions(long groupId)
184         throws PortalException, SystemException {
185 
186         Iterator itr = scFrameworkVersionPersistence.findByGroupId(
187             groupId).iterator();
188 
189         while (itr.hasNext()) {
190             SCFrameworkVersion frameworkVersion =
191                 (SCFrameworkVersion)itr.next();
192 
193             deleteFrameworkVersion(frameworkVersion);
194         }
195     }
196 
197     public SCFrameworkVersion getFrameworkVersion(long frameworkVersionId)
198         throws PortalException, SystemException {
199 
200         return scFrameworkVersionPersistence.findByPrimaryKey(
201             frameworkVersionId);
202     }
203 
204     public List getFrameworkVersions(long groupId, int begin, int end)
205         throws SystemException {
206 
207         return scFrameworkVersionPersistence.findByGroupId(groupId, begin, end);
208     }
209 
210     public List getFrameworkVersions(long groupId, boolean active)
211         throws SystemException {
212 
213         return scFrameworkVersionPersistence.findByG_A(groupId, active);
214     }
215 
216     public List getFrameworkVersions(
217             long groupId, boolean active, int begin, int end)
218         throws SystemException {
219 
220         return scFrameworkVersionPersistence.findByG_A(
221             groupId, active, begin, end);
222     }
223 
224     public int getFrameworkVersionsCount(long groupId)
225         throws SystemException {
226 
227         return scFrameworkVersionPersistence.countByGroupId(groupId);
228     }
229 
230     public int getFrameworkVersionsCount(long groupId, boolean active)
231         throws SystemException {
232 
233         return scFrameworkVersionPersistence.countByG_A(groupId, active);
234     }
235 
236     public List getProductVersionFrameworkVersions(long productVersionId)
237         throws PortalException, SystemException {
238 
239         return scProductVersionPersistence.getSCFrameworkVersions(
240             productVersionId);
241     }
242 
243     public SCFrameworkVersion updateFrameworkVersion(
244             long frameworkVersionId, String name, String url, boolean active,
245             int priority)
246         throws PortalException, SystemException {
247 
248         validate(name);
249 
250         SCFrameworkVersion frameworkVersion =
251             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
252 
253         frameworkVersion.setName(name);
254         frameworkVersion.setUrl(url);
255         frameworkVersion.setActive(active);
256         frameworkVersion.setPriority(priority);
257 
258         scFrameworkVersionPersistence.update(frameworkVersion);
259 
260         return frameworkVersion;
261     }
262 
263     protected void validate(String name) throws PortalException {
264         if (Validator.isNull(name)) {
265             throw new FrameworkVersionNameException();
266         }
267     }
268 
269 }