| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.resource.rewrite; |
| 2 | |
| 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 5 | import de.ids_mannheim.korap.config.KustvaktConfiguration; |
| 6 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 7 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 8 | |
| 9 | import java.util.Iterator; |
| 10 | |
| 11 | /** |
| 12 | * @author hanl |
| 13 | * @date 28/07/2015 |
| 14 | */ |
| Michael Hanl | 022543e | 2015-11-17 21:25:25 +0100 | [diff] [blame] | 15 | public class CollectionCleanupFilter implements RewriteTask.RewriteNodeAt { |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 16 | |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 17 | @Override |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 18 | public JsonNode preProcess(KoralNode node, KustvaktConfiguration config, |
| Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 19 | User user) { |
| Michael Hanl | 022543e | 2015-11-17 21:25:25 +0100 | [diff] [blame] | 20 | return process(node.rawNode()); |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | private JsonNode process(JsonNode root) { |
| Michael Hanl | 5d5a823 | 2015-09-14 23:12:31 +0200 | [diff] [blame] | 24 | JsonNode sub = root; |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 25 | 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 Hanl | 5d5a823 | 2015-09-14 23:12:31 +0200 | [diff] [blame] | 35 | |
| 36 | int count = node.size(); |
| Michael Hanl | bcb48c7 | 2015-11-17 22:11:05 +0100 | [diff] [blame] | 37 | // remove group element and replace with single doc |
| Michael Hanl | 5d5a823 | 2015-09-14 23:12:31 +0200 | [diff] [blame] | 38 | if (count == 1) |
| 39 | sub = node.path(0); |
| Michael Hanl | bcb48c7 | 2015-11-17 22:11:05 +0100 | [diff] [blame] | 40 | // indicate empty group |
| Michael Hanl | 5d5a823 | 2015-09-14 23:12:31 +0200 | [diff] [blame] | 41 | else if (count |
| 42 | == 0) // can't do anything here -- fixme: edge case?! |
| 43 | return null; |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 46 | if (!root.equals(sub)) { |
| Michael Hanl | 5d5a823 | 2015-09-14 23:12:31 +0200 | [diff] [blame] | 47 | if (sub.isObject()) { |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 48 | ObjectNode ob = (ObjectNode) root; |
| 49 | ob.removeAll(); |
| 50 | ob.putAll((ObjectNode) sub); |
| 51 | } |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | return root; |
| 55 | } |
| 56 | |
| 57 | // return null deletes node, if node return replace at level -1 |
| Michael Hanl | 5d5a823 | 2015-09-14 23:12:31 +0200 | [diff] [blame] | 58 | @Deprecated |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 59 | 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 Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public JsonNode postProcess(KoralNode node) { |
| 79 | return null; |
| 80 | } |
| Michael Hanl | 022543e | 2015-11-17 21:25:25 +0100 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | public String at() { |
| 84 | return "/collection"; |
| 85 | } |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 86 | } |