117 lines
3.8 KiB
Java
117 lines
3.8 KiB
Java
package com.poststats.golf.api.impl;
|
|
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
import java.io.PrintStream;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
import org.apache.commons.csv.CSVFormat;
|
|
import org.apache.commons.csv.CSVPrinter;
|
|
|
|
import com.brianlong.util.FlexMap;
|
|
import com.poststats.golf.api.model.EventPerson;
|
|
import com.poststats.golf.service.EventPersonService;
|
|
import com.poststats.golf.service.EventService;
|
|
import com.poststats.golf.service.PersonService;
|
|
import com.poststats.transformer.impl.DaoConverter;
|
|
|
|
import jakarta.enterprise.context.RequestScoped;
|
|
import jakarta.inject.Inject;
|
|
import jakarta.ws.rs.core.StreamingOutput;
|
|
|
|
@RequestScoped
|
|
public class EventPersonApi extends com.poststats.golf.api.EventPersonApi {
|
|
|
|
@Inject
|
|
private EventService eventService;
|
|
|
|
@Inject
|
|
private EventPersonService eventPersonService;
|
|
|
|
@Inject
|
|
private PersonService golferService;
|
|
|
|
@Inject
|
|
private DaoConverter converter;
|
|
|
|
@Override
|
|
public List<? extends EventPerson> get() {
|
|
return this.converter.convertValue(this.eventPersonService.getPeople(this.getEventId()), EventPerson.class);
|
|
}
|
|
|
|
@Override
|
|
public List<? extends EventPerson> getDetail() {
|
|
List<? extends FlexMap> persons = this.eventPersonService.getPeople(this.getEventId());
|
|
this.golferService.injectDeep("personID", persons, "golfer");
|
|
return this.converter.convertValue(persons, EventPerson.class);
|
|
}
|
|
|
|
@Override
|
|
public StreamingOutput getAsCsv() {
|
|
List<? extends FlexMap> persons = this.eventPersonService.getPeople(this.getEventId());
|
|
return this.toCsv(persons);
|
|
}
|
|
|
|
@Override
|
|
public List<? extends EventPerson> getParticipants() {
|
|
List<? extends FlexMap> participants = this.eventPersonService.getParticipants(this.getEventId());
|
|
this.golferService.injectDeep("personID", participants, "golfer");
|
|
return this.converter.convertValue(participants, EventPerson.class);
|
|
}
|
|
|
|
@Override
|
|
public StreamingOutput getParticipantsAsCsv() {
|
|
List<? extends FlexMap> persons = this.eventPersonService.getParticipants(this.getEventId());
|
|
return this.toCsv(persons);
|
|
}
|
|
|
|
@Override
|
|
public Set<Long> getSeriesParticipants() {
|
|
int seriesId = this.eventService.getSeriesId(this.getEventId());
|
|
Set<Long> eventIds = this.eventService.getIdsBySeriesId(seriesId);
|
|
|
|
Set<Long> personIds = new HashSet<>();
|
|
for (long eventId : eventIds) {
|
|
List<? extends FlexMap> tmpPersons = this.eventPersonService.getParticipants(eventId);
|
|
for (FlexMap person : tmpPersons)
|
|
personIds.add(person.getLong(com.poststats.sql.Constants.PERSON_ID));
|
|
}
|
|
|
|
return personIds;
|
|
}
|
|
|
|
private StreamingOutput toCsv(List<? extends FlexMap> persons) {
|
|
this.golferService.injectDeep("personID", persons, "golfer");
|
|
|
|
return new StreamingOutput() {
|
|
@Override
|
|
public void write(OutputStream output) throws IOException {
|
|
PrintStream pstream = new PrintStream(output);
|
|
CSVPrinter personsCsvPrinter = new CSVPrinter(pstream, CSVFormat.DEFAULT);
|
|
try {
|
|
personsCsvPrinter.printRecord("ID", "Prefix", "Last Name", "First Name", "Suffix", "Email Address",
|
|
"Mobile Phone", "Address Lines", "City", "State", "Country", "Postal Code");
|
|
|
|
for (FlexMap eperson : persons) {
|
|
FlexMap golfer = (FlexMap) eperson.get("golfer");
|
|
FlexMap person = (FlexMap) golfer.get("person");
|
|
|
|
personsCsvPrinter.printRecord(person.getLong("personID"), person.getString("prefix"),
|
|
person.getString("lname"), person.getString("fname"), person.getString("suffix"),
|
|
person.getString("email"), person.getString("cellphone"), person.getString("addrlines"),
|
|
person.getString("addrcity"), person.getString("addrstate"),
|
|
person.getString("addrcountry"), person.getString("addrzip"));
|
|
}
|
|
|
|
personsCsvPrinter.flush();
|
|
} finally {
|
|
personsCsvPrinter.close();
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
}
|