summaryrefslogtreecommitdiffstats
path: root/catalog-ui/app/scripts/models/components/resource.ts
blob: 243ef3463ccb7ce84e8907cb90d24cedd36a2542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed 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.
 * ============LICENSE_END=========================================================
 */
/**
 * Created by obarda on 2/3/2016.
 */
/// <reference path="../../references"/>
module Sdc.Models.Components {
    'use strict';

    export class Resource extends Component {

        public interfaces: any;
        public derivedFrom:Array<string>;
        public componentService: Services.Components.IResourceService;
        public resourceType:string;
        public payloadData:string;
        public payloadName:string;
        public importedFile: Sdc.Directives.FileUploadModel;

        // Onboarding parameters
        public csarUUID:string;
        public csarVersion:string;
        public csarPackageType:string;
        public packageId:string;

        constructor(componentService: Services.Components.IResourceService, $q: ng.IQService, component?:Resource) {
            super(componentService, $q, component);
            if(component) {

                this.interfaces = component.interfaces;
                this.derivedFrom = component.derivedFrom;
                this.payloadData = component.payloadData ? component.payloadData : undefined;
                this.payloadName = component.payloadName ? component.payloadName : undefined;
                this.resourceType = component.resourceType;
                this.csarUUID = component.csarUUID;
                this.csarVersion = component.csarVersion;
                this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version + ' ' + this.resourceType;

                if (component.categories && component.categories[0] && component.categories[0].subcategories && component.categories[0].subcategories[0]) {
                    component.mainCategory = component.categories[0].name;
                    component.subCategory = component.categories[0].subcategories[0].name;
                    this.selectedCategory =  component.mainCategory +  "_#_" + component.subCategory;
                    this.importedFile = component.importedFile;
                }
            } else {
                this.resourceType = Utils.Constants.ResourceType.VF;
            }

            this.componentService = componentService;
            this.iconSprite = "sprite-resource-icons";
        }

        public getComponentSubType = ():string => {
            return this.resourceType;
        };

        public isComplex = ():boolean => {
            return this.resourceType === Utils.Constants.ResourceType.VF;
        };

        public isVl = ():boolean => {
            return Utils.Constants.ResourceType.VL == this.resourceType;
        };

        public isCsarComponent = ():boolean => {
          return !!this.csarUUID;
        };

        public createComponentOnServer = ():ng.IPromise<Models.Components.Component> => {
            let deferred = this.$q.defer();
            let onSuccess = (component:Models.Components.Resource):void  => {
                this.payloadData = undefined;
                this.payloadName = undefined;
                deferred.resolve(component);
            };
            let onError = (error:any):void => {
                deferred.reject(error);
            };

            this.handleTags();
            if(this.importedFile){
                this.payloadData = this.importedFile.base64;
                this.payloadName = this.importedFile.filename;
            }
            this.componentService.createComponent(this).then(onSuccess, onError);
            return deferred.promise;
        };

        /* we need to change the name of the input to vfInstanceName + input name before sending to server in order to create the inputs on the service
         *  we also need to remove already selected inputs (the inputs that already create on server, and disabled in the view - but they are selected so they are still in the view model
         */
        public createInputsFormInstances = (instanceInputsPropertiesMap:Models.InstanceInputsPropertiesMapData):ng.IPromise<Array<Models.InputModel>> => {
            let deferred = this.$q.defer();
            /*
            let instanceInputsPropertiesMapToCreate: Models.InstanceInputsPropertiesMapData = new Models.InstanceInputsPropertiesMapData();
            _.forEach(instanceInputsPropertiesMap, (properties:Array<Models.PropertyModel>, instanceId:string) => {

                if(properties && properties.length > 0) {
                    let componentInstance:Models.ComponentsInstances.ComponentInstance = _.find(this.componentInstances, (instace:Models.ComponentsInstances.ComponentInstance) => {
                        return instace.uniqueId === instanceId;
                    });

                    instanceInputsPropertiesMapToCreate[instanceId] = new Array<Models.PropertyModel>();
                    _.forEach(properties, (property:Models.PropertyModel) => {

                        if(!property.isAlreadySelected) {
                            let newInput = new Models.PropertyModel(property);
                            newInput.name = componentInstance.normalizedName + '_' + property.name;
                            instanceInputsPropertiesMapToCreate[instanceId].push(newInput);
                        }
                    });
                    if( instanceInputsPropertiesMapToCreate[instanceId].length === 0) {
                        delete instanceInputsPropertiesMapToCreate[instanceId];
                    }
                } else {
                    delete instanceInputsPropertiesMapToCreate[instanceId];
                }
            });

            if(Object.keys(instanceInputsPropertiesMapToCreate).length > 0) {
                let deferred = this.$q.defer();
                let onSuccess = (propertiesCreated: Array<Models.PropertyModel>):void => {
                    this.inputs = propertiesCreated.concat(this.inputs);
                    deferred.resolve(propertiesCreated);
                };
                let onFailed = (error:any): void => {
                    deferred.reject(error);
                };
                this.componentService.createInputsFromInstancesInputsProperties(this.uniqueId, new Models.InstanceInputsPropertiesMap(instanceInputsPropertiesMapToCreate)).then(onSuccess, onFailed);
            }
             */
            return deferred.promise;
        };

        // we need to change the name of the input to vfInstanceName + input name before sending to server in order to create the inputs on the service
        public getResourceInputInputs = (inputId:string):ng.IPromise<Array<Models.InputModel>> => {
            let deferred = this.$q.defer();
            let onSuccess = (inputInputs: Array<Models.InputModel>):void => {
                let input: Models.InputModel = _.find(this.inputs, (input:Models.InputModel) => {
                    return input.uniqueId === inputId;
                });
                input.inputs = inputInputs;
                deferred.resolve(inputInputs);
            };
            let onFailed = (error:any): void => {
                deferred.reject(error);
            };
            this.componentService.getComponentInputInputs(this.uniqueId, inputId).then(onSuccess, onFailed);
            return deferred.promise;
        };

        public toJSON = ():any => {
            this.componentService = undefined;
            this.filterTerm = undefined;
            this.iconSprite = undefined;
            this.mainCategory = undefined;
            this.subCategory = undefined;
            this.selectedInstance = undefined;
            this.showMenu = undefined;
            this.$q = undefined;
            this.selectedCategory = undefined;
            this.importedFile = undefined;
            return this;
        };
    }
}