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.portlet.pageratings.lar;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.lar.BasePortletDataHandler;
27  import com.liferay.portal.lar.PortletDataContext;
28  import com.liferay.portal.lar.PortletDataException;
29  import com.liferay.portal.lar.PortletDataHandlerBoolean;
30  import com.liferay.portal.lar.PortletDataHandlerControl;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
33  
34  import javax.portlet.PortletPreferences;
35  
36  /**
37   * <a href="PageRatingsPortletDataHandlerImpl.java.html"><b><i>View Source</i>
38   * </b></a>
39   *
40   * @author Bruno Farache
41   *
42   */
43  public class PageRatingsPortletDataHandlerImpl extends BasePortletDataHandler {
44  
45      public PortletPreferences deleteData(
46              PortletDataContext context, String portletId,
47              PortletPreferences preferences)
48          throws PortletDataException {
49  
50          try {
51              RatingsStatsLocalServiceUtil.deleteStats(
52                  Layout.class.getName(), context.getPlid());
53  
54              return null;
55          }
56          catch (Exception e) {
57              throw new PortletDataException(e);
58          }
59      }
60  
61      public String exportData(
62              PortletDataContext context, String portletId,
63              PortletPreferences preferences)
64          throws PortletDataException {
65  
66          try {
67              context.addRatingsEntries(
68                  Layout.class, new Long(context.getPlid()));
69  
70              return String.valueOf(context.getPlid());
71          }
72          catch (Exception e) {
73              throw new PortletDataException(e);
74          }
75      }
76  
77      public PortletDataHandlerControl[] getExportControls() {
78          return new PortletDataHandlerControl[] {_ratings};
79      }
80  
81      public PortletDataHandlerControl[] getImportControls() {
82          return new PortletDataHandlerControl[] {_ratings};
83      }
84  
85      public PortletPreferences importData(
86              PortletDataContext context, String portletId,
87              PortletPreferences preferences, String data)
88          throws PortletDataException {
89  
90          try {
91              context.importRatingsEntries(
92                  Layout.class, GetterUtil.getLong(data),
93                  new Long(context.getPlid()));
94  
95              return null;
96          }
97          catch (Exception e) {
98              throw new PortletDataException(e);
99          }
100     }
101 
102     private static final String _NAMESPACE = "page_ratings";
103 
104     private static final PortletDataHandlerBoolean _ratings =
105         new PortletDataHandlerBoolean(_NAMESPACE, "ratings", true, true);
106 
107 }