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