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