package com.jaxfront.tamino.test; import com.softwareag.tamino.db.api.accessor.*; import com.softwareag.tamino.db.api.common.*; import com.softwareag.tamino.db.api.connection.*; import com.softwareag.tamino.db.api.objectModel.*; import com.softwareag.tamino.db.api.objectModel.jdom.*; import com.softwareag.tamino.db.api.response.*; import org.jdom.*; import org.jdom.input.*; import java.io.*; public class InsertPO { // URI of the Tamino database, please edit accordingly private final static String DATABASE_URI = "http://10.0.0.120/tamino/JAXFront"; // XML document to be written to the connected database private final static String XML = ""; // The database connection private TConnection connection = null; // The accessor, here a high-level TXMLObjectAccessor private TXMLObjectAccessor accessor = null; public InsertPO(String databaseURI,String collection) throws TConnectionException { // Obtain the connection factory TConnectionFactory connectionFactory = TConnectionFactory.getInstance(); // Obtain the connection to the database connection = connectionFactory.newConnection( databaseURI ); // Obtain a TXMLObjectAccessor with a JDOM object model accessor = connection.newXMLObjectAccessor( TAccessLocation.newInstance( collection ) , TJDOMObjectModel.getInstance() ); } public static void main(String[] args) throws TException { InsertPO insertPO = new InsertPO( DATABASE_URI , "po-example" ); insertPO.show(); } private void performDelete(TQuery query) throws TException { // Finally, we delete the document again try { TResponse response = accessor.delete( query ); System.out.println( "Deleted the document!" ); } catch (TDeleteException deleteException) { showAccessFailure( deleteException ); throw deleteException; } } private void performInsert(TXMLObject xmlObject) throws TException { // TResponse represents an access response from Tamino TResponse response = null; // Insert the XML document try { // Invoke the insert operation and obtain the response response = accessor.insert( xmlObject ); // Show the collection, doctype and id System.out.println( "Insert succeeded, ino:collection:" + xmlObject.getCollection() + ", ino:doctype:" + xmlObject.getDoctype() + ", ino:id:" + xmlObject.getId() ); } catch (TInsertException insertException) { showAccessFailure( insertException ); // Rethrow the exception throw insertException; } } private void performQuery(TQuery query) throws TException { // Now we query the updated object try { TResponse response = accessor.query( query ); TXMLObject xmlObject = response.getFirstXMLObject(); // Write the XML content into a StringWriter StringWriter stringWriter = new StringWriter(); xmlObject.writeTo( stringWriter ); System.out.println( "Queried document:" + stringWriter ); } catch (TQueryException queryException) { showAccessFailure( queryException ); throw queryException; } } private void performUpdate(TQuery query,String updatedText) throws TException { TLocalTransaction localTransaction = null; // Perform query and update operations within a transaction try { // Switch to local transaction mode localTransaction = connection.useLocalTransactionMode(); // Invoke the query to obtain the document and to lock it TResponse response = accessor.query( query ); // Obtain the TXMLObject from the response TXMLObject xmlObject = response.getFirstXMLObject(); if ( xmlObject == null ) return; // Obtain the JDOM element and update its content Element element = (Element)xmlObject.getElement(); element.setText( updatedText ); // Invoke the update response = accessor.update( xmlObject ); System.out.println( "Update succeeded!" ); // Commit the transaction localTransaction.commit(); } // TQueryException and TUpdateException are both derived from TAccessorException // so we simply use the base class here catch (TAccessorException accessorException) { showAccessFailure( accessorException ); localTransaction.rollback(); throw accessorException; } finally { // Switch back to auto-commit mode connection.useAutoCommitMode(); } } private void show() throws TException { try { // Instantiate a TXMLObject with the JDOM object model TXMLObject xmlObject = TXMLObject.newInstance( toJDOM( XML ) ); // Initiate the insert performInsert( xmlObject ); // Construct a query expression so that the inserted document can be referenced TQuery query = TQuery.newInstance( xmlObject.getDoctype() + "[@ino:id=\"" + xmlObject.getId() + "\"]" ); // Initiate the update performUpdate( query , "Hello World, updated :-)" ); // Initiate the query performQuery( query ); // Initiate the removal //performDelete( query ); } catch (JDOMException jdomException) { jdomException.printStackTrace(); } catch (TException taminoException) { taminoException.printStackTrace(); } finally { // Close the connection connection.close(); } } // Inform about the reason for the failure private void showAccessFailure(TAccessorException accessorException) { // Obtain an access failure message with the exact reason if Tamino request failed. TAccessFailureMessage accessFailure = accessorException.getAccessFailureMessage(); if ( accessFailure != null ) System.out.println( "Access failed:" + accessFailure ); else System.out.println( "Access failed:" + accessorException.getMessage() ); } // Build a JDOM Element from the given XML private Element toJDOM(String xml) throws JDOMException { SAXBuilder saxBuilder = new SAXBuilder(); Document document = saxBuilder.build( new StringReader( xml ) ); return document.getRootElement(); } }