Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* This file is part of *** M y C o R e ***
* See http://www.mycore.de/ for details.
*
* MyCoRe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MyCoRe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MyCoRe. If not, see .
*/
package org.mycore.datamodel.metadata;
import java.util.Objects;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.mycore.common.MCRConstants;
import org.mycore.common.MCRException;
import org.mycore.common.MCRUtils;
import org.mycore.common.config.MCRConfiguration2;
import org.mycore.datamodel.language.MCRLanguageFactory;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.gson.JsonObject;
import io.swagger.v3.oas.annotations.media.Schema;
/**
* This class implements any methods for handling the basic data for all
* metadata classes of the metadata objects. The methods createXML() and
* createTypedContent() and createTextSearch() are abstract methods.
*
* @author Jens Kupferschmidt
* @version $Revision$ $Date$
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
setterVisibility = JsonAutoDetect.Visibility.NONE)
public abstract class MCRMetaDefault implements MCRMetaInterface {
// public data
public static final int DEFAULT_LANG_LENGTH = 12;
public static final int DEFAULT_TYPE_LENGTH = 256;
public static final int DEFAULT_STRING_LENGTH = 4096;
// common data
protected static final String NL = System.getProperties().getProperty("line.separator");
protected static final String DEFAULT_LANGUAGE = MCRConfiguration2.getString("MCR.Metadata.DefaultLang")
.orElse(MCRConstants.DEFAULT_LANG);
protected static final String DEFAULT_DATAPART = "metadata";
protected static final int DEFAULT_INHERITED = 0;
// logger
private static Logger LOGGER = LogManager.getLogger();
// MetaLangText data
protected String subtag;
protected String lang;
protected String type;
protected int inherited;
protected String datapart;
/**
* This is the constructor.
* The language element was set to en . The datapart element was set
* to metadata All other elemnts was set to an empty string. The
* inherited value is set to 0!
*/
public MCRMetaDefault() {
inherited = DEFAULT_INHERITED;
datapart = DEFAULT_DATAPART;
}
/**
* This is the constructor.
* The language element was set. If the value of lang is
* empty or false en was set. The datapart was set to default. All
* other elemnts was set to an empty string. The inherited value is set to
* 0!
*
* @param lang
* the default language
*/
public MCRMetaDefault(String lang) {
this();
this.lang = lang;
}
/**
* This is the constructor.
* The language element was set. If the value of lang is
* null, empty or false en was set. The subtag element was set to
* the value of subtag. If the value of subtag
* is null or empty an exception was throwed. The type element was set to
* the value of type, if it is null, an empty string was set
* to the type element. The datapart element was set. If the value of
* datapart, is null or empty the default was set.
* @param subtag the name of the subtag
* @param lang the language
* @param type the optional type string
* @param inherited a int value , > 0 if the data are inherited,
* else = 0.
*
* @exception MCRException if the subtag value is null or empty
*/
public MCRMetaDefault(String subtag, String lang, String type, int inherited)
throws MCRException {
this(lang);
setInherited(inherited);
this.subtag = subtag;
this.type = type;
}
/**
* This method set the inherited level. This can be 0 or an integer higher
* 0.
*
* @param value
* the inherited level value, if it is < 0, 0 is set
*/
public final void setInherited(int value) {
inherited = value;
}
/**
* This method increments the inherited value with 1.
*/
public final void incrementInherited() {
inherited++;
}
/**
* This method decrements the inherited value with 1.
*/
public final void decrementInherited() {
inherited--;
}
/**
* This method set the language element. If the value of
* lang is null, empty or false nothing was changed.
*
* @param lang
* the language
*/
public final void setLang(String lang) {
this.lang = lang;
}
/**
* This method set the subtag element. If the value of subtag
* is null or empty an exception was throwed.
*
* @param subtag
* the subtag
* @exception MCRException
* if the subtag value is null or empty
*/
public final void setSubTag(String subtag) throws MCRException {
this.subtag = subtag;
}
/**
* This method set the type element. If the value of type is
* null or empty nothing was changed.
*
* @param type
* the optional type
*/
public final void setType(String type) {
this.type = type;
}
/**
* This method get the inherited element.
*
* @return the inherited flag as int
*/
public final int getInherited() {
return inherited;
}
/**
* This method get the language element.
*
* @return the language
*/
public final String getLang() {
return lang;
}
/**
* This method get the subtag element.
*
* @return the subtag
*/
@Schema(hidden = true)
@JsonIgnore
public final String getSubTag() {
return subtag;
}
/**
* This method get the type element.
*
* @return the type
*/
public final String getType() {
return type;
}
/**
* This method read the XML input stream part from a DOM part for the
* metadata of the document.
*
* @param element
* a relevant DOM element for the metadata
* @exception MCRException
* if the subtag value is null or empty
*/
public void setFromDOM(Element element) throws MCRException {
if (element == null) {
return;
}
subtag = element.getName();
MCRUtils.filterTrimmedNotEmpty(element.getAttributeValue("lang", Namespace.XML_NAMESPACE))
.ifPresent(tempLang -> lang = tempLang);
MCRUtils.filterTrimmedNotEmpty(element.getAttributeValue("type"))
.ifPresent(tempType -> type = tempType);
MCRUtils.filterTrimmedNotEmpty(element.getAttributeValue("inherited"))
.map(Integer::parseInt)
.ifPresent(tempInherited -> inherited = tempInherited);
}
/**
* This abstract method create a XML stream for all data in this class,
* defined by the MyCoRe XML MCRMeta... definition for the given subtag.
*
* @exception MCRException
* if the content of this class is not valid
* @return a JDOM Element with the XML MCRMeta... part
*/
public Element createXML() throws MCRException {
try {
validate();
} catch (MCRException exc) {
debug();
throw exc;
}
Element elm = new Element(subtag);
if (getLang() != null && getLang().length() > 0) {
elm.setAttribute("lang", getLang(), Namespace.XML_NAMESPACE);
}
if (getType() != null && getType().length() > 0) {
elm.setAttribute("type", getType());
}
elm.setAttribute("inherited", Integer.toString(getInherited()));
return elm;
}
/**
* Creates a json object in the form of:
*
*/
@Override
public JsonObject createJSON() {
JsonObject obj = new JsonObject();
if (getLang() != null) {
obj.addProperty("lang", getLang());
}
if (getType() != null) {
obj.addProperty("type", getType());
}
obj.addProperty("inherited", getInherited());
return obj;
}
/**
* This method check the validation of the content of this class. The method
* returns true if
*
*
the subtag is not null or empty
*
the lang value was supported
*
* otherwise the method return false
*
* @return a boolean value
*/
@JsonIgnore
public boolean isValid() {
try {
validate();
return true;
} catch (MCRException exc) {
LOGGER.warn("The the metadata element '{}' is invalid.", subtag, exc);
}
return false;
}
/**
* Validates this MCRMetaDefault. This method throws an exception if:
*
*
the subtag is not null or empty
*
the lang value was supported
*
the inherited value is lower than zero
*
*
* @throws MCRException the MCRMetaDefault is invalid
*/
public void validate() throws MCRException {
subtag = MCRUtils.filterTrimmedNotEmpty(subtag).orElse(null);
if (subtag == null) {
throw new MCRException("No tag name defined!");
}
if (lang != null && !MCRLanguageFactory.instance().isSupportedLanguage(lang)) {
throw new MCRException(getSubTag() + ": language is not supported: " + lang);
}
if (getInherited() < 0) {
throw new MCRException(getSubTag() + ": inherited can not be smaller than '0': " + getInherited());
}
}
@Override
public int hashCode() {
return Objects.hash(datapart, inherited, lang, subtag, type);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
MCRMetaDefault other = (MCRMetaDefault) obj;
return Objects.equals(datapart, other.datapart) && Objects.equals(inherited, other.inherited)
&& Objects.equals(lang, other.lang) && Objects.equals(subtag, other.subtag)
&& Objects.equals(type, other.type);
}
/**
* This method put debug data to the logger (for the debug mode).
*/
public void debug() {
if (LOGGER.isDebugEnabled()) {
debugDefault();
LOGGER.debug(" ");
}
}
/**
* This method put common debug data to the logger (for the debug mode).
*/
public final void debugDefault() {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("SubTag = {}", subtag);
LOGGER.debug("Language = {}", lang);
LOGGER.debug("Type = {}", type);
LOGGER.debug("DataPart = {}", datapart);
LOGGER.debug("Inhreited = {}", String.valueOf(inherited));
}
}
@Override
public MCRMetaDefault clone() {
try {
MCRMetaDefault clone = (MCRMetaDefault) super.clone();
clone.subtag = this.subtag;
clone.lang = this.lang;
clone.type = this.type;
clone.datapart = this.datapart;
clone.inherited = this.inherited;
return clone;
} catch (CloneNotSupportedException e) {
//this is impossible!
return null;
}
}
}