1   /**
2    * Copyright (c) 2000-2009 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.service.ServiceContext;
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, String name, String url, boolean active, int priority,
50              ServiceContext serviceContext)
51          throws PortalException, SystemException {
52  
53          // Framework version
54  
55          User user = userPersistence.findByPrimaryKey(userId);
56          long groupId = serviceContext.getScopeGroupId();
57          Date now = new Date();
58  
59          validate(name);
60  
61          long frameworkVersionId = counterLocalService.increment();
62  
63          SCFrameworkVersion frameworkVersion =
64              scFrameworkVersionPersistence.create(
65                  frameworkVersionId);
66  
67          frameworkVersion.setGroupId(groupId);
68          frameworkVersion.setCompanyId(user.getCompanyId());
69          frameworkVersion.setUserId(user.getUserId());
70          frameworkVersion.setUserName(user.getFullName());
71          frameworkVersion.setCreateDate(now);
72          frameworkVersion.setModifiedDate(now);
73          frameworkVersion.setName(name);
74          frameworkVersion.setUrl(url);
75          frameworkVersion.setActive(active);
76          frameworkVersion.setPriority(priority);
77  
78          scFrameworkVersionPersistence.update(frameworkVersion, false);
79  
80          // Resources
81  
82          if (serviceContext.getAddCommunityPermissions() ||
83              serviceContext.getAddGuestPermissions()) {
84  
85              addFrameworkVersionResources(
86                  frameworkVersion, serviceContext.getAddCommunityPermissions(),
87                  serviceContext.getAddGuestPermissions());
88          }
89          else {
90              addFrameworkVersionResources(
91                  frameworkVersion, serviceContext.getCommunityPermissions(),
92                  serviceContext.getGuestPermissions());
93          }
94  
95          return frameworkVersion;
96      }
97  
98      public void addFrameworkVersionResources(
99              long frameworkVersionId, boolean addCommunityPermissions,
100             boolean addGuestPermissions)
101         throws PortalException, SystemException {
102 
103         SCFrameworkVersion frameworkVersion =
104             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
105 
106         addFrameworkVersionResources(
107             frameworkVersion, addCommunityPermissions, addGuestPermissions);
108     }
109 
110     public void addFrameworkVersionResources(
111             SCFrameworkVersion frameworkVersion,
112             boolean addCommunityPermissions, boolean addGuestPermissions)
113         throws PortalException, SystemException {
114 
115         resourceLocalService.addResources(
116             frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
117             frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
118             frameworkVersion.getFrameworkVersionId(), false,
119             addCommunityPermissions, addGuestPermissions);
120     }
121 
122     public void addFrameworkVersionResources(
123             long frameworkVersionId, String[] communityPermissions,
124             String[] guestPermissions)
125         throws PortalException, SystemException {
126 
127         SCFrameworkVersion frameworkVersion =
128             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
129 
130         addFrameworkVersionResources(
131             frameworkVersion, communityPermissions, guestPermissions);
132     }
133 
134     public void addFrameworkVersionResources(
135             SCFrameworkVersion frameworkVersion, String[] communityPermissions,
136             String[] guestPermissions)
137         throws PortalException, SystemException {
138 
139         resourceLocalService.addModelResources(
140             frameworkVersion.getCompanyId(), frameworkVersion.getGroupId(),
141             frameworkVersion.getUserId(), SCFrameworkVersion.class.getName(),
142             frameworkVersion.getFrameworkVersionId(), communityPermissions,
143             guestPermissions);
144     }
145 
146     public void deleteFrameworkVersion(long frameworkVersionId)
147         throws PortalException, SystemException {
148 
149         scFrameworkVersionPersistence.remove(frameworkVersionId);
150     }
151 
152     public void deleteFrameworkVersion(SCFrameworkVersion frameworkVersion)
153         throws SystemException {
154 
155         scFrameworkVersionPersistence.remove(frameworkVersion);
156     }
157 
158     public void deleteFrameworkVersions(long groupId) throws SystemException {
159         List<SCFrameworkVersion> frameworkVersions =
160             scFrameworkVersionPersistence.findByGroupId(groupId);
161 
162         for (SCFrameworkVersion frameworkVersion : frameworkVersions) {
163             deleteFrameworkVersion(frameworkVersion);
164         }
165     }
166 
167     public SCFrameworkVersion getFrameworkVersion(long frameworkVersionId)
168         throws PortalException, SystemException {
169 
170         return scFrameworkVersionPersistence.findByPrimaryKey(
171             frameworkVersionId);
172     }
173 
174     public List<SCFrameworkVersion> getFrameworkVersions(
175             long groupId, int start, int end)
176         throws SystemException {
177 
178         return scFrameworkVersionPersistence.findByGroupId(groupId, start, end);
179     }
180 
181     public List<SCFrameworkVersion> getFrameworkVersions(
182             long groupId, boolean active)
183         throws SystemException {
184 
185         return scFrameworkVersionPersistence.findByG_A(groupId, active);
186     }
187 
188     public List<SCFrameworkVersion> getFrameworkVersions(
189             long groupId, boolean active, int start, int end)
190         throws SystemException {
191 
192         return scFrameworkVersionPersistence.findByG_A(
193             groupId, active, start, end);
194     }
195 
196     public int getFrameworkVersionsCount(long groupId)
197         throws SystemException {
198 
199         return scFrameworkVersionPersistence.countByGroupId(groupId);
200     }
201 
202     public int getFrameworkVersionsCount(long groupId, boolean active)
203         throws SystemException {
204 
205         return scFrameworkVersionPersistence.countByG_A(groupId, active);
206     }
207 
208     public List<SCFrameworkVersion> getProductVersionFrameworkVersions(
209             long productVersionId)
210         throws SystemException {
211 
212         return scProductVersionPersistence.getSCFrameworkVersions(
213             productVersionId);
214     }
215 
216     public SCFrameworkVersion updateFrameworkVersion(
217             long frameworkVersionId, String name, String url, boolean active,
218             int priority)
219         throws PortalException, SystemException {
220 
221         validate(name);
222 
223         SCFrameworkVersion frameworkVersion =
224             scFrameworkVersionPersistence.findByPrimaryKey(frameworkVersionId);
225 
226         frameworkVersion.setName(name);
227         frameworkVersion.setUrl(url);
228         frameworkVersion.setActive(active);
229         frameworkVersion.setPriority(priority);
230 
231         scFrameworkVersionPersistence.update(frameworkVersion, false);
232 
233         return frameworkVersion;
234     }
235 
236     protected void validate(String name) throws PortalException {
237         if (Validator.isNull(name)) {
238             throw new FrameworkVersionNameException();
239         }
240     }
241 
242 }