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.net.URI;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.SortedSet;
25
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.apache.solr.client.solrj.SolrClient;
29 import org.apache.solr.client.solrj.impl.HttpSolrClient;
30 import org.mycore.datamodel.classifications2.MCRCategory;
31 import org.mycore.datamodel.classifications2.MCRCategoryDAOFactory;
32 import org.mycore.datamodel.classifications2.MCRCategoryID;
33 import org.mycore.datamodel.classifications2.MCRLabel;
34 import org.mycore.datamodel.classifications2.impl.MCRCategoryDAOImpl;
35 import org.mycore.datamodel.classifications2.impl.MCRCategoryImpl;
36 import org.mycore.solr.search.MCRSolrSearchUtils;
37
38
39
40
41
42
43
44 public class MCRSolrCategoryDAO extends MCRCategoryDAOImpl {
45
46 private static final Logger LOGGER = LogManager.getLogger(MCRSolrCategoryDAO.class);
47
48 @Override
49 public MCRCategory setURI(MCRCategoryID id, URI uri) {
50 MCRCategory category = super.setURI(id, uri);
51 MCRSolrClassificationUtil.reindex(category);
52 return category;
53 }
54
55 @Override
56 public MCRCategory setLabel(MCRCategoryID id, MCRLabel label) {
57 MCRCategory category = super.setLabel(id, label);
58 MCRSolrClassificationUtil.reindex(category);
59 return category;
60 }
61
62 @Override
63 public MCRCategory setLabels(MCRCategoryID id, SortedSet<MCRLabel> labels) {
64 MCRCategory category = super.setLabels(id, labels);
65 MCRSolrClassificationUtil.reindex(category);
66 return category;
67 }
68
69 @Override
70 public MCRCategory removeLabel(MCRCategoryID id, String lang) {
71 MCRCategory category = super.removeLabel(id, lang);
72 MCRSolrClassificationUtil.reindex(category);
73 return category;
74 }
75
76 @Override
77 public MCRCategory addCategory(MCRCategoryID parentID, MCRCategory category, int position) {
78 MCRCategory parent = super.addCategory(parentID, category, position);
79 MCRSolrClassificationUtil.reindex(category, parent);
80 return parent;
81 }
82
83 @Override
84 public void deleteCategory(MCRCategoryID id) {
85 MCRCategory category = MCRCategoryDAOFactory.getInstance().getCategory(id, 0);
86 MCRCategory parent = category.getParent();
87 super.deleteCategory(id);
88 solrDelete(id, parent);
89 }
90
91 protected void solrDelete(MCRCategoryID id, MCRCategory parent) {
92 try {
93
94 HttpSolrClient solrClient = MCRSolrClassificationUtil.getCore().getClient();
95 List<String> toDelete = MCRSolrSearchUtils.listIDs(solrClient,
96 "ancestor:" + MCRSolrClassificationUtil.encodeCategoryId(id));
97 toDelete.add(id.toString());
98 solrClient.deleteById(toDelete);
99
100 MCRSolrClassificationUtil.reindex(parent);
101 } catch (Exception exc) {
102 LOGGER.error("Solr: unable to delete categories of parent {}", id);
103 }
104 }
105
106 @Override
107 public void moveCategory(MCRCategoryID id, MCRCategoryID newParentID, int index) {
108 super.moveCategory(id, newParentID, index);
109 solrMove(id, newParentID, index);
110 }
111
112 protected void solrMove(MCRCategoryID id, MCRCategoryID newParentID, int index) {
113 try {
114 SolrClient solrClient = MCRSolrClassificationUtil.getCore().getClient();
115 List<String> reindexList = MCRSolrSearchUtils.listIDs(solrClient,
116 "ancestor:" + MCRSolrClassificationUtil.encodeCategoryId(id));
117 reindexList.add(id.toString());
118 reindexList.add(newParentID.toString());
119 MCRSolrClassificationUtil.reindex(MCRSolrClassificationUtil.fromString(reindexList));
120 } catch (Exception exc) {
121 LOGGER.error("Solr: unable to move categories of category {} to {}", id, newParentID);
122 }
123 }
124
125 @Override
126 public Collection<MCRCategoryImpl> replaceCategory(MCRCategory newCategory) throws IllegalArgumentException {
127 Collection<MCRCategoryImpl> replacedCategories = super.replaceCategory(newCategory);
128
129 solrDelete(newCategory.getId(), newCategory.getParent());
130
131 MCRSolrClassificationUtil.reindex(replacedCategories.toArray(new MCRCategoryImpl[replacedCategories.size()]));
132 return replacedCategories;
133 }
134
135 }