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.PermissionCheckerFactory;
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  public class LayoutsLocalPublisherMessageListener extends BaseMessageListener {
51  
52      public LayoutsLocalPublisherMessageListener(
53          SingleDestinationMessageSender statusSender,
54          MessageSender responseSender) {
55  
56          super(statusSender, responseSender);
57      }
58  
59      protected void doReceive(Message message, MessageStatus messageStatus)
60          throws Exception {
61  
62          LayoutsLocalPublisherRequest publisherRequest =
63              (LayoutsLocalPublisherRequest)JSONFactoryUtil.deserialize(
64                  (String)message.getPayload());
65  
66          messageStatus.setPayload(publisherRequest);
67  
68          String command = publisherRequest.getCommand();
69          long userId = publisherRequest.getUserId();
70          long sourceGroupId = publisherRequest.getSourceGroupId();
71          long targetGroupId = publisherRequest.getTargetGroupId();
72          boolean privateLayout = publisherRequest.isPrivateLayout();
73          Map<Long, Boolean> layoutIdMap = publisherRequest.getLayoutIdMap();
74          Map<String, String[]> parameterMap = publisherRequest.getParameterMap();
75          Date startDate = publisherRequest.getStartDate();
76          Date endDate = publisherRequest.getEndDate();
77  
78          String range = MapUtil.getString(parameterMap, "range");
79  
80          if (range.equals("last")) {
81              int last = MapUtil.getInteger(parameterMap, "last");
82  
83              if (last > 0) {
84                  Date scheduledFireTime =
85                      publisherRequest.getScheduledFireTime();
86  
87                  startDate = new Date(
88                      scheduledFireTime.getTime() - (last * Time.HOUR));
89  
90                  endDate = scheduledFireTime;
91              }
92          }
93  
94          PrincipalThreadLocal.setName(userId);
95  
96          User user = UserLocalServiceUtil.getUserById(userId);
97  
98          PermissionChecker permissionChecker = PermissionCheckerFactory.create(
99              user, false);
100 
101         PermissionThreadLocal.setPermissionChecker(permissionChecker);
102 
103         if (command.equals(
104                 LayoutsLocalPublisherRequest.COMMAND_ALL_PAGES)) {
105 
106             StagingUtil.publishLayouts(
107                 sourceGroupId, targetGroupId, privateLayout, parameterMap,
108                 startDate, endDate);
109         }
110         else if (command.equals(
111             LayoutsLocalPublisherRequest.COMMAND_SELECTED_PAGES)) {
112 
113             StagingUtil.publishLayouts(
114                 sourceGroupId, targetGroupId, privateLayout, layoutIdMap,
115                 parameterMap, startDate, endDate);
116         }
117     }
118 
119 }