diff --git a/lib/chip-types/resource-type-field.js b/lib/chip-types/resource-type-field.js index bd85a6b1..22f13405 100644 --- a/lib/chip-types/resource-type-field.js +++ b/lib/chip-types/resource-type-field.js @@ -1,88 +1,88 @@ var Promise = require("bluebird"); function ResourceTypeField(declaration){ this.name = declaration.name; this.type_name = declaration.type; this.human_readable_name = declaration.human_readable_name || null; var type_constructor = Sealious.ChipManager.get_chip("field_type", declaration.type); this.type_parameters = declaration.params; this.type = new type_constructor(); this.type.set_params(declaration.params); this.type.set_fieldname(this.name); this.required = declaration.required || false; this.derived = declaration.derived || false; } ResourceTypeField.prototype = new function(){ /** * Shorthand for ResourceTypeField.type.isProperValue * @alias ResourceTypeField#isProperValue * @param {object} value * @return {Promise} */ this.check_value = function(context, value){ console.log("check value value", value) var _arguments; if(this.type.isProperValue.uses_context){ _arguments = [context, value] }else{ _arguments = [value] } return this.type.isProperValue.apply(this.type, _arguments) .catch(function(err){ var new_error={}; new_error[this.name] = err; return Promise.reject(new_error); }.bind(this)); } /** * Encodes a value for this field so it can be stored safey in database. Reverse of @link ResourceTypeField#decodeValue * @alias ResourceTypeField#encodeValue * @param {any} value * @param {Boolean} as_hashmap * @return {Promise} */ this.encode_value = function(context, value, as_hashmap){ var encode_function = this.type.encode.bind(this.type); if(encode_function.uses_context){ encode_function = encode_function.bind(context); } return encode_function(value) .then(function(encoded_value){ if(as_hashmap){ var ret = {}; ret[this.name] = encoded_value; return Promise.resolve(ret); }else{ return Promise.resolve(encoded_value); } }.bind(this)); } this.has_previous_value_sensitive_methods = function(){ - if (this.type.encode_value.old_value_sensitive || this.type.decodeValue.old_value_sensitive || this.type.isProperValue.old_value_sensitive) + if (this.encode_value.old_value_sensitive || this.decodeValue.old_value_sensitive || this.type.old_value_sensitive) return true; return false; } /** * @alias ResourceTypeField#decodeValue * @todo Zaimplementować tę funkcję */ this.decodeValue = function(){ } this.get_signature = function(){ var field_signature = {}; field_signature.name = this.name; field_signature.type = this.type_name; field_signature.required = this.required; field_signature.human_readable_name = this.human_readable_name; return field_signature; } } module.exports = ResourceTypeField; \ No newline at end of file diff --git a/lib/core-services/resource-manager.js b/lib/core-services/resource-manager.js index 9d82580f..1366c27a 100644 --- a/lib/core-services/resource-manager.js +++ b/lib/core-services/resource-manager.js @@ -1,218 +1,220 @@ var Promise = require("bluebird"); var ResourceRepresentation = require("../chip-types/resource-representation.js"); var UUIDGenerator = require("uid"); /** * Manages resources in the database * @class */ var ResourceManager = new function(){ this.name = "resources"; this.create = function(context, type_name, body){ if(!Sealious.ChipManager.chip_exists("resource_type", type_name)){ throw new Sealious.Errors.ValidationError("Unknown resource type: " + type_name); } var resource_type_object = Sealious.ChipManager.get_chip("resource_type", type_name); var access_strategy = resource_type_object.get_access_strategy("create"); var encoded_body = null; return access_strategy.check(context) .then(function(){ return resource_type_object.validate_field_values(context, body); }).then(function(){ return resource_type_object.encode_field_values(context, body); }).then(function(response){ encoded_body = response; var newID = UUIDGenerator(10); var resource_data = { sealious_id: newID, type: type_name, body: encoded_body }; return Sealious.Dispatcher.datastore.insert("resources", resource_data, {}); }).then(function(data){ var database_entry = data[0]; var resource = new ResourceRepresentation(database_entry); return Promise.resolve(resource.getData()); }) } this.delete = function(context, type_name, id){ if(!Sealious.ChipManager.chip_is_registred("resource_type." + type_name)){ throw new Sealious.Errors.ValidationError("Unknown resource type: " + type_name); } var resource_type_object = Sealious.ChipManager.get_chip("resource_type", type_name); var access_strategy = resource_type_object.get_access_strategy("delete"); return access_strategy.check(context) .then(function(){ return Sealious.Dispatcher.datastore.delete("resources", {sealious_id: id, type: type_name}, {}) }) .then(function(data){ return Promise.resolve(); }); } this.get_by_id = function(context, resource_id){ //var access_strategy = resource_type_object.get_access_strategy("get_by_id"); var get_resource = Sealious.Dispatcher.datastore.find("resources", { sealious_id: resource_id }, {}) .then(function(documents){ if (documents[0] === undefined) { return Promise.reject(new Sealious.Errors.NotFound("resource of id " + resource_id + " not found")); } else { var database_entry = documents[0]; var resource = new ResourceRepresentation(database_entry); return Promise.resolve([database_entry.type, resource.getData()]); } }).then(function(response){ var resource_type_name = response[0]; var resource_data = response[1]; var resource_type = Sealious.ChipManager.get_chip("resource_type", resource_type_name); var access_strategy = resource_type.get_access_strategy("retrieve"); return access_strategy.check(context, resource_data); }); return get_resource; } function filter_resource_list(context, access_strategy){ return function(item){ return new Promise(function(resolve, reject){ access_strategy.check(context, item) .then(function(){ resolve(true); }).catch(function(){ resolve(false); }) }); } } this.list_by_type = function(context, type_name){ var resource_type = Sealious.ChipManager.get_chip("resource_type", type_name); var access_strategy = resource_type.get_access_strategy("retrieve"); var preliminary_access_strategy_check; if(access_strategy.item_sensitive){ preliminary_access_strategy_check = Promise.resolve(); }else{ preliminary_access_strategy_check = access_strategy.check(context); } var get_items = preliminary_access_strategy_check.then(function(){ return Sealious.Dispatcher.datastore.find("resources", { type: type_name }, {}, {}); }).then(function(items) { return Promise.map(items, function(database_entry){ return new ResourceRepresentation(database_entry).getData(); }); }); if(access_strategy.item_sensitive){ return get_items.then(function(items){ return Promise.filter(items, filter_resource_list(context, access_strategy)); }); }else{ return get_items; } } this.find = function(context, field_values, type){ //throw new Error("ResourceManager.find not implemented."); if(arguments.length==2){ type = null; } var query = {}; if(type){ query.type = type; } for(var field_name in field_values){ query["body." + field_name] = field_values[field_name]; } return Sealious.Dispatcher.datastore.find("resources", query) .then(function(documents){ var parsed_documents = documents.map(function(document){return new ResourceRepresentation(document).getData()}); return Promise.resolve(parsed_documents); }); } this.update_resource = function(context, type_name, resource_id, new_resource_data){ var query = {}; var resource_type_object = Sealious.ChipManager.get_chip("resource_type", type_name); var access_strategy = resource_type_object.get_access_strategy("update"); + console.log("has_previous_value_sensitive_fields",resource_type_object.has_previous_value_sensitive_fields()) + return access_strategy.check(context) .then(function(){ Sealious.Dispatcher.resources.get_by_id(context,resource_id) .then(function(result){ var values = [new_resource_data.access_strategy, result.state]; return resource_type_object.validate_field_values(context, values); }); }); /* if(new_resource_data.hasOwnProperty("sealious_id")){ delete new_resource_data.sealious_id; } if(new_resource_data.hasOwnProperty("id")){ delete new_resource_data.id; } for(var field_name in new_resource_data){ query["body." + field_name] = new_resource_data[field_name]; } return Sealious.Dispatcher.datastore.update("resources", {sealious_id: resource_id}, {$set: query}) .then(function(){ return Sealious.Dispatcher.resources.get_by_id(context,resource_id); }) */ } this.search = function(context, type, field_name, query_string){ query = {body: {}}; query["body"][field_name] = new RegExp(query_string, "gi"); return new Promise(function(resolve, reject){ Sealious.Dispatcher.datastore.find("resources", query) .then(function(documents){ var resource_representations = documents.map(function(document){return new ResourceRepresentation(document).getData()}); resolve(resource_representations); }) }) } this.search_by_mode = function(context, type, mode){ return new Promise(function(resolve, reject){ Sealious.Dispatcher.datastore.find("resources", {access_mode: mode, type: type}, {}) .then(function(documents){ var database_entry = documents; var resource_representations = documents.map(function(document){ return new ResourceRepresentation(document).getData() }); resolve(resource_representations); }); }); } this.get_resource_type_signature = function(context, type_name){ return new Promise(function(resolve, reject){ var resource_type_chip = Sealious.ChipManager.get_chip("resource_type", type_name); if(resource_type_chip){ resolve(resource_type_chip.get_signature()); }else{ reject(new Sealious.Errors.Error("ResourceManager tried to access chip type `" + type_name + "`, which does not exist.")); } }) } } module.exports = ResourceManager;