Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F10360143
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
16 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/chip-types/resource-type-field.js b/lib/chip-types/resource-type-field.js
index 8b6b7424..bd85a6b1 100644
--- a/lib/chip-types/resource-type-field.js
+++ b/lib/chip-types/resource-type-field.js
@@ -1,80 +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)
+ 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/chip-types/resource-type.js b/lib/chip-types/resource-type.js
index bdbb1e71..11eb64d6 100644
--- a/lib/chip-types/resource-type.js
+++ b/lib/chip-types/resource-type.js
@@ -1,151 +1,168 @@
var Promise = require("bluebird");
var merge = require("merge");
var Chip = require("./chip.js");
var ResourceTypeField = require("./resource-type-field.js");
var ResourceType = function(parent_module_path, name, declaration){
Chip.call(this, parent_module_path, true, "resource_type", name);
this.fields = {};
this.references = {};
this.keys = {};
this.access_strategy = {
default: Sealious.ChipManager.get_chip("access_strategy", "public")
};
this._process_declaration(declaration);
}
ResourceType.prototype = new function(){
this._process_declaration = function(declaration){
if(declaration){
if(declaration.fields){
this.add_fields(declaration.fields);
}
this.set_access_strategy(declaration.access_strategy);
}
}
this.add_field = function(field_declaration){
var field_object = new ResourceTypeField(field_declaration);
var field_name = field_object.name;
if(!this.keys[field_name]){
this.fields[field_name] = field_object;
this.keys[field_name] = field_object;
}
}
this.add_fields = function(field_declarations_array){
for(var i in field_declarations_array){
var declaration = field_declarations_array[i];
this.add_field(declaration);
}
}
+ this.has_previous_value_sensitive_fields = function(){
+ for (var field_name in this.fields){
+ if (this.fields[field_name].has_previous_value_sensitive_methods()){
+ return true;
+ }
+ }
+ return false;
+ }
+
function ensure_no_unknown_fields(values){
var validation_errors = {};
+ console.log("ensure no unknown field",values)
for(var field_name in values){
+
if(this.keys[field_name]==undefined){
validation_errors[field_name] = "unknown_field";
}
}
return validation_errors;
}
+
+
function ensure_no_missing_required_fields(values){
var validation_errors = {};
for(var i in this.keys){
var field = this.keys[i];
if(field.required && !values[field.name]){
validation_errors[field.name] = "missing_value";
}
}
return validation_errors;
}
this.validate_field_values = function(context, values){
+ console.log("resource type values", values)
var that = this;
var validation_errors = {};
validation_errors = merge(validation_errors, ensure_no_unknown_fields.call(that, values));
validation_errors = merge(validation_errors, ensure_no_missing_required_fields.call(that, values));
var promise_array = [];
for(var key in values){
+ console.log("values[key]",values[key])
+ console.log("validation_errors[key]", validation_errors[key])
if(validation_errors[key]===undefined){
+
var _arguments;
var field = that.keys[key];
var value = values[key];
var validation_promise = field.check_value(context, value);
promise_array.push(validation_promise);
}
}
var error_message = "There are problems with some of the provided values";//TODO: ładnie generować tekst podsumowujący problem z inputem
return Promise.all(promise_array)
.then(function(result){
if(Object.keys(validation_errors).length>0){
throw new Sealious.Errors.ValidationError(error_message, {invalid_fields: validation_errors});
}else{
return Promise.resolve();
}
})
.catch(function(errors){
if(!(errors instanceof Error)){
for(var i in errors){
if(!(typeof errors[i]=="string") && errors.is_user_fault===false){
throw errors[i];
}
}
}
validation_errors = merge(validation_errors, errors);
return Promise.reject(new Sealious.Errors.ValidationError(error_message, {invalid_fields: validation_errors}));
});
}
this.encode_field_values = function(context, body){
var promises = [];
for(var field_name in body){
var current_value = body[field_name];
promises.push(this.keys[field_name].encode_value(context, current_value, true));
}
return Promise.all(promises).then(function(responses){
return new Promise(function(resolve, reject){
resolve(merge.apply(merge, responses));
})
});
}
this.get_signature = function(){
var resource_type_signature = [];
for(var field_name in this.fields){
var field_signature = this.fields[field_name].get_signature();
resource_type_signature.push(field_signature);
}
return resource_type_signature;
}
this.set_access_strategy = function(strategy_declaration){
if(typeof strategy_declaration == "string"){
access_strategy_name = strategy_declaration;
this.access_strategy = {
"default": Sealious.ChipManager.get_chip("access_strategy", access_strategy_name),
}
}else if(typeof strategy_declaration =="object"){
for(var action_name in strategy_declaration){
var access_strategy = Sealious.ChipManager.get_chip("access_strategy", strategy_declaration[action_name]);
this.access_strategy[action_name] = access_strategy;
}
}
}
this.get_access_strategy = function(action){
return this.access_strategy[action] || this.access_strategy["default"];
}
}
ResourceType.is_a_constructor = false;
module.exports = ResourceType;
\ No newline at end of file
diff --git a/lib/core-services/resource-manager.js b/lib/core-services/resource-manager.js
index 63e20488..9d82580f 100644
--- a/lib/core-services/resource-manager.js
+++ b/lib/core-services/resource-manager.js
@@ -1,200 +1,218 @@
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.");
- /*
+ //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, resource_id, new_resource_data){
+ 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");
+
+
+ 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 new Promise(function(resolve, reject){
- Sealious.Dispatcher.datastore.update("resources", {sealious_id: resource_id}, {$set: query})
- .then(function(){
- return Sealious.Dispatcher.resources.get_by_id(resource_id);
- })
- .then(resolve);
- })
+ 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;
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 8, 04:45 (1 d, 10 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1032660
Default Alt Text
(16 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment