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.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   * Extends the default category dao with solr support. Every create/write/delete operation
40   * on a classification/category results in a solr reindex additionally.
41   * 
42   * @author Matthias Eichner
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              // remove all descendants and itself
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              // reindex parent
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         // remove all old categories
129         solrDelete(newCategory.getId(), newCategory.getParent());
130         // reindex all new
131         MCRSolrClassificationUtil.reindex(replacedCategories.toArray(new MCRCategoryImpl[replacedCategories.size()]));
132         return replacedCategories;
133     }
134 
135 }