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="LayoutsLocalPublisherMessageListener.java.html"><b><i>View Source
46   * </i></b></a>
47   *
48   * @author Bruno Farache
49   *
50   */
51  public class LayoutsLocalPublisherMessageListener implements MessageListener {
52  
53      public void receive(Message message) {
54          PermissionChecker permissionChecker = null;
55  
56          try {
57              LayoutsLocalPublisherRequest publisherRequest =
58                  (LayoutsLocalPublisherRequest)JSONFactoryUtil.deserialize(
59                      (String)message.getPayload());
60  
61              String command = publisherRequest.getCommand();
62  
63              long userId = publisherRequest.getUserId();
64              long sourceGroupId = publisherRequest.getSourceGroupId();
65              long targetGroupId = publisherRequest.getTargetGroupId();
66              boolean privateLayout = publisherRequest.isPrivateLayout();
67              Map<Long, Boolean> layoutIdMap = publisherRequest.getLayoutIdMap();
68              Map<String, String[]> parameterMap =
69                  publisherRequest.getParameterMap();
70              Date startDate = publisherRequest.getStartDate();
71              Date endDate = publisherRequest.getEndDate();
72  
73              String range = MapUtil.getString(parameterMap, "range");
74  
75              if (range.equals("last")) {
76                  int last = MapUtil.getInteger(parameterMap, "last");
77  
78                  if (last > 0) {
79                      Date scheduledFireTime =
80                          publisherRequest.getScheduledFireTime();
81  
82                      startDate = new Date(
83                          scheduledFireTime.getTime() - (last * Time.HOUR));
84  
85                      endDate = scheduledFireTime;
86                  }
87              }
88  
89              PrincipalThreadLocal.setName(userId);
90  
91              User user = UserLocalServiceUtil.getUserById(userId);
92  
93              permissionChecker = PermissionCheckerFactory.create(user, false);
94  
95              PermissionThreadLocal.setPermissionChecker(permissionChecker);
96  
97              if (command.equals(
98                      LayoutsLocalPublisherRequest.COMMAND_ALL_PAGES)) {
99  
100                 StagingUtil.publishLayouts(
101                     sourceGroupId, targetGroupId, privateLayout, parameterMap,
102                     startDate, endDate);
103             }
104             else if (command.equals(
105                 LayoutsLocalPublisherRequest.COMMAND_SELECTED_PAGES)) {
106 
107                 StagingUtil.publishLayouts(
108                     sourceGroupId, targetGroupId, privateLayout, layoutIdMap,
109                     parameterMap, startDate, endDate);
110             }
111         }
112         catch (Exception e) {
113             _log.error(e, e);
114         }
115         finally {
116             try {
117                 PermissionCheckerFactory.recycle(permissionChecker);
118             }
119             catch (Exception e) {
120             }
121         }
122     }
123 
124     private static Log _log =
125         LogFactory.getLog(LayoutsLocalPublisherMessageListener.class);
126 
127 }