contracts, index, rootGBPcontroller
[groupbasedpolicy.git] / groupbasedpolicy-ui / module / src / main / resources / gbp / contract / contract.service.js
1 define([], function () {
2     'use strict';
3
4     angular.module('app.gbp').service('ContractService', ContractService);
5
6     ContractService.$inject = ['Restangular'];
7
8     function ContractService(Restangular) {
9         /* methods */
10         this.createObject = createObject;
11
12         /**
13          * Contract constructor
14          * @constructor
15          */
16         function Contract() {
17             /* properties */
18             this.data = {};
19             /* methods */
20             this.setData = setData;
21             this.get = get;
22
23             /* Implementation */
24             /**
25              * fills Contract object with data
26              * @param data
27              */
28             function setData(data) {
29                 this.data.id = data.id;
30                 this.data.description = data.description;
31                 this.data.parent = data.parent;
32
33                 // TODO: use objects
34                 this.data['forwarding-context'] = data['forwarding-context'];
35                 this.data.target = data.target;
36                 this.data.subject = data.subject;
37                 this.data.clause = data.clause;
38                 this.data.quality = data.quality;
39             }
40
41             /**
42              * gets one Contract object from Restconf
43              * @param id
44              * @returns {*}
45              */
46             function get(id) {
47                 var self = this;
48
49                 // var restObj = Restangular.one('restconf').one('config').one('policy:tenants')
50                 //             .one('tenant').one('tenant1').one('policy').one('contract').one(this.data.id || id);
51
52                 var restObj = Restangular.one('restconf').one('config').one('policy:tenants')
53                             .one('tenant').one(id).one('policy').one('contract').one(id);
54
55                 return restObj.get().then(function (data) {
56                     self.setData(data.contract[0]);
57                 });
58             }
59         }
60
61         /**
62          * creates Contract object and fills it with data if available
63          * @param data
64          * @returns {Contract}
65          */
66         function createObject(data) {
67             var obj = new Contract();
68
69             if (data) {
70                 obj.setData(data);
71             }
72
73             return obj;
74         }
75     }
76
77     return ContractService;
78 });