diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..08c1b6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.settings/ +.project +.classpath diff --git a/bin/core/ConnectionManager.class b/bin/core/ConnectionManager.class new file mode 100644 index 0000000..d418f31 Binary files /dev/null and b/bin/core/ConnectionManager.class differ diff --git a/bin/core/DataWorker.class b/bin/core/DataWorker.class new file mode 100644 index 0000000..614f507 Binary files /dev/null and b/bin/core/DataWorker.class differ diff --git a/bin/core/Persistent.class b/bin/core/Persistent.class new file mode 100644 index 0000000..1d0e5b1 Binary files /dev/null and b/bin/core/Persistent.class differ diff --git a/src/Core/ConnectionManager.java b/src/Core/ConnectionManager.java index fe1d6f1..3d9fd00 100644 --- a/src/Core/ConnectionManager.java +++ b/src/Core/ConnectionManager.java @@ -1,9 +1,7 @@ -package Core; +package core; import java.io.*; import com.intersys.globals.*; import java.io.PrintWriter; - import java.util.logging.Level; - import java.util.logging.Logger; public class ConnectionManager { private static ConnectionManager manager; @@ -46,7 +44,7 @@ public Connection getConnection() try { System.out.println("Step1"); - _connection = ConnectionContext.getConnection(); // ConnectionContext.getConnection(); + _connection = ConnectionContext.getConnection(); System.out.println("Step2"); if (!_connection.isConnected()) diff --git a/src/Core/Persistent.java b/src/Core/Persistent.java index daab3f3..467901b 100644 --- a/src/Core/Persistent.java +++ b/src/Core/Persistent.java @@ -1,5 +1,33 @@ -package Core; - -public abstract class Persistent { - +package core; +import java.util.Date; +public class Persistent { + + public String GetStorageGlobalName() + { + String name = this.getClass().toString(); + name = name.concat("D"); + return name; + } + + public String GetIndexGlobalName() + { + return this.getClass().toString().concat("I"); + } + + public static long GetNextId() + { + return 1; + } + + public long Id; + + public String Name ; + + public Date CreatedOn; + + public String toString() + { + return "Name:"+this.Name+ ", Id:"+this.Id+", CreatedOn:"+CreatedOn; + } + } diff --git a/src/core/DataWorker.java b/src/core/DataWorker.java new file mode 100644 index 0000000..aded6b2 --- /dev/null +++ b/src/core/DataWorker.java @@ -0,0 +1,245 @@ +package core; +import core.ConnectionManager; +import core.Persistent; +import com.intersys.globals.Connection; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import com.intersys.globals.NodeReference; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +public class DataWorker { + /* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + private HashMap fieldsMap; + private String SchemaClassName; + + private static DataWorker dataWorker; + public static DataWorker Instance() + { + if (dataWorker == null) + { + dataWorker = new DataWorker(); + } + return dataWorker; + } + + public void UpdateIndicesOnCreate(Persistent obj) + { + + } + + public void UpdateIndicesOnUpdate(Persistent obj) + { + + } + + public void UpdateIndicesOnDelete(Persistent obj) + { + + } + + public ArrayList GetAllByInstance(Persistent _obj) + { + ArrayList list = new ArrayList(); + NodeReference node = ConnectionManager.Instance().getConnection().createNodeReference(_obj.GetStorageGlobalName()); + node.appendSubscript(""); + String subscr = ""; + subscr = node.nextSubscript(); + while (!subscr.equals("")) + { + long id = new Long(subscr); + + Persistent obj = new Persistent(); + try { + obj = _obj.getClass().newInstance(); + } catch (InstantiationException ex) { + Logger.getLogger(DataWorker.class.getName()).log(Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + Logger.getLogger(DataWorker.class.getName()).log(Level.SEVERE, null, ex); + } + try { + System.out.println(id+ " . " + _obj.GetStorageGlobalName()); + + obj = LoadObjectById(id, obj); + } catch (IllegalAccessException ex) { + Logger.getLogger(DataWorker.class.getName()).log(Level.SEVERE, null, ex); + } + list.add(obj); + //System.out.print("\"" + subscr + "\"=" + /*(node.getLong()) + */ " "); + subscr = node.nextSubscript(); + node.setSubscript(node.getSubscriptCount(), subscr); + + } + + + return list; + + } + + public void Iterate(Persistent obj) + { + String globalName = obj.GetStorageGlobalName(); //obj.STORAGEGLOBALNAME; + NodeReference node = ConnectionManager.Instance().getConnection().createNodeReference(globalName); + node.appendSubscript(""); + String subscr = ""; + subscr = node.nextSubscript(); + while (!subscr.equals("")) + { + System.out.print("\"" + subscr + "\"=" + /*(node.getLong()) + */ " "); + subscr = node.nextSubscript(); + node.setSubscript(node.getSubscriptCount(), subscr); + + }; + + + } + + public Persistent SaveObject(Persistent obj) throws IllegalAccessException + { + + String globalName = obj.GetStorageGlobalName(); //obj.STORAGEGLOBALNAME; + NodeReference node = null; + try + { + //ConnectionManager.writeToFile("", "step0"); + Connection conn = ConnectionManager.Instance().getConnection(); + //ConnectionManager.writeToFile("", "step2"); + node = conn.createNodeReference(globalName); + } + catch (Exception ex) + { + ConnectionManager.writeToFile("", ex.toString()); + } + + node.increment(1); + + obj.Id = node.getLong(); + Class info = obj.getClass(); + Field[] fields = info.getFields(); + Field field; + + for (int i = 0; i < fields.length; i++) + { + field = fields[i]; + + Object value = field.get(obj); + if (value instanceof java.lang.String) + { + node.set(value.toString(), obj.Id, fields[i].getName()); + } + else + { + if (value instanceof java.lang.Long) + { + long longValue = field.getLong(obj); + + node.set(longValue, obj.Id, fields[i].getName()); + } + else + { + if (value instanceof java.util.Date) + { + Date dateValue = (Date) value; + //() new Date(value.toString()); + String strValue = dateValue.toGMTString(); + node.set(strValue, obj.Id, fields[i].getName()); + + } + + + } + + } + + + + } + + return obj; + + + } + + public void DeleteObjectById(Long id, String globalName) + { + NodeReference node = ConnectionManager.Instance().getConnection().createNodeReference(globalName); + node.kill(id); + } + + public Persistent LoadObjectById(Long id, Persistent obj) throws IllegalAccessException + { + + + NodeReference node = ConnectionManager.Instance().getConnection().createNodeReference(obj.GetStorageGlobalName()); + + InitSchema(obj); + + node.setSubscriptCount(0); + node.appendSubscript(id); + Field field; + String subscript = ""; + Object nodeValue = null; + do { + subscript = node.nextSubscript(subscript); + if (subscript.length() > 0) + { + field = fieldsMap.get(subscript); + nodeValue = node.getObject(subscript); + if (nodeValue instanceof java.lang.Long) + { + Long nodeLongValue = node.getLong(subscript); + field.setLong(obj, nodeLongValue); + } + else + { + if(field.getType() == java.util.Date.class) + { + Date dateValue = new Date(nodeValue.toString()); + + field.set(obj, dateValue); + } + else + { + + field.set(obj, nodeValue); + } + } + } + } while (subscript.length() > 0); + + + return obj; + + + } + private void InitSchema(Object obj) + { + Class classInfo = obj.getClass(); + + if (fieldsMap == null || SchemaClassName != classInfo.getName()) + { + fieldsMap = new HashMap(); + } + SchemaClassName = classInfo.getName(); + Field[] fields = classInfo.getFields(); + + + ///System.out.println("Step7"); + ///System.out.println("Access all the fields"); + for (int i = 0; i < fields.length; i++) + { + fieldsMap.put(fields[i].getName(), fields[i]); + } + } + + + + + +}