001
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.GetterUtil;
024 import com.liferay.portal.kernel.util.MapUtil;
025 import com.liferay.portal.kernel.util.Time;
026 import com.liferay.portal.model.LayoutSet;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.security.auth.PrincipalThreadLocal;
029 import com.liferay.portal.security.permission.PermissionChecker;
030 import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
031 import com.liferay.portal.security.permission.PermissionThreadLocal;
032 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
033 import com.liferay.portal.service.ServiceContext;
034 import com.liferay.portal.service.ServiceContextThreadLocal;
035 import com.liferay.portal.service.UserLocalServiceUtil;
036 import com.liferay.portal.util.PortalUtil;
037
038 import java.io.Serializable;
039
040 import java.util.Date;
041 import java.util.HashMap;
042 import java.util.Map;
043
044
047 public class LayoutsRemotePublisherMessageListener
048 extends BaseMessageStatusMessageListener {
049
050 public LayoutsRemotePublisherMessageListener() {
051 }
052
053
056 public LayoutsRemotePublisherMessageListener(
057 SingleDestinationMessageSender statusSender,
058 MessageSender responseSender) {
059
060 super(statusSender, responseSender);
061 }
062
063 @Override
064 protected void doReceive(Message message, MessageStatus messageStatus)
065 throws Exception {
066
067 LayoutsRemotePublisherRequest publisherRequest =
068 (LayoutsRemotePublisherRequest)message.getPayload();
069
070 messageStatus.setPayload(publisherRequest);
071
072 long userId = publisherRequest.getUserId();
073 long sourceGroupId = publisherRequest.getSourceGroupId();
074 boolean privateLayout = publisherRequest.isPrivateLayout();
075 Map<Long, Boolean> layoutIdMap = publisherRequest.getLayoutIdMap();
076 Map<String, String[]> parameterMap = publisherRequest.getParameterMap();
077 String remoteAddress = publisherRequest.getRemoteAddress();
078 int remotePort = publisherRequest.getRemotePort();
079 String remotePathContext = publisherRequest.getRemotePathContext();
080 boolean secureConnection = publisherRequest.isSecureConnection();
081 long remoteGroupId = publisherRequest.getRemoteGroupId();
082 boolean remotePrivateLayout = publisherRequest.isRemotePrivateLayout();
083 Date startDate = publisherRequest.getStartDate();
084 Date endDate = publisherRequest.getEndDate();
085
086 String range = MapUtil.getString(parameterMap, "range");
087
088 if (range.equals("fromLastPublishDate")) {
089 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
090 sourceGroupId, privateLayout);
091
092 long lastPublishDate = GetterUtil.getLong(
093 layoutSet.getSettingsProperty("last-publish-date"));
094
095 if (lastPublishDate > 0) {
096 endDate = new Date();
097
098 startDate = new Date(lastPublishDate);
099 }
100 }
101 else if (range.equals("last")) {
102 int last = MapUtil.getInteger(parameterMap, "last");
103
104 if (last > 0) {
105 Date scheduledFireTime =
106 publisherRequest.getScheduledFireTime();
107
108 startDate = new Date(
109 scheduledFireTime.getTime() - (last * Time.HOUR));
110
111 endDate = scheduledFireTime;
112 }
113 }
114
115 PrincipalThreadLocal.setName(userId);
116
117 User user = UserLocalServiceUtil.getUserById(userId);
118
119 PermissionChecker permissionChecker =
120 PermissionCheckerFactoryUtil.create(user);
121
122 PermissionThreadLocal.setPermissionChecker(permissionChecker);
123
124 ServiceContext serviceContext = new ServiceContext();
125
126 serviceContext.setCompanyId(user.getCompanyId());
127 serviceContext.setPathMain(PortalUtil.getPathMain());
128 serviceContext.setSignedIn(!user.isDefaultUser());
129 serviceContext.setUserId(user.getUserId());
130
131 Map<String, Serializable> attributes =
132 new HashMap<String, Serializable>();
133
134 for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
135 String param = entry.getKey();
136 String[] values = entry.getValue();
137
138 if ((values != null) && (values.length > 0)) {
139 if (values.length == 1) {
140 attributes.put(param, values[0]);
141 }
142 else {
143 attributes.put(param, values);
144 }
145 }
146 }
147
148 serviceContext.setAttributes(attributes);
149
150 ServiceContextThreadLocal.pushServiceContext(serviceContext);
151
152 try {
153 StagingUtil.copyRemoteLayouts(
154 sourceGroupId, privateLayout, layoutIdMap, parameterMap,
155 remoteAddress, remotePort, remotePathContext, secureConnection,
156 remoteGroupId, remotePrivateLayout, startDate, endDate);
157 }
158 finally {
159 PrincipalThreadLocal.setName(null);
160 PermissionThreadLocal.setPermissionChecker(null);
161 }
162 }
163
164 }