diff --git a/src/com/massivecraft/mcore/store/Coll.java b/src/com/massivecraft/mcore/store/Coll.java index b479f8d0..decb1f00 100644 --- a/src/com/massivecraft/mcore/store/Coll.java +++ b/src/com/massivecraft/mcore/store/Coll.java @@ -273,6 +273,9 @@ public class Coll implements CollInterface if (this.id2entity.containsKey(id)) return null; } + // PRE + this.preAttach(entity, id); + // Add entity reference info if (entity instanceof Entity) { @@ -291,29 +294,81 @@ public class Coll implements CollInterface this.changedIds.add(id); } + // POST + this.postAttach(entity, id); + return id; } + @SuppressWarnings("unchecked") @Override 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 public E detachId(Object 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 - E ret = this.removeAtLocal(id); + this.removeAtLocal(id); // Identify the change this.localDetachIds.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 implements CollInterface Long mtime = entry.getValue(); if (mtime == null) return; - E entity = this.get(id, true, false); - - this.copy(this.getGson().fromJson(raw, this.getEntityClass()), entity); - - // this.lastRaw.put(id, this.getStoreAdapter().read(this, entity)); - // Store adapter again since result of a database read may be "different" from entity read. - // 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); + 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); + + // Then attach! + this.attach(entity, oid, false); + } + this.lastRaw.put(id, raw); this.lastMtime.put(id, mtime); this.lastDefault.remove(id); } diff --git a/src/com/massivecraft/mcore/store/CollInterface.java b/src/com/massivecraft/mcore/store/CollInterface.java index d6ae72a1..c38f02df 100644 --- a/src/com/massivecraft/mcore/store/CollInterface.java +++ b/src/com/massivecraft/mcore/store/CollInterface.java @@ -80,11 +80,19 @@ public interface CollInterface // -------------------------------------------- // // ATTACH AND DETACH // -------------------------------------------- // + public String attach(E entity); public String attach(E entity, Object oid); + public E detachEntity(Object entity); 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 // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore/store/Entity.java b/src/com/massivecraft/mcore/store/Entity.java index 9c6db347..f805b358 100644 --- a/src/com/massivecraft/mcore/store/Entity.java +++ b/src/com/massivecraft/mcore/store/Entity.java @@ -15,6 +15,10 @@ import com.massivecraft.mcore.xlib.gson.Gson; // http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206 public abstract class Entity> implements Comparable { + // -------------------------------------------- // + // COLL & ID + // -------------------------------------------- // + protected transient Coll coll; protected void setColl(Coll val) { this.coll = val; } public Coll getColl() { return this.coll; } @@ -31,6 +35,10 @@ public abstract class Entity> implements Comparable return coll.getUniverse(); } + // -------------------------------------------- // + // ATTACH AND DETACH + // -------------------------------------------- // + @SuppressWarnings("unchecked") public String attach(Coll coll) { @@ -55,6 +63,31 @@ public abstract class Entity> implements Comparable 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() { String id = this.getId(); @@ -91,15 +124,9 @@ public abstract class Entity> implements Comparable this.getColl().loadFromRemote(id); } - @Override - public String toString() - { - Gson gson = MCore.gson; - Coll coll = this.getColl(); - if (coll != null) gson = coll.getGson(); - - return this.getClass().getSimpleName()+gson.toJson(this, this.getClass()); - } + // -------------------------------------------- // + // DERPINGTON + // -------------------------------------------- // @SuppressWarnings("unchecked") public E load(E that) @@ -113,6 +140,10 @@ public abstract class Entity> implements Comparable return false; } + // -------------------------------------------- // + // STANDARDS + // -------------------------------------------- // + @Override public int compareTo(E that) { @@ -138,4 +169,15 @@ public abstract class Entity> implements Comparable return ret; } + + @Override + public String toString() + { + Gson gson = MCore.gson; + Coll coll = this.getColl(); + if (coll != null) gson = coll.getGson(); + + return this.getClass().getSimpleName()+gson.toJson(this, this.getClass()); + } + }