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.List;
36  
37  /**
38   * <a href="SCFrameworkVersionLocalServiceImpl.java.html"><b><i>View Source</i>
39   * </b></a>
40   *
41   * @author Jorge Ferrer
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class SCFrameworkVersionLocalServiceImpl
46      extends SCFrameworkVersionLocalServiceBaseImpl {
47  
48      public SCFrameworkVersion addFrameworkVersion(
49              long userId, long plid, String name, String url, boolean active,
50              int priority, boolean addCommunityPermissions,
51              boolean addGuestPermissions)
52          throws PortalException, SystemException {
53  
54          return addFrameworkVersion(
55              userId, plid, name, url, active, priority,
56              Boolean.valueOf(addCommunityPermissions),
57              Boolean.valueOf(addGuestPermissions), null, null);
58      }
59  
60      public SCFrameworkVersion addFrameworkVersion(
61              long userId, long plid, String name, String url, boolean active,
62              int priority, String[] communityPermissions,
63              String[] guestPermissions)
64          throws PortalException, SystemException {
65  
66          return addFrameworkVersion(
67              userId, plid, name, url, active, priority, null, null,
68              communityPermissions, guestPermissions);
69      }
70  
71      public SCFrameworkVersion addFrameworkVersion(
72              long userId, long plid, String name, String url, boolean active,
73              int priority, Boolean addCommunityPermissions,
74              Boolean addGuestPermissions, String[] communityPermissions,
75              String[] guestPermissions)
76          throws PortalException, SystemException {
77  
78          // Framework version
79  
80          User user = userPersistence.findByPrimaryKey(userId);
81          long groupId = PortalUtil.getPortletGroupId(plid);
82          Date now = new Date();
83  
84          validate(name);
85  
86          long frameworkVersionId = counterLocalService.increment();
87  
88          SCFrameworkVersion frameworkVersion =
89              scFrameworkVersionPersistence.create(
90                  frameworkVersionId);
91  
92          frameworkVersion.setGroupId(groupId);
93          frameworkVersion.setCompanyId(user.getCompanyId());
94          frameworkVersion.setUserId(user.getUserId());
95          frameworkVersion.setUserName(user.getFullName());
96          frameworkVersion.setCreateDate(now);
97          frameworkVersion.setModifiedDate(now);
98          frameworkVersion.setName(name);
99          frameworkVersion.setUrl(url);
100         frameworkVersion.setActive(active);
101         frameworkVersion.setPriority(priority);
102 
103         scFrameworkVersionPersistence.update(frameworkVersion, false);
104 
105         // Resources
106 
107         if ((addCommunityPermissions != null) &&
108             (addGuestPermissions != null)) {
109 
110             addFrameworkVersionResources(
111                 frameworkVersion, addCommunityPermissions.booleanValue(),
112                 addGuestPermissions.booleanValue());
113         }
114         else {
115             addFrameworkVersionResources(
116                 frameworkVersion, communityPermissions, guestPermissions);
117         }
118 
119         return frameworkVersion;
120     }
121 
122     public void addFrameworkVersionResources(
123             long frameworkVersionId, boolean addCommunityPermissions,
124             boolean addGuestPermissions)
125         throws PortalException, SystemException {
126 
127         SCFrameworkVersion frameworkVersion =
128             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
129 
130         addFrameworkVersionResources(
131             frameworkVersion, addCommunityPermissions, addGuestPermissions);
132     }
133 
134     public void addFrameworkVersionResources(
135             SCFrameworkVersion frameworkVersion,
136             boolean addCommunityPermissions, boolean addGuestPermissions)
137         throws PortalException, SystemException {
138 
139         resourceLocalService.addResources(
140             frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
141             frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
142             frameworkVersion.getFrameworkVersionId(), false,
143             addCommunityPermissions, addGuestPermissions);
144     }
145 
146     public void addFrameworkVersionResources(
147             long frameworkVersionId, String[] communityPermissions,
148             String[] guestPermissions)
149         throws PortalException, SystemException {
150 
151         SCFrameworkVersion frameworkVersion =
152             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
153 
154         addFrameworkVersionResources(
155             frameworkVersion, communityPermissions, guestPermissions);
156     }
157 
158     public void addFrameworkVersionResources(
159             SCFrameworkVersion frameworkVersion, String[] communityPermissions,
160             String[] guestPermissions)
161         throws PortalException, SystemException {
162 
163         resourceLocalService.addModelResources(
164             frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
165             frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
166             frameworkVersion.getFrameworkVersionId(), communityPermissions,
167             guestPermissions);
168     }
169 
170     public void deleteFrameworkVersion(long frameworkVersionId)
171         throws PortalException, SystemException {
172 
173         scFrameworkVersionPersistence.remove(frameworkVersionId);
174     }
175 
176     public void deleteFrameworkVersion(SCFrameworkVersion frameworkVersion)
177         throws SystemException {
178 
179         scFrameworkVersionPersistence.remove(frameworkVersion);
180     }
181 
182     public void deleteFrameworkVersions(long groupId) throws SystemException {
183         List<SCFrameworkVersion> frameworkVersions =
184             scFrameworkVersionPersistence.findByGroupId(groupId);
185 
186         for (SCFrameworkVersion frameworkVersion : frameworkVersions) {
187             deleteFrameworkVersion(frameworkVersion);
188         }
189     }
190 
191     public SCFrameworkVersion getFrameworkVersion(long frameworkVersionId)
192         throws PortalException, SystemException {
193 
194         return scFrameworkVersionPersistence.findByPrimaryKey(
195             frameworkVersionId);
196     }
197 
198     public List<SCFrameworkVersion> getFrameworkVersions(
199             long groupId, int start, int end)
200         throws SystemException {
201 
202         return scFrameworkVersionPersistence.findByGroupId(groupId, start, end);
203     }
204 
205     public List<SCFrameworkVersion> getFrameworkVersions(
206             long groupId, boolean active)
207         throws SystemException {
208 
209         return scFrameworkVersionPersistence.findByG_A(groupId, active);
210     }
211 
212     public List<SCFrameworkVersion> getFrameworkVersions(
213             long groupId, boolean active, int start, int end)
214         throws SystemException {
215 
216         return scFrameworkVersionPersistence.findByG_A(
217             groupId, active, start, end);
218     }
219 
220     public int getFrameworkVersionsCount(long groupId)
221         throws SystemException {
222 
223         return scFrameworkVersionPersistence.countByGroupId(groupId);
224     }
225 
226     public int getFrameworkVersionsCount(long groupId, boolean active)
227         throws SystemException {
228 
229         return scFrameworkVersionPersistence.countByG_A(groupId, active);
230     }
231 
232     public List<SCFrameworkVersion> getProductVersionFrameworkVersions(
233             long productVersionId)
234         throws SystemException {
235 
236         return scProductVersionPersistence.getSCFrameworkVersions(
237             productVersionId);
238     }
239 
240     public SCFrameworkVersion updateFrameworkVersion(
241             long frameworkVersionId, String name, String url, boolean active,
242             int priority)
243         throws PortalException, SystemException {
244 
245         validate(name);
246 
247         SCFrameworkVersion frameworkVersion =
248             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
249 
250         frameworkVersion.setName(name);
251         frameworkVersion.setUrl(url);
252         frameworkVersion.setActive(active);
253         frameworkVersion.setPriority(priority);
254 
255         scFrameworkVersionPersistence.update(frameworkVersion, false);
256 
257         return frameworkVersion;
258     }
259 
260     protected void validate(String name) throws PortalException {
261         if (Validator.isNull(name)) {
262             throw new FrameworkVersionNameException();
263         }
264     }
265 
266 }