Searching for files with REST API
The reportNeedsReRun function searches to see if the report for the selected state already exists using the following code:
var fileName = "/Home/" + region + "/" + state + ".rptdocument";
console.log("region " + "= " + region);
console.log("state " + "= " + state);
params = {
search : fileName,
authId: $rootScope.AuthId
};
API.DownloadReport.query(params,
function (dataResponse) {
if (dataResponse.TotalCount > 0) {
A successful data set search returns a response body similar to the following:
{
"ItemList": {
"File": [
{
"Id": "320610000100",
"Name": "WA.rptdocument",
"FileType": "RPTDOCUMENT",
"PageCount": "1",
"Size": "2369202",
"Version": "1"
}
]
},
"TotalCount": "1"
}
If the file exists, it is queried for meta-data about the file with the following code:
if (dataResponse.TotalCount > 0) {
params = {
fileId : dataResponse.ItemList.File[dataResponse.ItemList.File.length - 1].Id,
authId: $rootScope.AuthId
};
API.Files.query(params,
function (dataResponse) {
var ts = dataResponse.File.TimeStamp;
$scope.timestamp = "Generated " + $filter('date')(new Date(ts), "MM/dd/yyyy 'at' h:mma");
A successful data set request returns a response body similar to the following:
{
"File": {
"Id": "320610000100",
"Name": "/Home/West/WA.rptdocument",
"FileType": "RPTDOCUMENT",
"PageCount": "1",
"Size": "2369202",
"TimeStamp": "2014-11-11T15:55:24.000Z",
"Version": "1",
"Owner": "flightdemo"
},
"ACL": {},
"ArchiveRules": {}
}
If the report time stamp validates within the report refresh time, then the visualizations of the report load into the application. If the report is older than the report refresh time, it is run again. The following code shows this validation check:
if((new Date().getTime() - new Date(ts).getTime()) > mainAppCtrl.report_refresh_Time * 60 * 1000 ) {
$scope.executeReport(fileName,region, state);
} else {
$scope.loadBookmarks(fileName);
}
},
See the source code for the complete example.