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.pi.urn;
20  
21  import java.util.Comparator;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Objects;
26  import java.util.Optional;
27  import java.util.concurrent.atomic.AtomicInteger;
28  import java.util.function.Predicate;
29  import java.util.regex.Matcher;
30  import java.util.regex.Pattern;
31  
32  import org.mycore.pi.MCRPIManager;
33  import org.mycore.pi.MCRPIRegistrationInfo;
34  
35  /**
36   * A Generator which helps to generate a URN with a counter inside.
37   */
38  public abstract class MCRCountingDNBURNGenerator extends MCRDNBURNGenerator {
39  
40      private static final Map<String, AtomicInteger> PATTERN_COUNT_MAP = new HashMap<>();
41  
42      MCRCountingDNBURNGenerator() {
43          super();
44      }
45  
46      protected AtomicInteger readCountFromDatabase(String countPattern) {
47          Pattern regExpPattern = Pattern.compile(countPattern);
48          Predicate<String> matching = regExpPattern.asPredicate();
49  
50          List<MCRPIRegistrationInfo> list = MCRPIManager.getInstance()
51              .getList(MCRDNBURN.TYPE, -1, -1);
52  
53          // extract the number of the PI
54          Optional<Integer> highestNumber = list.stream()
55              .map(MCRPIRegistrationInfo::getIdentifier)
56              .filter(matching)
57              .map(pi -> {
58                  // extract the number of the PI
59                  Matcher matcher = regExpPattern.matcher(pi);
60                  if (matcher.find() && matcher.groupCount() == 1) {
61                      String group = matcher.group(1);
62                      return Integer.parseInt(group, 10);
63                  } else {
64                      return null;
65                  }
66              }).filter(Objects::nonNull)
67              .min(Comparator.reverseOrder())
68              .map(n -> n + 1);
69          return new AtomicInteger(highestNumber.orElse(0));
70      }
71  
72      /**
73       * Gets the count for a specific pattern and increase the internal counter. If there is no internal counter it will
74       * look into the Database and detect the highest count with the pattern.
75       *
76       * @param pattern a reg exp pattern which will be used to detect the highest count. The first group is the count.
77       *                e.G. [0-9]+-mods-2017-([0-9][0-9][0-9][0-9])-[0-9] will match 31-mods-2017-0003-3 and the returned
78       *                count will be 4 (3+1).
79       * @return the next count
80       */
81      public final synchronized int getCount(String pattern) {
82          AtomicInteger count = PATTERN_COUNT_MAP
83              .computeIfAbsent(pattern, this::readCountFromDatabase);
84  
85          return count.getAndIncrement();
86      }
87  }