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