1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.pageratings.lar;
21  
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.lar.PortletDataContext;
24  import com.liferay.portal.lar.PortletDataException;
25  import com.liferay.portal.lar.PortletDataHandler;
26  import com.liferay.portal.lar.PortletDataHandlerBoolean;
27  import com.liferay.portal.lar.PortletDataHandlerControl;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
30  
31  import javax.portlet.PortletPreferences;
32  
33  /**
34   * <a href="PageRatingsPortletDataHandlerImpl.java.html"><b><i>View Source</i>
35   * </b></a>
36   *
37   * @author Bruno Farache
38   *
39   */
40  public class PageRatingsPortletDataHandlerImpl implements PortletDataHandler {
41  
42      public PortletPreferences deleteData(
43              PortletDataContext context, String portletId,
44              PortletPreferences prefs)
45          throws PortletDataException {
46  
47          try {
48              RatingsStatsLocalServiceUtil.deleteStats(
49                  Layout.class.getName(), context.getPlid());
50  
51              return null;
52          }
53          catch (Exception e) {
54              throw new PortletDataException(e);
55          }
56      }
57  
58      public String exportData(
59              PortletDataContext context, String portletId,
60              PortletPreferences prefs)
61          throws PortletDataException {
62  
63          try {
64              context.addRatingsEntries(
65                  Layout.class, new Long(context.getPlid()));
66  
67              return String.valueOf(context.getPlid());
68          }
69          catch (Exception e) {
70              throw new PortletDataException(e);
71          }
72      }
73  
74      public PortletDataHandlerControl[] getExportControls() {
75          return new PortletDataHandlerControl[] {_ratings};
76      }
77  
78      public PortletDataHandlerControl[] getImportControls() {
79          return new PortletDataHandlerControl[] {_ratings};
80      }
81  
82      public PortletPreferences importData(
83              PortletDataContext context, String portletId,
84              PortletPreferences prefs, String data)
85          throws PortletDataException {
86  
87          try {
88              context.importRatingsEntries(
89                  Layout.class, GetterUtil.getLong(data),
90                  new Long(context.getPlid()));
91  
92              return null;
93          }
94          catch (Exception e) {
95              throw new PortletDataException(e);
96          }
97      }
98  
99      public boolean isPublishToLiveByDefault() {
100         return false;
101     }
102 
103     private static final String _NAMESPACE = "page_ratings";
104 
105     private static final PortletDataHandlerBoolean _ratings =
106         new PortletDataHandlerBoolean(_NAMESPACE, "ratings", true, true);
107 
108 }