package com.bexp;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
import java.util.*;
//import org.apache.commons.collections.bidimap.*;
//import org.apache.commons.collections.*;
import org.JWrapper.*;
import com.bexp.ObjInfo.SavedTag;
import com.bexp.ejb.*;
public /*final*//*abstract*/ class
JHomeDynamicCompositeWrapper
extends JDynamicCompositeWrapper
//implements java.util.Observer
//,Runnable
{
private ReentrantLock lock = new ReentrantLock(true);
public Lock getLock() { return lock; }
protected IObjHome home;
protected HomeEventProxy event_proxy;
private int event_proxy_slot_id;
protected ObjInfo obj_info;
/*
protected java.util.Map wrappers
= new java.util.HashMap();//key/wrapper pairs
*/
protected Class entity_class;
protected ObjSrc obj_src = null;
private SDApp sd_app;
public JHomeDynamicCompositeWrapper
(
String _name,
Class _entity_class,
ObjSrc _obj_src,
ObjInfo _obj_info//,
//SDApp _sd_app
) throws Exception
{
super(_name);
obj_info = _obj_info;
entity_class = _entity_class;
obj_src = _obj_src;
sd_app = obj_info.getSDApp();//_sd_app;
home = ObjSession.getSession().getHome(entity_class);
event_proxy = ObjSession.getSession().getEventProxy(entity_class);
//event_proxy.addObserver(this);
event_proxy_slot_id = event_proxy.getRecvSignal()
.connect(new function1()
{
public Void Op(ObjHomeEvent evt) throws Exception
{ ASSERT.debug(this,"~~~~~~OP~~~~~~~~~"); update(evt); return null; }
}
);
if(! (home instanceof com.bexp.ejb.Access.ISDObjHome))
if(ObjSession.getSession().getCurrentPerson().getUsername().equals("root"))
this.getUserFunctions().put("access",
new UserFunction("Access rights for this entity type",this)
{
public void act() throws Exception
{
boolean edit_result;
boolean retry = false;
com.bexp.ejb.Access.SDObj access_object = home.getAccessObj();
JWrapper edit_wrp =
sd_app.getEntitiesMap().get(com.bexp.ejb.Access.SDObj.class.getPackage())
.getDefaultInfo().getDefaultDetailedWrapper(access_object);
do
{
edit_result = sd_app.edit(edit_wrp);
if(edit_result)
{
ASSERT.setWaitingState(true);
try { access_object.save(); retry=false; }
catch(Throwable th) { ASSERT.alert(th); retry=true; }
finally { ASSERT.setWaitingState(false);}
} else { retry=false; }
} while(retry);
}
} );
/*
final JHomeDynamicCompositeWrapper context_ = this;
this.getUserFunctions().put("dump_map",
new UserFunction("Dump map",this)
{
JHomeDynamicCompositeWrapper context = context_;
public void act() throws Exception
{
System.out.println("");//testing
for(int i=0; i> pair = wrappers.get(i);
for(JWrapper w : ((JCompositeWrapper)wrp).getChildren())//testing
{ System.out.print(w);}//testing
System.out.println();
System.out.println("pk :"+pair.get2nd().getPK());//testing
}
System.out.println("");//testing
}
} );
*/
//new Thread(this).start();
if(!(ObjSession.getSession().getHome(this.getEntityClass()))
.isCreateAllowed()) { this.setCreateAllowed(false); }
}
/*
public BlockingQueue< function0 > exec_queue
= new LinkedBlockingQueue< function0 >();
public void run()
{
while(true)
try
{
function0 f = exec_queue.take();
f.Op();
} catch (Exception ex)
{ ex.printStackTrace(); }
}
*/
public Class getEntityClass()
{ return entity_class; }
public final IObjHome getHome() { return home; }
protected List>> wrappers = new LinkedList();
public ObjHandle getHandleByWrapper(JWrapper wrp)
{
ASSERT.debug(this, "");
lock.lock();
for(Pair> pair : wrappers)
{
/*
ASSERT.debug(this, "COMPARING to "+wrp.hashCode());//testing
ASSERT.debug(this, "Pair is ");//testing
ASSERT.debug(this, pair.get1st().hashCode());//testing
ASSERT.debug(this, "|");//testing
ASSERT.debug(this, pair.get2nd());//testing
*/
if(pair.get1st()!=null)
if(pair.get1st()==wrp)
{
lock.unlock();
ASSERT.debug(this, "++++++++getHandleByWrapper: returning !null");
return pair.get2nd();
}
}
lock.unlock();
ASSERT.debug(this, "++++++++getHandleByWrapper: returning null");
return null;
}
public JWrapper getWrapperByObj(T obj) throws Exception
{
ASSERT.debug(this, "");
return this.findByObjPK(obj.getPK());
}
public JWrapper getWrapperByHandle(ObjHandle handle) throws Exception
{
ASSERT.debug(this, "");
return this.findByObjPK(handle.getPK());
}
public void terminate()
{
ASSERT.debug(this, "");
try
{
//if(lock.isLocked())lock.u nlock();
HomeEventProxy hep = (ObjSession.getSession().getEventProxy(entity_class));
//hep.deleteObserver(this);
hep.getRecvSignal().disconnect(event_proxy_slot_id);
for(JWrapper wrp : getChildren())
{ wrp.finalize(); }
} catch(Exception ex)
{
System.out.println("JHomeDynamicCompositeWrapper: terminate: ");
System.out.println(ex);
}
}
public void finalize()
{ this.terminate(); }
public boolean edit(JWrapper wrp) throws Exception
{
ASSERT.debug(this, "");
return edit(wrp,true);
}
public boolean edit(JWrapper wrp, boolean reread) throws Exception
{
ASSERT.debug(this, "");
//System.out.println("");//testing
lock.lock();
boolean edit_result;
try
{
ObjHandle handle = this.getHandleByWrapper(wrp);
Obj obj = handle.getObj();//home.getObj(handle.getPK());//
//System.out.println("1: obj hashcode: "+obj.hashCode());//testing
//System.out.println("2: obj hashcode: "+obj.hashCode());//testing
if(handle.getObj().getIsInTrash())
{ throw new Exception("This object is in Trash !"); }
boolean retry = false;
do
{
JWrapper dwrp = this.getDetailedWrapper(wrp);
edit_result = sd_app.edit(dwrp);
wrp.getAdditionalInfo().put(SavedTag.class,dwrp.getAdditionalInfo().get(SavedTag.class));
if(edit_result)
{
ASSERT.setWaitingState(true);
try { obj.save(); handle.setObj(obj); retry=false; }
catch(Throwable th) { ASSERT.alert(th); retry=true; }
finally { ASSERT.setWaitingState(false);}
wrp.refresh();
//System.out.println("OID: "+this.getHandleByWrapper(wrp).getObj().hashCode());//testing
} else
{ if(reread) {this.reread(wrp);} retry=false; }
} while(retry);
//System.out.println("3: obj hashcode: "+obj.hashCode());//testing
//System.out.println("");//testing
}
finally { lock.unlock(); }
ASSERT.debug(this, "");
return edit_result;
}
public List createChildren() throws Exception
{
ASSERT.debug(this, "");
//System.out.println("<"+this.getClass()+"::createChild>");//testing
lock.lock();
List wrps;
try
{
wrps = super.createChildren();
System.out.println("createChildren: wrps="+wrps);//testing
JWrapper wrp = wrps.get(0);
if(!this.edit(wrp,false))
if((wrp.getAdditionalInfo().get(SavedTag.class))==null)
{ lock.unlock(); removeChild(wrp); lock.lock(); wrps.remove(wrp);/*return null;*/ }//~~~
//System.out.println(""+this.getClass()+"::createChild>");//testing
}
finally { lock.unlock(); }
ASSERT.debug(this, "");
return wrps;
}
protected List create_children() throws Exception
{
ASSERT.debug(this, "");
//System.out.println("<"+this.getClass()+"::create_child>");//testing
ASSERT.setWaitingState(true);
T obj = null;
try
{ obj = (T) home.createObj(); }
finally { ASSERT.setWaitingState(false); }
JWrapper wrp = this._create_child(obj);
wrappers.add(new Pair>(wrp,new ObjHandle(obj,true)));
//System.out.println("create_child: wrp="+wrp);//testing
//System.out.println(""+this.getClass()+"::create_child>");//testing
List wrps = new Vector();
wrps.add(wrp);
ASSERT.debug(this, "");
return wrps;
}
public org.JWrapper.JWrapper getChildPrototype() throws Exception
{
return this._create_child((T)(entity_class.newInstance()));
}
private boolean initialized = false;
protected void init() throws Exception
{
ASSERT.debug(this, "");
//System.out.println("<"+this.getClass()+"::init>");//testing
if(!initialized)
{
//System.out.println(this.getClass()+" not initialized");//testing
ASSERT.setWaitingState(true);
try
{
initialized=true;
wrappers.clear();
ObjHandle[] objs = obj_src.getObjHandles(home);
if(objs==null) { throw new BEXPException("ObjSrc returned null!"); }
for(ObjHandle OBJH : objs)
{
//JWrapper child_wrp = _create_child((T)OBJ);//??
wrappers.add(new Pair(null,OBJH));
//childAdded.Op(child_wrp);//there's no sence to put it here, cause' childAdded isn't queried by anybody yet
}
//initialized=true;
} finally { ASSERT.setWaitingState(false); }
//System.out.println(this.getClass()+" initialized");
}
//System.out.println(""+this.getClass()+"::init>");//testing
ASSERT.debug(this, "");
}
public final int size()
{
ASSERT.debug(this, "");
ASSERT.debug(this, "hold count: "+lock.getHoldCount());
ASSERT.debug(this, "queue length: "+lock.getQueueLength());
ASSERT.debug(this, "locked?: "+lock.isLocked());
ASSERT.debug(this, "reread: locked by current?:"+lock.isHeldByCurrentThread());
lock.lock();
try{
init();
lock.unlock();
ASSERT.debug(this, "");
return wrappers.size();
}
catch(Throwable ex)
{
//ex.printStackTrace();
ASSERT.alert(ex);
lock.unlock();
ASSERT.debug(this, "");
return 0;
}
}
@Deprecated
public JWrapper[] getChildren() throws Exception
{
ASSERT.debug(this, "");
lock.lock();
try{
init();
//return wrappers.keySet().toArray(new JWrapper[0]);
//if(true) throw new Exception("not supported");
JWrapper[] result = new JWrapper[wrappers.size()];
for(int i=0;i");
return null;
}
protected static int page_size = 60;//should be at least 1 !!!
public static int getPageSize()
{ return page_size; }
public static void setPageSize(int ps)
{ page_size=ps; }
public JWrapper getWrapper(int i) throws Exception
{
ASSERT.debug(this, "");
lock.lock();
JWrapper result = null;
try
{
int size=wrappers.size();
int max = (page_size*((i/page_size) + 1)) - 1,
min = (page_size*((i/page_size) - 1)) + 1;
min=(min>=0)?min:0;
max=(max> handles = new Vector>(page_size*2-1);
for(int j=min; j<=max; j++)
{
handles.add(wrappers.get(j).get2nd());
}
try
{
sd_app.setWaitingState(true);
Obj.initHandles(handles,home);
} finally { sd_app.setWaitingState(false); }
}
for(int j=min; j<=max; j++)
{
Pair> pair = wrappers.get(j);
JWrapper wrp = pair.get1st();
if(wrp==null)
{
wrp = _create_child(pair.get2nd().getObj());
//wrappers.remove(j);
//wrappers.add(j,new Pair>(wrp,pair.get2nd()));
/*
System.out.println("putting ");//testing
//((JCompositeWrapper)wrp).dump();//testing
System.out.println("pk: "+pair.get2nd().getPK());//testing
System.out.println("at index "+j);//testing
*/
pair.set1st(wrp);
}
if(j==i) {result=wrp;}
}
/*
ObjHandle h2 = wrappers.get(i).get2nd();//testing
if(!h2.getPK().equals(h1pk))
{
System.out.println("Handles not equal, pks:"+h1pk+"|"+h2.getPK());
System.out.println("references:"+h1.hashCode()+"|"+h2.hashCode());
System.out.println("i_s:"+i1+"|"+i);
}//testing
*/
} finally { lock.unlock(); }
ASSERT.debug(this, "");
return result;
}
protected void remove_child(JWrapper child) throws Exception
{
ASSERT.debug(this, "");
Object PK=(this.getHandleByWrapper(child)).getPK();
home.delete(PK);
wrappers.remove(this.findPairByObjPK(PK));
System.out.println("Child removed");
ASSERT.debug(this, "");
}
protected JWrapper findByObjPK(Object PK) throws Exception
{
ASSERT.debug(this, "");
JWrapper result = this.findPairByObjPK(PK).get1st();
/*
for(JWrapper w : ((JCompositeWrapper)result).getChildren())//testing
System.out.println("findByObjPK(): "+w);//testing
*/
ASSERT.debug(this, "");
return result;
}
protected Pair> findPairByObjPK(Object PK) throws Exception
{
ASSERT.debug(this, "");
lock.lock();
Pair> result = null;
try
{
init();
//System.out.println("<"+this.getClass()+"::findByObjPK>");//testing
//System.out.print("Arg PK: ");/*testing*/System.out.println(PK);
//System.out.print("PK type: ");/*testing*/System.out.println(PK.getClass().getName());
if(PK==null)
{
//System.out.println("Returning null!");//testing
lock.unlock();
return null;
}
//for(Pair> pair : wrappers)
int size = size();
for(int i=0; i> pair = wrappers.get(i);
Object pk = pair.get2nd().getPK();
//System.out.print("Comparing with PK: ");/*testing*/System.out.println(pk);
if(PK.equals(pk))
{
//System.out.print("Found PK: ");/*testing*/System.out.println(PK);
//System.out.print("equals pk: ");/*testing*/
JWrapper wrp = getWrapper(i);
System.out.println(pk);
//System.out.println("at index "+i);//testing
result=pair;
break;
}
}
//System.out.print("Returning: ");/*testing*/System.out.println(result);
//System.out.println(""+this.getClass()+"::findByObjPK>");//testing
} finally { lock.unlock(); }
ASSERT.debug(this, "");
return result;
}
static boolean createEventIgnored = true;
static public boolean getCreateEventIgnored()
{ return createEventIgnored; }
static public void setCreateEventIgnored(boolean cevent)
{ createEventIgnored = cevent; }
public void update(ObjHomeEvent evt)
{
ASSERT.debug(this,"");//testing
ASSERT.debug(this,"wake in wrapper fired");//testing
lock.lock();
try
{
init();
Object PK = evt.getObjectPK();
if(PK==null) {System.out.println("Ignoring: PK is null");lock.unlock(); return;}
//System.out.println("PK:"+PK.toString());//testing
Pair> pair = findPairByObjPK(PK);
JWrapper wrp = (pair!=null)?(pair.get1st()):null;
switch(evt.getEventID())
{
case ObjHomeEvent.Changed:
//System.out.println("JHomeDynamicCompositeWrapper: Wrapper changed");//testing
if( (obj_src.match(evt,home)) && (wrp!=null))
{
reread(wrp);
ASSERT.debug(this,"+_+_+_+ 1");
break;
} else
if( (! obj_src.match(evt,home)) && (wrp!=null))
{
wrappers.remove(pair);
lock.unlock();
childRemoved.Op(wrp);
//ASSERT.alert(this,"============ Wrapper moved");//debug
ASSERT.debug(this,"+_+_+_+ 2");
break;
}else
if( (! obj_src.match(evt,home)) && (wrp==null))
{ ASSERT.debug(this,"+_+_+_+ 3"); break; }
// break;
//the control will pass lower because there's no break statement.
case ObjHomeEvent.Added:
//System.out.println("JHomeDynamicCompositeWrapper: Wrapper addded");//testing
if((evt.getEventID()==ObjHomeEvent.Added)&&(createEventIgnored))
{ lock.unlock(); return; }
if(obj_src.match(evt,home))
if(wrp==null)
{
//T OBJ = (T) home.getObj(PK);
T OBJ = (T) entity_class.newInstance();
OBJ.setPK((Long)PK);
JWrapper child_wrp = _create_child(OBJ);
wrappers.add(new Pair>(child_wrp,new ObjHandle(OBJ,true)));
//.put(child_wrp,OBJ);
lock.unlock();
childAdded.Op(child_wrp);
if(evt.getEventID()!=ObjHomeEvent.Added)//i.e. Changed, but create event was ignored
{ reread(child_wrp); }
}
break;
case ObjHomeEvent.Deleted:
//System.out.println("JHomeDynamicCompositeWrapper: Wrapper deleted");//testing
if(wrp!=null)
{
wrappers.remove(pair);
lock.unlock();
childRemoved.Op(wrp);
}
break;
}
} catch(Exception ex)
{
System.out.println("JHomeDynamicCompositeWrapper:");
System.out.println(ex);
System.out.println(ex.getCause());
ex.printStackTrace();
}
finally
{
if((lock.isHeldByCurrentThread())&&(lock.isLocked())) { lock.unlock(); }
}
ASSERT.debug(this, "hold count: "+lock.getHoldCount());
ASSERT.debug(this,"");//testing
}
//!!!!!!! leaves lock in locked state !!!!!!!!!!!!
protected ObjHandle reread(JWrapper wrp) throws Exception
{
final JWrapper wrp_ = wrp;
ASSERT.debug(this," ");//testing
lock.lock();
ASSERT.debug(this,"reread#1");
ObjHandle handle = this.getHandleByWrapper(wrp);//wrappers.get(wrp);
ASSERT.debug(this,"reread#2");
Object pk = handle.getPK();//new Long(OBJ.getPK());
T obj = (T) home.getObj(pk);
if(obj==null) { lock.unlock(); return null; }
handle.setObj(obj);
JWrapper child_wrp = _create_child(obj);
final JWrapper child_wrp_ = child_wrp;
wrappers.remove(this.findPairByObjPK(pk));
/*
new Thread()
{
JWrapper wrp = wrp_;
public void run() {try{childRemoved.Op(wrp);}catch(Throwable th){sd_app.alert(th);}}
}
.start();*/
ASSERT.debug(this,"reread#3");
wrappers.add(new Pair>(child_wrp,handle));
//.put(child_wrp,OBJ);
lock.unlock();
ASSERT.debug(this, "hold count: "+lock.getHoldCount());
ASSERT.debug(this, "queue length: "+lock.getQueueLength());
ASSERT.debug(this, "reread: locked?: "+lock.isLocked());
ASSERT.debug(this, "reread: locked by current?:"+lock.isHeldByCurrentThread());
new Thread()
{
JWrapper wrp = wrp_;
JWrapper child_wrp = child_wrp_;
public void run() {try{childRemoved.Op(wrp); childAdded.Op(child_wrp);}catch(Throwable th){sd_app.alert(th);}}
}
.start();
ASSERT.debug(this,"reread#3.1");
/*
ObjHandle OBJ = this.getHandleByWrapper(wrp);//wrappers.get(wrp);
Object pk = OBJ.getPK();//new Long(OBJ.getPK());
//OBJ.copy((T) home.getObj(pk));
OBJ.setObj((T) home.getObj(pk));
*/
//System.out.println("OIDr: "+OBJ.getObj().hashCode());//testing
//System.out.println("Descr: "+OBJ.getObj().getDescription());//testing
//System.out.println(OBJ.getDescription());//testing
/*
Object PK = wrappers.get(wrp).getPK();
wrappers.remove(wrp);
T OBJ = (T) home.getObj(PK);
JWrapper child_wrp = _create_child(OBJ);
wrappers.put(child_wrp,OBJ);
childAdded.Op(child_wrp);
*/
/*
final JWrapper _wrp_ = wrp;
exec_queue.put(
new function0()
{ JWrapper _wrp = _wrp_; public Void Op()throws Exception{_wrp.refresh();return null;} }
);*/
wrp.refresh();
ASSERT.debug(this,"reread#3.2");
ASSERT.debug(this,"rerad: thread locked?: "+lock.isLocked());
ASSERT.debug(this,"reread#4");
ASSERT.debug(this," ");//testing
return handle;
}
private JWrapper _create_child(T _obj) throws Exception
{
JWrapper wrp = obj_info.wrap(_obj);
wrp.setParent(this);
return wrp;
}
public JWrapper getDetailedWrapper(JWrapper wrp) throws Exception
{
ObjHandle obj = this.getHandleByWrapper(wrp);//wrappers.get(wrp);
//System.out.println("Class of selected object: "+obj.getClass());
System.out.println("Package of selected object: "+obj.getObj().getClass().getPackage());
ObjInfo _obj_info = sd_app.getEntitiesMap().get(obj.getObj().getClass().getPackage()).getDefaultInfo();
//System.out.println("Info class: "+_obj_info.getClass());
return _obj_info.getDefaultDetailedWrapper(obj.getObj());
//return obj_info.getDefaultDetailedWrapper(obj);
}
public boolean isRegular()
{ return obj_info.isRegular(); }
public void refresh() throws Exception
{
initialized = false;
init();
obj_info.refresh();
//super.refresh();
allChanged.Op();
}
//--------------------------------------------------
}
|