From c9e2d216d23e36c2548c3613ce3db0fa4c358fe8 Mon Sep 17 00:00:00 2001 From: Michael Morris-Pearce Date: Thu, 13 Jul 2017 17:17:36 -0700 Subject: [PATCH 01/62] Added patron reports, minus unit tests. --- .../CampusILLStatisticsRestController.java | 30 ++++ .../vdx/VdxBorrowerCategorySerializer.java | 23 +++ .../vdx/procedures/SpVdxBorrowingPatron.java | 145 ++++++++++++++++++ .../SpVdxBorrowingPatronRepository.java | 41 +++++ .../vdx/procedures/SpVdxLendingPatron.java | 145 ++++++++++++++++++ .../SpVdxLendingPatronRepository.java | 42 +++++ src/main/resources/templates/report.html | 6 + 7 files changed, 432 insertions(+) create mode 100644 src/main/java/org/cdlib/ill/report/vdx/VdxBorrowerCategorySerializer.java create mode 100644 src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxBorrowingPatron.java create mode 100644 src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxBorrowingPatronRepository.java create mode 100644 src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxLendingPatron.java create mode 100644 src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxLendingPatronRepository.java diff --git a/src/main/java/org/cdlib/ill/report/api/CampusILLStatisticsRestController.java b/src/main/java/org/cdlib/ill/report/api/CampusILLStatisticsRestController.java index 2e77351..b151357 100644 --- a/src/main/java/org/cdlib/ill/report/api/CampusILLStatisticsRestController.java +++ b/src/main/java/org/cdlib/ill/report/api/CampusILLStatisticsRestController.java @@ -11,11 +11,15 @@ import org.cdlib.ill.report.vdx.procedures.SpVdxBorrowingOCLCRepository; import org.cdlib.ill.report.vdx.procedures.SpVdxBorrowingUCRepository; import org.cdlib.ill.report.vdx.VdxCampus; +import org.cdlib.ill.report.vdx.procedures.SpVdxBorrowingPatron; +import org.cdlib.ill.report.vdx.procedures.SpVdxBorrowingPatronRepository; import org.cdlib.ill.report.vdx.procedures.SpVdxCopyright; import org.cdlib.ill.report.vdx.procedures.SpVdxCopyrightRepository; import org.cdlib.ill.report.vdx.procedures.SpVdxJournalBorrowing; import org.cdlib.ill.report.vdx.procedures.SpVdxJournalBorrowingRepository; import org.cdlib.ill.report.vdx.procedures.SpVdxLending; +import org.cdlib.ill.report.vdx.procedures.SpVdxLendingPatron; +import org.cdlib.ill.report.vdx.procedures.SpVdxLendingPatronRepository; import org.cdlib.ill.report.vdx.procedures.SpVdxLendingRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.annotation.DateTimeFormat; @@ -50,6 +54,10 @@ public class CampusILLStatisticsRestController { private SpVdxCopyrightRepository spVdxCopyrightRepo; @Autowired private SpVdxJournalBorrowingRepository spVdxJournalBorrowingRepo; + @Autowired + private SpVdxBorrowingPatronRepository spVdxBorrowingPatronRepo; + @Autowired + private SpVdxLendingPatronRepository spVdxLendingPatronRepo; @RequestMapping(value = "{campusCode}/borrowing_uc.csv", produces = {"text/csv"}) public void getVdxBorrowingUCCsv(Writer output, @@ -105,4 +113,26 @@ public void getVdxJournalBorrowingCsv(Writer output, List data = spVdxJournalBorrowingRepo.getJournalBorrowing(VdxCampus.fromCode(campusCode).map(VdxCampus::getCode).orElse(""), startDate, endDate).collect(Collectors.toList()); mapper.writer(schema).writeValue(output, data); } + + @RequestMapping(value = "{campusCode}/borrowing_patron.csv", produces = {"text/csv"}) + public void getVdxBorrowingPatron(Writer output, + @PathVariable("campusCode") String campusCode, + @RequestParam(required = false, name = "startDate", defaultValue = "1900-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate, + @RequestParam(required = false, name = "endDate", defaultValue = "2100-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) throws IOException { + CsvMapper mapper = new CsvMapper(); + CsvSchema schema = mapper.schemaFor(SpVdxBorrowingPatron.class).withHeader(); + List data = spVdxBorrowingPatronRepo.getBorrowingPatron(VdxCampus.fromCode(campusCode).map(VdxCampus::getCode).orElse(""), startDate, endDate).collect(Collectors.toList()); + mapper.writer(schema).writeValue(output, data); + } + + @RequestMapping(value = "{campusCode}/lending_patron.csv", produces = {"text/csv"}) + public void getVdxLendingPatron(Writer output, + @PathVariable("campusCode") String campusCode, + @RequestParam(required = false, name = "startDate", defaultValue = "1900-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate, + @RequestParam(required = false, name = "endDate", defaultValue = "2100-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) throws IOException { + CsvMapper mapper = new CsvMapper(); + CsvSchema schema = mapper.schemaFor(SpVdxLendingPatron.class).withHeader(); + List data = spVdxLendingPatronRepo.getLendingPatron(VdxCampus.fromCode(campusCode).map(VdxCampus::getCode).orElse(""), startDate, endDate).collect(Collectors.toList()); + mapper.writer(schema).writeValue(output, data); + } } diff --git a/src/main/java/org/cdlib/ill/report/vdx/VdxBorrowerCategorySerializer.java b/src/main/java/org/cdlib/ill/report/vdx/VdxBorrowerCategorySerializer.java new file mode 100644 index 0000000..83e9129 --- /dev/null +++ b/src/main/java/org/cdlib/ill/report/vdx/VdxBorrowerCategorySerializer.java @@ -0,0 +1,23 @@ +package org.cdlib.ill.report.vdx; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import java.io.IOException; + +public class VdxBorrowerCategorySerializer extends StdSerializer { + + public VdxBorrowerCategorySerializer() { + this(null); + } + + public VdxBorrowerCategorySerializer(Class t) { + super(t); + } + + @Override + public void serialize(VdxBorrowerCategory category, JsonGenerator jg, SerializerProvider sp) throws IOException { + jg.writeString(category.getCode()); + } + +} diff --git a/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxBorrowingPatron.java b/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxBorrowingPatron.java new file mode 100644 index 0000000..88ae305 --- /dev/null +++ b/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxBorrowingPatron.java @@ -0,0 +1,145 @@ +package org.cdlib.ill.report.vdx.procedures; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import java.util.Objects; +import org.cdlib.ill.report.vdx.VdxBorrowerCategory; +import org.cdlib.ill.report.vdx.VdxBorrowerCategorySerializer; +import org.cdlib.ill.report.vdx.VdxCampus; +import org.cdlib.ill.report.vdx.VdxCampusSerializer; +import org.cdlib.ill.report.vdx.VdxServiceType; +import org.cdlib.ill.report.vdx.VdxServiceTypeSerializer; + +@JsonPropertyOrder({ + "reqCampus", + "reqName", + "respName", + "serviceTp", + "borcat", + "count" +}) +public class SpVdxBorrowingPatron { + + @JsonProperty("borrowing campus") + @JsonSerialize(using = VdxCampusSerializer.class) + private VdxCampus reqCampus; + @JsonProperty("borrowing library") + private String reqName; + @JsonProperty("lending library") + private String respName; + @JsonProperty("loan service") + @JsonSerialize(using = VdxServiceTypeSerializer.class) + private VdxServiceType serviceTp; + @JsonProperty("borrower category") + @JsonSerialize(using = VdxBorrowerCategorySerializer.class) + private VdxBorrowerCategory borcat; + @JsonProperty("total") + private Long count; + + public SpVdxBorrowingPatron() { + } + + public SpVdxBorrowingPatron(VdxCampus reqCampus, String reqName, String respName, VdxServiceType serviceTp, VdxBorrowerCategory borcat, Long count) { + this.reqCampus = reqCampus; + this.reqName = reqName; + this.respName = respName; + this.serviceTp = serviceTp; + this.borcat = borcat; + this.count = count; + } + + public VdxCampus getReqCampus() { + return reqCampus; + } + + public void setReqCampus(VdxCampus reqCampus) { + this.reqCampus = reqCampus; + } + + public String getReqName() { + return reqName; + } + + public void setReqName(String reqName) { + this.reqName = reqName; + } + + public String getRespName() { + return respName; + } + + public void setRespName(String respName) { + this.respName = respName; + } + + public VdxServiceType getServiceTp() { + return serviceTp; + } + + public void setServiceTp(VdxServiceType serviceTp) { + this.serviceTp = serviceTp; + } + + public VdxBorrowerCategory getBorcat() { + return borcat; + } + + public void setBorcat(VdxBorrowerCategory borcat) { + this.borcat = borcat; + } + + public Long getCount() { + return count; + } + + public void setCount(Long count) { + this.count = count; + } + + @Override + public int hashCode() { + int hash = 5; + hash = 53 * hash + Objects.hashCode(this.reqCampus); + hash = 53 * hash + Objects.hashCode(this.reqName); + hash = 53 * hash + Objects.hashCode(this.respName); + hash = 53 * hash + Objects.hashCode(this.serviceTp); + hash = 53 * hash + Objects.hashCode(this.borcat); + hash = 53 * hash + Objects.hashCode(this.count); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final SpVdxBorrowingPatron other = (SpVdxBorrowingPatron) obj; + if (!Objects.equals(this.reqName, other.reqName)) { + return false; + } + if (!Objects.equals(this.respName, other.respName)) { + return false; + } + if (this.reqCampus != other.reqCampus) { + return false; + } + if (this.serviceTp != other.serviceTp) { + return false; + } + if (this.borcat != other.borcat) { + return false; + } + if (!Objects.equals(this.count, other.count)) { + return false; + } + return true; + } + +} diff --git a/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxBorrowingPatronRepository.java b/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxBorrowingPatronRepository.java new file mode 100644 index 0000000..3736514 --- /dev/null +++ b/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxBorrowingPatronRepository.java @@ -0,0 +1,41 @@ +package org.cdlib.ill.report.vdx.procedures; + +import java.time.LocalDate; +import java.util.List; +import java.util.stream.Stream; +import javax.persistence.EntityManager; +import org.cdlib.ill.report.Constants; +import org.cdlib.ill.report.vdx.VdxBorrowerCategory; +import org.cdlib.ill.report.vdx.VdxCampus; +import org.cdlib.ill.report.vdx.VdxServiceType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; + +@Transactional(readOnly = true) +@Repository +public class SpVdxBorrowingPatronRepository { + + @Autowired + private EntityManager em; + + public Stream getBorrowingPatron(String campus, LocalDate beginDate, LocalDate endDate) { + List results = em.createNativeQuery("call sp_vdx_borrowing_patron(?1, ?2, ?3)") + .setParameter(1, campus) + .setParameter(2, beginDate) + .setParameter(3, endDate) + .getResultList(); + return results.stream().map((Object[] values) -> { + Assert.isTrue(values.length == 6, Constants.BAD_PROCEDURE_MSG); + Assert.noNullElements(values, Constants.NULL_DATA_MSG); + return new SpVdxBorrowingPatron( + VdxCampus.fromCode(String.valueOf(values[0])).orElseThrow(Constants.BAD_DATA_EX_SUPPLIER), + String.valueOf(values[1]), + String.valueOf(values[2]), + VdxServiceType.fromCode(String.valueOf(values[3])).orElseThrow(Constants.BAD_DATA_EX_SUPPLIER), + VdxBorrowerCategory.fromCode(String.valueOf(values[4])).orElseThrow(Constants.BAD_DATA_EX_SUPPLIER), + Long.valueOf(String.valueOf(values[5]))); + }); + } +} diff --git a/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxLendingPatron.java b/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxLendingPatron.java new file mode 100644 index 0000000..523d92a --- /dev/null +++ b/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxLendingPatron.java @@ -0,0 +1,145 @@ +package org.cdlib.ill.report.vdx.procedures; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import java.util.Objects; +import org.cdlib.ill.report.vdx.VdxBorrowerCategory; +import org.cdlib.ill.report.vdx.VdxBorrowerCategorySerializer; +import org.cdlib.ill.report.vdx.VdxCampus; +import org.cdlib.ill.report.vdx.VdxCampusSerializer; +import org.cdlib.ill.report.vdx.VdxServiceType; +import org.cdlib.ill.report.vdx.VdxServiceTypeSerializer; + +@JsonPropertyOrder({ + "respCampus", + "respName", + "reqName", + "serviceTp", + "borcat", + "count" +}) +public class SpVdxLendingPatron { + + @JsonProperty("lending campus") + @JsonSerialize(using = VdxCampusSerializer.class) + private VdxCampus respCampus; + @JsonProperty("lending library") + private String respName; + @JsonProperty("borrowing library") + private String reqName; + @JsonProperty("loan service") + @JsonSerialize(using = VdxServiceTypeSerializer.class) + private VdxServiceType serviceTp; + @JsonProperty("borrower category") + @JsonSerialize(using = VdxBorrowerCategorySerializer.class) + private VdxBorrowerCategory borcat; + @JsonProperty("total") + private Long count; + + public SpVdxLendingPatron() { + } + + public SpVdxLendingPatron(VdxCampus respCampus, String respName, String reqName, VdxServiceType serviceTp, VdxBorrowerCategory borcat, Long count) { + this.respCampus = respCampus; + this.respName = respName; + this.reqName = reqName; + this.serviceTp = serviceTp; + this.borcat = borcat; + this.count = count; + } + + public VdxCampus getRespCampus() { + return respCampus; + } + + public void setRespCampus(VdxCampus respCampus) { + this.respCampus = respCampus; + } + + public String getRespName() { + return respName; + } + + public void setRespName(String respName) { + this.respName = respName; + } + + public String getReqName() { + return reqName; + } + + public void setReqName(String reqName) { + this.reqName = reqName; + } + + public VdxServiceType getServiceTp() { + return serviceTp; + } + + public void setServiceTp(VdxServiceType serviceTp) { + this.serviceTp = serviceTp; + } + + public VdxBorrowerCategory getBorcat() { + return borcat; + } + + public void setBorcat(VdxBorrowerCategory borcat) { + this.borcat = borcat; + } + + public Long getCount() { + return count; + } + + public void setCount(Long count) { + this.count = count; + } + + @Override + public int hashCode() { + int hash = 3; + hash = 59 * hash + Objects.hashCode(this.respCampus); + hash = 59 * hash + Objects.hashCode(this.respName); + hash = 59 * hash + Objects.hashCode(this.reqName); + hash = 59 * hash + Objects.hashCode(this.serviceTp); + hash = 59 * hash + Objects.hashCode(this.borcat); + hash = 59 * hash + Objects.hashCode(this.count); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final SpVdxLendingPatron other = (SpVdxLendingPatron) obj; + if (!Objects.equals(this.respName, other.respName)) { + return false; + } + if (!Objects.equals(this.reqName, other.reqName)) { + return false; + } + if (this.respCampus != other.respCampus) { + return false; + } + if (this.serviceTp != other.serviceTp) { + return false; + } + if (this.borcat != other.borcat) { + return false; + } + if (!Objects.equals(this.count, other.count)) { + return false; + } + return true; + } + +} diff --git a/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxLendingPatronRepository.java b/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxLendingPatronRepository.java new file mode 100644 index 0000000..c9dad36 --- /dev/null +++ b/src/main/java/org/cdlib/ill/report/vdx/procedures/SpVdxLendingPatronRepository.java @@ -0,0 +1,42 @@ +package org.cdlib.ill.report.vdx.procedures; + +import java.time.LocalDate; +import java.time.Year; +import java.util.List; +import java.util.stream.Stream; +import javax.persistence.EntityManager; +import org.cdlib.ill.report.Constants; +import org.cdlib.ill.report.vdx.VdxBorrowerCategory; +import org.cdlib.ill.report.vdx.VdxCampus; +import org.cdlib.ill.report.vdx.VdxServiceType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; + +@Transactional(readOnly = true) +@Repository +public class SpVdxLendingPatronRepository { + + @Autowired + private EntityManager em; + + public Stream getLendingPatron(String campus, LocalDate beginDate, LocalDate endDate) { + List results = em.createNativeQuery("call sp_vdx_lending_patron(?1, ?2, ?3)") + .setParameter(1, campus) + .setParameter(2, beginDate) + .setParameter(3, endDate) + .getResultList(); + return results.stream().map((Object[] values) -> { + Assert.isTrue(values.length == 6, Constants.BAD_PROCEDURE_MSG); + Assert.noNullElements(values, Constants.NULL_DATA_MSG); + return new SpVdxLendingPatron( + VdxCampus.fromCode(String.valueOf(values[0])).orElseThrow(Constants.BAD_DATA_EX_SUPPLIER), + String.valueOf(values[1]), + String.valueOf(values[2]), + VdxServiceType.fromCode(String.valueOf(values[3])).orElseThrow(Constants.BAD_DATA_EX_SUPPLIER), + VdxBorrowerCategory.fromCode(String.valueOf(values[4])).orElseThrow(Constants.BAD_DATA_EX_SUPPLIER), + Long.valueOf(String.valueOf(values[5]))); + }); + } +} diff --git a/src/main/resources/templates/report.html b/src/main/resources/templates/report.html index 44c2fc3..4af2ae5 100644 --- a/src/main/resources/templates/report.html +++ b/src/main/resources/templates/report.html @@ -59,10 +59,16 @@