package com.dbxml.db.common.xupdate;

/*
 * 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: XUpdateQuery.java,v 1.4 2004/07/29 12:26:47 bradford Exp $
 */

import com.dbxml.db.common.adapters.DOMAdapter;
import com.dbxml.db.common.query.QueryBase;
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.query.CompilationException;
import com.dbxml.db.core.query.ProcessingException;
import com.dbxml.db.core.query.QueryException;
import com.dbxml.db.core.query.ResultSet;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.xml.NamespaceMap;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 * XUpdateQuery
 */

final class XUpdateQuery extends QueryBase {
   private XUpdateImpl xu;
   private DOMAdapter domAdapter;

   public XUpdateQuery(Collection context, String query, NamespaceMap nsMap, Key[] keys) throws QueryException {
      super(context, query, nsMap, keys);
      domAdapter = new DOMAdapter(context);
   }

   public void compileQuery() throws CompilationException {
      try {
         xu = new XUpdateImpl();
         xu.setQString(query);
      }
      catch ( Exception e ) {
         throw new CompilationException("Error Compiling XUpdate Query", e);
      }
   }

   public String getQueryStyle() {
      return XUpdateQueryResolver.STYLE_XUPDATE;
   }

   public ResultSet execute(Transaction tx) throws QueryException {
      try {
         if ( keys != null ) {
            try {
               for ( int i = 0; i < keys.length; i++ ) {
                  Document doc = domAdapter.getDocument(tx, keys[i]);

                  if ( doc == null )
                     continue;

                  Node ctxNode = doc.getDocumentElement();
                  xu.execute(ctxNode);

                  // If there's still a Document Element, then update.
                  // Otherwise, XUpdate wanted to delete the Document
                  if ( doc.getDocumentElement() != null )
                     domAdapter.setDocument(tx, keys[i], doc);
                  else
                     domAdapter.remove(tx, keys[i]);
               }
            }
            catch ( Exception e ) {
               e.printStackTrace(System.err);
            }
         }
         else
            xu.execute(tx, domAdapter);

         return new XUpdateResultSet(tx, context, this, xu.getModified());
      }
      catch ( Exception e ) {
         throw new ProcessingException("Error executing XUpdate query", e);
      }
   }
}
