Attachment for Insert....

java code:

/
Example for the Tamino API for Java
||
|| It is assumed that a Tamino database called “mydb” has
|| been created and is running.
|| For storing data the default collection ino:etc is used
|| which should only be used for examples/test code.
|| We do not recommend this for real applications.
||
|| The following tasks are performed:
||
|| - establish a connection to the Tamino database
|| - obtain a system accessor and print out system information
|| - obtain an XML accessor
|| - read XML documents from files and insert them into the database
|| - query all complete documents and all persons with name “Atkins”
|| - delete all persons named “Atkins” if found in the database
|| - query again all persons and list them
|| - delete remaining person documents
|| - close the connection
/
package com.softwareag.tamino.db.API.examples.person;


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.dom.
;
import com.softwareag.tamino.db.API.response.;
import com.softwareag.tamino.db.api.io.
;
import org.w3c.dom.;
import java.io.
;

public class ProcessPersons {

// use a system accessor to check if the database is alive and
// print some system information to stdout
protected static boolean checkServerAndPrintSystemInformation(TConnection connection)
throws TAccessorException {
// Obtain the TSystemAccesor
TSystemAccessor systemaccessor = connection.newSystemAccessor();

if (!systemaccessor.isServerAlive()) {
return false;
} else {
System.out.println( " server is alive" );
System.out.println( “\nHere is some systeminformation” );
System.out.println( “------------------------------\n” );
System.out.println( “The Tamino server hosting " + DATABASE_URI +
" is version " + systemaccessor.getServerVersion() );
System.out.println( “(Server API version: " + systemaccessor.getServerAPIVersion() +
“, Tamino API for Java version: " + systemaccessor.getAPIVersion() +
“)\n” );
return true;
}
}

// utility method to retrieve text of an element specified by its tag from a DOM tree
private static String getDOMElementTextByTagName(Element element, String tagname) {
NodeList nl = element.getElementsByTagName(tagname);
Node n;
n = nl.item(0);
n = n.getFirstChild();
return n.getNodeValue();
}

// Prints some data about a person to stdout
private static void printPerson(TXMLObject xmlObject) throws TStreamWriteException {

// Obtain the DOM root element
Element e = (Element)xmlObject.getElement();

String surname = getDOMElementTextByTagName(e, “surname”);
String firstname= getDOMElementTextByTagName(e, “firstname”);
System.out.print( surname + “, " + firstname);

System.out.print( " (” );
System.out.print( “ino:id="” + xmlObject.getId() +”" " );
System.out.print( “collection="” + xmlObject.getCollection() +”" " );
System.out.print( “doctype="” + xmlObject.getDoctype() +”" " );
System.out.println( “)” );
}

// read a given filename into an TXMLObject, store it in the database and print the person
protected static TXMLObject performInsertPersonFromFile(TXMLObjectAccessor accessor, String filename)
throws TException, FileNotFoundException {
// Instantiate an empty TXMLObject instance using the DOM object model
TXMLObject xmlObject = TXMLObject.newInstance( TDOMObjectModel.getInstance() );

// Read a document from a file and insert it into the database.
// This is not used in this sample!
// xmlObject.readFrom( new FileReader( filename ) );

// Read a document out of the class path and insert it into the database
InputStream myInputStream = (new ProcessPersons()).getClass().getResourceAsStream( filename );
xmlObject.readFrom( myInputStream );

try {
// Invoke the insert operation and get response
TResponse response = accessor.insert( xmlObject );

System.out.print( " Inserted: " );
printPerson(xmlObject);
}
catch (TInsertException insertException) {
System.out.print( “\nCan’t insert: " );
printPerson( xmlObject );
System.out.println( " Reason: " +
insertException.getAccessFailureMessage().getMessageLine() + “\n” );
}
return xmlObject;
}

// take a given query, put the count() function around and return the result as String
// this shows, how to obtain the result from a query that returns a number or other literal
private static String performCountQuery(TXMLObjectAccessor accessor, TQuery query)
throws TException {
try {
// Build a new query string
TQuery countquery = TQuery.newInstance( “count(” + query.getExpression() + “)” );
return accessor.query( countquery ).getQueryContentAsString();
}
catch (TQueryException queryException) {
showAccessFailure( queryException );
throw queryException;
}
}

// perform a query and list all persons returned
protected static void performQueryAndListPersons(TXMLObjectAccessor accessor, TQuery query)
throws TException {

// Now let’s make the query
try {
TResponse response = accessor.query( query );
TXMLObjectIterator iterator = response.getXMLObjectIterator();
System.out.print( “The query "” + query + “" returns " );
if (!iterator.hasNext()) {
System.out.println( “no documents!” );
} else {
System.out.println( performCountQuery( accessor, query ) + " documents, which are:” );
while (iterator.hasNext()) {
TXMLObject xmlObject = iterator.next();
System.out.print( " " );
printPerson(xmlObject);
}
}
}
catch (TQueryException queryException) {
showAccessFailure( queryException );
throw queryException;
}
}

/
// delete all documents for a given query with a given accessor
protected static void performDelete(TXMLObjectAccessor accessor, TQuery query)
throws TException {
// Finally, delete the document again
try {
TResponse response = accessor.delete( query );
System.out.println(“\nDeleted all documents for query "” + query + “"”);
}
catch (TDeleteException deleteException) {
showAccessFailure( deleteException );
throw deleteException;
}
}
/
// Show the reason for the access failure.
private static void showAccessFailure(TAccessorException accessorException) {
// Obtain an access failure message telling the exact reason if Tamino request failed.
TAccessFailureMessage accessFailure = accessorException.getAccessFailureMessage();
if ( accessFailure != null )
System.out.println( “Access failed with:” + accessFailure );
else
System.out.println( “Access failed:” + accessorException.getMessage() );
}

public static void main(String args) throws TException {

// Print out a program header to stdout
System.out.println( “\nProcessPersons sample programm” );
System.out.println( “==============================” );
System.out.print( “Connecting to Tamino database " + DATABASE_URI + “, …” );

// Obtain the connection factory
TConnectionFactory connectionFactory = TConnectionFactory.getInstance();
// Obtain the connection to the database
TConnection connection = connectionFactory.newConnection( DATABASE_URI );

// Check if the connection is available and print out some system information
if ( !checkServerAndPrintSystemInformation( connection ) )
return;

// Obtain a TXMLObjectAccessor to the “ino:etc” collection using the DOM object model
TXMLObjectAccessor accessor = connection.newXMLObjectAccessor(
TAccessLocation.newInstance( “ino:etc” ),
TDOMObjectModel.getInstance() );

// Print header for database operations
System.out.println( “Insert and query and delete in default collection "ino:etc"” );
System.out.println( “-----------------------------------------------------------\n” );

try {
// Instantiate an empty TXMLObject instance using the DOM object model
TXMLObject xmlObject = TXMLObject.newInstance( TDOMObjectModel.getInstance() );

// read person documents from files and insert them into the “people” collection
System.out.println( “Reading documents from file and insert into database\n” );

performInsertPersonFromFile( accessor, “person1.xml” );
performInsertPersonFromFile( accessor, “person2.xml” );
performInsertPersonFromFile( accessor, “person3.xml” );
performInsertPersonFromFile( accessor, “person4.xml” );
xmlObject = performInsertPersonFromFile( accessor, “person5.xml” );

// Build a query to reference all documents of doctype “person”
TQuery queryall = TQuery.newInstance( xmlObject.getDoctype() );

// Build a query to reference documents of doctype “person” with surname = “Atkins”
TQuery querysome = TQuery.newInstance( xmlObject.getDoctype() + “[//surname=‘Atkins’]” );

System.out.println(””);
// Initiate a query to count and list all instances of a certain Doctype
performQueryAndListPersons( accessor, queryall );

// Create a boolean query and check if persons named “Atkins” are stored
// This shows how to get the result of a boolean query as a Java boolean
TQuery queryAtkins = TQuery.newInstance( “//surname=‘Atkins’” );
String queryAtkinsResult = accessor.query( queryAtkins ).getQueryContentAsString();
System.out.println( “\nThe query "” + queryAtkins +
“" returns "” + queryAtkinsResult + “"” );
boolean AtkinsAvailable = new Boolean( queryAtkinsResult ).booleanValue();

// Do some processing, if persons named “Atkins” are stored
if (AtkinsAvailable) {
System.out.println(“So list and then delete all "Atkins" documents\n”);
// Initiate a query to count and list some instances of a certain Doctype
performQueryAndListPersons( accessor, querysome);

// Initiate the removal of all “Atkins”
/////////// performDelete( accessor, querysome );

System.out.println(“”);
// Initiate a query to count and list all instances of a certain Doctype
performQueryAndListPersons( accessor, queryall );
}

// Initiate the removal
//////////////// performDelete( accessor, queryall );
}
catch (TException taminoException) {
taminoException.printStackTrace();
}
catch (FileNotFoundException filenotfoundException) {
filenotfoundException.printStackTrace();
}

// Close the connection
connection.close();
}

// URI of the Tamino database, please edit accordingly
private final static String DATABASE_URI = “http://localhost:8087/tamino/mydb”;
}

Run result:
ProcessPersons sample programm
==============================
Connecting to Tamino database http://localhost:8087/tamino/mydb, … server is alive
Here is some systeminformation
------------------------------
The Tamino server hosting http://localhost:8087/tamino/mydb is version 3.1.1.4
(Server API version: 1.1, Tamino API for Java version: 3.1.2.4)
Insert and query and delete in default collection “ino:etc”
-----------------------------------------------------------
Reading documents from file and insert into database
Inserted: Atkins, Paul (ino:id=“11” collection=“ino:etc” doctype=“person” )
Inserted: Bloggs, Fred (ino:id=“12” collection=“ino:etc” doctype=“person” )
Inserted: M

Just an administrative note: this posting belongs to this discussion.

Greetings,
Trevor.