1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.solr.classification;
20
21 import java.util.LinkedList;
22 import java.util.Set;
23
24 import org.apache.solr.common.SolrInputDocument;
25 import org.mycore.datamodel.classifications2.MCRCategory;
26 import org.mycore.datamodel.classifications2.MCRCategoryID;
27 import org.mycore.datamodel.classifications2.MCRLabel;
28
29 public class MCRSolrCategory {
30
31 private MCRCategory category;
32
33 public MCRSolrCategory(MCRCategory category) {
34 this.category = category;
35 }
36
37 public SolrInputDocument toSolrDocument() {
38 SolrInputDocument doc = new SolrInputDocument();
39 LinkedList<MCRCategory> ancestors = MCRSolrClassificationUtil.getAncestors(category);
40 MCRCategory parent = !ancestors.isEmpty() ? ancestors.getLast() : null;
41
42 MCRCategoryID id = category.getId();
43 doc.setField("id", id.toString());
44 doc.setField("classification", id.getRootID());
45 doc.setField("type", "node");
46 if (category.isCategory()) {
47 doc.setField("category", id.getID());
48 }
49
50 Set<MCRLabel> labels = category.getLabels();
51 for (MCRLabel label : labels) {
52 doc.addField("label." + label.getLang(), label.getText());
53 }
54
55 if (category.hasChildren()) {
56 for (MCRCategory child : category.getChildren()) {
57 doc.addField("children", child.getId().toString());
58 }
59 }
60
61 if (parent != null) {
62 doc.setField("parent", parent.getId().toString());
63 doc.setField("index", parent.getChildren().indexOf(category));
64 }
65
66 for (MCRCategory ancestor : ancestors) {
67 doc.addField("ancestors", ancestor.getId().toString());
68 }
69 return doc;
70 }
71
72 }