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="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(Object message) {
53          throw new UnsupportedOperationException();
54      }
55  
56      public void receive(String message) {
57          PermissionChecker permissionChecker = null;
58  
59          try {
60              LayoutsLocalPublisherRequest publisherRequest =
61                  (LayoutsLocalPublisherRequest)JSONFactoryUtil.deserialize(
62                      message);
63  
64              String command = publisherRequest.getCommand();
65  
66              long userId = publisherRequest.getUserId();
67              long sourceGroupId = publisherRequest.getSourceGroupId();
68              long targetGroupId = publisherRequest.getTargetGroupId();
69              boolean privateLayout = publisherRequest.isPrivateLayout();
70              Map<Long, Boolean> layoutIdMap = publisherRequest.getLayoutIdMap();
71              Map<String, String[]> parameterMap =
72                  publisherRequest.getParameterMap();
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             if (command.equals(
101                     LayoutsLocalPublisherRequest.COMMAND_ALL_PAGES)) {
102 
103                 StagingUtil.publishLayouts(
104                     sourceGroupId, targetGroupId, privateLayout, parameterMap,
105                     startDate, endDate);
106             }
107             else if (command.equals(
108                 LayoutsLocalPublisherRequest.COMMAND_SELECTED_PAGES)) {
109 
110                 StagingUtil.publishLayouts(
111                     sourceGroupId, targetGroupId, privateLayout, layoutIdMap,
112                     parameterMap, startDate, endDate);
113             }
114         }
115         catch (Exception e) {
116             _log.error(e, e);
117         }
118         finally {
119             try {
120                 PermissionCheckerFactory.recycle(permissionChecker);
121             }
122             catch (Exception e) {
123             }
124         }
125     }
126 
127     private static Log _log =
128         LogFactory.getLog(LayoutsLocalPublisherMessageListener.class);
129 
130 }