Skip to content

Commit

Permalink
updated XLSX support
Browse files Browse the repository at this point in the history
  • Loading branch information
Guy Davenport committed Oct 14, 2024
1 parent 15e3e39 commit d6c2aee
Show file tree
Hide file tree
Showing 11 changed files with 494 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public class MarkdownGenerator {
* @return the paths of the Markdown files generated from the complete BrAPI Specification
*/
public Response<List<Path>> generate(Path schemaDirectory) {
return schemaReader.readDirectories(schemaDirectory).mapResultToResponse(brAPISchemas -> new MarkdownGenerator.Generator(brAPISchemas).generate()) ;
return schemaReader.readDirectories(schemaDirectory)
.mapResultToResponse(brAPISchemas -> new MarkdownGenerator.Generator(brAPISchemas).generate()) ;
}

private class Generator {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.brapi.schematools.core.utils;

import lombok.AllArgsConstructor;
import org.brapi.schematools.core.model.*;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Predicate;

/**
* Utility class for creating a cache of {@link BrAPIClass}es. Takes a list of
* classes and caches those in the list if they pass the provided {@link #cachePredicate}.
* Additional classes are added to the cached depending on the subclass of {@link BrAPIClass}
* For {@link BrAPIObjectType} utility checks the properties and
* tries to cache any that are the return type of these properties {@link BrAPIClass}es.
* If an {@link BrAPIArrayType} is encountered then the {@link BrAPIArrayType#getItems()} is
* checked recursively to be included in the cache.
* For {@link BrAPIOneOfType} it is added to the cache and any of {@link BrAPIOneOfType#getPossibleTypes()}
* are checked recursively to be included in the cache.
* {@link BrAPIAllOfType} are ignored.
*/
@AllArgsConstructor
public class BrAPITClassCacheUtil {

private Predicate<BrAPIClass> cachePredicate ;

public Map<String, BrAPIClass> createMap(List<BrAPIClass> brAPIClasses) {
return new Cache(brAPIClasses).brAPIClassMap ;
}

private class Cache {

private final Map<String, BrAPIClass> brAPIClassMap ;

public Cache(List<BrAPIClass> brAPIClasses) {

brAPIClassMap = new TreeMap<>();

for (BrAPIClass brAPIClass : brAPIClasses) {
cacheClass(brAPIClass);
}
}

private void cacheClass(BrAPIType brAPIType) {
if (brAPIType instanceof BrAPIClass brAPIClass && cachePredicate.test(brAPIClass) && !brAPIClassMap.containsKey(brAPIClass.getName())) {
if (brAPIClass instanceof BrAPIObjectType brAPIObjectType) {
brAPIClassMap.put(brAPIClass.getName(), brAPIObjectType);

brAPIObjectType.getProperties().forEach(property -> cacheClass(property.getType()));
} else if (brAPIType instanceof BrAPIOneOfType brAPIOneOfType) {
brAPIClassMap.put(brAPIClass.getName(), brAPIOneOfType);

brAPIOneOfType.getPossibleTypes().forEach(this::cacheClass);
} else if (brAPIClass instanceof BrAPIEnumType brAPIEnumType) {
brAPIClassMap.put(brAPIClass.getName(), brAPIEnumType);
}
} else if (brAPIType instanceof BrAPIArrayType brAPIArrayType) {
cacheClass(brAPIArrayType.getItems());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.brapi.schematools.core.valdiation;

import lombok.Getter;
import org.brapi.schematools.core.options.Options;
import org.apache.commons.beanutils.PropertyUtils;
import org.brapi.schematools.core.response.Response;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.brapi.schematools.core.response.Response.empty;

Expand Down Expand Up @@ -33,13 +35,51 @@ public static Validation valid() {
*/
public Validation assertNotNull(Object value, String errorMessage, Object... args) {
if (value == null) {
String message = args != null && args.length > 0 ? String.format(errorMessage, args) : errorMessage;
errors.add(Response.Error.of("", message, Response.ErrorType.VALIDATION));
addError(errorMessage, args) ;
}

return this;
}

/**
* Validate that only one of the provided properties is non-null
* @param value the value to be tested
* @param properties the array of properties to be checked
* @return this Validation
*/
public Validation assertMutuallyExclusive(Object value, String... properties) {
if (value == null) {
addError("Value is null!") ;
} else {
int count = 0;

for (String property : properties) {
try {
if (PropertyUtils.getProperty(value, property) != null) {
++count;
}
} catch (Exception e) {
addError(e);
}
}

if (count > 1) {
addError("Only one of '%s' can be provided", String.join(", ", Arrays.asList(properties)));
}
}

return this ;
}

public Validation assertClass(Object value, List<Class<?>> classes) {
if (value != null && classes.stream().noneMatch(aClass -> value.getClass().isAssignableFrom(aClass))) {
addError("Value must one of '%s', but was '%s'",
classes.stream().map(Class::getName).collect(Collectors.joining(", ")), value.getClass().getName());
}

return this ;
}

/**
* Was the validation successful,
* @return <code>true</code> if the validation was successful, <code>false</code> otherwise
Expand All @@ -49,13 +89,73 @@ public boolean isValid() {
}

/**
* Merge the options to Validation, by calling the {@link Options#validate()} method
* Merge the objects to Validation, by calling the {@link Validatable#validate()} method
* and adding any errors to this Validation
* @param options the options to be validated
* @param validatable the object to be validated
* @return this Validation
*/
public Validation merge(Options options) {
errors.addAll(options.validate().getErrors()) ;
public Validation merge(Validatable validatable) {
addAllErrors(validatable.validate().getErrors()) ;

return this ;
}

/**
* Merge the objects to Validation, by calling the {@link Validatable#validate()} method
* and adding any errors to this Validation
* @param validatable the object to be validated
* @param prefixMessage prefix any errors with this message
* @return this Validation
*/
public Validation merge(Validatable validatable, String prefixMessage) {
addAllErrors(validatable.validate().getErrors(), prefixMessage) ;

return this ;
}

/**
* If the condition is true, merge the objects to Validation, by calling the {@link Validatable#validate()} method
* and adding any errors to this Validation
* @param condition if <code>true</code> merge, otherwise don't
* @param validatable the object to be validated
* @return this Validation
*/
public Validation mergeOnCondition(boolean condition, Validatable validatable) {
if (condition) {
addAllErrors(validatable.validate().getErrors());
}

return this ;
}

/**
* If the condition is true, merge the objects to Validation, by calling the {@link Validatable#validate()} method
* and adding any errors to this Validation
* @param condition if <code>true</code> merge, otherwise don't
* @param validatable the object to be validated
* @param prefixMessage prefix any errors with this message
* @return this Validation
*/
public Validation mergeOnCondition(boolean condition, Validatable validatable, String prefixMessage) {
if (condition) {
addAllErrors(validatable.validate().getErrors(), prefixMessage);
}

return this ;
}

/**
* Merge the objects to Validation, by calling the {@link Validatable#validate()} method
* in each object and adding any errors to this Validation
* @param validatableList the options to be validated
* @return this Validation
*/
public <T extends Validatable> Validation merge(List<T> validatableList) {
validatableList
.stream()
.map(Validatable::validate)
.map(Validation::getErrors)
.forEach(this::addAllErrors);

return this ;
}
Expand All @@ -72,4 +172,27 @@ public <U> Response<U> asResponse () {
return empty().merge(this) ;
}
}

private void addError(String errorMessage, Object... args) {
String message = args != null && args.length > 0 ? String.format(errorMessage, args) : errorMessage;
errors.add(Response.Error.of("", message, Response.ErrorType.VALIDATION));
}

private void addError(String message) {
errors.add(Response.Error.of("", message, Response.ErrorType.VALIDATION));
}

private void addError(Exception e) {
errors.add(Response.Error.of("", e.getMessage(), Response.ErrorType.VALIDATION));
}

private void addAllErrors(List<Response.Error> errors) {
this.errors.addAll(errors);
}

private void addAllErrors(List<Response.Error> errors, String prefixMessage) {
errors.forEach(error ->
this.errors.add(Response.Error.of(error.getCode(), String.format("%s %s", prefixMessage, error.getMessage()), error.getType()))
);
}
}
Loading

0 comments on commit d6c2aee

Please sign in to comment.