/* ModifiedMusician.java package com.softwareag.tamino.db.api.examples.jazz; */ import com.softwareag.tamino.db.api.accessor.TXMLObjectAccessor; import com.softwareag.tamino.db.api.accessor.TQuery; import com.softwareag.tamino.db.api.accessor.TQueryException; import com.softwareag.tamino.db.api.accessor.TAccessorException; import com.softwareag.tamino.db.api.accessor.TSystemAccessor; import com.softwareag.tamino.db.api.accessor.TInsertException; import com.softwareag.tamino.db.api.accessor.TDeleteException; import com.softwareag.tamino.db.api.accessor.TAccessLocation; 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 org.jdom.output.*; import java.io.*; import java.util.*; public class ModifiedMusician { /** Creates new musicianCollaborationResult */ public ModifiedMusician(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 the concrete TXMLObjectAccessor using the JDOM object model accessor = connection.newXMLObjectAccessor( TAccessLocation.newInstance( collection ) , TJDOMObjectModel.getInstance() ); } // process query private TResponse processQuery(String s) throws Exception { TQuery query = TQuery.newInstance( s ); try { // Invoke the query operation return accessor.query( query ); } catch (TQueryException queryException) { // Inform about the reason for the failure showAccessFailure(queryException); return null; } } // Show the reason for the access failure private void showAccessFailure(TAccessorException accessorException) throws Exception { // Obtain an access failure message telling the exact reason // if Tamino request failed TAccessFailureMessage accessFailure = accessorException.getAccessFailureMessage(); if ( accessFailure != null ) throw new Exception( "Access failed:" + accessFailure ); else throw new Exception( "Access failed:" + accessorException.getMessage() ); } // show result private void show(String keyValue) throws Exception { try { // Build a query and process it TResponse response = processQuery( "jazzMusician[@ID='" + keyValue + "']" ); // Get first (and single) jazzMusician object if (!response.hasFirstXMLObject()) throw new Exception("Nothing found"); TXMLObject xmlObject = response.getFirstXMLObject(); // Get top-level JDOM element Element jazzMusician = (Element) xmlObject.getElement(); // Get and change the first name of the Musician Element name = jazzMusician.getChild("name"); Element firstName = name.getChild("first"); System.out.println ("First Name was: " + firstName.getText()); firstName.setText("Charlie"); System.out.println ("First Name is now: " + firstName.getText()); // Output with JDOM output tool XMLOutputter outputter = new XMLOutputter(); outputter.output(jazzMusician, System.out); // Update the existing jazzMusician instance in Tamino response = accessor.update(xmlObject); System.out.println("Response to update: " + response.getReturnValue()); // Remove the ino:id of the existing jazzMusician jazzMusician.removeAttribute("id",INO_NAMESPACE); // Now use the rest of the existing jazzMusician JDOM object to build a new one... Element lastName = name.getChild("last"); System.out.println ("Name was: " + firstName.getText() + " " + lastName.getText()); firstName.setText("Buddy"); lastName.setText("Rich"); jazzMusician.setAttribute("ID","RichBuddy"); jazzMusician.setAttribute("type","instrumentalist"); Element birthDate = jazzMusician.getChild("birthDate"); Element instrument = jazzMusician.getChild("instrument"); birthDate.setText("1917-09-30"); instrument.setText("drums"); System.out.println ("Name is now: " + firstName.getText() + " " + lastName.getText()); // Output with JDOM output tool outputter.output(jazzMusician, System.out); // Insert the new Object to Tamino TXMLObject newMusician = TXMLObject.newInstance(jazzMusician); response = accessor.insert(newMusician); System.out.println("Response to insert: " + response.getReturnValue()); System.out.println("Successfully inserted ino:id " + newMusician.getId()); } catch (Exception e) { throw e; } finally { // Close the connection connection.close(); } } /** * @param args the command line arguments */ public static void main(String[] args) throws Exception { ModifiedMusician modifiedMusician = new ModifiedMusician( DATABASE_URI , COLLECTION ); modifiedMusician.show(args[0]); } // Constant for the database URI private final static String DATABASE_URI = "http://localhost/tamino/welcome_4_1_4"; // Constant for the collection private final static String COLLECTION = "encyclopedia"; // Constant for ino namespace private final static Namespace INO_NAMESPACE = Namespace.getNamespace("ino", "http://namespaces.softwareag.com/tamino/response2"); // The database connection private TConnection connection = null; // The accessor instance, here a high level TXMLObjectAccessor. private TXMLObjectAccessor accessor = null; }