Skip to content

Commit

Permalink
first working implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ximex committed Mar 21, 2015
1 parent fa6b68b commit c245dd9
Show file tree
Hide file tree
Showing 15 changed files with 27,228 additions and 2 deletions.
52 changes: 52 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
'use strict';

var RisBka = require('./lib/index.js');

// Query for searchDocuments
var query = {
searchTerms: '', // PhraseSearchExpression
title: '', // PhraseSearchExpression
index: '', // PhraseSearchExpression
section: {
number: [
1,
2
],
character: [
'a',
'z'
],
typ: '' // "Alle", "Artikel", "Paragraph", "Anlage"
},
verionDate: '', // Date
announcementInstitution: '', // PhraseSearchExpression
announcementInstitutionNumber: '', // PhraseSearchExpression
includedDate: '', // "Undefined", "EinerWoche", "ZweiWochen", "EinemMonat", "DreiMonaten", "SechsMonaten", "EinemJahr"
paging: {
docsPerPage: '', // "Ten", "Twenty", "Fifty", "OneHundred"
page: 1 // Integer
},
sort: {
direction: '', // "Ascending", "Descending"
column: '' // "ArtikelParagraphAnlage", "Kurzinformation"
}
};


// getVersion
RisBka.getVersion(function (soapBody, raw) {
console.log(soapBody);
}, function (err, soapBody, raw) {
console.log(err, soapBody, raw);
});

// searchDocuments
RisBka.searchDocuments(query, function (soapBody, raw) {
console.log(soapBody);
}, function (err, soapBody, raw) {
console.log(err, soapBody, raw);
});

// getDocument
RisBka.getDocument('NOR40091435', function (soapBody, raw) {
console.log(soapBody);
}, function (err, soapBody, raw) {
console.log(err, soapBody, raw);
});
29 changes: 29 additions & 0 deletions lib/SOAPRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

let request = require('request');
let SOAPResponse = require('./SOAPResponse');

const PARAMS = {
URL: 'http://data.bka.gv.at/ris/OGDService.asmx',
HEADERS: {
'Content-Type': 'application/soap+xml; charset=utf-8'
},
SOAP_BODY: '<?xml version="1.0" encoding="utf-8" standalone="yes"?>' +
'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">' +
'<soap12:Body>{{ACTION}}</soap12:Body>' +
'</soap12:Envelope>',
APPLICATION: 'Br'
};

module.exports = function (buildBody, callback) {
let body = buildBody(PARAMS);

request.post({
url: PARAMS.URL,
headers: PARAMS.HEADERS,
body: body
}, function (error, response, body) {
SOAPResponse(error, response, body, callback);
});
};
46 changes: 46 additions & 0 deletions lib/SOAPResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

let Jsonix = require('jsonix').Jsonix;
let SOAP_Envelope = require('../mappings/SOAP_Envelope').SOAP_Envelope;

module.exports = function (error, response, body, callback) {
let err = null;
let soapBody = null;
let raw = {
error: error,
response: response,
body: body
};

if (error) {
err = {
errorType: 'request',
error: error
};
} else {
let context = new Jsonix.Context([SOAP_Envelope]);
let unmarshaller = context.createUnmarshaller();

let parsedBody = unmarshaller.unmarshalString(body);
soapBody = parsedBody.value.body.any;

if (soapBody[0].name && soapBody[0].name.localPart === 'Fault') {
err = {
errorType: 'soap',
error: soapBody[0].value
};
delete err.error.TYPE_NAME;
} else {
try {
soapBody = soapBody[0].childNodes[0].childNodes[0].data;
} catch (e) {
err = {
errorType: 'other',
error: e
};
}
}
}

callback(err, soapBody, raw);
};
49 changes: 49 additions & 0 deletions lib/getDocument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

