-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymmetricDifferenceView.java
206 lines (177 loc) · 7.76 KB
/
SymmetricDifferenceView.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
194
195
196
197
198
199
200
201
202
203
204
205
206
package org.orienteering.wmoc.resulthelperui;
import com.helger.commons.mutable.MutableInt;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasText;
import com.vaadin.flow.component.grid.ColumnPathRenderer;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.data.renderer.Renderer;
import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.router.Route;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import org.orienteering.datastandard._3.EntryList;
import org.orienteering.datastandard._3.Iof3ResultList;
import org.orienteering.datastandard._3.PersonEntry;
import org.orienteering.wmoc.services.StartTimeService;
import org.vaadin.firitin.components.DynamicFileDownloader;
import org.vaadin.firitin.components.grid.VGrid;
import org.vaadin.firitin.components.orderedlayout.VHorizontalLayout;
import org.vaadin.firitin.components.upload.UploadFileHandler;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@Route(layout = TopLayout.class)
public class SymmetricDifferenceView extends AbstractCalculatorView {
private final Unmarshaller unmarshaller;
private final Marshaller marshaller;
private final StartTimeService startTimeService;
private EntryList el;
List<PersonEntry> entries = new ArrayList<>();
VGrid<PersonEntry> entryGrid = new VGrid<>();
public SymmetricDifferenceView(StartTimeService startTimeService) {
this.startTimeService = startTimeService;
add("Symmetric disjoint of entries (e.g. to find only those that have not paid). Uploading another file -> only uniques are left visible.");
JAXBContext jaxbContext = null;
try {
jaxbContext = JAXBContext.newInstance(Iof3ResultList.class, EntryList.class);
unmarshaller = jaxbContext.createUnmarshaller();
marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
UploadFileHandler uploadFileHandler = new UploadFileHandler((content, m) -> {
String mimeType = m.mimeType();
try {
var xml = unmarshaller.unmarshal(content);
if (xml instanceof EntryList el) {
this.el = el;
}
} catch (JAXBException e) {
throw new RuntimeException(e);
}
return () -> {
showReport();
notifyErrors();
};
}).allowMultiple();
add(new VHorizontalLayout(
uploadFileHandler)
.space()
.withComponents(
new DynamicFileDownloader(out -> gridToCsv(out))
)
);
addAndExpand(entryGrid);
entryGrid.setMultiSort(true);
}
private void showReport() {
if (el != null) {
List<PersonEntry> personEntry = el.getPersonEntry();
if(entries == null) {
entries = personEntry;
} else {
personEntry.forEach(pe -> {
var existing = entries.stream().filter(current ->
current.getPerson().getId().get(0).getValue().equals(pe.getPerson().getId().get(0).getValue())
).findFirst();
if(!existing.isPresent()) {
entries.add(pe);
} else {
entries.remove(existing.get());
}
});
}
entryGrid.removeAllColumns();
entryGrid.addColumn(pe -> pe.getPerson().getId().get(0).getValue()).setHeader("IOF Id");
if(startTimeService.getRaceCount() > 0) {
// expects bibs calculated if start times available
entryGrid.addColumn(pe -> pe.getId().getValue()).setHeader("Bib");
// Sorry for further maintainers, this section is hardcoded, might not match in the future
final int SQ = 1;
final int FQ = 3;
entryGrid.addColumn(pe -> {
String iofId = pe.getPerson().getId().get(0).getValue();
return startTimeService.getStartTime(SQ, iofId);
}).setHeader("Start SQ");
entryGrid.addColumn(pe -> {
String iofId = pe.getPerson().getId().get(0).getValue();
return startTimeService.getStartTime(FQ, iofId);
}).setHeader("Start FQ");
}
entryGrid.addColumn(pe -> {
return pe.getClazz().get(0).getName();
}).setHeader("Class").setSortable(true);
entryGrid.addColumn(pe -> {
return pe.getPerson().getName().getGiven() + " " + pe.getPerson().getName().getFamily();
}).setHeader("Name");
entryGrid.withColumnSelector();
entryGrid.setItems(entries);
}
}
private void gridToCsv(OutputStream outputStream) {
final String DELIM = ";";
PrintStream out = new PrintStream(outputStream);
MutableInt idx = new MutableInt(0);
entryGrid.getColumns().forEach(c -> {
if (c.isVisible()) {
String headerText = c.getHeaderText();
if (headerText != null) {
out.print(headerText);
} else if (c.getKey() != null) {
out.print(c.getKey());
} else {
out.print(idx.intValue());
}
out.print(DELIM);
}
});
out.println();
entryGrid.getGenericDataView().getItems().forEach(
i -> {
entryGrid.getColumns().forEach(c -> {
if (c.isVisible()) {
Renderer<PersonEntry> renderer = c.getRenderer();
if (renderer instanceof ColumnPathRenderer<PersonEntry> cpr) {
try {
Field field = ColumnPathRenderer.class.getDeclaredField("provider");
field.setAccessible(true);
ValueProvider vp = (ValueProvider) field.get(cpr);
Object object = vp.apply(i);
out.print(object.toString());
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
if (renderer instanceof ComponentRenderer cr) {
Component component = cr.createComponent(i);
if (component instanceof HasText ht) {
out.print(ht.getText());
} else {
out.print(component.toString());
}
}
out.print(DELIM);
}
});
out.println();
}
);
}
private static final String DELIM = ";";
private PrintWriter writer;
private void cell(Object o) {
writer.print(o);
writer.print(DELIM);
}
private void nl() {
writer.println();
}
}