This commit is contained in:
gexce
2017-09-29 14:52:39 +02:00
parent 37ed0f291e
commit d49c6faf08
4 changed files with 116 additions and 15 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -8,13 +8,22 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
/**
* Reads the MAM categories (https://service.media-assistant.animalhealth.bayer.com/rest/migration/category) from a JSON file and converts
* them into a tree structure.
*/
public class CategoryTree {
private static final Logger log = LoggerFactory.getLogger(CategoryTree.class);
static class Node implements Comparable<Node> {
int id;
String name;
@@ -22,6 +31,9 @@ public class CategoryTree {
int parentId;
List<Node> children = new ArrayList<Node>();
String action;
String parameter;
@Override
public int compareTo(Node o) {
return this.name.compareTo(o.name);
@@ -43,24 +55,114 @@ public class CategoryTree {
}
Node root;
public HashMap<Integer, Node> nodeMap = new HashMap<Integer, Node>();
public Node root; // technical root
public static int ROOT_NODE_ID = 1; // ID of logical root "Intern"
public CategoryTree(String jsonFile) throws IOException {
ObjectMapper jsonMapper = new ObjectMapper();
public CategoryTree(String jsonFile, String mappingsFile) throws IOException {
readTree(jsonFile);
readMappings(mappingsFile);
// String categoriesString = FileUtils.readFileToString(new File(jsonFile), "UTF-8");
// JsonCategoryList categoryList = jsonMapper.readValue(categoriesString, JsonCategoryList.class);
printTree();
}
private void readMappings(String mappingsFile) {
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.getFormat().setLineSeparator("\r\n");
parserSettings.getFormat().setDelimiter(';');
parserSettings.getFormat().setCharToEscapeQuoteEscaping('"');
parserSettings.setHeaderExtractionEnabled(false);
CsvParser parser = new CsvParser(parserSettings);
List<String[]> inputRows = parser.parseAll(new File(mappingsFile));
int rowNum = 0;
String[] currentPath = new String[6];
for (String[] row : inputRows) {
rowNum++;
if (row[0] == null) {
log.info(rowNum + " - skipping, first cell ist empty");
continue;
}
int rowLevel = 3;
while (row[rowLevel] == null)
rowLevel++;
rowLevel = rowLevel - 3;
if (currentPath[rowLevel] != null) {
for (int i = rowLevel; i < currentPath.length; i++)
currentPath[i] = null;
}
currentPath[rowLevel] = row[rowLevel + 3].trim();
// int i = 1;
// System.out.print("[" + currentPath[0]);
// while (i < currentPath.length && currentPath[i] != null) {
// System.out.print(", " + currentPath[i]);
// i++;
// }
// System.out.println("]");
Node node = getNode(currentPath);
node.action = row[0];
node.parameter = row[1];
}
}
private Node getNode(String[] currentPath) {
Node currentNode = nodeMap.get(ROOT_NODE_ID);
int level = 0;
while (level < currentPath.length && currentPath[level] != null) {
Node next = null;
for (Node node : currentNode.children) {
if (node.name.equals(currentPath[level])) {
next = node;
break;
}
}
if (next == null) {
String path = "[" + currentPath[0];
int i = 1;
while (i < currentPath.length && currentPath[i] != null) {
i++;
path += ", " + currentPath[i] ;
}
path += "]";
log.error("No match for " + path);
break;
}
currentNode = next;
level++;
}
return currentNode;
}
private void readTree(String jsonFile) throws IOException {
ObjectMapper jsonMapper = new ObjectMapper();
JavaType type = jsonMapper.getTypeFactory().constructCollectionType(List.class, JsonCategory.class);
List<JsonCategory> categoryList = jsonMapper.readValue(new File(jsonFile), type);
HashMap<Integer, Node> nodeMap = new HashMap<Integer, Node>();
for (JsonCategory cat : categoryList) {
Node node = new Node();
node.id = cat.id;
node.parentId = cat.parent;
node.name = cat.name.get("en");
if (node.name != null)
node.name = node.name.trim();
nodeMap.put(node.id, node);
if (cat.id == 0) {
@@ -76,22 +178,21 @@ public class CategoryTree {
node.parent.children.add(node);
}
}
// printout
System.out.println("Categories:");
Node startNode = nodeMap.get(1);
}
private void printTree() {
Node startNode = nodeMap.get(ROOT_NODE_ID);
java.util.Collections.sort(startNode.children);
for (Node node : startNode.children) {
iterate(node, 0);
}
}
private void iterate(Node node, int depth) {
for (int i = 0; i < depth; i++) {
System.out.print(";");
}
System.out.println(node.name);
System.out.println(node.name + ": " + node.action + ", " + node.parameter);
if (node.children.size() > 0) {
java.util.Collections.sort(node.children);
@@ -103,7 +204,7 @@ public class CategoryTree {
public static void main(String[] args) {
try {
new CategoryTree("C:\\Temp\\Migration AH\\categories.json");
new CategoryTree("C:\\Temp\\Migration AH\\categories.json", "C:\\Temp\\Migration AH\\Media Assistant Categories - Code.csv");
} catch (IOException e) {
e.printStackTrace();
}