Initialize the UI code
[holmes/rule-management.git] / rulemgt / src / main / frontend / src / public / framework / appserver / api.js
1 /*
2
3     Copyright 2016-2017, Huawei Technologies Co., Ltd.
4
5     Licensed under the Apache License, Version 2.0 (the "License");
6     you may not use this file except in compliance with the License.
7     You may obtain a copy of the License at
8
9             http://www.apache.org/licenses/LICENSE-2.0
10
11     Unless required by applicable law or agreed to in writing, software
12     distributed under the License is distributed on an "AS IS" BASIS,
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14     See the License for the specific language governing permissions and
15     limitations under the License.
16
17 */
18
19 var express = require('express');
20 var router = express.Router();
21 /*var js2xmlparser = require("js2xmlparser");
22 var xml2js = require('xml2js');*/
23 var fs = require('fs');
24 var http = require('http');
25 var bodyParser = require('body-parser');
26 var path=require('path');
27
28 var MongoClient = require('mongodb').MongoClient;
29 var ObjectId = require('mongodb').ObjectId;
30
31 var url = 'mongodb://localhost/rest_test';
32
33 /*var exists = require('file-exists');*/
34
35 // ############################################  Sign In code #################################################
36 // Routes
37 router.post('/signin', function (req, res) {
38         console.log("signin");
39         var obj = {
40                 "name": req.body.name,
41                 "pswd": req.body.pswd
42         };
43
44         var ObjLst =[] ;
45         var nameExist =0;
46         console.log(obj);
47         ObjLst = JSON.parse(fs.readFileSync('input.json', 'utf8'));
48         for(var index = 0; index < ObjLst.length; index++) {
49                 if (ObjLst[index].name === req.body.name && ObjLst[index].pswd === req.body.pswd) {
50                         console.log("success");
51                         nameExist++;
52                         res.statusCode=200;
53                         res.statusMessage="Sucessfully loged in";
54                         break;
55                 }
56         }
57         if (nameExist != 1) {
58                 console.log("failed");
59                 res.statusCode=404;
60                 res.statusMessage="failed to login";
61         }
62         res.send();
63 });
64
65 // ############################################  Sign Up Code #################################################
66
67 router.post('/signup', function (req, res) {
68
69         console.log("in api.js");
70
71         var ObjLst =[] ;
72         var nameNotExist =0;
73         var obj = {
74                 "name": req.body.name,
75                 "pswd": req.body.pswd,
76                 "email": req.body.email
77         };
78         if(fs.existsSync("input.json")) {
79                 console.log("File Exist");
80                 ObjLst = JSON.parse(fs.readFileSync('input.json', 'utf8'));
81         }
82
83         for(var index = 0; index < ObjLst.length; index++) {
84                 if (ObjLst[index].name === req.body.name && ObjLst[index].email === req.body.email) {
85                         console.log("success");
86                         nameNotExist++;
87                         break;
88                 }
89         }
90         if (nameNotExist == 0) {
91                 ObjLst.push(obj);
92                 console.log(ObjLst);
93                 fs.writeFile('input.json', JSON.stringify(ObjLst),  function(err) {
94                         if (err) {
95                                 return console.error(err);
96                         }
97                         console.log("Data written successfully!");
98                         console.log("Let's read newly written data");
99                         res.statusCode=200;
100                         res.statusMessage="Sucessfully signed up";
101                 });
102         }
103         res.send();
104 });
105
106 function getDataFrmProvince() {
107     var provinceData =[] ;
108     if(fs.existsSync("provinceData.json")) {
109         console.log("File Exist");
110         provinceData = JSON.parse(fs.readFileSync('provinceData.json', 'utf8'));
111     }
112     return provinceData;
113 }
114
115 function saveDataToProvince(provinceData) {
116     fs.writeFile('provinceData.json', JSON.stringify(provinceData),  function(err) {
117         var statueCode = 0;
118         if (err) {
119             console.error(err);
120             return statueCode = 404;
121         }
122         console.log("Data written successfully!");
123         console.log("Let's read newly written data");
124         return statueCode = 200;
125     });
126 }
127
128 function getIndexOfIdProvince(id) {
129     var provinceData = getDataFrmProvince();
130     var returnIndx = -1;
131     for (var index = 0; index < provinceData.length; index++) {
132         if(provinceData[index].id == id) {
133             returnIndx = index;
134             break;
135         }
136     }
137     return returnIndx;
138 }
139
140 function deleteIdFromProvince(idList) {
141     var provinceData = getDataFrmProvince();
142     /*for (var index = 0; index < provinceData.length; index++) {
143         if(provinceData[index].id == id) {
144             console.log("Deleting id : " + index);
145             provinceData.splice(index, 1);
146             break;
147         }
148     }*/
149     for(var i = 0; i < idList.length; i++) {
150         for (var index = 0; index < provinceData.length; index++) {
151             if(provinceData[index].id == idList[i]) {
152                 console.log("Deleting id : " + index);
153                 provinceData.splice(index, 1);
154                 break;
155             }
156         }
157     }
158     return provinceData;
159 }
160
161 router.get('/getAllProvinceData', function (req, res) {
162     var provinceData = getDataFrmProvince();
163     if(provinceData) {
164         output = '{"provinceData" : ' + JSON.stringify(provinceData) + '}';
165
166         res.setHeader("Content-Type", "application/json");
167         console.log("output : " + JSON.stringify(output));
168         res.end(output);
169     }
170     /*MongoClient.connect(url, function(err, db) {
171         console.log("Connected... :-)");
172         var cursor = db.collection('ProvinceData').find({},function(err, cursor) {
173             cursor.toArray(function (err, items) {
174                 output = '{"provinceData" : ' + JSON.stringify(items) + '}';
175
176                 res.setHeader("Content-Type", "application/json");
177                 //console.log("output : " + JSON.stringify(output));
178                 res.end(output);
179             });
180         });
181         db.close(function(){
182             console.log("Connection Closed... :-)");
183         });
184     });*/
185 });
186
187
188 router.post('/addProvinceData', function(req, res) {
189     var provinceData = getDataFrmProvince();
190     var gen_id = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000;
191     provinceData.push({
192         "id": gen_id,
193         "province_name": req.body.province_name,
194         "ip": req.body.ip,
195         "port": req.body.port
196     });
197     var statusCode = saveDataToProvince(provinceData);
198     res.statusCode=statusCode;
199
200     if(statusCode == 200) {
201         res.statusMessage="Sucessfully signed up";
202     }
203     else {
204         res.statusMessage="Error";
205     }
206
207     /*MongoClient.connect(url, function(err, db) {
208         db.collection('ProvinceData').insertOne({
209             province_name: req.body.province_name,
210             ip: req.body.ip,
211             port: req.body.port
212         });
213         db.close();
214     });*/
215     res.statusCode=200;
216     res.statusMessage="Sucessfully signed up";
217     res.send();
218 });
219
220 router.post('/deleteProvinceData', function (req, res) {
221     //var provinceData = getDataFrmProvince();
222     console.log("IdList: "+ req.body.idList);
223     /*for(var i = req.body.idList.length - 1; i >= 0; i--) {
224         var index = getIndexOfIdProvince(req.body.idList[i]);
225         console.log("Deleting id : " + index);
226         if(index != -1) {
227             provinceData.splice(index, 1);
228         }
229         //deleteIdFromProvince(req.body.idList[i]);
230     };*/
231
232     var provinceData = deleteIdFromProvince(req.body.idList);
233
234     saveDataToProvince(provinceData);
235
236     /*MongoClient.connect(url, function(err, db) {
237
238         console.log("Deleting Province Data... " + req.body.idList);
239         for(var i = 0; i < req.body.idList.length; i++) {
240             db.collection('ProvinceData').deleteOne({ "_id": ObjectId(req.body.idList[i])});
241         }
242         /!*db.collection('ProvinceData').deleteOne({ "_id": ObjectId(req.body.idList)});*!/
243
244         db.close(function(){
245             console.log("Connection Closed... :-)");
246         });
247         res.send();
248     });*/
249     res.statusCode=200;
250     res.statusMessage="Sucessfully signed up";
251     res.send();
252 });
253
254 router.post('/editProvinceData', function (req, res) {
255     var provinceData = getDataFrmProvince();
256     var index = getIndexOfIdProvince(req.body.id);
257     console.log("Editing id : " + index);
258     if(index != -1) {
259         provinceData[index].province_name = req.body.province_name;
260         provinceData[index].ip = req.body.ip;
261         provinceData[index].port = req.body.port;
262     }
263     saveDataToProvince(provinceData);
264
265     res.statusCode=200;
266     res.statusMessage="Sucessfully signed up";
267     res.send();
268     /*MongoClient.connect(url, function(err, db) {
269
270         console.log("Editing Province Data... " + req.body._id);
271         db.collection('ProvinceData').updateOne(
272             { "_id": ObjectId(req.body._id)},
273             {
274                 $set: {'province_name': req.body.province_name, 'ip': req.body.ip, 'port': req.body.port}
275             }
276         );
277
278         db.close(function(){
279             console.log("Connection Closed... :-)");
280         });
281         res.send();
282     });*/
283 });
284
285 // Return router
286 module.exports = router; 
287