use of eu.esdihumboldt.hale.common.lookup.LookupTableInfo in project hale by halestudio.
the class ClassificationMappingUtil method getClassificationLookup.
/**
* Get the classification lookup table from the transformation parameters.
*
* @param parameters the transformation parameters
* @param serviceProvider service provider in case a lookup table has to be
* retrieved through a service
* @return the classification lookup table
*/
public static LookupTable getClassificationLookup(Multimap<String, ? extends Value> parameters, ServiceProvider serviceProvider) {
try {
if (!(parameters.get(PARAMETER_LOOKUPTABLE).isEmpty())) {
Collection<? extends Value> tmpMap = parameters.get(PARAMETER_LOOKUPTABLE);
return tmpMap.iterator().next().as(LookupTable.class);
}
if (!(parameters.get(PARAMETER_LOOKUPTABLE_ID).isEmpty())) {
LookupService lookupServiceImpl = serviceProvider.getService(LookupService.class);
Collection<? extends Value> tmpMap = parameters.get(PARAMETER_LOOKUPTABLE_ID);
LookupTableInfo lookupTableInfo = lookupServiceImpl.getTable(tmpMap.iterator().next().as(String.class));
return lookupTableInfo.getTable();
}
} catch (NullPointerException e) {
log.error("Service provider not accessible for retrieving lookup table", e);
}
// For reason of compatibility we need the following code
// lookup table in strangely encoded string parameter
Collection<? extends Value> mappings = parameters.get(PARAMETER_CLASSIFICATIONS);
try {
Map<Value, Value> lookupMap = new HashMap<Value, Value>();
for (Value mapping : mappings) {
String[] parts = mapping.as(String.class).split(" ");
if (parts.length > 0) {
Value target = Value.of(URLDecoder.decode(parts[0], "UTF-8"));
for (int i = 1; i < parts.length; i++) {
lookupMap.put(Value.of(URLDecoder.decode(parts[i], "UTF-8")), target);
}
}
}
return new LookupTableImpl(lookupMap);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Failed to decode classification mapping.");
}
}
Aggregations