View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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          // ids
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          // labels
50          Set<MCRLabel> labels = category.getLabels();
51          for (MCRLabel label : labels) {
52              doc.addField("label." + label.getLang(), label.getText());
53          }
54          // children
55          if (category.hasChildren()) {
56              for (MCRCategory child : category.getChildren()) {
57                  doc.addField("children", child.getId().toString());
58              }
59          }
60          // parent
61          if (parent != null) {
62              doc.setField("parent", parent.getId().toString());
63              doc.setField("index", parent.getChildren().indexOf(category));
64          }
65          // ancestors
66          for (MCRCategory ancestor : ancestors) {
67              doc.addField("ancestors", ancestor.getId().toString());
68          }
69          return doc;
70      }
71  
72  }