added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / services / bpmn-validate / bpmn-validate.class.js
diff --git a/otf-frontend/server/src/feathers/services/bpmn-validate/bpmn-validate.class.js b/otf-frontend/server/src/feathers/services/bpmn-validate/bpmn-validate.class.js
new file mode 100644 (file)
index 0000000..8b969bc
--- /dev/null
@@ -0,0 +1,297 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+const axios = require('axios');\r
+const Response = require('http-response-object');\r
+const logger = require('../../../lib/logger');\r
+const util = require('../../../lib/otf-util');\r
+const beautify = require('json-beautify');\r
+const Bpmn = require('./bpmn.class');\r
+const Readable = require('stream').Readable;\r
+const request = require('request');\r
+\r
+class Service {\r
+       constructor (options) {\r
+               this.options = options || {};\r
+       }\r
+\r
+       async find (params) {\r
+               return [];\r
+       }\r
+\r
+       // Check process definition key to see if unique\r
+       async get (id, params) {\r
+               let errors = {};\r
+               // Get List of Definition keys from Camunda\r
+               let options = {\r
+                       url: this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,\r
+                       headers: {\r
+                               'Authorization': 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')\r
+                       },\r
+                       rejectUnauthorized: false\r
+               };\r
+\r
+               return await new Promise(async (resolve, reject) => {\r
+                       request.get(options, (err, response, body) => {\r
+                               if(err){\r
+                                       reject(err);\r
+                               }\r
+                               resolve(response);\r
+                       });\r
+               })\r
+               .then(\r
+                       result => {\r
+                               if (result.statusCode == 200) {\r
+                                       //check to make sure they have access\r
+                                       params.query.$limit = '-1';\r
+                                       params.query.processDefinitionKey = id;\r
+                                       return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(params).then(\r
+                                               res => {\r
+                                                       if(res.length > 0){\r
+                                                               return new Response(200, {}, res);\r
+                                                       }else{\r
+                                                               let resp = new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}});\r
+                                                               return resp;\r
+                                                       }\r
+                                               }\r
+                                       );\r
+                               }else{\r
+                                       return new Response(200, {});\r
+                               }\r
+                       }\r
+               ).catch(err => {\r
+                       return new Response(400, {});\r
+               });\r
+\r
+               // return await axios.get(\r
+               //      this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,\r
+               //      {\r
+               //              headers: {\r
+               //                      Authorization: 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')\r
+               //              }\r
+               //      })\r
+               //      .then(result => {\r
+               //              console.log(result);\r
+               //              if (result.status === 200) {\r
+               //                      //check to make sure they have access\r
+               //                      params.query.$limit = '-1';\r
+               //                      params.query.processDefinitionKey = id;\r
+               //                      return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(params).then(\r
+               //                              res => {\r
+               //                                      console.log('res 1');\r
+               //                                      console.log(res);\r
+               //                                      if(res.length > 0){\r
+               //                                              return new Response(200, {}, res);\r
+               //                                      }else{\r
+\r
+               //                                              console.log('err 1');\r
+               //                                              let resp = new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}});\r
+               //                                              console.log(resp);\r
+               //                                              return resp;\r
+               //                                      }\r
+               //                              }\r
+               //                      );\r
+               //              }else{\r
+               //                      console.log('not 200')\r
+               //                      return new Response(400, {}, {errors: errors});\r
+               //              }\r
+               //      })\r
+               //      .catch(err => {\r
+               //              return new Response(200, {});\r
+               //      });\r
+       }\r
+       // async get (id, params) {\r
+       //      console.log("bpmn-upload: get")\r
+       //      let errors = {};\r
+       //      // Get List of Definition keys from Camunda\r
+\r
+       //      // let options = {\r
+       //      //      url: this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,\r
+       //      //      headers: {\r
+       //      //              'Authorization': 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')\r
+       //      //      },\r
+       //      //      rejectUnauthorized: false\r
+       //      // }\r
+\r
+       //      // return await new Promise((resolve, reject) => {\r
+       //      //      request.post(options, (err, res, body) => {\r
+       //      //              if(err){\r
+       //      //                      reject(err);\r
+       //      //              }\r
+       //      //              resolve(res);\r
+       //      //      });\r
+       //      // }).then(\r
+       //      //      result => {\r
+       //      //              console.log(result);\r
+       //      //              if (result.statusCode === 200) {\r
+       //      //                      //check to make sure they have access\r
+       //      //                      params.query.$limit = '-1';\r
+       //      //                      params.query.processDefinitionKey = id;\r
+       //      //                      return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(params).then(\r
+       //      //                              res => {\r
+       //      //                                      return new Response(200, {}, res);\r
+       //      //                              },\r
+       //      //                              err => {\r
+       //      //                                      return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})\r
+       //      //                              }\r
+       //      //                      );\r
+       //      //              }else if(result.statusCode == 404){\r
+       //      //                      return new Response(400, {}, {errors: errors});\r
+       //      //              }else{\r
+       //      //                      return new Response(result.statusCode, {}, {errors: errors});\r
+       //      //              }\r
+       //      //      }\r
+       //      // ).catch(\r
+       //      //      err => {\r
+       //      //              console.log("Err: " + err)\r
+       //      //              //return new Response(200, {});\r
+       //      //              let newParams = Object.assign({}, params);\r
+       //      //              newParams.query.$limit = -1;\r
+       //      //              newParams.query.processDefinitionKey = id;\r
+       //      //              //console.log(params);\r
+       //      //              return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(newParams).then(\r
+       //      //                      res => {\r
+       //      //                              //return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})\r
+       //      //                              return new Response(200, {}, res);\r
+       //      //                      },\r
+       //      //                      err => {\r
+       //      //                              return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})\r
+       //      //                      }\r
+       //      //              );\r
+       //      //      }\r
+       //      // );\r
+\r
+       //      return await axios.get(\r
+       //              this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,\r
+       //              {\r
+       //                      headers: {\r
+       //                              Authorization: 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')\r
+       //                      }\r
+       //              })\r
+       //              .then(result => {\r
+       //                      console.log(result);\r
+       //                      if (result.status === 200) {\r
+       //                              console.log('in here')\r
+       //                              //check to make sure they have access\r
+       //                              params.query.$limit = '-1';\r
+       //                              params.query.processDefinitionKey = id;\r
+       //                              return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(params).then(\r
+       //                                      res => {\r
+       //                                              return new Response(200, {}, res);\r
+       //                                      }\r
+       //                              ).catch(err => {\r
+       //                                      console.log('err')\r
+       //                                      return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})\r
+       //                              });\r
+       //                      }else if(result.status === 404){\r
+       //                              console.log('or here')\r
+       //                              return new Response(400, {}, {errors: errors});\r
+       //                      }else{\r
+       //                              return new Response(result.status, {}, {errors: errors});\r
+       //                      }\r
+       //              })\r
+       //              .catch(err => {\r
+       //                      console.log("Err: " + err)\r
+       //                      //return new Response(200, {});\r
+       //                      let newParams = Object.assign({}, params);\r
+       //                      newParams.query.$limit = -1;\r
+       //                      newParams.query.processDefinitionKey = id;\r
+       //                      //console.log(params);\r
+       //                      return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(newParams).then(\r
+       //                              res => {\r
+       //                                      //return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})\r
+       //                                      return new Response(200, {}, res);\r
+       //                              }\r
+       //                      ).catch(err => {\r
+       //                              console.log('err 2')\r
+       //                              return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})\r
+       //                      });\r
+       //              });\r
+       // }\r
+\r
+       async create (data, params) {\r
+               let bpmn = new Bpmn(this.options.app, data, params);\r
+               return await bpmn.validate();\r
+       }\r
+\r
+       //validates then saves bpmn file and returns file meta data\r
+       async update (id, data, params) {\r
+               let bpmn = new Bpmn(this.options.app, data, params);\r
+               let res = await bpmn.validate();\r
+               if(res.statusCode != 200){\r
+                       return res;\r
+               }\r
+               \r
+               let b = new Buffer(res.body.bpmnXml);\r
+               let r = new Readable();\r
+               r.push(b);\r
+               r.push(null);\r
+               //save new bpmn file and return\r
+               let formData = {\r
+                       'file': {\r
+                               value: r.read(),\r
+                               options: {\r
+                                       filename: res.body.processDefinitionKey + '.bpmn'\r
+                               }\r
+                       }\r
+               };\r
+               let options = {\r
+                       url: 'https://localhost/' + this.options.app.get('base-path') + 'file-transfer',\r
+                       headers: {\r
+                               'Authorization': params.headers.Authorization,\r
+                               'Content-Type': "multipart/form-data"\r
+                       },\r
+                       rejectUnauthorized: false,\r
+                       formData: formData\r
+               }\r
+\r
+               return await new Promise((resolve, reject) => {\r
+                       request.post(options, (err, res, body) => {\r
+                               if(err){\r
+                                       reject(err);\r
+                               }\r
+                               resolve(body);\r
+                       });\r
+               }).then(\r
+                       result => {\r
+                               return result;\r
+                       }\r
+               ).catch(\r
+                       err => {\r
+                               return err;\r
+                       }\r
+               );\r
+\r
+       }\r
+\r
+       async patch (id, data, params) {\r
+               return data;\r
+       }\r
+\r
+       async remove (id, params) {\r
+               return { id };\r
+       }\r
+\r
+       async parseAndUpload (data, params, method) {\r
+\r
+       }\r
+}\r
+\r
+module.exports = function (options) {\r
+       return new Service(options);\r
+};\r
+\r
+module.exports.Service = Service;\r