package com.dbxml.db.client.xmldb;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: QueryServiceImpl.java,v 1.2 2004/02/12 00:17:55 bradford Exp $
 */

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import com.dbxml.db.client.ResultSetClient;
import com.dbxml.util.dbXMLException;

import org.xmldb.api.base.*;

/**
 * QueryServiceImpl
 */

public abstract class QueryServiceImpl implements Service {
   protected Properties props = new Properties();
   protected Map nsMap = Collections.synchronizedMap(new LinkedHashMap());
   protected CollectionImpl col;
   protected String style;

   public QueryServiceImpl(CollectionImpl col, String style) {
      this.col = col;
      this.style = style;
   }

   public void setNamespace(String prefix, String uri) throws XMLDBException {
      nsMap.put(prefix, uri);
   }

   public String getNamespace(String prefix) throws XMLDBException {
      return (String)nsMap.get(prefix);
   }

   public void removeNamespace(String prefix) throws XMLDBException {
      nsMap.remove(prefix);
   }

   public void clearNamespaces() throws XMLDBException {
      nsMap.clear();
   }

   public String getProperty(String name) throws XMLDBException {
      return props.getProperty(name);
   }

   public void setProperty(String name, String value) throws XMLDBException {
      props.setProperty(name, value);
   }

   public void setCollection(Collection col) throws XMLDBException {
      // NOOP
   }

   protected ResourceSet getResourceSet(ResultSetClient rs) throws XMLDBException {
      return new ResourceSetImpl(col, rs);
   }

   protected ResultSetClient performQuery(String query) throws XMLDBException {
      try {
         return col.getCollectionClient().queryCollection(style, query, nsMap);
      }
      catch ( dbXMLException e ) {
         throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage());
      }
   }

   protected ResultSetClient performQueryResource(String id, String query) throws XMLDBException {
      try {
          return col.getCollectionClient().queryDocument(style, query, nsMap, id);
      }
      catch ( dbXMLException e ) {
         throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage());
      }
   }
}