let Jsonix = require('jsonix').Jsonix;
let SOAPRequest = require('./SOAPRequest');
let OGD_DocumentResponse = require('../mappings/OGD_DocumentResponse').OGD_DocumentResponse;
let RISDok = require('../mappings/RISDok').RISDok;
let RISJudikaturNutzdaten = require('../mappings/RISJudikaturNutzdaten').RISJudikaturNutzdaten;

module.exports = function (docId, cbOk, cbFail) {

SOAPRequest(function (PARAMS) {
let action = '<getDocument xmlns="http://ogd.bka.gv.at/"><application>' + PARAMS.APPLICATION + '</application>' +
'<docId>' + docId + '</docId></getDocument>';
let body = PARAMS.SOAP_BODY.replace('{{ACTION}}', action);

return body;
}, function (err, soapBody, raw) {
if (err) {
cbFail(err, soapBody, raw);
} else {
let context = new Jsonix.Context([OGD_DocumentResponse, RISDok, RISJudikaturNutzdaten]);
let unmarshaller = context.createUnmarshaller();

let parsedBody = unmarshaller.unmarshalString(soapBody);
let status = parsedBody.value.status;

if (status === 'ok') {
let parsedDocument = parseDocument(parsedBody.value.ogdDocumentMetadata);

cbOk(parsedDocument, raw);
} else {
err = {
errorType: 'invalidDocId',
error: parsedBody.value.error
};
delete err.error.TYPE_NAME;

cbFail(err, soapBody, raw);
}
}
});

};

function parseDocument (ogdDocumentMetadata) {
let nutzdaten = ogdDocumentMetadata.dokumentinhalt.contentReference[0].risdok.nutzdaten;

return ogdDocumentMetadata;
}
16 changes: 16 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
'use strict';

let versionMethod = require('./version');
let requestMethod = require('./request');
let getDocumentMethod = require('./getDocument');

module.exports = {
getVersion: function (cbOk, cbFail) {
versionMethod(cbOk, cbFail);
},
searchDocuments: function (query, cbOk, cbFail) {
requestMethod(query, cbOk, cbFail);
},
getDocument: function (docId, cbOk, cbFail) {
getDocumentMethod(docId, cbOk, cbFail);
}
};
133 changes: 133 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
'use strict';

let Jsonix = require('jsonix').Jsonix;
let SOAPRequest = require('./SOAPRequest');
let OGD_Request = require('../mappings/OGD_Request').OGD_Request;
let OGD_ResponseList = require('../mappings/OGD_ResponseList').OGD_ResponseList;
let OGD_ResponseList_Type = require('../mappings/OGD_ResponseList_Type').OGD_ResponseList_Type;

module.exports = function (query, cbOk, cbFail) {

SOAPRequest(function (PARAMS) {
let context = new Jsonix.Context([OGD_Request]);
let marshaller = context.createMarshaller();

let jsonQuery = mapQuery(query);

// TODO: better convert than String.replace()
let OGDSearchRequest = marshaller.marshalString(jsonQuery)
.replace(/&/g,'&amp;')
.replace(/:p0/g, '').replace(/p0:/g, '')
.replace(/</g,'&lt;').replace(/>/g,'&gt;')
.replace('xmlns="http://ris.bka.gv.at/Search/1.3/OGD"',
'xmlns="http://ris.bka.gv.at/Search/1.3/OGD" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"');

let action = '<request xmlns="http://ogd.bka.gv.at/"><application>' + PARAMS.APPLICATION + '</application>' +
'<query>' + OGDSearchRequest + '</query></request>';
let body = PARAMS.SOAP_BODY.replace('{{ACTION}}', action);

return body;
}, function (err, soapBody, raw) {
if (err) {
cbFail(err, soapBody, raw);
} else {
let context = new Jsonix.Context([OGD_ResponseList, OGD_ResponseList_Type]);
let unmarshaller = context.createUnmarshaller();

let parsedBody = unmarshaller.unmarshalString(soapBody);
let status = parsedBody.value.status;

if (status === 'ok') {
let parsedFoundDocuments = parseFoundDocuments(parsedBody.value.searchDocumentsResult);

cbOk(parsedFoundDocuments, raw);
} else {
err = {
errorType: 'invalidDocId',
error: parsedBody.value.error
};
delete err.error.TYPE_NAME;

cbFail(err, soapBody, raw);
}
}
});

};

