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.mods.enrichment;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.concurrent.Callable;
24  
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.jdom2.Element;
28  
29  /**
30   * Used to request publication data from a given data source,
31   * trying the identifiers supported by this data source one by one.
32   * 
33   * Depending on the configuration properties  
34   * MCR.MODS.EnrichmentResolver.DefaultStopOnFirstResult=true|false
35   * and
36   * MCR.MODS.EnrichmentResolver.DataSource.[ID].StopOnFirstResult=true|false
37   * the data source will stop trying to resolve publication data after 
38   * the first successful call with a given identifier and skip others, 
39   * or will try to retrieve data for all given identifiers.
40   * 
41   * @see MCRDataSource
42   *
43   * @author Frank L\u00FCtzenkirchen
44   */
45  class MCRDataSourceCall implements Callable<Boolean> {
46  
47      private static final Logger LOGGER = LogManager.getLogger(MCRDataSourceCall.class);
48  
49      private MCRDataSource ds;
50  
51      private MCRIdentifierPool idPool;
52  
53      private List<Element> results = new ArrayList<Element>();
54  
55      private boolean gotResults = false;
56  
57      MCRDataSourceCall(MCRDataSource ds, MCRIdentifierPool idPool) {
58          this.ds = ds;
59          this.idPool = idPool;
60      }
61  
62      /**
63       * Used to request publication data from a given data source,
64       * trying the identifiers supported by this data source one by one.
65       *
66       * @return true, if the data source returned valid publication data
67       */
68      public Boolean call() {
69          if (!isFinished()) {
70              loop: for (MCRIdentifierResolver idResolver : ds.getResolvers()) {
71                  for (MCRIdentifier id : idPool.getCurrentIdentifiersOfType(idResolver.getType())) {
72                      if (isFinished()) {
73                          break loop;
74                      }
75  
76                      Element result = idResolver.resolve(id.getValue());
77                      if (result != null) {
78                          gotResults = true;
79                          results.add(result);
80                          idPool.addIdentifiersFrom(result);
81                      }
82  
83                      LOGGER.info(ds + " with " + id + " returned " + (result != null ? "" : "no ") + "valid data");
84                  }
85              }
86          }
87  
88          return wasSuccessful();
89      }
90  
91      boolean wasSuccessful() {
92          return gotResults;
93      }
94  
95      private boolean isFinished() {
96          return ds.shouldStopOnFirstResult() ? wasSuccessful() : false;
97      }
98  
99      List<Element> getResults() {
100         return results;
101     }
102 
103     void clearResults() {
104         results.clear();
105     }
106 }