Accessing databaseContextNewPrimaryKey from Java App
Accessing databaseContextNewPrimaryKey from Java App
- Subject: Accessing databaseContextNewPrimaryKey from Java App
- From: Calven Eggert <email@hidden>
- Date: Fri, 29 Jul 2011 10:59:00 -0400
I had to create a Java App to run on a daily basis from a cronjob to import records from one database to another. I needed to use the databaseContextNewPrimaryKey call in order to create a special primary key consisting of the year and then a 3 digit number. i.e. 2011001, 2011002, 2011003, etc. In case others are interested I thought I'd post my solution here. FYI, I initially forgot the public keyword in front of the databaseContextNewPrimaryKey method and so it was never called. doh. lesson learned.
Calven
-------------------------------------------------------
public class GLImport extends Object {
protected JDBCAdaptor adaptor1;
protected EODatabase database1;
protected EODatabaseContext dbContext1;
protected EOEditingContext ec;
public static void main(String args[]) {
try {
GLImport theImport = new GLImport();
theImport.doImport();
} catch(Exception e) {
e.printStackTrace();
System.out.println("Error "+ e);
}
}
// Setup all the databases.
public GLImport() throws Exception {
super();
adaptor1 = new JDBCAdaptor("JavaJDBCAdaptor");
database1 = new EODatabase(adaptor1);
dbContext1 = new EODatabaseContext(database1);
EOObjectStoreCoordinator.defaultCoordinator().addCooperatingObjectStore(dbContext1);
dbContext1.setDelegate(this);
EOModelGroup modelGroup = EOModelGroup.defaultGroup();
String classPath = GLImport.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String modelPath = "file://"+NSPathUtilities.stringByDeletingLastPathComponent(classPath);
java.net.URL greenlightModelURL = new java.net.URL(modelPath+"/Greenlight.eomodeld");
EOModel model1 = modelGroup.addModelWithPathURL(greenlightModelURL);
adaptor1.setConnectionDictionary(model1.connectionDictionary());
database1.addModel(model1);
modelGroup.loadAllModelObjects();
ec = new EOEditingContext();
}
public void doImport() {
...
// loop - get records from other database
EOEnterpriseObject newRequest = createRequest(ec);
...
// end of loop
ec.saveChanges();
}
public EOEnterpriseObject createRequest(EOEditingContext ec) {
String entityName = "Request";
EOEnterpriseObject newRequest = createInstance(ec, entityName);
ec.insertObject(newRequest);
newRequest.takeValueForKey(new NSTimestamp(), "creationdate");
...
return newRequest;
}
public EOEnterpriseObject createInstance(EOEditingContext ec, String tblName) {
EOClassDescription cd = EOClassDescription.classDescriptionForEntityName(tblName);
return (EOEnterpriseObject)cd.createInstanceWithEditingContext(ec, null);
}
public NSDictionary databaseContextNewPrimaryKey(EODatabaseContext dbCtxt, Object object, EOEntity entity) {
if (entity.name().equals("Request")) {
NSTimestamp todaysDate = new NSTimestamp();
GregorianCalendar todayGC = new GregorianCalendar();
todayGC.setTime(todaysDate);
Integer tempInteger = new Integer(todayGC.get(GregorianCalendar.YEAR));
int year = tempInteger.intValue();
NSArray rawRows = EOUtilities.rawRowsForSQL(new EOEditingContext(), "Greenlight", "SELECT MAX(REQUEST_ID) FROM REQUEST", null);
int pk = year * 1000;
if (rawRows.count() > 0) {
NSDictionary rowWithPK = (NSDictionary)rawRows.objectAtIndex(0);
Object maxPK = rowWithPK.objectForKey("MAX_REQUEST_ID_");
if (maxPK != NSKeyValueCoding.NullValue) {
Double newPK = new Double(maxPK.toString());
int tempPK = newPK.intValue();
if (tempPK < pk) {
tempPK = pk;
} else {
pk = tempPK;
}
}
}
NSMutableDictionary newPrimaryKey = new NSMutableDictionary();
newPrimaryKey.takeValueForKey(new Integer(++pk), "requestid");
return newPrimaryKey;
} else {
return null;
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden