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.classification; 20 21 import org.apache.logging.log4j.LogManager; 22 import org.apache.logging.log4j.Logger; 23 import org.mycore.common.MCRCache; 24 import org.mycore.datamodel.classifications2.MCRCategory; 25 import org.mycore.datamodel.classifications2.MCRCategoryDAO; 26 import org.mycore.datamodel.classifications2.MCRCategoryDAOFactory; 27 import org.mycore.datamodel.classifications2.MCRCategoryID; 28 import org.mycore.datamodel.classifications2.MCRLabel; 29 import org.w3c.dom.Element; 30 31 /** 32 * MCRAuthorityInfo holds a combination of either authority ID and value code, or authorityURI and valueURI. In MODS, 33 * this combination typically represents a value from a normed vocabulary like a classification. The AuthorityInfo can 34 * be mapped to a MCRCategory in MyCoRe. 35 * 36 * @see <a href="http://www.loc.gov/standards/mods/userguide/classification.html">MODS classification guidelines</a> 37 * @author Frank L\u00FCtzenkirchen 38 */ 39 abstract class MCRAuthorityInfo { 40 41 private static Logger LOGGER = LogManager.getLogger(MCRAuthorityInfo.class); 42 43 private static final MCRCategoryDAO DAO = MCRCategoryDAOFactory.getInstance(); 44 45 /** 46 * A cache that maps authority information to the category ID that is represented by that info. 47 */ 48 private static final MCRCache<String, Object> CATEGORY_ID_BY_AUTHORITY_INFO = new MCRCache<>(1000, 49 "Category ID by authority info"); 50 51 /** 52 * Used in the cache to indicate the case when no category ID maps to the given authority info 53 */ 54 private static final String NULL = "null"; 55 56 /** 57 * Returns the label value of the given type ("language"), or the given default if that label does not exist in the 58 * category. 59 */ 60 protected static String getLabel(MCRCategory category, String labelType, String defaultLabel) { 61 return category.getLabel(labelType).map(MCRLabel::getText).orElse(defaultLabel); 62 } 63 64 /** 65 * Returns the category ID that is represented by this authority information. 66 * 67 * @return the category ID that maps this authority information, or null if no matching category exists. 68 */ 69 public MCRCategoryID getCategoryID() { 70 String key = toString(); 71 LOGGER.debug("get categoryID for {}", key); 72 73 Object categoryID = CATEGORY_ID_BY_AUTHORITY_INFO.getIfUpToDate(key, DAO.getLastModified()); 74 if (categoryID == null) { 75 LOGGER.debug("lookup categoryID for {}", key); 76 categoryID = lookupCategoryID(); 77 if (categoryID == null) { 78 categoryID = NULL; // Indicate that no matching category found, null can not be cached directly 79 } 80 CATEGORY_ID_BY_AUTHORITY_INFO.put(key, categoryID); 81 } 82 return categoryID instanceof MCRCategoryID ? (MCRCategoryID) categoryID : null; 83 } 84 85 /** 86 * Looks up the category ID for this authority information in the classification database. 87 */ 88 protected abstract MCRCategoryID lookupCategoryID(); 89 90 /** 91 * Sets this authority information in the given MODS XML element by setting authority/authorityURI/valueURI 92 * attributes and/or value code as text. 93 */ 94 public abstract void setInElement(org.jdom2.Element modsElement); 95 96 /** 97 * Sets this authority information in the given MODS XML element by setting authority/authorityURI/valueURI 98 * attributes and/or value code as text. 99 */ 100 public abstract void setInElement(Element modsElement); 101 102 }