Skip to content

Commit

Permalink
added uniqueness check on creation and amendment
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltester committed Apr 6, 2024
1 parent 75bf1ab commit 199d5e6
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package uk.co.compendiumdev.challenge.practicemodes.simpleapi;

import uk.co.compendiumdev.thingifier.api.http.HttpApiRequest;
import uk.co.compendiumdev.thingifier.api.http.HttpApiResponse;
import uk.co.compendiumdev.thingifier.apiconfig.ThingifierApiConfig;
import uk.co.compendiumdev.thingifier.application.httpapimessagehooks.HttpApiRequestHook;
import uk.co.compendiumdev.thingifier.core.EntityRelModel;
import uk.co.compendiumdev.thingifier.core.domain.instances.ERInstanceData;
import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstanceCollection;

public class AddMoreItemsIfNecessary implements HttpApiRequestHook {

private final EntityRelModel erModel;

public AddMoreItemsIfNecessary(EntityRelModel eRmodel){
this.erModel = eRmodel;
}

@Override
public HttpApiResponse run(HttpApiRequest request, ThingifierApiConfig config) {

ERInstanceData instanceData = erModel.getInstanceData(EntityRelModel.DEFAULT_DATABASE_NAME);
if(instanceData!=null){
EntityInstanceCollection collection = instanceData.
getInstanceCollectionForEntityNamed("item");
if(collection != null && collection.countInstances()<5) {
erModel.populateDatabase(EntityRelModel.DEFAULT_DATABASE_NAME);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public SimpleApiRoutes(){
makeMandatory().
withValidation(new MatchesRegexValidationRule("[0-9]{3}[-]?[0-9]{1}[-]?[0-9]{2}[-]?[0-9]{6}[-]?[0-9]{1}")).
withValidation(new MaximumLengthValidationRule(17)).
setMustBeUnique(true).
withExample("123-4-56-789012-3"),
Field.is("price",FieldType.FLOAT).
makeMandatory().
Expand All @@ -72,7 +73,7 @@ public SimpleApiRoutes(){
// TODO: should probably have a support multiple databases config somewhere
simplethings.getERmodel().populateDatabase(EntityRelModel.DEFAULT_DATABASE_NAME);

// TODO: add a hook to add more instances if the database runs low e.g. < 5
// TODO: add a uniqueness check on the isbn and not allow adding or amending if duplicate

}

Expand All @@ -92,6 +93,7 @@ public void configure() {
gui);

simpleApiHttpRouting = new ThingifierHttpApiRoutings(simplethings, apiDocDefn);
simpleApiHttpRouting.registerHttpApiRequestHook(new AddMoreItemsIfNecessary(simplethings.getERmodel()));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public final class Field {
private float minimumFloatValue;
private DefinedFields objectDefinition;

private boolean mustBeUnique;

private int truncatedStringLength;

// todo: rather than all these fields, consider moving to more validation rules
Expand All @@ -57,6 +59,7 @@ private Field(final String name, final FieldType type) {
minimumIntegerValue = Integer.MIN_VALUE;
maximumFloatValue = Float.MAX_VALUE;
minimumFloatValue = Float.MIN_VALUE;
mustBeUnique = false;
allowedNullable=false;
}

Expand Down Expand Up @@ -89,6 +92,15 @@ public boolean hasDefaultValue() {
return defaultValue != null;
}

public Field setMustBeUnique(boolean uniqueness) {
this.mustBeUnique = uniqueness;
return this;
}

public boolean mustBeUnique(){
return this.mustBeUnique;
}

public FieldType getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import uk.co.compendiumdev.thingifier.core.domain.definitions.EntityDefinition;
import uk.co.compendiumdev.thingifier.core.domain.definitions.field.definition.Field;
import uk.co.compendiumdev.thingifier.core.domain.definitions.field.definition.FieldType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ERInstanceData {
Expand Down Expand Up @@ -153,4 +151,5 @@ public void clearInstanceDataFor(String instanceName) {
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import uk.co.compendiumdev.thingifier.core.domain.definitions.field.definition.Field;
import uk.co.compendiumdev.thingifier.core.domain.definitions.EntityDefinition;
import uk.co.compendiumdev.thingifier.core.domain.definitions.field.definition.FieldType;
import uk.co.compendiumdev.thingifier.core.domain.definitions.field.instance.FieldValue;
import uk.co.compendiumdev.thingifier.core.domain.definitions.field.instance.NamedValue;
import uk.co.compendiumdev.thingifier.core.reporting.ValidationReport;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -271,4 +273,36 @@ public void setNextIdCountersToAccomodate(List<NamedValue> fieldValues) {
}

}

public ValidationReport checkFieldsForUniqueNess(EntityInstance instance, boolean isAmendment) {

ValidationReport report = new ValidationReport();

for(String fieldName : instance.getEntity().getFieldNames()){
Field field = instance.getEntity().getField(fieldName);
if(field.mustBeUnique()){
String valueThatMustBeUnique = instance.getFieldValue(fieldName).asString();
// check all instances to see if it is
for(EntityInstance instanceToCheck : instances.values()){
FieldValue existingValue = instanceToCheck.getFieldValue(fieldName);
if(valueThatMustBeUnique.equals(existingValue.asString())){
// it is not
boolean dupeFound=true;
if(isAmendment){
if(instanceToCheck.getPrimaryKeyValue().equals(instanceToCheck.getPrimaryKeyValue())){
// same item so ignore this one
dupeFound=false;
}
}
if(dupeFound) {
report.setValid(false);
report.addErrorMessage("Field %s Value is not unique".formatted(fieldName));
}
}
}
}
}

return report;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public ApiResponse amendInstance(final BodyParser bodyargs, final EntityInstance
ValidationReport relationshipsValidation = new BodyRelationshipValidator(thingifier).validate(bodyargs, cloned.getEntity(), database);
validation.combine(relationshipsValidation);

ValidationReport uniquenessCheck = thingifier.getERmodel().getInstanceData(database).
getInstanceCollectionForEntityNamed(instance.getEntity().getName()).
checkFieldsForUniqueNess(cloned, true);
validation.combine(uniquenessCheck);

if (validation.isValid()) {
if(clearFieldsBeforeSettingFromArgs){
instance.clearAllFields();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ private ApiResponse insertNewThingWithFields(final BodyParser bodyargs, final En
}

private ApiResponse addValidatedInstance(BodyParser bodyargs, EntityInstance instance, EntityInstanceCollection thing, String database, ValidationReport validation) {

// check for uniqueness prior to adding
ValidationReport uniquenessCheck = thingifier.getERmodel().getInstanceData(database).
getInstanceCollectionForEntityNamed(instance.getEntity().getName()).
checkFieldsForUniqueNess(instance, false);
validation.combine(uniquenessCheck);

if (validation.isValid()) {
try {
thing.addInstance(instance);
Expand Down

0 comments on commit 199d5e6

Please sign in to comment.