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