Apprenez à créer une légende de service fonctionnel pouvant lire et stocker vers une instance MongoDB à l'aide de l'API flexible de gestion de sources de données de Corticon
Progress Corticon 5.7 inclut un fichier de configuration de source de données unifiée permettant aux utilisateurs de configurer des connexions externes sources de données. Bien que ce fichier soit principalement utilisé pour se connecter à des bases de données externes (ADC ou EDC), il est exposé dans le cadre des appels de service afin que les utilisateurs puissent connecter Corticon Server à une source de données personnalisée. La base de données n'est pas supportée nativement. Ce guide explique comment se connecter à une instance MongoDB à l'aide d'une API REST.
Avant de commencer, assurez-vous d'avoir tous les logiciels prérequis:
- Corticon Studio 5.7
- Corticon Server 5.7 (pour le déploiement)
- MongoDB 3.6
- MongoDB Compass (pour configurer la base de données et le catalogue)
- RESTHeart 3.2.2
Configurer MongoDB
Suivez les instructions du site Web MongoDB pour installer et démarrer MongoDB. Le site Web propose des guides d'installation pour tous les principaux systèmes d'exploitation de bureau
Open MongoDB Compass
Les paramètres de connexion par défaut doivent vous permettre de vous connecter.
Étape 3
Testez le point de terminaison en utilisant votre navigateur préféré ou votre client HTTP.
// Convertir nos DataObjects en JSON
[1945909]
//
// Puisque nous parlons à d'une API REST [19659115] convertir les 'voitures' en JSON
[19659108] Ensemble voitures = dataObjectManager.getEntitiesByName ( "Voiture" );
// Ce sera le JSONObject que nous envoyons
[1945909]
JSONObject carsJson = nouveau JSONObject ();
// Et ce sera le tableau que nous avons mis tous les 'voitures' en
[19659108] JSONArray carsArray = nouveau JSONArray ();
[19659069]
// Convertir les objets de données, chacun sera un objet json dans le tableau
// Chaque attribut sera une propriété
pour (ICcDataObject currentCar: voitures) {
[19659069] JSONObject currentCarJSON = nouveau JSONObject ();
// Récupère les noms d'attributs
Ensemble AttributNoms = currentCar.getAttributeNames ();
[19659069
// Boucle à travers les attributs
pour (String currentAttributeName: attributeNames) {
// Placer la valeur d'attribut [19659069]
currentCarJSON.put (currentAttributeName, currentCar.getAttributeValue (nomAttribut actuel));
}
[196AIS59184] // Ajouter la nouvelle voiture objet JSON à la rangée
// Récupère les noms d'attributs Ensemble AttributNoms = currentCar.getAttributeNames (); // Boucle à travers les attributs pour (String currentAttributeName: attributeNames) { // Placer la valeur d'attribut [19659069] currentCarJSON.put (currentAttributeName, currentCar.getAttributeValue (nomAttribut actuel)); } [196AIS59184] // Ajouter la nouvelle voiture objet JSON à la rangée
carsArray.put (currentCarJSON);
}
[1945905]
// Placer le tableau dans l'objet JSON
[19659108] voituresJson.put ( "Voitures" (Object) carsArray);
[1965906EN]. 9]
[1945904]
//
// Connectez à MongoDB
//
// Récupère le gestionnaire de sources de données
IDatasourceManager datasourceManager = dataObjectManager.getDatasourceManager ();
// Récupère notre source de données MongoDB
IDatasource mongoDBDatasource = datasourceManager. getDatasource ( "MongoDB" );
[19659069]
// Ouvert une connexion http
à la base de données
// Connectez à MongoDB // // Récupère le gestionnaire de sources de données IDatasourceManager datasourceManager = dataObjectManager.getDatasourceManager (); // Récupère notre source de données MongoDB IDatasource mongoDBDatasource = datasourceManager. getDatasource ( "MongoDB" ); [19659069] // Ouvert une connexion http à la base de données
URL mongoDBConnectionURL = nouvelle URL (mongoDBDatasource.getDatasour ceConnectionURL ());
HttpURLConnection mongoDBConnection = (HttpURLConnection) mongoDBConnectionURL.openConnection ();
[1945905]
[19459028Cetarticleapparaîtdansl'éditionbilingue] // Puisque nous stockons, nous voulons que à utilisent la méthode HTTP 'PUT' [19659069]
mongoDBConnection.setRequestMethod ( "PUT" );
// Définir le type de contenu
mongoDBConnection.setRequestProperty ( "Content-Type" "application / json" );
// Assurez-vous de pouvoir écrire à la connexion
mongoDBConnection.setDoOutput ( true )
// Nous allons à écrire les données à la connexion sortie flux
[1945909]
BufferedWriter jsonContentWriter = null ;
essayez {
Qu'est-ce que c'est? jsonContentWriter = new BufferedWriter (nouveau OutputStreamWriter (mongoDBConnection.getOutputStream ()));
// Convertir notre JSON vers [19659105] une chaîne à envoie à l'API REST
[19659184] jsonContentWriter.write (carsJson.toString ())
[19659069]
// Rincer le ruisseau
jsonContentWriter.flush ()
// Ouvert connexion
// Nous allons à écrire les données à la connexion sortie flux BufferedWriter jsonContentWriter = null ;
essayez {
Qu'est-ce que c'est? jsonContentWriter = new BufferedWriter (nouveau OutputStreamWriter (mongoDBConnection.getOutputStream ()));
// Convertir notre JSON vers [19659105] une chaîne à envoie à l'API REST
[19659184] jsonContentWriter.write (carsJson.toString ())
[19659069] // Rincer le ruisseau jsonContentWriter.flush () // Ouvert connexion
mongoDBConnection. connect ();
[19459028Ensavoirplussurcetartiste]
// Imprimer sortir une règle [19659105] le succès ou échec
int mongoDBResponseCode = mongoDBConnection.getResponseCode ();
si (mongoDBResponseCode> = 200 && mongoDBResponseCode <30 0) {
dataObjectManager.postMessage (ICcDataObjectManager.INFO, "Appel MongoDB PUT RÉUSSI avec un statut de " + Integer .toString (mongoDBResponseCode), null );
[19659069] } autre {
dataObjectManager .postMessage (ICcDataObjectManager.INFO, "L'appel de MongoDB PUT a échoué avec un statut de" + Integer .toString (mongoDBResponseCode), null );
} [19659069]
} finalement [
// Fermer le flux et connexion lorsque nous avons terminé
si (jsonContentWriter! = null ) {
jsonContentWriter. fermer ();
}
mongoDBConnection.disconnect ();
}
[19459028Cetarticleaététraduitle]
} [19659069] } finalement [ // Fermer le flux et connexion lorsque nous avons terminé si (jsonContentWriter! = null ) { jsonContentWriter. fermer (); } mongoDBConnection.disconnect ();
}
[19459028Cetarticleaététraduitle]
}
Exporter le fichier JAR et tester le nouveau service Légende
Etape 1
Pour exporter le fichier JAR, faites un clic droit sur le projet, sélectionnez Exporter dans le menu et sélectionnez Fichier Java / JAR depuis le options de l'assistant.
Étape 4
Faire une nouvelle fiche de test dans la règle et sélectionner le Ruleflow comme sujet de test
//
// Connectez à MongoDB
[19659069] //
// Récupère le gestionnaire de sources de données
IDatasourceManager gestionnaire de données = dataObjectManager.getDatasourceManager ();
// Récupérer notre source de données MongoDB [19659069]
IDatasource mongoDBDatasource = datasourceManager.getDatasource ( "MongoDB" ); [19659069]
// Ouvrir une connexion http à la base de données
URL mongoDBConnectionURL = nouvelle URL (mongoDBDatasource.getDatasourceConnectionURL ());
HttpURLConnexion mongoDBConnection = (HttpURLConnection) mongoDBConnectionURL.openConnection ();
// Puisque nous lisons, nous voulons ] à utilisent la méthode HTTP 'GET' " title="" data-openoriginalimageonclick="true"/>
mongoDBConnection.setRequestMethod ( "OBTENIR" );
[1945908]
// Servez le type de contenu
[1945909]
mongoDBConnection.setRequestProperty ( "Content-Type" "application / json" );
//Make the connection
mongoDBConnection.connect();
//Print out a rule message on success [19659156]or failure
int mongoDBResponseCode = mongoDBConnection.getResponseCode();
if(mongoDBResponseCode >= 200 && mongoDBResponseCode < 300) {
dataObjectManager.postMessage(ICcDataObjectManager.INFO, "MongoDB GET call SUCCEEDED with a status of " + Integer.toString(mongoDBResponseCode), null);
} else[19659105]{
dataObjectManager.postMessage(ICcDataObjectManager.INFO, "MongoDB GET call FAILED with a status of " + Integer.toString(mongoDBResponseCode), null);
}
//Read response data
BufferedReader jsonContentReader = null;
[19659069] try {
jsonContentReader = new BufferedReader(new InputStreamReader(mongoDBConnection.getInputStream()));
//Get all the data from the stream
StringBuilder jsonResultStringBuilder = new StringBuilder();
//Read each line of the input to the end
String lineOfJson = null;
while( (lineOfJson = jsonContentReader.readLine()) != null) {
jsonResultStringBuilder.append(lineOfJson);
}
//Convert the entire message payload into a JSONObject
mongoDBCarDocument = new JSONObject(jsonResultStringBuilder.toString());
} finally {
if(jsonContentReader != null) {
jsonContentReader.close();
}
mongoDBConnection.disconnect();
}
//
// Convert our JSON to DataObjects
//
//Get the 'cars' array out of the Document
JSONArray carsJsonArray = mongoDBCarDocument.getJSONArray("Cars");
//Create a data object based off of each car in the array.
for(int carsIndex = 0; carsIndex < carsJsonArray.length(); carsIndex++) {
//Get the JSONObject out of the array
JSONObject currentCar = carsJsonArray.getJSONObject(carsIndex);
//Create the ICcDataObject
ICcDataObject carDataObject = dataObjectManager.createEntity("Car");
Set attributeNames = carDataObject.getAttributeNames();
//Loop through the data object attributes andif they are properties in the JSONObject, set them
for(String currentAttributeName : attributeNames) {
if(currentCar.has(currentAttributeName)) {
//Get the attribute's type
String currentAttributeType = carDataObject.getAttributeDataType(currentAttributeName);
/ /Create a variable for storing the attribute value in
Object currentAttributeValue = null;
//Long
if(currentAttributeType.contains("Long")) {
currentAttribu teValue = new Long(currentCar.getLong(currentAttributeName));
}
//BigDecimal
else if(currentAttributeType.contains("BigDecimal")) {
currentAttributeValue = new BigDecimal(currentCar.getLong(currentAttributeName));
[19659884]}
// // Connectez à MongoDB // // Récupère le gestionnaire de sources de données IDatasourceManager gestionnaire de données = dataObjectManager.getDatasourceManager (); // Récupérer notre source de données MongoDB [19659069]
IDatasource mongoDBDatasource = datasourceManager.getDatasource ( "MongoDB" ); [19659069]
// Ouvrir une connexion http à la base de données
URL mongoDBConnectionURL = nouvelle URL (mongoDBDatasource.getDatasourceConnectionURL ());
HttpURLConnexion mongoDBConnection = (HttpURLConnection) mongoDBConnectionURL.openConnection ();
// Puisque nous lisons, nous voulons ] à utilisent la méthode HTTP 'GET' " title="" data-openoriginalimageonclick="true"/>
mongoDBConnection.setRequestMethod ( "OBTENIR" );
[1945908]
// Servez le type de contenu
[1945909]
mongoDBConnection.setRequestProperty ( "Content-Type" "application / json" );
//Make the connection
mongoDBConnection.connect();
//Print out a rule message on success [19659156]or failure
int mongoDBResponseCode = mongoDBConnection.getResponseCode();
if(mongoDBResponseCode >= 200 && mongoDBResponseCode < 300) {
dataObjectManager.postMessage(ICcDataObjectManager.INFO, "MongoDB GET call SUCCEEDED with a status of " + Integer.toString(mongoDBResponseCode), null);
} else[19659105]{
dataObjectManager.postMessage(ICcDataObjectManager.INFO, "MongoDB GET call FAILED with a status of " + Integer.toString(mongoDBResponseCode), null);
}
//Read response data
BufferedReader jsonContentReader = null;
[19659069] try {
jsonContentReader = new BufferedReader(new InputStreamReader(mongoDBConnection.getInputStream()));
//Get all the data from the stream
StringBuilder jsonResultStringBuilder = new StringBuilder();
//Read each line of the input to the end
String lineOfJson = null;
while( (lineOfJson = jsonContentReader.readLine()) != null) {
jsonResultStringBuilder.append(lineOfJson);
}
//Convert the entire message payload into a JSONObject
mongoDBCarDocument = new JSONObject(jsonResultStringBuilder.toString());
} finally {
if(jsonContentReader != null) {
jsonContentReader.close();
}
mongoDBConnection.disconnect();
}
//
// Convert our JSON to DataObjects
//
//Get the 'cars' array out of the Document
JSONArray carsJsonArray = mongoDBCarDocument.getJSONArray("Cars");
//Create a data object based off of each car in the array.
for(int carsIndex = 0; carsIndex < carsJsonArray.length(); carsIndex++) {
//Get the JSONObject out of the array
JSONObject currentCar = carsJsonArray.getJSONObject(carsIndex);
//Create the ICcDataObject
ICcDataObject carDataObject = dataObjectManager.createEntity("Car");
Set attributeNames = carDataObject.getAttributeNames();
//Loop through the data object attributes andif they are properties in the JSONObject, set them
for(String currentAttributeName : attributeNames) {
if(currentCar.has(currentAttributeName)) {
//Get the attribute's type
String currentAttributeType = carDataObject.getAttributeDataType(currentAttributeName);
/ /Create a variable for storing the attribute value in
Object currentAttributeValue = null;
//Long
if(currentAttributeType.contains("Long")) {
currentAttribu teValue = new Long(currentCar.getLong(currentAttributeName));
}
//BigDecimal
else if(currentAttributeType.contains("BigDecimal")) {
currentAttributeValue = new BigDecimal(currentCar.getLong(currentAttributeName));
[19659884]}
// Ouvrir une connexion http à la base de données URL mongoDBConnectionURL = nouvelle URL (mongoDBDatasource.getDatasourceConnectionURL ()); HttpURLConnexion mongoDBConnection = (HttpURLConnection) mongoDBConnectionURL.openConnection (); // Puisque nous lisons, nous voulons ] à utilisent la méthode HTTP 'GET' " title="" data-openoriginalimageonclick="true"/> mongoDBConnection.setRequestMethod ( "OBTENIR" ); // Servez le type de contenu mongoDBConnection.setRequestProperty ( "Content-Type" "application / json" ); //Make the connection mongoDBConnection.connect(); //Print out a rule message on success [19659156]or failure int mongoDBResponseCode = mongoDBConnection.getResponseCode(); if(mongoDBResponseCode >= 200 && mongoDBResponseCode < 300) { dataObjectManager.postMessage(ICcDataObjectManager.INFO, "MongoDB GET call SUCCEEDED with a status of " + Integer.toString(mongoDBResponseCode), null); } else[19659105]{ dataObjectManager.postMessage(ICcDataObjectManager.INFO, "MongoDB GET call FAILED with a status of " + Integer.toString(mongoDBResponseCode), null); } //Read response data BufferedReader jsonContentReader = null; try { jsonContentReader = new BufferedReader(new InputStreamReader(mongoDBConnection.getInputStream())); //Get all the data from the stream StringBuilder jsonResultStringBuilder = new StringBuilder(); //Read each line of the input to the end String lineOfJson = null; while( (lineOfJson = jsonContentReader.readLine()) != null) { jsonResultStringBuilder.append(lineOfJson); } //Convert the entire message payload into a JSONObject mongoDBCarDocument = new JSONObject(jsonResultStringBuilder.toString()); } finally { if(jsonContentReader != null) { jsonContentReader.close(); } mongoDBConnection.disconnect(); } // // Convert our JSON to DataObjects // //Get the 'cars' array out of the Document JSONArray carsJsonArray = mongoDBCarDocument.getJSONArray("Cars"); //Create a data object based off of each car in the array. for(int carsIndex = 0; carsIndex < carsJsonArray.length(); carsIndex++) { //Get the JSONObject out of the array JSONObject currentCar = carsJsonArray.getJSONObject(carsIndex); //Create the ICcDataObject ICcDataObject carDataObject = dataObjectManager.createEntity("Car"); Set attributeNames = carDataObject.getAttributeNames(); //Loop through the data object attributes andif they are properties in the JSONObject, set them for(String currentAttributeName : attributeNames) { if(currentCar.has(currentAttributeName)) { //Get the attribute's type String currentAttributeType = carDataObject.getAttributeDataType(currentAttributeName); / /Create a variable for storing the attribute value in Object currentAttributeValue = null; //Long if(currentAttributeType.contains("Long")) { currentAttribu teValue = new Long(currentCar.getLong(currentAttributeName)); } //BigDecimal else if(currentAttributeType.contains("BigDecimal")) { currentAttributeValue = new BigDecimal(currentCar.getLong(currentAttributeName)); [19659884]}
//Integer
else if(currentAttributeType.contains("Integer")) {
currentAttributeValue = new Integer(currentCar.getInt(currentAttributeName));
}
//String
else {
currentAttributeValue = currentCar.getString(currentAttributeName);
}
carDataObject.setAttributeValue(currentAttributeName, currentAttributeValue);
}
}
}
}
Step 3
Export the JAR file again and restart Corticon Studio.
Step 4
Create a new Ruleflow to handle retrieval from the database. Put the calculator Rulesheet in the Ruleflow to use the data retrieved from the database.
Source link
