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.portal.service.impl;
24  
25  import com.liferay.portal.NoSuchPortletItemException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.PortletItemNameException;
28  import com.liferay.portal.SystemException;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.PortletItem;
31  import com.liferay.portal.model.PortletPreferences;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.service.base.PortletItemLocalServiceBaseImpl;
34  import com.liferay.portal.util.PortalUtil;
35  
36  import java.util.Date;
37  import java.util.List;
38  
39  /**
40   * <a href="PortletItemLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Jorge Ferrer
43   */
44  public class PortletItemLocalServiceImpl
45      extends PortletItemLocalServiceBaseImpl {
46  
47      public PortletItem addPortletItem(
48              long userId, long groupId, String name, String portletId,
49              String className)
50          throws PortalException, SystemException {
51  
52          User user = userPersistence.findByPrimaryKey(userId);
53          long classNameId = PortalUtil.getClassNameId(className);
54          Date now = new Date();
55  
56          validate(name);
57  
58          long portletItemId = counterLocalService.increment();
59  
60          PortletItem portletItem = portletItemPersistence.create(
61              portletItemId);
62  
63          portletItem.setGroupId(groupId);
64          portletItem.setCompanyId(user.getCompanyId());
65          portletItem.setUserId(user.getUserId());
66          portletItem.setUserName(user.getFullName());
67          portletItem.setCreateDate(now);
68          portletItem.setModifiedDate(now);
69          portletItem.setName(name);
70          portletItem.setPortletId(portletId);
71          portletItem.setClassNameId(classNameId);
72  
73          portletItemPersistence.update(portletItem, false);
74  
75          return portletItem;
76      }
77  
78      public PortletItem getPortletItem(long portletItemId)
79          throws PortalException, SystemException {
80  
81          return portletItemPersistence.findByPrimaryKey(portletItemId);
82      }
83  
84      public PortletItem getPortletItem(
85              long groupId, String name, String portletId, String className)
86          throws PortalException, SystemException {
87  
88          long classNameId = PortalUtil.getClassNameId(className);
89  
90          return portletItemPersistence.findByG_N_P_C(
91              groupId, name, portletId, classNameId);
92      }
93  
94      public List<PortletItem> getPortletItems(long groupId, String className)
95          throws SystemException {
96  
97          long classNameId = PortalUtil.getClassNameId(className);
98  
99          return portletItemPersistence.findByG_C(groupId, classNameId);
100     }
101 
102     public List<PortletItem> getPortletItems(
103             long groupId, String portletId, String className)
104         throws SystemException {
105 
106         long classNameId = PortalUtil.getClassNameId(className);
107 
108         return portletItemPersistence.findByG_P_C(
109             groupId, portletId, classNameId);
110     }
111 
112     public PortletItem updatePortletItem(
113             long userId, long groupId, String name, String portletId,
114             String className)
115         throws PortalException, SystemException {
116 
117         PortletItem portletItem = null;
118 
119         try {
120             User user = userPersistence.findByPrimaryKey(userId);
121 
122             portletItem = getPortletItem(
123                 groupId, name, portletId, PortletPreferences.class.getName());
124 
125             portletItem.setUserId(userId);
126             portletItem.setUserName(user.getFullName());
127             portletItem.setModifiedDate(new Date());
128 
129             portletItemPersistence.update(portletItem, false);
130         }
131         catch (NoSuchPortletItemException nsste) {
132             portletItem = addPortletItem(
133                 userId, groupId, name, portletId,
134                 PortletPreferences.class.getName());
135         }
136 
137         return portletItem;
138     }
139 
140     protected void validate(String name) throws PortalException {
141         if (Validator.isNull(name)) {
142             throw new PortletItemNameException();
143         }
144     }
145 
146 }