001    /*
002     * 
003     * $Revision: 14003 $ $Date: 2008-09-16 11:45:07 +0200 (Di, 16 Sep 2008) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.datamodel.metadata;
025    
026    import org.mycore.common.MCRException;
027    
028    /**
029     * This class implements all method for handling with the MCRMetaClassification
030     * part of a metadata object. The MCRMetaClassification class present a link to
031     * a category of a classification.
032     * <p>
033     * &lt;tag class="MCRMetaClassification" heritable="..."&gt; <br>
034     * &lt;subtag classid="..." categid="..." /&gt; <br>
035     * &lt;/tag&gt; <br>
036     * 
037     * @author Jens Kupferschmidt
038     * @version $Revision: 14003 $ $Date: 2008-03-18 22:53:44 +0000 (Di, 18 Mrz
039     *          2008) $
040     */
041    public class MCRMetaClassification extends MCRMetaDefault {
042        /** The length of the classification ID * */
043        public static final int MAX_CLASSID_LENGTH = MCRObjectID.MAX_LENGTH;
044    
045        public static final int MAX_CATEGID_LENGTH = 128;
046    
047        // MCRMetaClassification data
048        protected String classid;
049    
050        protected String categid;
051    
052        /**
053         * This is the constructor. <br>
054         * The language element was set to <b>en </b>. The classid and categid value
055         * was set to an empty string.
056         */
057        public MCRMetaClassification() {
058            super();
059            classid = "";
060            categid = "";
061        }
062    
063        /**
064         * This is the constructor. <br>
065         * The language element was set to <b>en </b>. The subtag element was set to
066         * the value of <em>set_subtag<em>. If the
067         * value of <em>set_subtag</em> is null or empty an exception was throwed.
068         * The type element was set to an empty string.
069         * the <em>set_classid</em> and the <em>categid</em> must be not null
070         * or empty!
071         *
072         * @param set_datapart     the global part of the elements like 'metadata'
073         *                         or 'service'
074         * @param set_subtag       the name of the subtag
075         * @param set_inherted     a value >= 0
076         * @param set_type         the type attribute
077         * @param set_classid      the classification ID
078         * @param set_categid      the category ID
079         * @exception MCRException if the set_subtag value, the set_classid value or
080         * the set_categid are null, empty, too long or not a MCRObjectID
081         */
082        public MCRMetaClassification(String set_datapart, String set_subtag, int set_inherted, String set_type, String set_classid, String set_categid)
083                throws MCRException {
084            super(set_datapart, set_subtag, "en", set_type, set_inherted);
085            setValue(set_classid, set_categid);
086        }
087    
088        /**
089         * The method return the classification ID.
090         * 
091         * @return the classId
092         */
093        public final String getClassId() {
094            return classid;
095        }
096    
097        /**
098         * The method return the category ID.
099         * 
100         * @return the categId
101         */
102        public final String getCategId() {
103            return categid;
104        }
105    
106        /**
107         * This method set values of classid and categid.
108         * 
109         * @param set_classid
110         *            the classification ID
111         * @param set_categid
112         *            the category ID
113         * @exception MCRException
114         *                if the set_classid value or the set_categid are null,
115         *                empty, too long or not a MCRObjectID
116         */
117        public final void setValue(String set_classid, String set_categid) throws MCRException {
118            if ((set_classid == null) || ((set_classid = set_classid.trim()).length() == 0)) {
119                throw new MCRException("The classid is empty.");
120            }
121    
122            if ((set_categid == null) || ((set_categid = set_categid.trim()).length() == 0)) {
123                throw new MCRException("The categid is empty.");
124            }
125    
126            if (set_classid.length() > MAX_CLASSID_LENGTH) {
127                throw new MCRException("The classid is too long.");
128            }
129    
130            if (set_categid.length() > MAX_CATEGID_LENGTH) {
131                throw new MCRException("The categid is too long.");
132            }
133    
134            classid = set_classid;
135            categid = set_categid;
136        }
137    
138        /**
139         * This method read the XML input stream part from a DOM part for the
140         * metadata of the document.
141         * 
142         * @param element
143         *            a relevant JDOM element for the metadata
144         * @exception MCRException
145         *                if the set_classid value or the set_categid are null,
146         *                empty, too long or not a MCRObjectID
147         */
148        public void setFromDOM(org.jdom.Element element) throws MCRException {
149            super.setFromDOM(element);
150    
151            String set_classid = element.getAttributeValue("classid");
152            String set_categid = element.getAttributeValue("categid");
153            setValue(set_classid, set_categid);
154        }
155    
156        /**
157         * This method create a XML stream for all data in this class, defined by
158         * the MyCoRe XML MCRMetaClassification definition for the given subtag.
159         * 
160         * @exception MCRException
161         *                if the content of this class is not valid
162         * @return a JDOM Element with the XML MCRClassification part
163         */
164        public org.jdom.Element createXML() throws MCRException {
165            if (!isValid()) {
166                throw new MCRException("The content of MCRMetaClassification in subtag " + subtag + " is not valid.");
167            }
168    
169            org.jdom.Element elm = new org.jdom.Element(subtag);
170    
171            if ((type != null) && ((type = type.trim()).length() != 0)) {
172                elm.setAttribute("type", type);
173            }
174    
175            elm.setAttribute("inherited", Integer.toString(inherited));
176            elm.setAttribute("classid", classid);
177            elm.setAttribute("categid", categid);
178    
179            return elm;
180        }
181    
182        /**
183         * This method check the validation of the content of this class. The method
184         * returns <em>true</em> if
185         * <ul>
186         * <li>the subtag is not null or empty
187         * </ul>
188         * otherwise the method return <em>false</em>
189         * 
190         * @return a boolean value
191         */
192        public boolean isValid() {
193            if (!super.isValid()) {
194                return false;
195            }
196            return classid != null && classid.length() > 0 && categid != null && categid.length() > 0;
197        }
198    
199        /**
200         * This method make a clone of this class.
201         */
202        public Object clone() {
203            return new MCRMetaClassification(datapart, subtag, inherited, type, classid, categid);
204        }
205    
206        /**
207         * This method put debug data to the logger (for the debug mode).
208         */
209        public void debug() {
210            super.debugDefault();
211            LOGGER.debug("ClassID            = " + classid);
212            LOGGER.debug("CategID            = " + categid);
213            LOGGER.debug(" ");
214        }
215    }