<commit_before>var app = require('./app'), http = require('http'), parser = require('xml2json'), util = require('util'), path = require('path'), error = app.log.error;

module.exports = {	
	saveMDRecord: function(record, params, query) {
	    var doc = parser.toJson(record, {reversible: true, coerce: false});    
	    var mdrecord = app.schema.CreateMDRecord({ dkclarinID: params.id, data: doc, 'type.name': params.name });
	    
	    this.saveDocument(mdrecord);
	    var itemsToAdd = (query.search_ids.length > 0) ? { result_collection: mdrecord._id, search_ids: params.id }
							    :  { result_collection: mdrecord._id };
	    app.schema.models.Query.update({_id: query._id}, { $addToSet: itemsToAdd }, function(err) {
		if(err) return app.log.error("Error on Query update: " + err);
		app.log.info("Update +Query-" + query._id);
		query.result_collection.push(mdrecord);
	    });
	},

	updateMDRecord: function(new_data, mdrecord) {
	    app.log.debug("Updating MDRecord: " + mdrecord._id);
	    mdrecord.data = JSON.stringify(new_data);
	    mdrecord.set('update', true);
	    this.saveDocument(mdrecord);
	},

	updateQuery: function(query) {
	    app.log.debug("Updating Query: " + query._id);
	    query.set('update', true);
	    this.saveDocument(query);
	},

	saveDocument: function(model, callback) {
	    model.save(function(err) { 
		if(err) error('Error:'+err);
		else app.log.debug('+' + model.constructor.modelName + "-" + model._id); 
		if(callback && !err) callback(model);
	    });
	},

	removeDocument: function(model, callback) {
	    var modelDesc = model.constructor.modelName + "-" + model._id;
	    model.remove(function(err, doc) {
		if(err) error('Error:'+err);
		else app.log.debug('-' + modelDesc);
		if(callback && !err) callback(doc);
	    });
	},

	rest_options: function(params, isUpload) {
	    return { 
		host: (app.config.get('updaterHost') != null) ? app.config.get('updaterHost') : 'localhost',
		port: (app.config.get('updaterPort') != null) ? app.config.get('updaterPort') : '80',
		path: '/v0.9/items/'+params.id+'/metadata/'+params.name+'?escidocurl='+params.target+'&d='+Date.now(),
		headers: {'Cookie': 'escidocCookie='+params.handle, 'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'},
		method: (!isUpload) ? 'GET' : 'PUT'
	    };
	},

	srw_options: function(queryCQL, record_vals) {
	    var search_path_limits = "&maximumRecords="+record_vals.limit+"&startRecord="+record_vals.start;
	    var targetUrl = app.config.get('targetUrl');
	    return { 
		host: (targetUrl.indexOf('http://') != -1) ? targetUrl.replace('http://', '') : targetUrl,
		path: '/srw/search/escidoc_all?query='+queryCQL.replace(/\s/g,"%20")+search_path_limits+'&d='+Date.now()
	    };
	},

	ir_options: function(params) {
	    var targetUrl = app.config.get('targetUrl');
	    return {
		host: (targetUrl.indexOf('http://') != -1) ? targetUrl.replace('http://', '') : targetUrl,
		path: '/ir/item/' + params.id + '/' + params.method,
		headers: (params.lastModificationDate != null) ? {'Content-Type': 'application/xml', 'Cookie': 'escidocCookie='+params.handle, 'Cache-Control':'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'} : null,
		method: (params.lastModificationDate != null) ? 'POST' : 'GET'
	    };
	},
<commit_msg>Fix UTF8 encoding on SRW request CQL query param.<commit_after>var app = require('./app'), http = require('http'), parser = require('xml2json'), util = require('util'), path = require('path'), error = app.log.error;

module.exports = {	
	saveMDRecord: function(record, params, query) {
	    var doc = parser.toJson(record, {reversible: true, coerce: false});    
	    var mdrecord = app.schema.CreateMDRecord({ dkclarinID: params.id, data: doc, 'type.name': params.name });
	    
	    this.saveDocument(mdrecord);
	    var itemsToAdd = (query.search_ids.length > 0) ? { result_collection: mdrecord._id, search_ids: params.id }
							    :  { result_collection: mdrecord._id };
	    app.schema.models.Query.update({_id: query._id}, { $addToSet: itemsToAdd }, function(err) {
		if(err) return app.log.error("Error on Query update: " + err);
		app.log.info("Update +Query-" + query._id);
		query.result_collection.push(mdrecord);
	    });
	},

	updateMDRecord: function(new_data, mdrecord) {
	    app.log.debug("Updating MDRecord: " + mdrecord._id);
	    mdrecord.data = JSON.stringify(new_data);
	    mdrecord.set('update', true);
	    this.saveDocument(mdrecord);
	},

	updateQuery: function(query) {
	    app.log.debug("Updating Query: " + query._id);
	    query.set('update', true);
	    this.saveDocument(query);
	},

	saveDocument: function(model, callback) {
	    model.save(function(err) { 
		if(err) error('Error:'+err);
		else app.log.debug('+' + model.constructor.modelName + "-" + model._id); 
		if(callback && !err) callback(model);
	    });
	},

	removeDocument: function(model, callback) {
	    var modelDesc = model.constructor.modelName + "-" + model._id;
	    model.remove(function(err, doc) {
		if(err) error('Error:'+err);
		else app.log.debug('-' + modelDesc);
		if(callback && !err) callback(doc);
	    });
	},

	rest_options: function(params, isUpload) {
	    return { 
		host: (app.config.get('updaterHost') != null) ? app.config.get('updaterHost') : 'localhost',
		port: (app.config.get('updaterPort') != null) ? app.config.get('updaterPort') : '80',
		path: '/v0.9/items/'+params.id+'/metadata/'+params.name+'?escidocurl='+params.target+'&d='+Date.now(),
		headers: {'Cookie': 'escidocCookie='+params.handle, 'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'},
		method: (!isUpload) ? 'GET' : 'PUT'
	    };
	},

	srw_options: function(queryCQL, record_vals) {
	    var search_path_limits = "&maximumRecords="+record_vals.limit+"&startRecord="+record_vals.start;
	    var targetUrl = app.config.get('targetUrl');
	    app.log.debug("queryCQL: " + queryCQL);
	    return { 
		host: (targetUrl.indexOf('http://') != -1) ? targetUrl.replace('http://', '') : targetUrl,
		path: '/srw/search/escidoc_all?query='+encodeURIComponent(queryCQL.replace(/\s/g,"%20"))+search_path_limits+'&d='+Date.now()
	    };
	},

	ir_options: function(params) {
	    var targetUrl = app.config.get('targetUrl');
	    return {
		host: (targetUrl.indexOf('http://') != -1) ? targetUrl.replace('http://', '') : targetUrl,
		path: '/ir/item/' + params.id + '/' + params.method,
		headers: (params.lastModificationDate != null) ? {'Content-Type': 'application/xml', 'Cookie': 'escidocCookie='+params.handle, 'Cache-Control':'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'} : null,
		method: (params.lastModificationDate != null) ? 'POST' : 'GET'
	    };
	},

{'fraction_non_alphanumeric': 0.1947267638827515, 'fraction_numerical': 0.004860804242156429, 'mean_word_length': 4.1792524790236465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3981310', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2339, 'n_words': 396}