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