Package com.dbxml.db.common.adapters

Source Code of com.dbxml.db.common.adapters.JAXBElementAdapter

package com.dbxml.db.common.adapters;

/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: JAXBElementAdapter.java,v 1.4 2006/02/02 18:53:47 bradford Exp $
*/

import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.FaultCodes;
import com.dbxml.db.core.adapter.SimpleAdapter;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.labrador.types.Variant;
import com.dbxml.xml.dtsm.DTSMException;
import com.dbxml.xml.dtsm.DTSMHelper;
import com.dbxml.xml.dtsm.DocumentTable;
import javax.xml.bind.Element;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
* JAXElementAdapter adapts a dbXML Collection to support the management of
* JAXB-bound Java classes.
*/

public final class JAXBElementAdapter extends SimpleAdapter {
   public JAXBElementAdapter(Collection col) {
      super(col);
   }

   private void putObject(Transaction tx, Object key, Element obj) throws DBException {
      Node node = new Variant(obj).getNode();
      DocumentTable dt;

      try {
         dt = DTSMHelper.nodeToTable(node, col.getSymbols());
      }
      catch ( DTSMException e ) {
         throw new DBException(FaultCodes.COL_CANNOT_STORE, "Error storing object '" + key + "'", e);
      }

      col.setDocument(tx, key, dt);
   }

   /**
    * setObject sets an JAXB Element object in the Collection based on the
    * provided Key.  dbXML takes care of associating the implementation class
    * with the JAXB Element object.
    *
    * @param tx The controlling Transaction
    * @param key The Key to use
    * @param obj The Object to set
    * @throws DBException If a Database Exception occurs
    */
   public void setObject(Transaction tx, Object key, Element obj) throws DBException {
      putObject(tx, key, obj);
   }

   /**
    * insertObject inserts an JAXB Element object into the Collection and
    * returns a newly generated Key.  dbXML takes care of associating the
    * implementation class with the JAXB Element object.
    *
    * @param tx The controlling Transaction
    * @param obj The Object to insert
    * @return The new Object Identifier
    * @throws DBException If a Database Exception occurs
    */
   public Key insertObject(Transaction tx, Element obj) throws DBException {
      Key key = col.createNewOID();
      putObject(tx, key, obj);
      return key;
   }

   /**
    * insertObject inserts an JAXB Element object into the Collection based
    * on the specified Key.  dbXML takes care of associating the
    * implementation class with the JAXB Element object.
    *
    * @param tx The controlling Transaction
    * @param key The Key to use
    * @param obj The Object to insert
    * @throws DBException If a Database Exception occurs
    */
   public void insertObject(Transaction tx, Object key, Element obj) throws DBException {
      putObject(tx, key, obj);
   }

   /**
    * getObject instantiates and returns a JAXB Element object based on the
    * provided Key.  dbXML takes care of instantiating the correct class, but
    * only if a class was registered with the Document in the first place.
    *
    * @param tx The controlling Transaction
    * @param key The Document Key
    * @return an Castable JAXB Element Instance
    * @throws DBException If a Database Exception occurs
    */
   public Element getObject(Transaction tx, Object key) throws DBException {
      Document doc = null;
      try {
         DocumentTable dt = col.getDocument(tx, key);
         if ( dt != null )
            doc = DTSMHelper.tableToDocument(dt);
      }
      catch ( DTSMException e ) {
         throw new DBException(FaultCodes.COL_CANNOT_RETRIEVE, "Error retrieving object '" + key + "'", e);
      }

      if ( doc != null ) {
         Variant v = new Variant(doc);
         return v.getJAXBElement();
      }

      return null;
   }
}

TOP

Related Classes of com.dbxml.db.common.adapters.JAXBElementAdapter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.