Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
Merge pull request #398 from sw360/dev/machineReadableSpreadsheet#353
Browse files Browse the repository at this point in the history
feat(export): make lists and maps in spreadsheet export machine-readable
review-by:[email protected]
tested-by:[email protected]
  • Loading branch information
mcjaeger authored May 12, 2017
2 parents 6461c77 + ec95520 commit 036b9ab
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
package org.eclipse.sw360.datahandler.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
Expand Down Expand Up @@ -360,20 +362,25 @@ public static String fieldValueAsString(Object fieldValue) {
return nullToEmpty((String) fieldValue);
}
if (fieldValue instanceof Map) {
List<String> mapEntriesAsStrings = nullToEmptyMap(((Map<String, Object>) fieldValue)).entrySet().stream()
.map(e -> {
String valueString = e.getValue() != null ? e.getValue().toString():"";
return e.getKey() + " : " + valueString;
})
.collect(Collectors.toList());
return joinStrings(mapEntriesAsStrings);
Map<String, Object> originalMap = nullToEmptyMap(((Map<String, Object>) fieldValue));
Map<String, String> map = Maps.transformValues(originalMap, v -> v != null ? v.toString() : "");
return serializeToJson(map);
}
if (fieldValue instanceof Iterable){
return joinStrings((Iterable<String>) fieldValue);
return serializeToJson(fieldValue);
}
return fieldValue.toString();
}

private static String serializeToJson(Object value) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException(String.format("Cannot serialize field value %s to JSON", value), e);
}
}

public static String displayNameFor(String name, Map<String, String> nameToDisplayName){
return nameToDisplayName.containsKey(name)? nameToDisplayName.get(name) : name;
}
Expand Down

0 comments on commit 036b9ab

Please sign in to comment.