1   /**
2    * Copyright (c) 2000-2008 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.communities.messaging;
24  
25  import com.liferay.portal.kernel.json.JSONFactoryUtil;
26  import com.liferay.portal.kernel.messaging.MessageListener;
27  import com.liferay.portal.kernel.util.Time;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.security.auth.PrincipalThreadLocal;
30  import com.liferay.portal.security.permission.PermissionChecker;
31  import com.liferay.portal.security.permission.PermissionCheckerFactory;
32  import com.liferay.portal.security.permission.PermissionThreadLocal;
33  import com.liferay.portal.service.UserLocalServiceUtil;
34  import com.liferay.portlet.communities.util.StagingUtil;
35  import com.liferay.util.MapUtil;
36  
37  import java.util.Date;
38  import java.util.Map;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /**
44   * <a href="LayoutsRemotePublisherMessageListener.java.html"><b><i>View Source
45   * </i></b></a>
46   *
47   * @author Bruno Farache
48   *
49   */
50  public class LayoutsRemotePublisherMessageListener implements MessageListener {
51  
52      public void receive(Object message) {
53          throw new UnsupportedOperationException();
54      }
55  
56      public void receive(String message) {
57          PermissionChecker permissionChecker = null;
58  
59          try {
60              LayoutsRemotePublisherRequest publisherRequest =
61                  (LayoutsRemotePublisherRequest)JSONFactoryUtil.deserialize(
62                      message);
63  
64              long userId = publisherRequest.getUserId();
65              long sourceGroupId = publisherRequest.getSourceGroupId();
66              boolean privateLayout = publisherRequest.isPrivateLayout();
67              Map<Long, Boolean> layoutIdMap = publisherRequest.getLayoutIdMap();
68              Map<String, String[]> parameterMap =
69                  publisherRequest.getParameterMap();
70              String remoteAddress = publisherRequest.getRemoteAddress();
71              int remotePort = publisherRequest.getRemotePort();
72              boolean secureConnection = publisherRequest.isSecureConnection();
73              long remoteGroupId = publisherRequest.getRemoteGroupId();
74              boolean remotePrivateLayout =
75                  publisherRequest.isRemotePrivateLayout();
76              Date startDate = publisherRequest.getStartDate();
77              Date endDate = publisherRequest.getEndDate();
78  
79              String range = MapUtil.getString(parameterMap, "range");
80  
81              if (range.equals("last")) {
82                  int last = MapUtil.getInteger(parameterMap, "last");
83  
84                  if (last > 0) {
85                      Date scheduledFireTime =
86                          publisherRequest.getScheduledFireTime();
87  
88                      startDate = new Date(
89                          scheduledFireTime.getTime() - (last * Time.HOUR));
90  
91                      endDate = scheduledFireTime;
92                  }
93              }
94  
95              PrincipalThreadLocal.setName(userId);
96  
97              User user = UserLocalServiceUtil.getUserById(userId);
98  
99              permissionChecker = PermissionCheckerFactory.create(user, false);
100 
101             PermissionThreadLocal.setPermissionChecker(permissionChecker);
102 
103             StagingUtil.copyRemoteLayouts(
104                 sourceGroupId, privateLayout, layoutIdMap, parameterMap,
105                 remoteAddress, remotePort, secureConnection, remoteGroupId,
106                 remotePrivateLayout, parameterMap, startDate, endDate);
107         }
108         catch (Exception e) {
109             _log.error(e, e);
110         }
111         finally {
112             try {
113                 PermissionCheckerFactory.recycle(permissionChecker);
114             }
115             catch (Exception e) {
116             }
117         }
118     }
119 
120     private static Log _log =
121         LogFactory.getLog(LayoutsRemotePublisherMessageListener.class);
122 
123 }