summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/models/components/resource.ts
blob: e594451e1a7f345282b050d849f51d3bef0591e2 (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
/**
 * Created by obarda on 2/3/2016.
 */
'use strict';
import {InstancesInputsOrPropertiesMapData} from "../instance-inputs-properties-map";
import {PropertyModel} from "../properties";
import {DisplayModule} from "../modules/base-module";
import {InputModel} from "../inputs";
import {ResourceType} from "../../utils/constants";
import {Component} from "./component";
import {FileUploadModel} from "../../directives/file-upload/file-upload";
import {IResourceService} from "../../services/components/resource-service";
import {ComponentMetadata} from "../component-metadata";

export class Resource extends Component {

    public interfaces:any;
    public derivedFrom:Array<string>;
    public componentService:IResourceService;
    public resourceType:string;
    public payloadData:string;
    public payloadName:string;
    public importedFile:FileUploadModel;

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

    constructor(componentService: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 = ResourceType.VF;
        }

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

    public setComponentMetadata(componentMetadata: ComponentMetadata) {
        super.setComponentMetadata(componentMetadata);
        this.resourceType = componentMetadata.resourceType;
        this.csarUUID = componentMetadata.csarUUID;
        this.csarVersion = componentMetadata.csarVersion;
        this.derivedFrom = componentMetadata.derivedFrom;
        this.setComponentDisplayData();
    };

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

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

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

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

    public createComponentOnServer = ():ng.IPromise<Component> => {
        let deferred = this.$q.defer();
        let onSuccess = (component: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;
    };


    public updateResourceGroupProperties = (module:DisplayModule, properties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>> => {
        let deferred = this.$q.defer();
        let onSuccess = (updatedProperties:Array<PropertyModel>):void => {
            _.forEach(updatedProperties, (property:PropertyModel) => { // Replace all updated properties on the module we needed to update
                _.extend(_.find(module.properties, {uniqueId: property.uniqueId}), property);

            });
            //_.extend(_.findWhere(this.groups, {uniqueId: module.uniqueId }), module); // replace the module on the component so all data will be updates if the module sent to the function is a copy
            deferred.resolve(updatedProperties);
        };
        let onError = (error:any):void => {
            deferred.reject(error);
        };

        this.componentService.updateResourceGroupProperties(this.uniqueId, module.uniqueId, properties).then(onSuccess, onError);
        return deferred.promise;
    };

    // For now we only implement the logic in service level
    public createInputsFormInstances = (instanceInputsPropertiesMap:InstancesInputsOrPropertiesMapData):ng.IPromise<Array<InputModel>> => {
        let deferred = this.$q.defer();
        return deferred.promise;
    };

    getTypeUrl():string {
        return 'resources/';
    }


    setComponentDisplayData():void {
        this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version + ' ' + this.resourceType;
        if (this.categories && this.categories[0] && this.categories[0].subcategories && this.categories[0].subcategories[0]) {
            this.mainCategory = this.categories[0].name;
            this.subCategory = this.categories[0].subcategories[0].name;
            this.selectedCategory = this.mainCategory + "_#_" + this.subCategory;
            this.iconSprite = "sprite-resource-icons";
        }
    };

    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;
    };
}