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