blob: 6f12d59f0007b452c147365b3edd37db5c768c49 [file] [log] [blame]
Michael Hanl1e18cb42015-08-06 20:57:35 +02001package de.ids_mannheim.korap.resource.rewrite;
2
3import com.fasterxml.jackson.databind.JsonNode;
4import com.fasterxml.jackson.databind.node.ObjectNode;
Michael Hanl59bff812015-10-27 23:10:32 +01005import de.ids_mannheim.korap.config.KustvaktConfiguration;
6import de.ids_mannheim.korap.user.User;
Michael Hanl1e18cb42015-08-06 20:57:35 +02007import de.ids_mannheim.korap.utils.JsonUtils;
8
9import java.util.Iterator;
10
11/**
12 * @author hanl
13 * @date 28/07/2015
14 */
Michael Hanl022543e2015-11-17 21:25:25 +010015public class CollectionCleanupFilter implements RewriteTask.RewriteNodeAt {
Michael Hanl1e18cb42015-08-06 20:57:35 +020016
Michael Hanl1e18cb42015-08-06 20:57:35 +020017 @Override
Michael Hanlf0785322015-11-13 16:14:45 +010018 public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
Michael Hanl59bff812015-10-27 23:10:32 +010019 User user) {
Michael Hanl022543e2015-11-17 21:25:25 +010020 return process(node.rawNode());
Michael Hanl1e18cb42015-08-06 20:57:35 +020021 }
22
23 private JsonNode process(JsonNode root) {
Michael Hanl5d5a8232015-09-14 23:12:31 +020024 JsonNode sub = root;
Michael Hanl1e18cb42015-08-06 20:57:35 +020025 if (root.isObject()) {
26 if (root.has("operands")) {
27 JsonNode node = root.at("/operands");
28 Iterator<JsonNode> it = node.elements();
29 while (it.hasNext()) {
30 JsonNode n = it.next();
31 JsonNode s = process(n);
32 if (s == null)
33 it.remove();
34 }
Michael Hanl5d5a8232015-09-14 23:12:31 +020035
36 int count = node.size();
Michael Hanlbcb48c72015-11-17 22:11:05 +010037 // remove group element and replace with single doc
Michael Hanl5d5a8232015-09-14 23:12:31 +020038 if (count == 1)
39 sub = node.path(0);
Michael Hanlbcb48c72015-11-17 22:11:05 +010040 // indicate empty group
Michael Hanl5d5a8232015-09-14 23:12:31 +020041 else if (count
42 == 0) // can't do anything here -- fixme: edge case?!
43 return null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020044 }
45
Michael Hanl1e18cb42015-08-06 20:57:35 +020046 if (!root.equals(sub)) {
Michael Hanl5d5a8232015-09-14 23:12:31 +020047 if (sub.isObject()) {
Michael Hanl1e18cb42015-08-06 20:57:35 +020048 ObjectNode ob = (ObjectNode) root;
49 ob.removeAll();
50 ob.putAll((ObjectNode) sub);
51 }
Michael Hanl1e18cb42015-08-06 20:57:35 +020052 }
53 }
54 return root;
55 }
56
57 // return null deletes node, if node return replace at level -1
Michael Hanl5d5a8232015-09-14 23:12:31 +020058 @Deprecated
Michael Hanl1e18cb42015-08-06 20:57:35 +020059 private JsonNode processNodes(JsonNode jsonNode) {
60 if (jsonNode.isObject()) {
61 if (jsonNode.has("operands")) {
62 JsonNode node = jsonNode.at("/operands");
63 int count = node.size();
64 if (count == 1) {
65 // move to super node if any
66 return node.path(0);
67 }else if (count == 0) {
68 // remove container
69 return null;
70 }
71 return jsonNode;
72 }
73 }
74 return JsonUtils.createArrayNode();
75 }
Michael Hanlf0785322015-11-13 16:14:45 +010076
77 @Override
78 public JsonNode postProcess(KoralNode node) {
79 return null;
80 }
Michael Hanl022543e2015-11-17 21:25:25 +010081
82 @Override
83 public String at() {
84 return "/collection";
85 }
Michael Hanl1e18cb42015-08-06 20:57:35 +020086}