Skip to content

Commit

Permalink
Merge 5fee860 into 2cb1770
Browse files Browse the repository at this point in the history
  • Loading branch information
imed-bh committed Apr 30, 2018
2 parents 2cb1770 + 5fee860 commit 0a04449
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
import com.fasterxml.jackson.module.jsonSchema.types.IntegerSchema;

class CustomIntegerSchema extends IntegerSchema {

private final SchemaDecoratorHandler schemaDecoratorHandler;

CustomIntegerSchema(SchemaDecoratorHandler schemaDecoratorHandler) {
this.schemaDecoratorHandler = schemaDecoratorHandler;
}

@Override
public void enrichWithBeanProperty(BeanProperty beanProperty) {
super.enrichWithBeanProperty(beanProperty);
SchemaDecoratorUtil.get().decorate(beanProperty, this);
schemaDecoratorHandler.decorate(beanProperty, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
import com.fasterxml.jackson.module.jsonSchema.types.StringSchema;

class CustomJsonSchemaFactory extends JsonSchemaFactory {

private final SchemaDecoratorHandler schemaDecoratorHandler;

CustomJsonSchemaFactory(SchemaDecoratorHandler schemaDecoratorHandler) {
this.schemaDecoratorHandler = schemaDecoratorHandler;
}

@Override
public StringSchema stringSchema() {
return new CustomStringSchema();
return new CustomStringSchema(schemaDecoratorHandler);
}

@Override
public NumberSchema numberSchema() {
return new CustomNumberSchema();
return new CustomNumberSchema(schemaDecoratorHandler);
}

@Override
public IntegerSchema integerSchema() {
return new CustomIntegerSchema();
return new CustomIntegerSchema(schemaDecoratorHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
import com.fasterxml.jackson.module.jsonSchema.types.NumberSchema;

class CustomNumberSchema extends NumberSchema {

private final SchemaDecoratorHandler schemaDecoratorHandler;

CustomNumberSchema(SchemaDecoratorHandler schemaDecoratorHandler) {
this.schemaDecoratorHandler = schemaDecoratorHandler;
}

@Override
public void enrichWithBeanProperty(BeanProperty beanProperty) {
super.enrichWithBeanProperty(beanProperty);
SchemaDecoratorUtil.get().decorate(beanProperty, this);
schemaDecoratorHandler.decorate(beanProperty, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@

class CustomSchemaFactoryWrapper extends SchemaFactoryWrapper {

CustomSchemaFactoryWrapper(SchemaDecoratorHandler schemaDecoratorHandler) {
super(new SFSchemaFactoryWrapperFactory(schemaDecoratorHandler));
schemaProvider = new CustomJsonSchemaFactory(schemaDecoratorHandler);
}

private static class SFSchemaFactoryWrapperFactory extends WrapperFactory {

private final SchemaDecoratorHandler schemaDecoratorHandler;

private SFSchemaFactoryWrapperFactory(SchemaDecoratorHandler schemaDecoratorHandler) {
this.schemaDecoratorHandler = schemaDecoratorHandler;
}

@Override
public SchemaFactoryWrapper getWrapper(SerializerProvider p, VisitorContext rvc) {
SchemaFactoryWrapper wrapper = new CustomSchemaFactoryWrapper();
SchemaFactoryWrapper wrapper = new CustomSchemaFactoryWrapper(schemaDecoratorHandler);
if (p != null) {
wrapper.setProvider(p);
}
wrapper.setVisitorContext(rvc);
return wrapper;
}
}

CustomSchemaFactoryWrapper() {
super(new SFSchemaFactoryWrapperFactory());
schemaProvider = new CustomJsonSchemaFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@

class CustomStringSchema extends StringSchema {

private final SchemaDecoratorHandler schemaDecoratorHandler;

CustomStringSchema(SchemaDecoratorHandler schemaDecoratorHandler) {
this.schemaDecoratorHandler = schemaDecoratorHandler;
}

@Override
public void enrichWithBeanProperty(BeanProperty beanProperty) {
super.enrichWithBeanProperty(beanProperty);
SchemaDecoratorUtil.get().decorate(beanProperty, this);
schemaDecoratorHandler.decorate(beanProperty, this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.asfjava.ui.core.schema;

import java.lang.annotation.Annotation;
import java.util.Optional;

import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;

import io.asfjava.ui.core.SchemaDecoratorFactory;
import io.asfjava.ui.core.schema.decorators.SchemaDecorator;

final class SchemaDecoratorHandler {

SchemaDecoratorHandler() {}

void decorate(BeanProperty beanProperty, JsonSchema simpleTypeSchema) {
beanProperty.getMember().annotations()
.forEach(annotation -> decorate(beanProperty, simpleTypeSchema, annotation));
}

private void decorate(BeanProperty beanProperty, JsonSchema simpleTypeSchema, Annotation annotation) {
getDecorator(annotation)
.ifPresent(decorator -> decorator.customizeSchema(beanProperty, simpleTypeSchema));
}

private Optional<SchemaDecorator> getDecorator(Annotation annotation) {
return SchemaDecoratorFactory.getInstance().get(annotation.annotationType().getName());
}
}
30 changes: 0 additions & 30 deletions src/main/java/io/asfjava/ui/core/schema/SchemaDecoratorUtil.java

This file was deleted.

25 changes: 14 additions & 11 deletions src/main/java/io/asfjava/ui/core/schema/UiFormSchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,21 @@ public final class UiFormSchemaGenerator {
private static final String KEY_TITLE = "title";
private static final String KEY_TYPE = "type";
private static final String KEY_ITEMS = "items";

private static UiFormSchemaGenerator instance;

public static UiFormSchemaGenerator get() {
return (instance == null)
? instance = new UiFormSchemaGenerator()
: instance;
}

private final SchemaDecoratorHandler schemaDecoratorHandler;

private UiFormSchemaGenerator() {
schemaDecoratorHandler = new SchemaDecoratorHandler();
}

public UiForm generate(Class<? extends Serializable> formDto) throws JsonMappingException {
Field[] declaredFields = formDto.getDeclaredFields();
ObjectMapper mapper = new ObjectMapper();
Expand Down Expand Up @@ -190,7 +203,7 @@ private JsonSchema generateSchema(Class<? extends Serializable> formDto, JsonSch
}

private JsonSchemaGenerator initSchemaGen(ObjectMapper mapper) {
return new JsonSchemaGenerator(mapper, new CustomSchemaFactoryWrapper());
return new JsonSchemaGenerator(mapper, new CustomSchemaFactoryWrapper(schemaDecoratorHandler));
}

private void groupFieldsByTab(Map<Field, JsonNode> nodes, Field field, Map<String, List<JsonNode>> groupedFields) {
Expand Down Expand Up @@ -234,14 +247,4 @@ private Map<Field, JsonNode> reorderFieldsBasedOnIndex(Map<Field, JsonNode> node
(result, currentElement) -> result.put(currentElement.getKey(), currentElement.getValue()),
Map::putAll);
}

public static UiFormSchemaGenerator get() {
if (instance == null) {
instance = new UiFormSchemaGenerator();
}
return instance;
}

private UiFormSchemaGenerator() {
}
}

0 comments on commit 0a04449

Please sign in to comment.