001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.messaging;
016    
017    import com.liferay.portal.kernel.messaging.BaseMessageStatusMessageListener;
018    import com.liferay.portal.kernel.messaging.Message;
019    import com.liferay.portal.kernel.messaging.MessageStatus;
020    import com.liferay.portal.kernel.messaging.sender.MessageSender;
021    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
022    import com.liferay.portal.kernel.staging.StagingUtil;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.MapUtil;
026    import com.liferay.portal.kernel.util.Time;
027    import com.liferay.portal.model.CompanyConstants;
028    import com.liferay.portal.model.LayoutSet;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.security.auth.CompanyThreadLocal;
031    import com.liferay.portal.security.auth.PrincipalThreadLocal;
032    import com.liferay.portal.security.permission.PermissionChecker;
033    import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
034    import com.liferay.portal.security.permission.PermissionThreadLocal;
035    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
036    import com.liferay.portal.service.ServiceContext;
037    import com.liferay.portal.service.ServiceContextThreadLocal;
038    import com.liferay.portal.service.UserLocalServiceUtil;
039    import com.liferay.portal.util.PortalUtil;
040    
041    import java.io.Serializable;
042    
043    import java.util.Date;
044    import java.util.HashMap;
045    import java.util.Map;
046    
047    /**
048     * @author Bruno Farache
049     */
050    public class LayoutsRemotePublisherMessageListener
051            extends BaseMessageStatusMessageListener {
052    
053            public LayoutsRemotePublisherMessageListener() {
054            }
055    
056            /**
057             * @deprecated As of 6.1.0
058             */
059            public LayoutsRemotePublisherMessageListener(
060                    SingleDestinationMessageSender statusSender,
061                    MessageSender responseSender) {
062    
063                    super(statusSender, responseSender);
064            }
065    
066            @Override
067            protected void doReceive(Message message, MessageStatus messageStatus)
068                    throws Exception {
069    
070                    LayoutsRemotePublisherRequest publisherRequest =
071                            (LayoutsRemotePublisherRequest)message.getPayload();
072    
073                    messageStatus.setPayload(publisherRequest);
074    
075                    long userId = publisherRequest.getUserId();
076                    long sourceGroupId = publisherRequest.getSourceGroupId();
077                    boolean privateLayout = publisherRequest.isPrivateLayout();
078                    Map<Long, Boolean> layoutIdMap = publisherRequest.getLayoutIdMap();
079                    Map<String, String[]> parameterMap = publisherRequest.getParameterMap();
080                    String remoteAddress = publisherRequest.getRemoteAddress();
081                    int remotePort = publisherRequest.getRemotePort();
082                    String remotePathContext = publisherRequest.getRemotePathContext();
083                    boolean secureConnection = publisherRequest.isSecureConnection();
084                    long remoteGroupId = publisherRequest.getRemoteGroupId();
085                    boolean remotePrivateLayout = publisherRequest.isRemotePrivateLayout();
086                    Date startDate = publisherRequest.getStartDate();
087                    Date endDate = publisherRequest.getEndDate();
088    
089                    String range = MapUtil.getString(parameterMap, "range");
090    
091                    if (range.equals("fromLastPublishDate")) {
092                            LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
093                                    sourceGroupId, privateLayout);
094    
095                            long lastPublishDate = GetterUtil.getLong(
096                                    layoutSet.getSettingsProperty("last-publish-date"));
097    
098                            if (lastPublishDate > 0) {
099                                    endDate = new Date();
100    
101                                    startDate = new Date(lastPublishDate);
102                            }
103                    }
104                    else if (range.equals("last")) {
105                            int last = MapUtil.getInteger(parameterMap, "last");
106    
107                            if (last > 0) {
108                                    Date scheduledFireTime =
109                                            publisherRequest.getScheduledFireTime();
110    
111                                    if (scheduledFireTime == null) {
112                                            scheduledFireTime = new Date();
113                                    }
114    
115                                    startDate = new Date(
116                                            scheduledFireTime.getTime() - (last * Time.HOUR));
117    
118                                    endDate = scheduledFireTime;
119                            }
120                    }
121    
122                    initThreadLocals(userId, parameterMap);
123    
124                    try {
125                            StagingUtil.copyRemoteLayouts(
126                                    sourceGroupId, privateLayout, layoutIdMap, parameterMap,
127                                    remoteAddress, remotePort, remotePathContext, secureConnection,
128                                    remoteGroupId, remotePrivateLayout, startDate, endDate);
129                    }
130                    finally {
131                            resetThreadLocals();
132                    }
133            }
134    
135            protected void initThreadLocals(
136                            long userId, Map<String, String[]> parameterMap)
137                    throws Exception {
138    
139                    User user = UserLocalServiceUtil.getUserById(userId);
140    
141                    CompanyThreadLocal.setCompanyId(user.getCompanyId());
142    
143                    PermissionChecker permissionChecker =
144                            PermissionCheckerFactoryUtil.create(user);
145    
146                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
147    
148                    PrincipalThreadLocal.setName(user.getUserId());
149    
150                    ServiceContext serviceContext = new ServiceContext();
151    
152                    serviceContext.setCompanyId(user.getCompanyId());
153                    serviceContext.setPathMain(PortalUtil.getPathMain());
154                    serviceContext.setSignedIn(!user.isDefaultUser());
155                    serviceContext.setUserId(user.getUserId());
156    
157                    Map<String, Serializable> attributes =
158                            new HashMap<String, Serializable>();
159    
160                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
161                            String param = entry.getKey();
162                            String[] values = entry.getValue();
163    
164                            if (ArrayUtil.isNotEmpty(values)) {
165                                    if (values.length == 1) {
166                                            attributes.put(param, values[0]);
167                                    }
168                                    else {
169                                            attributes.put(param, values);
170                                    }
171                            }
172                    }
173    
174                    serviceContext.setAttributes(attributes);
175    
176                    ServiceContextThreadLocal.pushServiceContext(serviceContext);
177            }
178    
179            protected void resetThreadLocals() {
180                    CompanyThreadLocal.setCompanyId(CompanyConstants.SYSTEM);
181                    PermissionThreadLocal.setPermissionChecker(null);
182                    PrincipalThreadLocal.setName(null);
183                    ServiceContextThreadLocal.popServiceContext();
184            }
185    
186    }