1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.BaseMessageListener;
27  import com.liferay.portal.kernel.messaging.Message;
28  import com.liferay.portal.kernel.messaging.MessageStatus;
29  import com.liferay.portal.kernel.messaging.sender.MessageSender;
30  import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
31  import com.liferay.portal.kernel.util.MapUtil;
32  import com.liferay.portal.kernel.util.Time;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.security.auth.PrincipalThreadLocal;
35  import com.liferay.portal.security.permission.PermissionChecker;
36  import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
37  import com.liferay.portal.security.permission.PermissionThreadLocal;
38  import com.liferay.portal.service.UserLocalServiceUtil;
39  import com.liferay.portlet.communities.util.StagingUtil;
40  
41  import java.util.Date;
42  import java.util.Map;
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 extends BaseMessageListener {
52  
53      public LayoutsLocalPublisherMessageListener(
54          SingleDestinationMessageSender statusSender,
55          MessageSender responseSender) {
56  
57          super(statusSender, responseSender);
58      }
59  
60      protected void doReceive(Message message, MessageStatus messageStatus)
61          throws Exception {
62  
63          LayoutsLocalPublisherRequest publisherRequest =
64              (LayoutsLocalPublisherRequest)JSONFactoryUtil.deserialize(
65                  (String)message.getPayload());
66  
67          messageStatus.setPayload(publisherRequest);
68  
69          String command = publisherRequest.getCommand();
70          long userId = publisherRequest.getUserId();
71          long sourceGroupId = publisherRequest.getSourceGroupId();
72          long targetGroupId = publisherRequest.getTargetGroupId();
73          boolean privateLayout = publisherRequest.isPrivateLayout();
74          Map<Long, Boolean> layoutIdMap = publisherRequest.getLayoutIdMap();
75          Map<String, String[]> parameterMap = publisherRequest.getParameterMap();
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 permissionChecker =
100             PermissionCheckerFactoryUtil.create(user, false);
101 
102         PermissionThreadLocal.setPermissionChecker(permissionChecker);
103 
104         if (command.equals(
105                 LayoutsLocalPublisherRequest.COMMAND_ALL_PAGES)) {
106 
107             StagingUtil.publishLayouts(
108                 sourceGroupId, targetGroupId, privateLayout, parameterMap,
109                 startDate, endDate);
110         }
111         else if (command.equals(
112             LayoutsLocalPublisherRequest.COMMAND_SELECTED_PAGES)) {
113 
114             StagingUtil.publishLayouts(
115                 sourceGroupId, targetGroupId, privateLayout, layoutIdMap,
116                 parameterMap, startDate, endDate);
117         }
118     }
119 
120 }