Skip to content

Instantly share code, notes, and snippets.

@andrewfinnell
Created September 26, 2018 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewfinnell/5a2b7ca51006f1a046925ea7ef2992e0 to your computer and use it in GitHub Desktop.
Save andrewfinnell/5a2b7ca51006f1a046925ea7ef2992e0 to your computer and use it in GitHub Desktop.
Agile SDK Transaction Management
package com.agile.api;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static com.agile.api.CommonConstants.ATT_PAGE_TWO_MULTITEXT10;
import static com.agile.api.ItemConstants.ATT_TITLE_BLOCK_NUMBER;
import static com.agile.api.ItemConstants.CLASS_PART;
public class TestTransactionManager
{
private static String AGILE_URL = "";
private static String USERNAME = "";
private static String PASSWORD = "";
@Test
public void testRollback() throws APIException
{
final String ITEM_NUMBER = "TEST-PART-TX-1";
// Open a session, create the part, and close the session.
IAgileSession session = createSession();
// Clean up from last run.
deleteIfExists(session, ITEM_NUMBER);
// Create a new part
Map<Object, Object> objectProps = new HashMap<>();
objectProps.put(ATT_TITLE_BLOCK_NUMBER, ITEM_NUMBER);
objectProps.put(ATT_PAGE_TWO_MULTITEXT10, "Notes");
IItem item = (IItem) session.createObject(CLASS_PART, objectProps);
session.close();
// Open the session again to ensure nothing is cached. This likely
// doesn't work anyways.
session = createSession();
// Start a Transaction to change a Value and Roll it back.
ITransactionManager tx = session.getTransactionManager();
tx.begin();
item = (IItem) session.getObject(CLASS_PART, ITEM_NUMBER);
// Verify the notes is set correctly.
String notes = (String) item.getValue(ATT_PAGE_TWO_MULTITEXT10);
System.out.println("Is " + notes + " Should Be Notes");
// change the value
item.setValue(ATT_PAGE_TWO_MULTITEXT10, "Foo");
notes = (String) item.getValue(ATT_PAGE_TWO_MULTITEXT10);
System.out.println("Is " + notes + " Should Be Foo");
// Roll back that change
tx.rollback();
// The rollback doesn't work.
notes = (String) item.getValue(ATT_PAGE_TWO_MULTITEXT10);
System.out.println("Is " + notes + " Should Be Notes");
session.close();
// Even getting a new session shows the change was committed immediately.
session = createSession();
item = (IItem) session.getObject(CLASS_PART, ITEM_NUMBER);
// This still returns "Foo"
notes = (String) item.getValue(ATT_PAGE_TWO_MULTITEXT10);
System.out.println("Is " + notes + " Should Be Notes");
}
private IAgileSession createSession() throws APIException
{
Map<Object, Object> config = new HashMap<>();
config.put(AgileSessionFactory.URL, AGILE_URL);
config.put(AgileSessionFactory.USERNAME, USERNAME);
config.put(AgileSessionFactory.PASSWORD, PASSWORD);
return AgileSessionFactory.createSessionEx(config);
}
private void deleteIfExists(IAgileSession session, String itemNumber)
{
try
{
IItem item = (IItem) session.getObject(CLASS_PART, itemNumber);
if (item != null)
{
item.delete();
if (item.isDeleted())
{
item.delete();
}
}
}
catch (APIException ex)
{
ex.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment