-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlatformEvents.java
193 lines (169 loc) · 7.12 KB
/
PlatformEvents.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright 2011-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.application.manager.business;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.appng.api.ApplicationException;
import org.appng.api.BusinessException;
import org.appng.api.DataContainer;
import org.appng.api.DataProvider;
import org.appng.api.Environment;
import org.appng.api.FieldProcessor;
import org.appng.api.Options;
import org.appng.api.Request;
import org.appng.api.Scope;
import org.appng.api.model.Application;
import org.appng.api.model.Site;
import org.appng.api.support.SelectionBuilder;
import org.appng.api.support.SelectionFactory;
import org.appng.application.manager.ManagerSettings;
import org.appng.application.manager.MessageConstants;
import org.appng.application.manager.service.PlatformEventService;
import org.appng.core.domain.PlatformEvent;
import org.appng.xml.platform.Selection;
import org.appng.xml.platform.SelectionGroup;
import org.appng.xml.platform.SelectionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;
import lombok.Data;
@Service
public class PlatformEvents implements DataProvider {
public static final String EVENT_FILTER = "eventFilter";
private static final FastDateFormat FDF = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
private PlatformEventService platformEventEventService;
private EventFilter filter;
private SelectionFactory selectionFactory;
@Autowired
public PlatformEvents(PlatformEventService platformEventEventService, SelectionFactory selectionFactory,
EventFilter filter) {
this.platformEventEventService = platformEventEventService;
this.filter = filter;
this.selectionFactory = selectionFactory;
}
public DataContainer getData(Site site, Application application, Environment environment, Options options,
Request request, FieldProcessor fieldProcessor) {
SelectionGroup group = new SelectionGroup();
List<Selection> selections = group.getSelections();
Selection typeFilter = selectionFactory.fromEnum(EventFilter.TYPE, MessageConstants.TYPE,
PlatformEvent.Type.values(), filter.eventTypes());
typeFilter.setType(SelectionType.CHECKBOX);
selections.add(typeFilter);
selections.add(selectionFactory.getTextSelection(EventFilter.EVENT, MessageConstants.EVENT, filter.getEX()));
Collection<String> users = platformEventEventService.getUsers();
Selection userSelection;
if (users.size() > application.getProperties().getInteger(ManagerSettings.EVENT_FILTER_MAX_USERS)) {
String username = StringUtils.trimToEmpty(filter.getEU());
userSelection = new SelectionBuilder<>(EventFilter.USER).title(MessageConstants.USER)
.options(Arrays.asList(username)).select(username).type(SelectionType.TEXT).build();
} else {
userSelection = getStringSelection(EventFilter.USER, users, filter.getEU(), MessageConstants.USER);
}
selections.add(userSelection);
Collection<String> applications = platformEventEventService.getApplications();
Selection applicationSelection = getStringSelection(EventFilter.APP, applications, filter.getEAp(),
MessageConstants.APPLICATION);
selections.add(applicationSelection);
Collection<String> hostNames = platformEventEventService.getOrigins();
Selection hostSelection = getStringSelection(EventFilter.HOST, hostNames, filter.getEH(),
MessageConstants.HOST);
selections.add(hostSelection);
Collection<String> hosts = platformEventEventService.getHostNames();
Selection hostNameSelection = getStringSelection(EventFilter.HOSTNAME, hosts, filter.getEN(),
MessageConstants.HOST_NAME);
selections.add(hostNameSelection);
selections.add(selectionFactory.getDateSelection(EventFilter.AFTER, MessageConstants.CREATED_AFTER,
filter.getEA(), FDF));
selections.add(selectionFactory.getDateSelection(EventFilter.BEFORE, MessageConstants.CREATED_BEFORE,
filter.getEB(), FDF));
DataContainer dataContainer = new DataContainer(fieldProcessor);
dataContainer.getSelectionGroups().add(group);
environment.setAttribute(Scope.SESSION, EVENT_FILTER, filter.copy());
Page<PlatformEvent> events = platformEventEventService.getEvents(fieldProcessor.getPageable(), filter);
dataContainer.setPage(events);
return dataContainer;
}
private Selection getStringSelection(String id, Collection<String> values, String selected, String label) {
SelectionBuilder<String> builder = new SelectionBuilder<>(id);
return builder.title(label).options(values).select(selected).type(SelectionType.SELECT)
.defaultOption(StringUtils.EMPTY, StringUtils.EMPTY).build();
}
@Data
@Component(EVENT_FILTER)
@RequestScope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public static class EventFilter implements DataProvider, Serializable {
static final String BEFORE = "eB";
static final String AFTER = "eA";
static final String EVENT = "eX";
static final String USER = "eU";
static final String HOST = "eH";
static final String HOSTNAME = "eN";
static final String APP = "eAp";
static final String TYPE = "eT";
/** before */
private Date eB;
/** after */
private Date eA;
/** text */
private String eX;
/** user */
private String eU;
/** host */
private String eH;
/** hostnames */
private String eN;
/** application */
private String eAp;
/** types */
private List<String> eT = new ArrayList<>();
public DataContainer getData(Site site, Application application, Environment environment, Options options,
Request request, FieldProcessor fp) {
try {
request.fillBindObject(this, fp, request, site.getSiteClassLoader());
DataContainer dataContainer = new DataContainer(fp);
dataContainer.setItem(this);
return dataContainer;
} catch (BusinessException e) {
throw new ApplicationException("error filling filter", e);
}
}
public EventFilter copy() {
EventFilter copy = new EventFilter();
copy.setEA(eA);
copy.setEB(eB);
copy.setEH(eH);
copy.setEN(eN);
copy.setET(eT);
copy.setEU(eU);
copy.setEX(eX);
return copy;
}
public List<PlatformEvent.Type> eventTypes() {
return eT.stream().filter(t -> StringUtils.isNotBlank(t)).map(t -> PlatformEvent.Type.valueOf(t))
.collect(Collectors.toList());
}
}
}