Skip to content

Commit

Permalink
second
Browse files Browse the repository at this point in the history
  • Loading branch information
Bael committed Nov 27, 2011
1 parent 4a5d879 commit 93bd413
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.settings/
.project
.classpath
Binary file added bin/core/ConnectionManager.class
Binary file not shown.
Binary file added bin/core/DataWorker.class
Binary file not shown.
Binary file added bin/core/Persistent.class
Binary file not shown.
6 changes: 2 additions & 4 deletions src/Core/ConnectionManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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())
Expand Down
36 changes: 32 additions & 4 deletions src/Core/Persistent.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
245 changes: 245 additions & 0 deletions src/core/DataWorker.java
Original file line number Diff line number Diff line change
@@ -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<String, Field> 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<String, Field>();
}
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]);
}
}





}

0 comments on commit 93bd413

Please sign in to comment.