Updating a Member of the House

To update the characteristics of a given node or member, perform the following steps:

Prerequisites

See the topic Searching for a Member of the House for the previous procedure.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.tibco.tgdb.connection.TGConnection;
import com.tibco.tgdb.connection.TGConnectionFactory;
import com.tibco.tgdb.model.TGEntity;
import com.tibco.tgdb.model.TGGraphObjectFactory;
import com.tibco.tgdb.model.TGKey;


/**
 * For a given member of the House, update the attributes
 * 
 * Usage : java UpdateGraph [options]
 * 
 *  where options are:
 *   -memberName <memberName> Required. Member name - "Napoleon Bonaparte"
 *   -crownName <crownName>   Optional. Name while reigning - "Napoleon XVIII"
 *   -crownTitle <crownTitle> Optional. Title while reigning - "King of USA"    
 *   -houseHead <houseHead>   Optional. Head of the house - true or false
 *   -yearBorn <yearBorn>     Optional. Year of birth - 2004
 *   -yearDied <yearDied>     Optional. Year of death - 2016 or null if still alive
 *   -reignStart <reignStart> Optional. Date reign starts (format dd MMM yyyy) - 20 Jan 2008 or null if never reigned
 *   -reignEnd <reignEnd>     Optional. Date reign ends (format dd MMM yyyy) - 08 Nov 2016 or null if never reigned or still reigning
 *   
 *  For instance to update the house member named "Napoleon Bonaparte" :
 *  java UpdateGraph -memberName "Napoleon Bonaparte" -crownName "Napoleon XVIII" -crownTitle "King of USA" -yearDied null -reignEnd "31 Jan 2016"
 *
 */
public class UpdateGraph {
	
	static String url = "tcp://127.0.0.1:8222";
	static String user = "napoleon";
	static String pwd = "bonaparte";
	
	static String memberName = null;
	static String crownName = null;
	static String crownTitle = null;
	static String yearBorn = null;
	static String yearDied = null;
	static String reignStart = null;
	static String reignEnd= null;
	static String houseHead = null;
	
	static void parseArgs(String[] args) throws Exception {
		for (int i=0; i<args.length; i++) {
			if (args[i].equals("-memberName"))
				memberName = args[i+1];
			else if (args[i].equals("-crownName"))
				crownName = args[i+1];
			else if (args[i].equals("-crownTitle"))
				crownTitle = args[i+1];
			else if (args[i].equals("-yearBorn"))
				yearBorn = args[i+1];
			else if (args[i].equals("-yearDied"))
				yearDied = args[i+1];
			else if (args[i].equals("-houseHead"))
				houseHead = args[i+1];
			else if (args[i].equals("-reignStart")) 
				reignStart = args[i+1];
			else if (args[i].equals("-reignEnd")) 
				reignEnd = args[i+1];
		}
	}
	
	public static void main(String[] args) throws Exception {
		
		parseArgs(args);
		
		if (memberName == null) {
			System.out.println("No house member to update.\nArguments example: -memberName \"Napoleon Bonaparte\" -crownName \"Grand Napoleon\" -crownTitle \"King of the world\" -reignStart \"8 Nov 2001\" -yearDied 2016");
			return;
		}
		
		TGConnection conn = null;
		try {
			conn = TGConnectionFactory.getInstance().createConnection(url, user, pwd, null);
			conn.connect();

			TGGraphObjectFactory gof = conn.getGraphObjectFactory();
			if (gof == null) {
				throw new Exception("Graph object not found");
			}

			conn.getGraphMetadata(true);
			
			TGKey houseKey = gof.createCompositeKey("houseMemberType");
			
			houseKey.setAttribute("memberName", memberName);
			System.out.printf("Searching for member '%s'...\n",memberName);
      		TGEntity houseMember = conn.getEntity(houseKey, null);
      		if (houseMember != null) {
      			System.out.printf("House member '%s' found\n",houseMember.getAttribute("memberName").getAsString());
      			if (crownName != null)
      				houseMember.setAttribute("crownName", crownName);
      			if (crownTitle != null)
      				houseMember.setAttribute("crownTitle", crownTitle);
      			if (houseHead != null)
      				houseMember.setAttribute("houseHead", Boolean.parseBoolean(houseHead));
      			if (yearBorn != null)
      				houseMember.setAttribute("yearBorn", Integer.parseInt(yearBorn));
      			if (yearDied != null) { 
      				if (yearDied.equals("null"))
      					houseMember.setAttribute("yearDied", null);
      				else
      					houseMember.setAttribute("yearDied", Integer.parseInt(yearDied));
      			}
      			Calendar date = Calendar.getInstance();
      			if (reignStart != null) {
      				if (reignStart.equals("null"))
      					houseMember.setAttribute("reignStart", null);
      				else {
      					try {
      						date.setTime((new SimpleDateFormat("dd MMM yyyy").parse(reignStart)));
      						houseMember.setAttribute("reignStart", date); 
      					}
      					catch (ParseException e) {
      						throw new Exception("Member update failed - Wrong parameter: -reignStart format should be \"dd MMM yyyy\"");
      					}
      				}
      			}
      			if (reignEnd != null) {
      				if (reignEnd.equals("null"))
      					houseMember.setAttribute("reignEnd", null);
      				else {
      					try {
      						date.setTime((new SimpleDateFormat("dd MMM yyyy").parse(reignEnd)));
      						houseMember.setAttribute("reignEnd", date); 
      					}
      					catch (ParseException e) {
      						throw new Exception("Member update failed - Wrong parameter: -reignEnd format should be \"dd MMM yyyy\"");
      					}
      				}
      			}
      			
      			conn.updateEntity(houseMember);
      			conn.commit();
      			System.out.printf("House member '%s' updated successfully\n", memberName);	
      		} 
      		else {
      			System.out.printf("House member '%s' not found\n", memberName);
      		}
		}
		finally {
			if (conn != null)
				conn.disconnect();
		}
	}
}

Procedure

  1. Save the earlier provided java code in a file named UpdateGraph.java.
  2. Compile the code file using the command javac -cp <tgdb_home>/lib/tgdb-client.jar UpdateGraph.java.
  3. Run the compiled file using the command java -cp <tgdb_home>/lib/tgdb-client.jar:. UpdateGraph -memberName "Napoleon Bonaparte" -houseHead true -yearBorn 1946 -yearDied null -crownTitle "King of USA" -crownName "Napoleon X" -reignStart "20 Jan 2017" -reignEnd "19 Jan 2021"
    The output upon successful update is as follows:
    Searching for member 'Napoleon Bonaparte'...
    House member 'Napoleon Bonaparte' found
    House member 'Napoleon Bonaparte' updated successfully
    
  4. You can run the SearchGraph program again to validate the changes. See the topic Searching for a Member of the House for more details.
    The search results is as follows:
    House member 'Napoleon Bonaparte' found
    reignStart: 20 Jan 2017
    houseHead: true
    yearDied:
    crownName: Napoleon X
    crownTitle: King of USA
    memberName: Napoleon Bonaparte
    yearBorn: 1946
    reignEnd: 19 Jan 2021
    
    Attention:
    • See how the attributes have been updated with the node.setAttribute() call, and see how the node has been updated using the updateEntity() call followed by the commit() call. This is called a transacted update.