CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / utils / artifacts-utils.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import * as _ from "lodash";
22 import {ArtifactModel} from "../models/artifacts";
23 import {IArtifactResourceFormViewModelScope} from "../view-models/forms/artifact-form/artifact-form-view-model";
24 import {Component} from "../models/components/component";
25 import {ArtifactGroupType, ArtifactType} from "./constants";
26
27 export class ArtifactsUtils {
28
29     static '$inject' = [
30         '$filter'
31     ];
32
33     constructor(private $filter:ng.IFilterService) {
34
35     }
36
37     public getArtifactTypeByState(currentState:string):string {
38         switch (currentState) {
39             case "workspace.composition.lifecycle":
40                 return "interface";
41             case "workspace.composition.api":
42                 return "api";
43             case "workspace.deployment_artifacts":
44             case "workspace.composition.deployment":
45                 return "deployment";
46             case "workspace.composition.artifacts":
47                 return "informational";
48             default:
49                 return "informational";
50         }
51     }
52
53     public getTitle(artifactType:string, selectedComponent:Component):string {
54         switch (artifactType) {
55             case "interface":
56                 return "Lifecycle Management";
57             case "api":
58                 return "API Artifacts";
59             case "deployment":
60                 return "Deployment Artifacts";
61             case "informational":
62                 return "Informational Artifacts";
63             default:
64                 if (!selectedComponent) {
65                     return "";
66                 } else {
67                     return this.$filter("resourceName")(selectedComponent.name) + ' Artifacts';
68                 }
69         }
70     }
71
72     public setArtifactType = (artifact:ArtifactModel, artifactType:string):void => {
73         switch (artifactType) {
74             case "api":
75                 artifact.artifactGroupType = ArtifactGroupType.SERVICE_API;
76                 break;
77             case "deployment":
78                 artifact.artifactGroupType = ArtifactGroupType.DEPLOYMENT;
79                 break;
80             default:
81                 artifact.artifactGroupType = ArtifactGroupType.INFORMATION;
82                 break;
83         }
84     };
85
86     public isLicenseType = (artifactType:string):boolean => {
87         let isLicense:boolean = false;
88
89         if (ArtifactType.VENDOR_LICENSE === artifactType || ArtifactType.VF_LICENSE === artifactType) {
90             isLicense = true;
91         }
92
93         return isLicense;
94     };
95
96     public removeArtifact = (artifact:ArtifactModel, artifactsArr:Array<ArtifactModel>):void => {
97
98         if (!artifact.mandatory && (ArtifactGroupType.INFORMATION == artifact.artifactGroupType ||
99             ArtifactGroupType.DEPLOYMENT == artifact.artifactGroupType)) {
100             _.remove(artifactsArr, {uniqueId: artifact.uniqueId});
101         }
102         else {
103             let artifactToDelete = _.find(artifactsArr, {uniqueId: artifact.uniqueId});
104
105             delete artifactToDelete.esId;
106             delete artifactToDelete.description;
107             delete artifactToDelete.artifactName;
108             delete artifactToDelete.apiUrl;
109         }
110     };
111
112     public addAnotherAfterSave(scope:IArtifactResourceFormViewModelScope) {
113         let newArtifact = new ArtifactModel();
114         this.setArtifactType(newArtifact, scope.artifactType);
115         scope.editArtifactResourceModel.artifactResource = newArtifact;
116
117         scope.forms.editForm['description'].$setPristine();
118         if (scope.forms.editForm['artifactLabel']) {
119             scope.forms.editForm['artifactLabel'].$setPristine();
120         }
121         if (scope.forms.editForm['type']) {
122             scope.forms.editForm['type'].$setPristine();
123         }
124
125     }
126 }