Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More about logging, Session.save basic test now pass !!!!

  • Loading branch information...
commit 1bcbbe09d4947a6f3c49829a47ad23e490e039c0 1 parent 43c0127
@nka11 nka11 authored
View
6 conf/log4js.json
@@ -1,6 +1,10 @@
{ "appenders": [
{
- "type": "console"
+ "type": "logLevelFilter",
+ "level": "DEBUG",
+ "appender": {
+ "type": "console"
+ }
}
],
"levels": {
2  node_modules/NoCR
@@ -1 +1 @@
-Subproject commit df315d8e167d84ac82f47cd40ff2ef3bbbe24f52
+Subproject commit bf297b81fc5b21bda40bb97a046c6583abc99fcc
View
26 src/Node.js
@@ -23,25 +23,26 @@ nodeproto = {
getProperty: function(name, callback) {
var self = this, errmsg;
if (name in self['properties']) {
- _.debug('[getProperty]: found in instances'); // memory cache L1
+ self.logger.debug('[getProperty]: found in instances'); // memory cache L1
if (typeof callback !== "undefined") {
- //_.debug(_.inspect(self['properties'][name]));
+ self.logger.trace(_.inspect(self['properties'][name]));
callback(null, self['properties'][name]);
} else {
return self['properties'][name];
}
} else if (name in self['node:properties']) {
- _.debug('[getProperty]: found in data');
+ self.logger.debug('[getProperty]: found in data');
self.session.getRepository().getDataById(self['node:properties'][name],function(err, propData) {
self['properties'][name] = new Property(propData, self.session);
if (typeof callback !== "undefined") {
+ self.logger.trace(_.inspect(self['properties'][name]));
callback(null, self['properties'][name]);
} else {
return self['properties'][name];
}
});
} else {
- _.debug('[getProperty]: not found');
+ self.logger.debug('[getProperty]: not found');
errmsg = "Property not found in node";
if (typeof callback !== "undefined") {
callback(errmsg);
@@ -54,8 +55,8 @@ nodeproto = {
* Can accept callback func or not, as operations are performed in-memory, there is no async
*/
setProperty: function(name, value, type, callback) {
- //_.debug("[setProperty]: " + _.inspect(arguments));
var self = this, type,error, prop;
+ self.logger.trace("[setProperty]: " + _.inspect(arguments));
if (arguments.length === 0) {
throw new Error("at least name should be specified");
}
@@ -84,7 +85,7 @@ nodeproto = {
}
if (self.type.canSetProperty(name, value)) {
self.ismodified = true;
- //_.debug(_.inspect(self));
+ self.logger.trace(_.inspect(self));
if (name in self['node:properties'] || name in self['properties']) { //such property already exist ethier persisted or in session scope
self.getProperty(name, function(err, prop) {//gets the property instance and sets the value
if (err === null) {
@@ -114,16 +115,16 @@ nodeproto = {
}
}
} else { //deleting the property if null or no value specified
- _.debug("value is not specified or null, removing " + name + " property from registries");
+ self.logger.info("value is not specified or null, removing " + name + " property from registries");
//_.debug(_.inspect(self['node:properties']));
if (name in self['node:properties']) {
- _.debug("Property found in data, deleting");
+ self.logger.debug("Property found in data, deleting");
self['node:properties'][name] = undefined;
delete self['node:properties'][name];
self.ismodified = true;
}
if (name in self['properties']) {
- _.debug("Property found in instances, deleting");
+ self.logger.debug("Property found in instances, deleting");
self['properties'][name] = undefined;
delete self['properties'][name];
self.ismodified = true;
@@ -176,7 +177,7 @@ nodeproto = {
'path': parent.getPath() + childName + '/', // Implementation data, Index
'node:type': 'nt:unstructured' // implementation reference
}, self.session);
- //_.debug(_.inspect(node));
+ self.logger.trace(_.inspect(node));
// parent node by name index
parent['childrens'][childName] = node;
@@ -188,7 +189,7 @@ nodeproto = {
'property:type': 'NAME'
})
);
- //_.debug(_.inspect(node));
+ self.logger.trace(_.inspect(node));
} else {
throw new Error("Integrity problem, Operation addNode can't be performed");
}
@@ -240,7 +241,6 @@ nodeproto = {
function Node(data, session) {
var self = this,
workspace = session.getWorkspace();
- //_.debug("Initializing Node :" + _.inspect(data));
self['properties'] = {}; // data structure for properties instance (lazy load)
self['childrens'] = {}; //data structure for nodes instances (lazy load)
if (!'node:properties' in data || data['node:properties'] === undefined) {
@@ -252,6 +252,8 @@ function Node(data, session) {
self['node:properties'] = data['node:properties'];
self['node:childrens'] = data['node:childrens'];
Item.call(self, data, session);
+ self.logger.info("Initializing Node");
+ self.logger.trace(_.inspect(data));
if (data['node:properties:' + workspace.getName()] !== undefined) {
for (k in data['node:properties:' + workspace.getName()]) {
self['node:properties'][k] = data['node:properties:' + workspace.getName()][k];
View
1  src/Property.js
@@ -1,5 +1,6 @@
var nocr = require("NoCR"),
_ = require('util'),
+ log4js = require('log4js')(),
wrapper = require('./wrapper.js'),
Item = require('./Item.js'),
Value = require('./Value.js'),
View
2  src/Repository.js
@@ -51,7 +51,7 @@ function Repository(config, callback) {
});
}
}
-
+ this.getItemsCollection = getItemsCollection;
this.login = function(credentials, workspaceName, callback) {
var
session = new Session(self,credentials, function(err,sess) {
View
9 src/Session.js
@@ -61,7 +61,8 @@ Session = function(repository, credentials, callback) {
if(err) {
callback(err, null);
} else if(user) {
- self.logger.debug("user found :" + _.inspect(users));
+ self.logger.debug("user found");
+ self.logger.trace(_.inspect(users));
if (user.password === credentials.password) {
self.user = {username:user.username, id: user.userid, workspace:user.workspace};
setUserContext();
@@ -86,9 +87,11 @@ Session = function(repository, credentials, callback) {
setAnonymous();
callback(null, self);
} else {
- self.logger.debug("credentials != null :" + _.inspect(credentials));
+ self.logger.debug("credentials != null :");
+ self.logger.trace(_.inspect(credentials));
if (credentials.username !== undefined || credentials.username !== null) {
- self.logger.debug("credentials provided :" + _.inspect(credentials));
+ self.logger.debug("credentials provided :");
+ self.logger.trace(_.inspect(credentials));
usersCollection.find({username: credentials.username}).limit(1).
toArray(checkUserAuth);
} else {
View
4 src/Value.js
@@ -1,5 +1,7 @@
var nocr = require("NoCR"),
_ = require('util'),
+ log4js = require('log4js')(),
+ logger = log4js.getLogger("nocr-mongo.Value"),
wrapper = require('./wrapper.js'),
Value, valueproto;
@@ -54,7 +56,7 @@ function Value(data) {
function checkIntegrity(data) {
var guess = guessType(data['property:value']),
compatibility = require('./utils/itemslookup.js').valueCompatibility;
- _.debug("Checking types integrity : " + data['property:type'] + " vs " + guess);
+ logger.debug("Checking types integrity : " + data['property:type'] + " vs " + guess);
if (compatibility[guess].indexOf(data['property:type']) === -1 ) {
throw new Error("Incompatible type/value found");
}
View
268 src/Workspace.js
@@ -29,7 +29,8 @@ function Workspace(session, data, callback) {
data = {name: 'default'};
}
self.name = data.name;
- self.logger = log4js.getLogger("nocr-mongo.Workspace." + self.name);
+ self.id = data['_id'];
+ self.logger = log4js.getLogger("nocr-mongo.Workspace");
self.session = session;
callback(null, self);
for (k in wsproto) {
@@ -41,7 +42,7 @@ function Workspace(session, data, callback) {
function instanciateNode(err, nodeData) {
var node;
if (err !== null) {
- console.log(err);
+ self.logger.error(err);
callback(err);
}
nodeData.path = abspath;
@@ -57,7 +58,7 @@ function Workspace(session, data, callback) {
function instanciateProperty(err, propertyData) {
var property;
if (err !== null) {
- console.log(err);
+ self.logger.error(err);
callback(err);
}
propertyData.path = abspath;
@@ -73,7 +74,7 @@ function Workspace(session, data, callback) {
if (self.itemsIndex !== undefined) {
callback(null, self.itemsIndex);
} else {
- client.collection('workspace.' + self.name + '.itemsIndex', function(err, itemsIndex) {
+ client.collection('workspace.' + self.id + '.itemsIndex', function(err, itemsIndex) {
self.itemsIndex = itemsIndex;
self.itemsIndex.count(function(err, count) {
if (count === 0 ) {
@@ -90,13 +91,13 @@ function Workspace(session, data, callback) {
function indexRootNode(err, rootNodeData) {
var rootNode = new Node(rootNodeData, session);
self.logger.debug("Indexing rootNode");
- //_.log(_.inspect(rootNode));
+ self.logger.trace(_.inspect(rootNode));
itemsIndex.insert({
'item:id': rootNode.data._id.toString(),
'item:type': 'Node',
'item:path': "/"
}, {safe: true}, function(err, result) {
- console.log(result);
+ self.logger.trace(result);
if (err !== null) {
callback(err);
} else {
@@ -124,7 +125,7 @@ function Workspace(session, data, callback) {
},
getItem: function(abspath,callback) {
self.logger.debug("getting " + abspath);
- //self.logger.trace(_.inspect(this.data));
+ self.logger.trace(_.inspect(this.data));
if (abspath in this.data && this.data[abspath] !== undefined) {
callback(null, this.data[abspath]);
} else {
@@ -243,10 +244,15 @@ function Workspace(session, data, callback) {
});
});
};
+
+ /**
+ * API method
+ */
session.getNodeByIdentifier = function(nodeId, callback) {
+ // TODO: use cache object
getItemsIndex(function(err, itemsIndex){
itemsIndex.find({'item:id': nodeId, 'item:type': 'Node'}).toArray(function(err, items){
- self.logger.debug("found item :" + _.inspect(items));
+ self.logger.trace("found item :" + _.inspect(items));
if (items.length === 1) {
repository.getDataById(nodeId, getInstanciateNode(items[0]['path'], callback));
} else if (items.length === 0) {
@@ -257,6 +263,252 @@ function Workspace(session, data, callback) {
});
});
};
+
+ /**
+ * api method for saving content which remains to the session
+ */
+ session.save = function(callback) {
+ var
+ modifiedNodes = [],
+ newNodes = [];
+ if (callback === undefined) { // Default callback throws an exception
+ callback = function(err,res) {
+ if (err !== null) {
+ throw new Error(err);
+ }
+ };
+ }
+ for (path in session.items.data) {
+ if (session.items.data[path] instanceof nocr.Node && session.items.data[path].isnew) {
+ newNodes.push(session.items.data[path]);
+ } else if (session.items.data[path] instanceof nocr.Node && session.items.data[path].ismodified) {
+ modifiedNodes.push(session.items.data[path]);
+ }
+ }
+
+
+ function updateIndex(callback) {
+
+ }
+ repository.getItemsCollection(function(err, itemsCollection) {
+ /**
+ * Insert in database new properties of node
+ */
+ function insertNodeProperties(node, callback) {
+ var propscount,
+ propsmap = {},
+ insertedprops = 0,
+ propdata = {}, k,
+ propkey;
+ self.logger.debug("start inserting node properties");
+ self.logger.trace(_.inspect(node));
+ Object.keys(node['properties']).forEach(function(index) { //iterate over each node property
+ var property = node['properties'][index];
+ if (property.isnew) {
+ propsmap[index] = node['properties'][index];
+ } //build a map of properties to insert
+ });
+ propscount = Object.keys(propsmap).length;
+ self.logger.debug(propscount + " Properties to insert");
+ if (propscount === 0) {
+ callback(null, node);
+ } else {
+ self.logger.trace(_.inspect(propsmap));
+ for (propkey in propsmap) { // iterate over each property
+ propdata = {};
+ for (k in propsmap[propkey].data) {
+ if (k !== 'path') {
+ propdata[k] = propsmap[propkey].data[k];
+ }
+ }
+ propdata['item:type'] = 'Property';
+ itemsCollection.insert(propdata,{safe:true},function(err, pdata){
+ if (err === null) {
+ callback(err);
+ } else {
+ propsmap[propkey].data['_id'] = pdata._id;
+ propsmap[propkey].isnew = false;
+ propsmap[propkey].isModified = false;
+ node['node:properties'][propkey] = pdata._id;
+ self.logger.debug('Inserting property - ' + (insertedprops++) + "/" + propscount);
+ if (insertedprops === propscount) {
+ callback(err, node);
+ }
+ }
+ });
+ }
+ }
+ }
+ function updateNodeProperties(node, callback) {
+ var propscount,
+ propsmap = {},
+ insertedprops = 0,
+ propdata = {}, k, propkey;
+ self.logger.debug("start updating node properties");
+ Object.keys(node['properties']).forEach(function(index) {
+ var property = node['properties'][index];
+ if (property.ismodified && !property.isnew) {
+ propsmap[index] = node['properties'][index];
+ }
+ });
+ propscount = Object.keys(propsmap).length;
+ self.logger.debug(propscount + " Properties to update");
+ if (propscount === 0) {
+ callback(null, node);
+ } else {
+ for (propkey in propsmap) {
+ for (k in propsmap[propkey].data) {
+ if (k !== 'path') {
+ propdata[k] = propsmap[propkey].data[k];
+ }
+ }
+ propdata['item:type'] = 'Property';
+ itemsCollection.update({"_id":propdata['_id']}, propdata,{safe:true},function(err, pdata){
+ propsmap[propkey].data['_id'] = pdata._id;
+ propsmap[propkey].isnew = false;
+ propsmap[propkey].isModified = false;
+ node['node:properties'][propkey] = pdata._id;
+ if (++insertedprops === propscount) {
+ callback(err, node);
+ }
+ });
+ }
+ }
+ }
+ /**
+ *
+ */
+ function insertNodes(callback) {
+ var insertedNodesCount = 0, node, propertyName, properties = [];
+ function insertNode(node, callback) { // insert a Node and its properties
+ insertNodeProperties(node, function(err, node) {
+ if (err === null) {
+ callback(err);
+ } else {
+ var nodeData = {}, k;
+ for (k in node.data) {
+ if (k !== 'path') {
+ nodeData[k] = node.data[k];
+ }
+ }
+ nodeData['node:properties:' + self.getName()] = node['node:properties'];
+ nodeData['item:type'] = 'Node';
+ itemsCollection.insert(nodeData,{safe:true},function(err, ndata){
+ self.logger.debug("Inserting node : " + node.path);
+ node['data']['_id'] = ndata._id;
+ node.isnew = false;
+ node.isModified = false;
+ callback(err,node);
+ });
+ }
+ });
+ }
+ if (newNodes.length === 0) {
+ self.logger.debug("No nodes to insert");
+ callback(null, {'message': "No nodes to insert",
+ "createdNodes": 0});
+ } else {
+ newNodes.forEach(function(node, nk) {
+ self.logger.debug("start inserting node " + nk);
+ insertNode(node, function(err, node) {
+ self.logger.trace(_.inspect(err));
+ self.logger.debug("insertedNodesCount " + (++insertedNodesCount) + "/" + newNodes.length);
+ if (insertedNodesCount === newNodes.length) {
+ callback(err, {'message': "Nodes inserted",
+ "createdNodes": newNodes.length});
+ }
+ });
+ });
+ }
+
+ } // ------------------------------ /function InsertNodes
+ function updateNodes(callback) {
+ var updatedNodescount = 0, node, propertyName, properties = [];
+ function updateNode(node, callback) { // insert a Node and its properties
+ insertNodeProperties(node, function(err, node) {
+ if (err !== null) {
+ self.logger.error("Error inserting properties");
+ self.logger.error(_.inspect(err));
+ callback(err);
+ } else {
+ updateNodeProperties(node, function(err, node) {
+ if (err !== null) {
+ callback(err);
+ } else {
+ var nodeData = {}, k;
+ for (k in node.data) {
+ if (k !== 'path') {
+ nodeData[k] = node.data[k];
+ }
+ }
+ nodeData['node:properties:' + self.getName()] = node['node:properties'];
+ nodeData['item:type'] = 'Node';
+ self.logger.debug("Data prepared for Node update");
+ self.logger.trace(_.inspect(nodeData));
+ self.logger.trace(_.inspect(node));
+ itemsCollection.update({"_id": nodeData['_id']},nodeData,{safe:true},function(err, ndata){
+ self.logger.debug("Node data updated");
+ self.logger.trace(_.inspect(ndata));
+ if (err !== null) {
+ callback(err);
+ } else {
+ node['data']['_id'] = ndata._id;
+ node.isnew = false;
+ node.isModified = false;
+ callback(err, node);
+ }
+ });
+ }
+ });
+ }
+ });
+ } // ------------------------------ /function updateNode
+ self.logger.debug(modifiedNodes.length + " Nodes to update");
+ if (modifiedNodes.length === 0) {
+ callback(null,{'message': "No nodes to update",
+ "updatedNodes": 0});
+ } else {
+ modifiedNodes.forEach(function(node) {
+ self.logger.debug("Updating " + node.getPath());
+ updateNode(node, function(err, node) {
+ if (err !== null) {
+ callback(err);
+ } else {
+ self.logger.debug('Updated node - ' + (++updatedNodescount) + "/" + modifiedNodes.length);
+ if (updatedNodescount === modifiedNodes.length) {
+ callback(err, {'message': "Nodes updated",
+ "updatedNodes": updatedNodescount});
+ }
+ }
+ });
+ });
+ }
+ } //------------------------------ /function updateNodes
+
+ // process Node
+ insertNodes(function(err, insertRes) {
+ if (err === null) {
+ updateNodes(function(err, updateRes) {
+ if (err === null) {
+ var result = {
+ 'message': "Session.save() - operation successed",
+ 'createdNodes': insertRes['createdNodes'],
+ "updatedNodes": updateRes['updatedNodes']
+ };
+ self.logger.debug(_.inspect(result));
+ callback(null, result);
+ } else {
+ callback(err);
+ }
+ });
+ } else {
+ callback(err);
+ }
+ });
+ });
+
+ };
+
initWorkspace();
}
View
4 src/types/NodeType.js
@@ -1,6 +1,7 @@
var NodeType, ntproto,
nocr = require("NoCR"),
+ log4js = require('log4js')(),
nodeTypeManager = require('./nodeTypeManager.js'),
NodeTypeDefinition = require('./NodeTypeDefinition.js'),
assert = require('assert'),
@@ -9,6 +10,7 @@ var NodeType, ntproto,
ntproto = {
constructor: function(data) {
this.assertMandatory(data);
+ this.logger = log4js.getLogger("nocr-mongo.NodeType." + data['jcr:nodeTypeName']);
if (!('jcr:childNodeDefinition' in data)) {
data['jcr:childNodeDefinition'] = [];
}
@@ -77,7 +79,7 @@ ntproto = {
}
}
}
- //_.log(_.inspect(typedata));
+ this.logger.trace(_.inspect(typedata));
type = new NodeType(typedata);
type['data']['jcr:supertypes'].push(this['data']['jcr:nodeTypeName']);
type._super_ = this;
View
6 src/types/nodeTypeManager.js
@@ -1,6 +1,8 @@
//persist this (3.7.14)
var typesMap = {},
_ = require('util'),
+ log4js = require('log4js')(),
+ logger = log4js.getLogger("nocr-mongo.nodeTypeManager"),
//nodeTypeManager = function(){
// console.log("Creating node type manager");
//};
@@ -48,7 +50,7 @@ nodeTypeManager = {
typesMap[type.data['jcr:nodeTypeName']]['typedef'] = type;
typesMap[type.data['jcr:nodeTypeName']]['allowupdate'] = allowupdate;
} else {
- _.error(_.inspect(type));
+ logger.error(_.inspect(type));
throw new Error('Update is not allowed');
}
} else {
@@ -57,7 +59,7 @@ nodeTypeManager = {
'typedef': type
};
}
- _.log("Registered node type [" + type.data['jcr:nodeTypeName'] + "]" );
+ logger.info("Registered node type [" + type.data['jcr:nodeTypeName'] + "]" );
},
registerNodeTypes: function registerNodeTypes(types, allowupdate, callback) {
View
10 test/testSuite.js
@@ -1,13 +1,13 @@
-var Repository = require('../src/Repository.js'),
+var
+ log4js = require('log4js')().configure('./conf/log4js.json'),
+ Repository = require('../src/Repository.js'),
nocr = require("NoCR"),
vows = require('vows'),
- log4js = require('log4js')(),
assert = require('assert'),
testSuite, testrepository,
_ = require('util')
// This is the coffee test compiled in js
,nocrTests = nocr.test.ImplTest;
-log4js.configure('./conf/log4js.json');
testSuite = vows.describe('KaraCos Nu-Q test Suite');
testSuite.addBatch({
"Clearing Database": {
@@ -61,9 +61,7 @@ testSuite.addBatch({
topic: function(){
nocrTests.getRepository().login({username: 'admin', password:'notdemo'}, "testWorkSpace",this.callback);
},
- 'Test result error' : function(err,session){
- _.debug(_.inspect(err));
- _.debug(typeof err);
+ 'Test result error' : function(err,session) {
assert.ok(typeof err === 'string','Error must be a String');
assert.ok((session === null || session === undefined),'Session is not null');
}}}});
Please sign in to comment.
Something went wrong with that request. Please try again.