Skip to content

Commit

Permalink
Merge pull request #1936 from TAMULib/55-submission_type_filter-to_up…
Browse files Browse the repository at this point in the history
…stream

Add custom filter for submission type by list.
  • Loading branch information
cstarcher authored May 30, 2024
2 parents 3ba8e53 + d55cf63 commit 115f47d
Show file tree
Hide file tree
Showing 22 changed files with 419 additions and 34 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/tdl/vireo/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ public void run(String... args) throws Exception {
Random random = new Random();
long idOffset = cliService.countUsers();
User helpfulHarry = null;
boolean hasSubmissionTypes = false;

if (expansive) {
helpfulHarry = cliService.createHelpfulHarry(idOffset++, CliService.EMAIL_DATE);
hasSubmissionTypes = cliService.hasSubmissionTypes();
}

for (long i = itemsGenerated; i < num1 + itemsGenerated; i++) {
cliService.operateGenerate(expansive, num2, random, idOffset, helpfulHarry, i);
cliService.operateGenerate(expansive, num2, random, idOffset, helpfulHarry, hasSubmissionTypes, i);
System.out.print("\r" + (i - itemsGenerated) + " of " + num1 + " generated...");
}

Expand Down
40 changes: 40 additions & 0 deletions src/main/java/org/tdl/vireo/controller/FieldValueController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.tdl.vireo.controller;

import static edu.tamu.weaver.response.ApiStatus.SUCCESS;

import edu.tamu.weaver.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tdl.vireo.model.FieldPredicate;
import org.tdl.vireo.model.repo.FieldPredicateRepo;
import org.tdl.vireo.model.repo.FieldValueRepo;

/**
* Controller in which to manage controlled vocabulary.
*
*/
@RestController
@RequestMapping("/settings/field-values")
public class FieldValueController {

@Autowired
private FieldValueRepo fieldValueRepo;

/**
* Endpoint to request a field predicate by value.
*
* @param value The Field Predicate value (not the Field Value).
*
* @return ApiResponse with all matching field values.
*/
@GetMapping("/predicate/{value}")
@PreAuthorize("hasRole('STUDENT')")
public ApiResponse getFieldValuesByPredicateValue(@PathVariable String value) {
return new ApiResponse(SUCCESS, fieldValueRepo.getAllValuesByFieldPredicateValue("submission_type"));
}

}
3 changes: 1 addition & 2 deletions src/main/java/org/tdl/vireo/model/repo/FieldValueRepo.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.tdl.vireo.model.repo;

import edu.tamu.weaver.data.model.repo.WeaverRepo;
import org.tdl.vireo.model.FieldValue;
import org.tdl.vireo.model.repo.custom.FieldValueRepoCustom;

import edu.tamu.weaver.data.model.repo.WeaverRepo;

public interface FieldValueRepo extends WeaverRepo<FieldValue>, FieldValueRepoCustom {

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.tdl.vireo.model.repo.custom;

import java.util.List;
import org.tdl.vireo.model.FieldPredicate;
import org.tdl.vireo.model.FieldValue;

public interface FieldValueRepoCustom {

public FieldValue create(FieldPredicate fieldPredicate);

public List<String> getAllValuesByFieldPredicateValue(String fieldPredicateValue);

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package org.tdl.vireo.model.repo.impl;

import edu.tamu.weaver.data.model.repo.impl.AbstractWeaverRepoImpl;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.tdl.vireo.model.FieldPredicate;
import org.tdl.vireo.model.FieldValue;
import org.tdl.vireo.model.repo.FieldValueRepo;
import org.tdl.vireo.model.repo.custom.FieldValueRepoCustom;

import edu.tamu.weaver.data.model.repo.impl.AbstractWeaverRepoImpl;

public class FieldValueRepoImpl extends AbstractWeaverRepoImpl<FieldValue, FieldValueRepo> implements FieldValueRepoCustom {

final static String VALUES_BY_PREDICATE = "SELECT DISTINCT fv.value AS value FROM field_value fv WHERE fv.field_predicate_id IN " +
"(SELECT fp.id FROM field_predicate fp WHERE fp.value = ?) ORDER BY fv.value ASC";

@Autowired
private FieldValueRepo fieldValueRepo;

private JdbcTemplate jdbcTemplate;

public FieldValueRepoImpl(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}

@Override
public FieldValue create(FieldPredicate fieldPredicate) {
return fieldValueRepo.save(new FieldValue(fieldPredicate));
Expand All @@ -23,4 +35,14 @@ protected String getChannel() {
return "/channel/field-value";
}

@Override
public List<String> getAllValuesByFieldPredicateValue(String fieldPredicateValue) {
List<String> list = new ArrayList<>();
jdbcTemplate.queryForList(VALUES_BY_PREDICATE, fieldPredicateValue).forEach(row -> {
list.add((String) row.get("value"));
});

return list;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,9 @@ public int compare(SubmissionListColumn svc1, SubmissionListColumn svc2) {
// Note that the OR query is used inside the column, represented by both default_embargos and proquest_embargos.
boolean hasNone = false;
for (String filterString : submissionListColumn.getFilters()) {
sqlBuilder.append(" value = '").append(escapeString(filterString, false)).append("' OR");
if (filterString != null) {
sqlBuilder.append(" value = '").append(escapeString(filterString, false, true)).append("' OR");
}

if ("None".equals(filterString)) {
hasNone = true;
Expand All @@ -769,6 +771,36 @@ public int compare(SubmissionListColumn svc1, SubmissionListColumn svc2) {

break;

case "submissionTypes.name":
// This is not a select column but is instead only a custom filter.
if (submissionListColumn.getFilters().size() > 0) {
sqlBuilder = new StringBuilder();
sqlBuilder.append("s.id IN (SELECT submission_id FROM submission_field_values WHERE field_values_id IN (select id FROM field_value WHERE field_predicate_id IN (SELECT id FROM field_predicate WHERE value = 'submission_type') and (");

// Note that the OR query is used inside the column, represented by submission_type.
boolean hasNone = false;
for (String filterString : submissionListColumn.getFilters()) {
if (filterString != null) {
sqlBuilder.append(" value = '").append(escapeString(filterString, false, true)).append("' OR");
}

if ("None".equals(filterString)) {
hasNone = true;
}
}
sqlBuilder.setLength(sqlBuilder.length() - 3);
sqlBuilder.append(")))");

if (hasNone) {
sqlBuilder.append(" OR s.id NOT IN (SELECT submission_id FROM submission_field_values WHERE field_values_id IN (SELECT id FROM field_value WHERE field_predicate_id IN (SELECT id FROM field_predicate WHERE value = 'submission_type')))");
}

sqlWhereBuilderList.add(sqlBuilder);
getFromBuildersMap(sqlCountWhereFilterBuilders, "submissionTypes.name").add(sqlBuilder);
}

break;

case "lastEvent":
sqlBuilder = new StringBuilder()
.append("\nLEFT JOIN")
Expand Down Expand Up @@ -1153,7 +1185,7 @@ private ArrayList<StringBuilder> getFromBuildersMap(Map<String, ArrayList<String
* An escaped string.
*/
private String escapeString(String original) {
return escapeString(original, true);
return escapeString(original, true, false);
}

/**
Expand All @@ -1164,15 +1196,19 @@ private String escapeString(String original) {
*
* @param original The original string to escape.
* @param lower TRUE to make lower case, FALSE to leave case alone.
* @param quoted If TRUE, then only perform escaping on "\" for strings inside of a quote.
* @return
* An escaped string.
*/
private String escapeString(String original, boolean lower) {
private String escapeString(String original, boolean lower, boolean quoted) {
String escaped = original.replace("\\", "\\\\");
escaped = escaped.replace("_", "\\_");
escaped = escaped.replace("%", "\\%");

return lower == true ? escaped.toLowerCase() : escaped;
if (!quoted) {
escaped = escaped.replace("_", "\\_");
escaped = escaped.replace("%", "\\%");
}

return lower ? escaped.toLowerCase() : escaped;
}

private class QueryStrings {
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/org/tdl/vireo/service/CliService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.tdl.vireo.model.repo.CustomActionDefinitionRepo;
import org.tdl.vireo.model.repo.DegreeRepo;
import org.tdl.vireo.model.repo.EmbargoRepo;
import org.tdl.vireo.model.repo.FieldPredicateRepo;
import org.tdl.vireo.model.repo.FieldValueRepo;
import org.tdl.vireo.model.repo.LanguageRepo;
import org.tdl.vireo.model.repo.OrganizationRepo;
Expand Down Expand Up @@ -83,6 +84,9 @@ public class CliService {
@Autowired
private DegreeRepo degreeRepo;

@Autowired
private FieldPredicateRepo fieldPredicateRepo;

@Autowired
private BCryptPasswordEncoder passwordEncoder;

Expand Down Expand Up @@ -110,7 +114,11 @@ public void operateAdminAccounts(boolean expansive, int generateTotal) {
}
}

public void operateGenerate(boolean expansive, int maxActionLogs, Random random, long idOffset, User helpfulHarry, long i) throws OrganizationDoesNotAcceptSubmissionsException {
public boolean hasSubmissionTypes() {
return fieldValueRepo.getAllValuesByFieldPredicateValue("submission_type").size() > 0;
}

public void operateGenerate(boolean expansive, int maxActionLogs, Random random, long idOffset, User helpfulHarry, boolean hasSubmissionTypes, long i) throws OrganizationDoesNotAcceptSubmissionsException {
Calendar now = Calendar.getInstance();
User submitter = userRepo.create("bob" + EMAIL_DATE.format(now.getTime()) + (idOffset + i + 1) + "@boring.bob", "bob", "boring " + (idOffset + i + 1), Role.ROLE_STUDENT);
Credentials credentials = new Credentials();
Expand All @@ -129,6 +137,7 @@ public void operateGenerate(boolean expansive, int maxActionLogs, Random random,
final List<VocabularyWord> departmentsVW = new ArrayList<>();
final List<VocabularyWord> schoolsVW = new ArrayList<>();
final List<VocabularyWord> majorsVW = new ArrayList<>();
final List<String> submissionTypeValues = new ArrayList<>();
Organization org = orgs.get(getRandomNumber(organizationRepo.findAll().size()));
SubmissionStatus state = statuses.get(0);
setAcceptSubmissions(org);
Expand All @@ -147,6 +156,12 @@ public void operateGenerate(boolean expansive, int maxActionLogs, Random random,
}
});

if (hasSubmissionTypes) {
fieldValueRepo.getAllValuesByFieldPredicateValue("submission_type").forEach((String value) -> {
submissionTypeValues.add(value);
});
}

// Status is chosen completely randomly for every option available when expansive is enabled.
if (expansive) {
state = statuses.get(random.nextInt(statuses.size()));
Expand Down Expand Up @@ -292,6 +307,17 @@ public void operateGenerate(boolean expansive, int maxActionLogs, Random random,
val.setValue("test " + pred.getValue() + " " + i);
val.setContacts(Arrays.asList(new String[] { "test" + pred.getValue() + i + AT_ADDRESS }));
sub.addFieldValue(val);
} else if (pred.getValue().equalsIgnoreCase("submission_type")) {
String value = null;
if (submissionTypeValues.size() > 0) {
value = submissionTypeValues.get(getRandomNumber(submissionTypeValues.size()));
}

if (value == null) {
val.setValue("test " + pred.getValue() + " " + getRandomNumber(10));
} else {
val.setValue(value);
}
} else {
val.setValue("test " + pred.getValue() + " " + i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,17 @@
"inputType": {
"name": "INPUT_TEXT"
}
},
{
"title": "Submission Type (List)",
"sort": "NONE",
"valuePath": [
"submissionTypes",
"name"
],
"status": null,
"inputType": {
"name": "INPUT_TEXT"
}
}
]
Loading

0 comments on commit 115f47d

Please sign in to comment.