-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement and verify File Integration use cases #27
Comments
Following table shows the list of file integration use cases that needs to be verified and implemented. File Integration Use Cases
|
I have drafted a sample program to implement the functionality of the file listener listening to a local folder. Here when a modification is done to the listening folder, the files are processed and move them to a specified location. import ballerina/file;
import ballerina/log;
import ballerina/internal;
import ballerina/io;
listener file:Listener localFolder = new ({
path: "/Users/nipuni/Documents/Ballerina_Integrator/file-integration/fs",
recursive: false
});
service fileSystem on localFolder {
resource function onModify(file:FileEvent m) {
error? e = localFolder.__start();
log:printInfo("File created");
string source = "/Users/nipuni/Documents/Ballerina_Integrator/file-integration/fs";
string fileNamePattern = "xml";
internal:Path src = new(source);
internal:Path[]|error fileList = src.list();
if (fileList is internal:Path[]) {
foreach (var file in fileList) {
string ext = file.getExtension();
if (ext == fileNamePattern) {
string name = file.getName();
string srcPath = "/Users/nipuni/Documents/Ballerina_Integrator/file-integration/fs";
srcPath = srcPath + "/" + name;
string target = "/Users/nipuni/Documents/Ballerina_Integrator/file-integration/out";
target = target + "/" + name;
internal:Path srcP = new(srcPath);
internal:Path dest = new(target);
processFile(ext, srcPath);
log:printInfo("Processing finished");
error? er = srcP.moveTo(dest);
io:println(er);
log:printInfo("Operation successful");
}
}
}
}
}
// User can define the the processing ( Following is an example).
public function processFile(string ext, string source) {
log:printInfo("Started processing the file ");
if (ext == "xml") {
log:printInfo("Reading xml file");
io:ReadableByteChannel rbc = io:openReadableFile(source);
io:println("Get byte channel");
io:ReadableCharacterChannel rch = new(rbc, "UTF8");
var result = rch.readXml();
if (result is xml) {
json j1 = result.toJSON({
});
}
}
if (ext == "csv") {
log:printInfo("Reading csv file");
io:ReadableCSVChannel rCsvChannel = io:openReadableCsvFile(source);
while (rCsvChannel.hasNext()) {
var records = rCsvChannel.getNext();
if (records is string[]) {
io:println(records);
}
}
}
}
|
Following util functions are implemented to process xml and json files. // Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import ballerina/log;
import ballerina/io;
import ballerina/config;
import ballerina/http;
// Util function to read a json file.
public function readJsonFile(io:ReadableByteChannel result) returns json|error {
io:ReadableCharacterChannel? charChannelResult = getCharChannel(result);
var resultJson = charChannelResult.readJson();
if (resultJson is json) {
io:println("File content: ", resultJson);
return resultJson;
} else {
log:printError("An error occured.", err = resultJson);
return resultJson;
}
}
// Util function to read a xml file.
public function readXmlFile(io:ReadableByteChannel result) returns xml|error? {
io:ReadableCharacterChannel? charChannelResult = getCharChannel(result);
var resultXml = charChannelResult.readXml();
if (resultXml is xml) {
io:println("File content: ", resultXml);
return resultXml;
} else {
log:printError("An error occured.", err = resultXml);
return resultXml;
}
}
// Util function to convert a byte channel to a character channel.
public function getCharChannel(io:ReadableByteChannel getResult) returns io:ReadableCharacterChannel? {
io:ReadableCharacterChannel? charChannel = new io:ReadableCharacterChannel(getResult, "utf-8");
if (charChannel is io:ReadableCharacterChannel) {
return charChannel;
} else {
log:printError("An error occured.");
return;
}
}
|
Update employee_db_service.bal and README.md
Update README.md
Release 0.980.1
Add minor improvements
Updated for 0.991.0 release
The text was updated successfully, but these errors were encountered: