org.mycore.datamodel.metadata.MCRMetaClassification Maven / Gradle / Ivy
/*
* 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.mycore.common.MCRException;
import org.mycore.datamodel.classifications2.MCRCategoryID;
import com.google.gson.JsonObject;
/**
* This class implements all method for handling with the MCRMetaClassification
* part of a metadata object. The MCRMetaClassification class present a link to
* a category of a classification.
*
* <tag class="MCRMetaClassification" heritable="...">
* <subtag classid="..." categid="..." />
* </tag>
*
* @author Jens Kupferschmidt
* @version $Revision$ $Date: 2008-03-18 22:53:44 +0000 (Di, 18 Mrz
* 2008) $
*/
public class MCRMetaClassification extends MCRMetaDefault {
private static final Logger LOGGER = LogManager.getLogger();
// MCRMetaClassification data
protected MCRCategoryID category;
/**
* This is the constructor.
* The language element was set to en . The classid and categid value
* was set to an empty string.
*/
public MCRMetaClassification() {
super();
}
/**
* This is the constructor.
* The language element was set to en . 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 an empty string.
* the classid and the categid must be not null
* or empty!
* @param subtag the name of the subtag
* @param inherted a value >= 0
* @param type the type attribute
* @param classid the classification ID
* @param categid the category ID
*
* @exception MCRException if the subtag value, the classid value or
* the categid are null, empty, too long or not a MCRObjectID
*/
public MCRMetaClassification(String subtag, int inherted, String type, String classid,
String categid) throws MCRException {
super(subtag, null, type, inherted);
setValue(classid, categid);
}
/**
* This is the constructor.
* The language element was set to en . 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 an empty string.
* the classid and the categid must be not null
* or empty!
* @param subtag the name of the subtag
* @param inherted a value >= 0
* @param type the type attribute
* @param category a category id
*
* @exception MCRException if the subtag value is empty, too long or not a MCRObjectID
*/
public MCRMetaClassification(String subtag, int inherted, String type, MCRCategoryID category)
throws MCRException {
super(subtag, null, type, inherted);
if (category == null) {
throw new MCRException("Category is not set in " + getSubTag());
}
this.category = category;
}
/**
* The method return the classification ID.
*
* @return the classId
*/
public final String getClassId() {
return category.getRootID();
}
/**
* The method return the category ID.
*
* @return the categId
*/
public final String getCategId() {
return category.getID();
}
/**
* This method set values of classid and categid.
*
* @param classid
* the classification ID
* @param categid
* the category ID
* @exception MCRException
* if the classid value or the categid are null,
* empty, too long or not a MCRObjectID
*/
public final void setValue(String classid, String categid) throws MCRException {
category = new MCRCategoryID(classid, categid);
}
/**
* This method read the XML input stream part from a DOM part for the
* metadata of the document.
*
* @param element
* a relevant JDOM element for the metadata
* @exception MCRException
* if the classid value or the categid are null,
* empty, too long or not a MCRObjectID
*/
@Override
public void setFromDOM(Element element) throws MCRException {
super.setFromDOM(element);
String classid = element.getAttributeValue("classid");
String categid = element.getAttributeValue("categid");
setValue(classid, categid);
}
/**
* This method create a XML stream for all data in this class, defined by
* the MyCoRe XML MCRMetaClassification definition for the given subtag.
*
* @exception MCRException
* if the content of this class is not valid
* @return a JDOM Element with the XML MCRClassification part
*/
@Override
public Element createXML() throws MCRException {
Element elm = super.createXML();
elm.setAttribute("classid", getClassId());
elm.setAttribute("categid", getCategId());
return elm;
}
/**
* Creates the JSON representation. Extends the {@link MCRMetaDefault#createJSON()} method
* with the following data.
*
*
* {
* classid: "mycore_class_00000001",
* categid: "category1"
* }
*
*
*/
@Override
public JsonObject createJSON() {
JsonObject obj = super.createJSON();
obj.addProperty("classid", category.getRootID());
obj.addProperty("categid", category.getID());
return obj;
}
/**
* Validates this MCRMetaClassification. 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
* - the category is null
*
*
* @throws MCRException the MCRMetaClassification is invalid
*/
public void validate() throws MCRException {
super.validate();
if (category == null) {
throw new MCRException(getSubTag() + ": category is not yet set");
}
}
/**
* clone of this instance
*
* you will get a (deep) clone of this element
*
* @see java.lang.Object#clone()
*/
@Override
public MCRMetaClassification clone() {
MCRMetaClassification clone = (MCRMetaClassification) super.clone();
clone.category = this.category; // immutable so shallow copy ok
return clone;
}
/**
* This method put debug data to the logger (for the debug mode).
*/
@Override
public void debug() {
if (LOGGER.isDebugEnabled()) {
super.debugDefault();
LOGGER.debug("Category = {}", category);
LOGGER.debug(" ");
}
}
@Override
public boolean equals(Object obj) {
if (!super.equals(obj)) {
return false;
}
final MCRMetaClassification other = (MCRMetaClassification) obj;
return Objects.equals(this.category, other.category);
}
}