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

feat(export): make lists and maps in spreadsheet export machine-readable #398

Merged
merged 1 commit into from
May 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() : "");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function you have implemented here as lambda is already in the system as nullToEmptyString

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops! That's embarrassing. I'll fix it.

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might improve performance to move the ObjectMapper to a static field

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a seldom used function. I wouldn't care about this performance gain at this point.

try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException(String.format("Cannot serialize field value %s to JSON", value), e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to use no RuntimeException here, maybe use instead SW360Exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please log the exception with log.error(msg,e)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right.

}
}

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