function mapQuery (query) {
let jsonQuery = {
name: {
namespaceURI: 'http://ris.bka.gv.at/Search/1.3/OGD',
localPart: 'OGDSearchRequest'
//prefix: '',
//key: '{http://ris.bka.gv.at/Search/1.3/OGD}OGDSearchRequest',
//string: '{http://ris.bka.gv.at/Search/1.3/OGD}OGDSearchRequest'
},
value: {
//TYPE_NAME: 'OGDSearchRequest.TOGDSearchRequest',
//suchworte: { // PhraseSearchExpression
// //TYPE_NAME: 'OGDSearchRequest.PhraseSearchExpression',
// value: ''
//},
//titel: { // PhraseSearchExpression
// //TYPE_NAME: 'OGDSearchRequest.PhraseSearchExpression',
// value: ''
//},
//index: { // PhraseSearchExpression
// //TYPE_NAME: 'OGDSearchRequest.PhraseSearchExpression',
// value: ''
//},
abschnitt: {
//TYPE_NAME: 'OGDSearchRequest.NormabschnittSucheinschraenkung',
nummerVon: 0, // Integer
nummerBis: 9, // Integer
buchstabeVon: 'a', // String
buchstabeBis: 'z', // String
typ: 'Alle' // "Alle", "Artikel", "Paragraph", "Anlage"
},
fassungVom: { // Date
year: 2012, // Integer
month: 4, // Integer
day: 12 // Integer
},
//kundmachungsorgan: { // PhraseSearchExpression
// //TYPE_NAME: 'OGDSearchRequest.PhraseSearchExpression',
// value: ''
//},
//kundmachungsorgannummer: { // PhraseSearchExpression
// //TYPE_NAME: 'OGDSearchRequest.PhraseSearchExpression',
// value: ''
//},
imRisSeit: 'Undefined', // "Undefined", "EinerWoche", "ZweiWochen", "EinemMonat", "DreiMonaten", "SechsMonaten", "EinemJahr"
dokumenteProSeite: 'Ten', // "Ten", "Twenty", "Fifty", "OneHundred"
seitennummer: 1, // Integer
sortierung: {
//TYPE_NAME: 'OGDSearchRequest.BundesnormenSortExpression',
sortDirection: 'Ascending', // "Ascending", "Descending"
sortedByColumn: 'ArtikelParagraphAnlage' // "ArtikelParagraphAnlage", "Kurzinformation"
}
}
};

return jsonQuery;
}


function parseFoundDocuments (searchDocumentsResult) {
let paging = {
page: searchDocumentsResult.hits.pageNumber,
size: searchDocumentsResult.hits.pageSize,
count: searchDocumentsResult.hits.value
};

let ogdDocumentReference = searchDocumentsResult.documentReferences.ogdDocumentReference;
ogdDocumentReference.forEach(function (foundDocument) {
delete foundDocument.TYPE_NAME;
});

return {
paging: paging,
results: ogdDocumentReference
};
}
20 changes: 20 additions & 0 deletions lib/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

let SOAPRequest = require('./SOAPRequest');

module.exports = function (cbOk, cbFail) {

SOAPRequest(function (PARAMS) {
let action = '<version xmlns="http://ogd.bka.gv.at/"></version>';
let body = PARAMS.SOAP_BODY.replace('{{ACTION}}', action);

return body;
}, function (err, soapBody, raw) {
if (err) {
cbFail(err, soapBody, raw);
} else {
cbOk(soapBody, raw);
}
});

};
Loading

0 comments on commit c245dd9

Please sign in to comment.