1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.NoSuchPortletItemException;
18  import com.liferay.portal.PortletItemNameException;
19  import com.liferay.portal.kernel.exception.PortalException;
20  import com.liferay.portal.kernel.exception.SystemException;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.model.PortletItem;
23  import com.liferay.portal.model.PortletPreferences;
24  import com.liferay.portal.model.User;
25  import com.liferay.portal.service.base.PortletItemLocalServiceBaseImpl;
26  import com.liferay.portal.util.PortalUtil;
27  
28  import java.util.Date;
29  import java.util.List;
30  
31  /**
32   * <a href="PortletItemLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Jorge Ferrer
35   */
36  public class PortletItemLocalServiceImpl
37      extends PortletItemLocalServiceBaseImpl {
38  
39      public PortletItem addPortletItem(
40              long userId, long groupId, String name, String portletId,
41              String className)
42          throws PortalException, SystemException {
43  
44          User user = userPersistence.findByPrimaryKey(userId);
45          long classNameId = PortalUtil.getClassNameId(className);
46          Date now = new Date();
47  
48          validate(name);
49  
50          long portletItemId = counterLocalService.increment();
51  
52          PortletItem portletItem = portletItemPersistence.create(
53              portletItemId);
54  
55          portletItem.setGroupId(groupId);
56          portletItem.setCompanyId(user.getCompanyId());
57          portletItem.setUserId(user.getUserId());
58          portletItem.setUserName(user.getFullName());
59          portletItem.setCreateDate(now);
60          portletItem.setModifiedDate(now);
61          portletItem.setName(name);
62          portletItem.setPortletId(portletId);
63          portletItem.setClassNameId(classNameId);
64  
65          portletItemPersistence.update(portletItem, false);
66  
67          return portletItem;
68      }
69  
70      public PortletItem getPortletItem(long portletItemId)
71          throws PortalException, SystemException {
72  
73          return portletItemPersistence.findByPrimaryKey(portletItemId);
74      }
75  
76      public PortletItem getPortletItem(
77              long groupId, String name, String portletId, String className)
78          throws PortalException, SystemException {
79  
80          long classNameId = PortalUtil.getClassNameId(className);
81  
82          return portletItemPersistence.findByG_N_P_C(
83              groupId, name, portletId, classNameId);
84      }
85  
86      public List<PortletItem> getPortletItems(long groupId, String className)
87          throws SystemException {
88  
89          long classNameId = PortalUtil.getClassNameId(className);
90  
91          return portletItemPersistence.findByG_C(groupId, classNameId);
92      }
93  
94      public List<PortletItem> getPortletItems(
95              long groupId, String portletId, String className)
96          throws SystemException {
97  
98          long classNameId = PortalUtil.getClassNameId(className);
99  
100         return portletItemPersistence.findByG_P_C(
101             groupId, portletId, classNameId);
102     }
103 
104     public PortletItem updatePortletItem(
105             long userId, long groupId, String name, String portletId,
106             String className)
107         throws PortalException, SystemException {
108 
109         PortletItem portletItem = null;
110 
111         try {
112             User user = userPersistence.findByPrimaryKey(userId);
113 
114             portletItem = getPortletItem(
115                 groupId, name, portletId, PortletPreferences.class.getName());
116 
117             portletItem.setUserId(userId);
118             portletItem.setUserName(user.getFullName());
119             portletItem.setModifiedDate(new Date());
120 
121             portletItemPersistence.update(portletItem, false);
122         }
123         catch (NoSuchPortletItemException nsste) {
124             portletItem = addPortletItem(
125                 userId, groupId, name, portletId,
126                 PortletPreferences.class.getName());
127         }
128 
129         return portletItem;
130     }
131 
132     protected void validate(String name) throws PortalException {
133         if (Validator.isNull(name)) {
134             throw new PortletItemNameException();
135         }
136     }
137 
138 }