use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class IOReferenceContent method getIOProviderContent.
/**
* @param providerId The id of the provider
* @return The in input stream or null, if the given provider does not
* exist.
* @throws Exception - if an error occurs
*/
private InputStream getIOProviderContent(String providerId) throws Exception {
final String id = providerId;
// try to get I/O provider
IOProviderDescriptor providerDescriptor = HaleIO.findIOProviderFactory(IOProvider.class, null, providerId);
if (providerDescriptor == null) {
log.warn("I/O provider " + id + " does not exist.");
return null;
}
final IOProviderDescriptor descriptor = providerDescriptor;
return getContentFromTemplate(providerId, TEMPLATE_PROVIDER, new Callable<VelocityContext>() {
@Override
public VelocityContext call() throws Exception {
VelocityContext context = new VelocityContext();
// I/O provider descriptor
context.put("descriptor", descriptor);
// I/O provider type
String type = (ImportProvider.class.isAssignableFrom(descriptor.getProviderType())) ? ("reader") : ("writer");
context.put("providerType", type);
// collect all optional parameter
Collection<IOProviderParameter> parameter = new ArrayList<IOProviderParameter>();
Map<String, String> example = new HashMap<String, String>();
Map<String, String> defaults = new HashMap<String, String>();
for (IOProviderParameter param : descriptor.getProviderParameter()) {
parameter.add(param);
// get example use of parameter
if (param.getValueDescriptor() != null) {
// store sample value
Value sample = param.getValueDescriptor().getSampleData();
String displayValue = toDisplayValue(sample);
if (displayValue != null) {
example.put(param.getName(), displayValue);
}
// store default value
Value defaultValue = param.getValueDescriptor().getDefaultValue();
displayValue = toDisplayValue(defaultValue);
if (displayValue != null) {
defaults.put(param.getName(), displayValue);
}
}
}
context.put("parameter", parameter);
context.put("example", example);
context.put("defaults", defaults);
return context;
}
});
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class LookupTableTypeTest method testComplexLookupJsonGroovy.
/**
* Test if a lookup table containing only complex values is the same when
* converted to JSON and back again.
*/
@Test
public void testComplexLookupJsonGroovy() {
Map<Value, Value> values2 = createComplexLookup();
LookupTable org2 = new LookupTableImpl(values2);
// converter
LookupTableType ltt = new LookupTableType();
// convert to Json
Object json = ltt.toJson(org2);
System.out.println(new JsonBuilder(json).toString());
// convert back
LookupTable conv = ltt.fromJson(json, null);
checkTable(conv, values2);
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class LookupTableTypeTest method createComplexLookup.
private Map<Value, Value> createComplexLookup() {
// simple internal table
Map<Value, Value> values = new HashMap<Value, Value>();
values.put(Value.of("a"), Value.of("1"));
values.put(Value.of("b"), Value.of("1"));
LookupTable org = new LookupTableImpl(values);
// external table
Map<Value, Value> values2 = new HashMap<Value, Value>();
values2.put(Value.of("sub"), Value.complex(org));
values2.put(Value.of("c"), Value.of("2"));
return values2;
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class CodeListAssocationFactory method restore.
@Override
public CodeListAssociation restore(Value value, Definition<?> definition, TypeResolver typeIndex, ClassResolver resolver) throws Exception {
// is it a simple value? (single String code list reference)
String single = value.as(String.class);
if (single != null) {
return new CodeListAssociation(Collections.singleton(single));
}
// is it a value list? (multiple String code list references)
ValueList list = value.as(ValueList.class);
if (list != null) {
List<String> refList = list.stream().map(val -> val.as(String.class)).filter(val -> val != null).collect(Collectors.toList());
return new CodeListAssociation(refList);
}
// fall-back
return new CodeListAssociation();
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class ReferenceFactory method restore.
@Override
public Reference restore(Value value, Definition<?> definition, TypeResolver typeIndex, ClassResolver resolver) throws Exception {
ValueProperties props = value.as(ValueProperties.class);
Reference ref = new Reference(props.get(P_IS_REF).as(Boolean.class, false));
Value types = props.get(P_TYPES);
if (types.isComplex()) {
ValueList list = types.as(ValueList.class);
if (list != null) {
for (Value entry : list) {
Optional<TypeDefinition> type = typeIndex.resolve(entry);
if (type.isPresent()) {
ref.addReferencedType(type.get());
} else {
throw new IllegalStateException("Could not resolve type definition for index " + entry);
}
}
}
}
return ref;
}
Aggregations