// Generated by the WOLips Templateengine Plug-in at Jan 14, 2010 2:16:25 PM
package your.app;

import com.webobjects.eoaccess.EODatabaseContext;
import com.webobjects.eoaccess.EOUtilities;
import com.webobjects.eocontrol.EOEditingContext;
import com.webobjects.eocontrol.EOEnterpriseObject;
import com.webobjects.eocontrol.EOObjectStore;
import com.webobjects.eocontrol.EOObjectStoreCoordinator;
import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSData;
import com.webobjects.foundation.NSLog;

import com.webobjects.appserver.WOApplication;

public class Application extends WOApplication {
	public static void main(String[] argv) {
		WOApplication.main(argv, Application.class);
	}

	private void loadSampleDataIfAbsent(EOEditingContext context){
		NSArray<EOEnterpriseObject> currentTalent = EOUtilities.objectsForEntityNamed(context, "Talent");
		if((currentTalent == null) || currentTalent.count() == 0){
			EOEnterpriseObject newTalent = EOUtilities.createAndInsertInstance(context, "Talent");
			newTalent.takeValueForKey("Mark", "firstName");
			newTalent.takeValueForKey("Testing", "lastName");
			try {
				context.saveChanges();
			} catch (Exception e){
				NSLog.err.appendln("Caught exception during loadSampleDataIfAbsent()\nException was:\n" + e);
			}
		}			
	}

	private EOEnterpriseObject fetchSampleRecord(EOEditingContext context){
		return EOUtilities.faultWithPrimaryKeyValue(context, "Talent", 1);
	}
	
	private void logObjectsMissingGIDs(EOEditingContext context, EOEnterpriseObject eo){
		EOObjectStore os = context.rootObjectStore();
		if(os instanceof EOObjectStoreCoordinator){
			EOObjectStoreCoordinator osc = (EOObjectStoreCoordinator)os;
			EOObjectStore os2 = osc.objectStoreForObject(eo);
			if(os2 instanceof EODatabaseContext){
				EODatabaseContext dc = (EODatabaseContext)os2;
				NSArray<EOEnterpriseObject> objectsWithMissingGlobalIds = dc.missingObjectGlobalIDs();
				NSLog.err.appendln("objectsWithMissingGlobalIds = " + objectsWithMissingGlobalIds);
			}
		}

	}
	
	private void loadFetchAndProcess(){
		EOEditingContext ec = new EOEditingContext();
		try {
			ec.lock();

			NSLog.err.appendln("**");
			NSLog.err.appendln("** Loading sample data if absent **");
			loadSampleDataIfAbsent(ec);
			
			NSLog.err.appendln("**");
			NSLog.err.appendln("** Fetching sample Talent EO **");
			EOEnterpriseObject talent = fetchSampleRecord(ec);
			NSLog.err.appendln("talent is " + talent.eoShallowDescription());
			NSLog.err.appendln("talent is a fault: " + talent.isFault());
			logObjectsMissingGIDs(ec, talent);

			NSLog.err.appendln("**");
			NSLog.err.appendln("** Fetching it's TalentPhoto EO **");
			EOEnterpriseObject talentPhoto = (EOEnterpriseObject)talent.valueForKey("photo");
			NSLog.err.appendln("talentPhoto is " + talentPhoto);
			NSLog.err.appendln("talentPhoto is a fault: " + talentPhoto.isFault());
			NSLog.err.appendln("talentPhoto.eoDescription(): " + talentPhoto.eoDescription());
			logObjectsMissingGIDs(ec, talent);

			NSLog.err.appendln("**");
			NSLog.err.appendln("** Tripping the fault for the TalentPhoto EO **");
			NSData photo = (NSData)talentPhoto.valueForKey("photo");
			NSLog.err.appendln("photo is " + photo);
			NSLog.err.appendln("talentPhoto is " + talentPhoto);
			NSLog.err.appendln("talentPhoto is a fault: " + talentPhoto.isFault());
			NSLog.err.appendln("talentPhoto.eoDescription(): " + talentPhoto.eoDescription());
			logObjectsMissingGIDs(ec, talent);

			NSLog.err.appendln("**");
			NSLog.err.appendln("** Change the firstName on Talent EO and save changes**");
			String firstName = (String)talent.valueForKey("firstName");
			if((firstName != null) && (firstName.startsWith("Mark"))){
				talent.takeValueForKey("Bob", "firstName");
			}else{
				talent.takeValueForKey("Mark", "firstName");
			}
			ec.saveChanges();
			logObjectsMissingGIDs(ec, talent);
			
			NSLog.err.appendln("**");
			NSLog.err.appendln("** Set the photo on the TalentPhoto EO and save changes**");
			talentPhoto.takeValueForKey(new NSData(), "photo");
			talentPhoto.takeValueForKey(talent, "talent");
			ec.saveChanges();
			logObjectsMissingGIDs(ec, talent);
		}finally{
			ec.unlock();
		}		
	}

	public Application() {
		NSLog.out.appendln("Welcome to " + name() + " !");
		loadFetchAndProcess();
	}
}
