001    /*
002     * 
003     * $Revision: 15101 $ $Date: 2009-04-22 16:14:02 +0200 (Wed, 22 Apr 2009) $
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.services.webservices;
025    
026    import java.util.Collection;
027    import java.util.Collections;
028    
029    import org.apache.log4j.Logger;
030    import org.jdom.Document;
031    import org.mycore.common.xml.MCRURIResolver;
032    import org.mycore.datamodel.common.MCRLinkTableManager;
033    import org.mycore.datamodel.common.MCRXMLTableManager;
034    import org.mycore.datamodel.metadata.MCRObjectID;
035    import org.mycore.services.fieldquery.MCRHit;
036    import org.mycore.services.fieldquery.MCRQuery;
037    import org.mycore.services.fieldquery.MCRQueryManager;
038    import org.mycore.services.fieldquery.MCRResults;
039    
040    /**
041     * This class contains MyCoRe Webservices
042     * 
043     * @author Harald Richter
044     * 
045     * @version $Revision: 15101 $ $Date: 2009-04-22 16:14:02 +0200 (Wed, 22 Apr 2009) $
046     * 
047     */
048    public class MCRWebService implements MCRWS {
049        private static final Logger logger = Logger.getLogger(MCRWebService.class);
050    
051        private static MCRXMLTableManager TM = MCRXMLTableManager.instance();
052    
053        /*
054         * (non-Javadoc)
055         * 
056         * @see MCRWS#MCRDoRetrieveObject(java.lang.String)
057         */
058        public org.w3c.dom.Document MCRDoRetrieveObject(String ID) throws Exception {
059            // check the ID and retrieve the data
060            org.jdom.Document d = TM.readDocument(new MCRObjectID(ID));
061    
062            org.jdom.output.DOMOutputter doo = new org.jdom.output.DOMOutputter();
063    
064            if (logger.isDebugEnabled()) {
065                org.jdom.output.XMLOutputter outputter = new org.jdom.output.XMLOutputter();
066                logger.debug(outputter.outputString(d));
067            }
068    
069            return doo.output(d);
070        }
071    
072        /*
073         * (non-Javadoc)
074         * 
075         * @see MCRWS#MCRDoRetrieveClassification(java.lang.String,
076         *      java.lang.String, java.lang.String)
077         */
078        public org.w3c.dom.Document MCRDoRetrieveClassification(String level, String type, String classID, String categID, String format)
079                throws Exception {
080            if (null == format)
081                format = "metadata";
082    
083            String uri = "classification:" + format + ":" + level + ":" + type + ":" + classID + ":" + categID;
084            org.jdom.Element cl = MCRURIResolver.instance().resolve(uri);
085    
086            if (logger.isDebugEnabled()) {
087                org.jdom.output.XMLOutputter outputter = new org.jdom.output.XMLOutputter();
088                logger.debug(outputter.outputString(cl));
089            }
090    
091            org.jdom.Document d = new org.jdom.Document((org.jdom.Element) (cl.clone()));
092            return new org.jdom.output.DOMOutputter().output(d);
093        }
094    
095        /*
096         * (non-Javadoc)
097         * 
098         * @see MCRWS#MCRDoQuery(org.w3c.dom.Document)
099         */
100        public org.w3c.dom.Document MCRDoQuery(org.w3c.dom.Document query) throws Exception {
101            Document doc = null;
102            try {
103                org.jdom.input.DOMBuilder d = new org.jdom.input.DOMBuilder();
104                doc = d.build(query);
105    
106                if (logger.isDebugEnabled()) {
107                    org.jdom.output.XMLOutputter outputter = new org.jdom.output.XMLOutputter();
108                    logger.debug(outputter.outputString(doc));
109                }
110    
111                // Execute query
112                MCRResults res = MCRQueryManager.search(MCRQuery.parseXML(doc), true);
113                Document result = new Document(res.buildXML());
114    
115                org.jdom.output.DOMOutputter doo = new org.jdom.output.DOMOutputter();
116                return doo.output(result);
117            } catch (Exception e) {
118                org.jdom.output.XMLOutputter outputter = new org.jdom.output.XMLOutputter();
119                logger.error("Error while excuting query:\n" + outputter.outputString(doc), e);
120                throw e;
121            }
122        }
123    
124        /*
125         * (non-Javadoc)
126         * 
127         * @see MCRWS#MCRDoQuery(org.w3c.dom.Document)
128         */
129        public org.w3c.dom.Document MCRDoRetrieveLinks(String from, String to, String type) throws Exception {
130            // set default empty answer
131            MCRResults results = new MCRResults();
132            // check parameter
133            if ((type == null) || (type.length() == 0)) {
134                type = MCRLinkTableManager.ENTRY_TYPE_REFERENCE;
135            }
136            if (from == null)
137                from = "";
138            if (to == null)
139                to = "";
140            if ((from.length() != 0) || (to.length() != 0)) {
141                logger.debug("Input parameter : type=" + type + "   from=" + from + "   to=" + to);
142                Collection<String> links = Collections.emptyList();
143                MCRLinkTableManager LM = MCRLinkTableManager.instance();
144                // Look for links
145                if ((from = from.trim()).length() != 0) {
146                    // logger.debug("Use MCRLinkTableManager.getDestinationOf("+from+","+type+")");
147                    links = LM.getDestinationOf(from, type);
148                } else {
149                    // logger.debug("Use MCRLinkTableManager.getSourceOf("+to+","+type+")");
150                    links = LM.getSourceOf(to, type);
151                }
152                // logger.debug("Get "+(new Integer(links.size())).toString()+" results");
153                for (String link : links) {
154                    MCRHit hit = new MCRHit(link);
155                    results.addHit(hit);
156                }
157            } else {
158                logger.warn("Input parameter from and to are empty!");
159            }
160            // write output
161            Document result = new Document(results.buildXML());
162            org.jdom.output.DOMOutputter doo = new org.jdom.output.DOMOutputter();
163            return doo.output(result);
164        }
165    
166    }