Add in pre/post detach/attach hooks and ensure entity contains all data before attachin on first load from remote.

This commit is contained in:
Olof Larsson 2013-05-31 11:34:23 +02:00
parent cc75cb8e15
commit 28f96fcab0
3 changed files with 136 additions and 22 deletions

View File

@ -273,6 +273,9 @@ public class Coll<E> implements CollInterface<E>
if (this.id2entity.containsKey(id)) return null; if (this.id2entity.containsKey(id)) return null;
} }
// PRE
this.preAttach(entity, id);
// Add entity reference info // Add entity reference info
if (entity instanceof Entity) if (entity instanceof Entity)
{ {
@ -291,29 +294,81 @@ public class Coll<E> implements CollInterface<E>
this.changedIds.add(id); this.changedIds.add(id);
} }
// POST
this.postAttach(entity, id);
return id; return id;
} }
@SuppressWarnings("unchecked")
@Override @Override
public E detachEntity(Object entity) public E detachEntity(Object entity)
{ {
return this.detachId(this.getId(entity)); E e = (E)entity;
String id = this.getId(e);
this.detach(e, id);
return e;
} }
@Override @Override
public E detachId(Object oid) public E detachId(Object oid)
{ {
String id = this.fixId(oid); String id = this.fixId(oid);
if (id == null) return null; E e = this.get(id);
this.detach(e, id);
return e;
}
private void detach(E entity, String id)
{
// PRE
this.preDetach(entity, id);
// Remove @ local // Remove @ local
E ret = this.removeAtLocal(id); this.removeAtLocal(id);
// Identify the change // Identify the change
this.localDetachIds.add(id); this.localDetachIds.add(id);
this.changedIds.add(id); this.changedIds.add(id);
return ret; // POST
this.postDetach(entity, id);
}
@Override
public void preAttach(E entity, String id)
{
if (entity instanceof Entity)
{
((Entity<?>)entity).preAttach(id);
}
}
@Override
public void postAttach(E entity, String id)
{
if (entity instanceof Entity)
{
((Entity<?>)entity).postAttach(id);
}
}
@Override
public void preDetach(E entity, String id)
{
if (entity instanceof Entity)
{
((Entity<?>)entity).preDetach(id);
}
}
@Override
public void postDetach(E entity, String id)
{
if (entity instanceof Entity)
{
((Entity<?>)entity).postDetach(id);
}
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -452,16 +507,25 @@ public class Coll<E> implements CollInterface<E>
Long mtime = entry.getValue(); Long mtime = entry.getValue();
if (mtime == null) return; if (mtime == null) return;
E entity = this.get(id, true, false); E entity = this.get(id, false);
if (entity != null)
{
// It did already exist
this.copy(this.getGson().fromJson(raw, this.getEntityClass()), entity);
}
else
{
// Create first
entity = this.createNewInstance();
// Copy over data first
this.copy(this.getGson().fromJson(raw, this.getEntityClass()), entity); this.copy(this.getGson().fromJson(raw, this.getEntityClass()), entity);
// this.lastRaw.put(id, this.getStoreAdapter().read(this, entity)); // Then attach!
// Store adapter again since result of a database read may be "different" from entity read. this.attach(entity, oid, false);
// WARN: This was causing many issues with config files not updating etc. }
// The approach below may not work with MongoDB at all since that is not tested.
this.lastRaw.put(id, raw);
this.lastRaw.put(id, raw);
this.lastMtime.put(id, mtime); this.lastMtime.put(id, mtime);
this.lastDefault.remove(id); this.lastDefault.remove(id);
} }

View File

@ -80,11 +80,19 @@ public interface CollInterface<E>
// -------------------------------------------- // // -------------------------------------------- //
// ATTACH AND DETACH // ATTACH AND DETACH
// -------------------------------------------- // // -------------------------------------------- //
public String attach(E entity); public String attach(E entity);
public String attach(E entity, Object oid); public String attach(E entity, Object oid);
public E detachEntity(Object entity); public E detachEntity(Object entity);
public E detachId(Object oid); public E detachId(Object oid);
public void preAttach(E entity, String id);
public void postAttach(E entity, String id);
public void preDetach(E entity, String id);
public void postDetach(E entity, String id);
// -------------------------------------------- // // -------------------------------------------- //
// IDENTIFIED CHANGES // IDENTIFIED CHANGES
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -15,6 +15,10 @@ import com.massivecraft.mcore.xlib.gson.Gson;
// http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206 // http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206
public abstract class Entity<E extends Entity<E>> implements Comparable<E> public abstract class Entity<E extends Entity<E>> implements Comparable<E>
{ {
// -------------------------------------------- //
// COLL & ID
// -------------------------------------------- //
protected transient Coll<E> coll; protected transient Coll<E> coll;
protected void setColl(Coll<E> val) { this.coll = val; } protected void setColl(Coll<E> val) { this.coll = val; }
public Coll<E> getColl() { return this.coll; } public Coll<E> getColl() { return this.coll; }
@ -31,6 +35,10 @@ public abstract class Entity<E extends Entity<E>> implements Comparable<E>
return coll.getUniverse(); return coll.getUniverse();
} }
// -------------------------------------------- //
// ATTACH AND DETACH
// -------------------------------------------- //
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public String attach(Coll<E> coll) public String attach(Coll<E> coll)
{ {
@ -55,6 +63,31 @@ public abstract class Entity<E extends Entity<E>> implements Comparable<E>
return ! this.attached(); return ! this.attached();
} }
public void preAttach(String id)
{
}
public void postAttach(String id)
{
}
public void preDetach(String id)
{
}
public void postDetach(String id)
{
}
// -------------------------------------------- //
// SYNC AND IO ACTIONS
// -------------------------------------------- //
public void changed() public void changed()
{ {
String id = this.getId(); String id = this.getId();
@ -91,15 +124,9 @@ public abstract class Entity<E extends Entity<E>> implements Comparable<E>
this.getColl().loadFromRemote(id); this.getColl().loadFromRemote(id);
} }
@Override // -------------------------------------------- //
public String toString() // DERPINGTON
{ // -------------------------------------------- //
Gson gson = MCore.gson;
Coll<E> coll = this.getColl();
if (coll != null) gson = coll.getGson();
return this.getClass().getSimpleName()+gson.toJson(this, this.getClass());
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public E load(E that) public E load(E that)
@ -113,6 +140,10 @@ public abstract class Entity<E extends Entity<E>> implements Comparable<E>
return false; return false;
} }
// -------------------------------------------- //
// STANDARDS
// -------------------------------------------- //
@Override @Override
public int compareTo(E that) public int compareTo(E that)
{ {
@ -138,4 +169,15 @@ public abstract class Entity<E extends Entity<E>> implements Comparable<E>
return ret; return ret;
} }
@Override
public String toString()
{
Gson gson = MCore.gson;
Coll<E> coll = this.getColl();
if (coll != null) gson = coll.getGson();
return this.getClass().getSimpleName()+gson.toJson(this, this.getClass());
}
} }