Add input formatting and validation to client and server side. Close dialog box on submission.

This commit is contained in:
chubtub 2021-01-25 17:00:45 -05:00
parent 4acfbf3026
commit 177e307a17
2 changed files with 44 additions and 31 deletions

View File

@ -41,6 +41,8 @@ import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* Controller for the Validation Reports page. * Controller for the Validation Reports page.
@ -55,6 +57,7 @@ public class ValidationReportsPageController extends PageController<NoPageParams
private static final Logger LOGGER = getLogger(ValidationReportsPageController.class); private static final Logger LOGGER = getLogger(ValidationReportsPageController.class);
private static final String DEFAULT_COMPANY = "AllDevices"; private static final String DEFAULT_COMPANY = "AllDevices";
private static final String UNDEFINED = "undefined";
/** /**
* Constructor providing the Page's display and routing specification. * Constructor providing the Page's display and routing specification.
@ -148,7 +151,8 @@ public class ValidationReportsPageController extends PageController<NoPageParams
LOGGER.info("Downloading validation report"); LOGGER.info("Downloading validation report");
String company = ""; String company = "";
String contractNumber = ""; String contractNumber = "";
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM/dd/uuuu"); Pattern pattern = Pattern.compile("[A-Za-z0-9\\s]*");
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd");
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"); DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
LocalDate startDate = null; LocalDate startDate = null;
LocalDate endDate = null; LocalDate endDate = null;
@ -158,56 +162,59 @@ public class ValidationReportsPageController extends PageController<NoPageParams
while (parameters.hasMoreElements()) { while (parameters.hasMoreElements()) {
String parameter = (String) parameters.nextElement(); String parameter = (String) parameters.nextElement();
String parameterValue = request.getParameter(parameter); String parameterValue = request.getParameter(parameter);
LOGGER.info(parameter + ": " + parameterValue);
switch (parameter) { switch (parameter) {
case "company": case "company":
Matcher companyMatcher = pattern.matcher(parameterValue);
if (companyMatcher.matches()) {
company = parameterValue; company = parameterValue;
} else {
company = DEFAULT_COMPANY;
}
break; break;
case "contract": case "contract":
Matcher contractMatcher = pattern.matcher(parameterValue);
if (contractMatcher.matches()) {
contractNumber = parameterValue; contractNumber = parameterValue;
} else {
contractNumber = "none";
}
break; break;
case "dateStart": case "dateStart":
if (parameterValue != null && !parameterValue.isEmpty()) { if (parameterValue != null && !parameterValue.isEmpty()) {
startDate = LocalDate.parse(parameterValue, dateFormat); startDate = LocalDate.parse(parameterValue, dateFormat);
} else {
startDate = LocalDate.ofEpochDay(0);
} }
break; break;
case "dateEnd": case "dateEnd":
if (parameterValue != null && !parameterValue.isEmpty()) { if (parameterValue != null && !parameterValue.isEmpty()) {
endDate = LocalDate.parse(parameterValue, dateFormat); endDate = LocalDate.parse(parameterValue, dateFormat);
} else {
endDate = LocalDate.now();
} }
break; break;
case "createTimes": case "createTimes":
if (!parameterValue.equals(UNDEFINED)) {
String[] timestamps = parameterValue.split(","); String[] timestamps = parameterValue.split(",");
for (String timestamp : timestamps) { for (String timestamp : timestamps) {
createTimes.add(LocalDateTime.parse(timestamp, createTimes.add(LocalDateTime.parse(timestamp,
dateTimeFormat).toLocalDate()); dateTimeFormat).toLocalDate());
LOGGER.info("Create time added: " }
+ createTimes.get(createTimes.size() - 1));
} }
break; break;
case "deviceNames": case "deviceNames":
if (!parameterValue.equals(UNDEFINED)) {
deviceNames = parameterValue.split(","); deviceNames = parameterValue.split(",");
}
break; break;
default: default:
} }
LOGGER.info(parameter + ": " + parameterValue);
} }
if (company.equals("")) {
company = DEFAULT_COMPANY;
}
if (contractNumber.equals("")) {
contractNumber = "none";
}
if (startDate == null) {
startDate = LocalDate.ofEpochDay(0);
}
if (endDate == null) {
endDate = LocalDate.now();
}
LOGGER.info("Start date: " + startDate.toString() + ", end date: " + endDate.toString());
response.setHeader("Content-Type", "text/csv"); response.setHeader("Content-Type", "text/csv");
response.setHeader("Content-Disposition", response.setHeader("Content-Disposition",
"attachment;filename=\"validation_report.csv\""); "attachment;filename=" + company + "_validation_report.csv");
BufferedWriter bufferedWriter = new BufferedWriter( BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(response.getOutputStream(), "UTF-8")); new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
String columnHeaders = "Verified Manufacturer, " String columnHeaders = "Verified Manufacturer, "
@ -243,13 +250,13 @@ public class ValidationReportsPageController extends PageController<NoPageParams
+ "\nComponent status: " + "\nComponent status: "
+ ((ComponentIdentifierV2) ci).getAttributeStatus()); + ((ComponentIdentifierV2) ci).getAttributeStatus());
} else { } else {
//("Platform Components" + "\n");
LOGGER.info("\nPlatform Components"); LOGGER.info("\nPlatform Components");
} }
LOGGER.info("Component manufacturer : " LOGGER.info("Component manufacturer : "
+ ci.getComponentManufacturer().getString() + ci.getComponentManufacturer().getString()
+ "\nComponent model: " + ci.getComponentModel().getString() + "\nComponent model: " + ci.getComponentModel().getString()
+ "\nComponent revision: " + ci.getComponentRevision().getString()); + "\nComponent revision: "
+ ci.getComponentRevision().getString());
} }
attributeStatuses = attributeStatuses.substring(0, attributeStatuses = attributeStatuses.substring(0,
attributeStatuses.length() - 1); attributeStatuses.length() - 1);

View File

@ -28,11 +28,13 @@
<form:form id="download" method="POST" action="${portal}/validation-reports/download"> <form:form id="download" method="POST" action="${portal}/validation-reports/download">
Download Validation Reports Download Validation Reports
<my:download-info id="validationReportsDownload" label="Download Validation Reports"> <my:download-info id="validationReportsDownload" label="Download Validation Reports">
<label>Company<input id="company" type="text" name="company" /></label> <label>Company<input id="company" type="text" pattern="[A-Za-z0-9\s]*"
<label>Contract #<input id="contract" type="text" name="contract" /></label> title="Letters, numbers, and spaces only" name="company" /></label>
<label>Contract #<input id="contract" type="text" pattern="[A-Za-z0-9\s]*"
title="Letters, numbers, and spaces only" name="contract" /></label>
<br> <br>
<label>Date range start (mm/dd/yyyy)<input id="dateStart" type="text" name="dateStart" /></label> <label>Date range start<input id="dateStart" type="date" name="dateStart" /></label>
<label>Date range end (mm/dd/yyyy)<input id="dateEnd" type="text" name="dateEnd" /></label> <label>Date range end<input id="dateEnd" type="date" name="dateEnd" /></label>
</my:download-info> </my:download-info>
</form:form> </form:form>
@ -166,6 +168,10 @@
})); }));
}); });
$(".btn-primary").click(function() {
$("#validationReportsDownload").modal('hide');
});
/** /**
* Gets HTML to display (icon tag) for the specified validation type. * Gets HTML to display (icon tag) for the specified validation type.
* If a validation for the requested type is not found, an empty * If a validation for the requested type is not found, an empty