// Generated by the WOLips Templateengine Plug-in at 11.08.2011 15:22:27
package rrrr;
import com.webobjects.eocontrol.EOEditingContext;
import com.webobjects.eocontrol.EOFetchSpecification;
import com.webobjects.eocontrol.EOGenericRecord;
import com.webobjects.eocontrol.EOObserverCenter;
import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSLog;
import com.webobjects.appserver.WOApplication;
public class Application extends WOApplication {
int counter =1;
public static void main(String[] argv) {
WOApplication.main(argv, Application.class);
}
public Application() {
this.setAllowsConcurrentRequestHandling(true); 
NSLog.out.appendln("workerThreadCountMax:" + workerThreadCountMax() +", concurr RH:" + allowsConcurrentRequestHandling());
NSLog.setAllowedDebugGroups(NSLog.DebugGroupDatabaseAccess + NSLog.DebugGroupMultithreading);
//executeSQL("update customer set age=-1, secondValue=-1"); // do this before start
// start the long running thread 
MyThread demoThread = new MyThread(counter, 15000);
demoThread.start();
}
 
class MyThread extends Thread
{
int identifier;
int waitingtime =0;
EOFetchSpecification fs = new EOFetchSpecification("Customer", null, null);
EOGenericRecord customer;
public MyThread(int identifier, int waitingtime)
{
this.identifier = identifier;
this.waitingtime =waitingtime;
}
public void run()
{
EOEditingContext ec= new myec();
System.out.println(ec.toString());
ec.setDelegate(ec);
ec.lock();
NSArray myListofOEs = ec.objectsWithFetchSpecification(fs);
customer= (EOGenericRecord) myListofOEs.objectAtIndex(0);
System.out.println(identifier + ":init:age" + customer.valueForKey("age")); 
System.out.println( identifier + ":init:secondAttribute:" + customer.valueForKey("secondAttribute")); 
System.out.println( "Set Attributes start");
customer.takeValueForKey( 
new Integer(identifier)
,"age");
customer.takeValueForKey( 
new Integer(identifier)
,"secondAttribute");
System.out.println( "Set Attributes END");
System.out.println( identifier + " age before save:" + customer.valueForKey("age")); 
System.out.println( identifier + " secondAttribute before save:" + customer.valueForKey("secondAttribute")); 
System.out.println("Snaphot:" + customer.snapshot());
System.out.println("SnaphotC:" + ec.committedSnapshotForObject(customer));
ec.unlock();
if (identifier==1 ) // the first long running thread calls the first intermediate thread here
{
Application.this.counter ++;
MyThread demoThread = new MyThread( Application.this.counter, 800);
demoThread.start();
}
// the first thread sleeps 14 seconds here, the other 800 ms
try {
sleep(waitingtime);
} catch (InterruptedException e) {
e.printStackTrace();
}
ec.lock();
if (identifier==1 ) 
{
System.out.println( "Set second value. Here should appear a objectWillChange....");
//EOObserverCenter.notifyObserversObjectWillChange(null); // this resolves the problem
customer.takeValueForKey( // notification lost in cyberspace 
new Integer(9999)
,"secondAttribute");

//customer.takeValueForKey( // notification lost in cyberspace 
//new Integer(9999)
//,"age"); 

	System.out.println("Snaphot:" + customer.snapshot());
System.out.println("SnaphotC:" + ec.committedSnapshotForObject(customer));
System.out.println( "Set Second Attribute ,first thread, end");
} 
System.out.println( "Savechanges!");
ec.saveChanges();
System.out.println( identifier + " age after save:" + customer.valueForKey("age")); 
System.out.println( identifier + " secondAttribute after save:" + customer.valueForKey("secondAttribute")); 
System.out.println("Snaphot:" + customer.snapshot());
System.out.println("SnaphotC:" + ec.committedSnapshotForObject(customer));
ec.unlock();
// intermediate thread calls subsequent intermediate thread
if (identifier >1 && identifier <8)
{
Application.this.counter ++;
MyThread demoThread = new MyThread( Application.this.counter, 800);
demoThread.start();
}
} // run
} // end of inner class
} // end of app
