Fix mod ui build issues
[dcaegen2/platform.git] / mod2 / ui / src / app / ms-instance-add / ms-instance-add.component.ts
1 /* 
2  *  # ============LICENSE_START=======================================================
3  *  # Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
4  *  # ================================================================================
5  *  # Licensed under the Apache License, Version 2.0 (the "License");
6  *  # you may not use this file except in compliance with the License.
7  *  # You may obtain a copy of the License at
8  *  #
9  *  #      http://www.apache.org/licenses/LICENSE-2.0
10  *  #
11  *  # Unless required by applicable law or agreed to in writing, software
12  *  # distributed under the License is distributed on an "AS IS" BASIS,
13  *  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  # See the License for the specific language governing permissions and
15  *  # limitations under the License.
16  *  # ============LICENSE_END=========================================================
17  */
18
19 import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
20 import { MicroserviceInstanceService } from '../services/microservice-instance.service';
21 import { MessageService } from 'primeng/api';
22 import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
23 import { AuthService } from '../services/auth.service';
24 import { DatePipe } from '@angular/common';
25
26 @Component({
27   selector: 'app-ms-instance-add',
28   templateUrl: './ms-instance-add.component.html',
29   styleUrls: ['./ms-instance-add.component.css']
30 })
31 export class MsInstanceAddComponent implements OnInit {
32
33   guiHeader: string = "Microservice Instance ADD";
34   // Used for the Add/Update button label
35   addOrUpdate: string = "Add";
36
37   msInstanceAddForm: FormGroup;
38   msInstanceToAdd: AddMsInstance;
39   addInstanceTo: string = "";
40   username: string;
41   msInstanceReleases: { label: string, value: string }[] = [
42     { label: '2004', value: '2004' },
43     { label: '2006', value: '2006' },
44     { label: '2008', value: '2008' },
45     { label: '2009', value: '2009' },
46     { label: '2010', value: '2010' },
47     { label: '2011', value: '2011' },
48     { label: '2012', value: '2012' }
49   ]
50
51   @Input() visible: boolean;
52   @Input() msName: string;
53   @Input() msInstanceChange: string;     // Use to differentiate Add from Change, since currentRow can be problematic
54   @Input() currentRow: any;
55   @Output() handler: EventEmitter<any> = new EventEmitter();
56
57   constructor(private addChangeMsInstanceApi: MicroserviceInstanceService, private messageService: MessageService, private fb: FormBuilder, private authService: AuthService, private datePipe: DatePipe) { }
58
59   ngOnInit() {
60     this.username = this.authService.getUser().username;
61
62     this.msInstanceAddForm = new FormGroup({
63       name: new FormControl(),
64       release: new FormControl(),
65       scrumLead: new FormControl(),
66       scrumLeadId: new FormControl(),
67       systemsEngineer: new FormControl(),
68       systemsEngineerId: new FormControl(),
69       developer: new FormControl(),
70       developerId: new FormControl(),
71       status: new FormControl(),
72       pstDueDate: new FormControl(),
73       pstDueIteration: new FormControl(),
74       eteDueDate: new FormControl(),
75       eteDueIteration: new FormControl(),
76       labels: new FormControl(),
77       notes: new FormControl()
78     });
79
80     this.msInstanceAddForm = this.fb.group({
81       name: ['', []],
82       release: ['', [Validators.required]],
83       scrumLead: ['', []],
84       scrumLeadId: ['', []],
85       systemsEngineer: ['', []],
86       systemsEngineerId: ['', []],
87       developer: ['', [Validators.required]],
88       developerId: ['', [Validators.required]],
89       status: ['', []],
90       pstDueDate: ['', []],
91       pstDueIteration: ['', []],
92       eteDueDate: ['', []],
93       eteDueIteration: ['', []],
94       labels: ['', []],
95       notes: ['', []]
96     });
97     
98     if (this.msInstanceChange) {
99         this.guiHeader   = "Microservice Instance Update";
100         this.addOrUpdate = "Update";
101         this.populateFields();
102     }
103   }
104
105   populateFields() {
106     this.msName = this.currentRow['name'];
107
108     let labelsStr: string;
109     if (this.currentRow['metadata']['labels']) {
110         labelsStr = this.currentRow['metadata']['labels'].join(' ')
111     }
112     
113     this.msInstanceAddForm.patchValue({
114         release:           this.currentRow['release'],
115         scrumLead:         this.currentRow['metadata']['scrumLead'],
116         scrumLeadId:       this.currentRow['metadata']['scrumLeadId'],
117         systemsEngineer:   this.currentRow['metadata']['systemsEngineer'],
118         systemsEngineerId: this.currentRow['metadata']['systemsEngineerId'],
119         developer:         this.currentRow['metadata']['developer'],
120         developerId:       this.currentRow['metadata']['developerId'],
121         pstDueDate:        this.currentRow['pstDueDate'],
122         pstDueIteration:   this.currentRow['pstDueIteration'],
123         eteDueDate:        this.currentRow['eteDueDate'],
124         eteDueIteration:   this.currentRow['eteDueIteration'],
125         labels:            labelsStr,
126         notes:             this.currentRow['metadata']['notes']
127     })
128 }
129
130   /* * * * On click of cancel * * * */
131   closeDialog() {
132     this.visible = false;
133     this.handler.emit(null)
134   }
135
136   /* * * * On click of add * * * */
137   submitMsInstance() {
138     //  Prevent error on "split" if record existed before "labels" were implemented
139     let labels: string[] = []
140     if (!this.msInstanceAddForm.value['labels']){
141        labels = []
142     } else {
143         labels = this.msInstanceAddForm.value['labels'].trim().replace(/\s{2,}/g, ' ').split(" ")
144     }
145
146     //build request body
147     this.msInstanceToAdd = {
148       name:    this.msName,
149       release: this.msInstanceAddForm.value['release'],
150       metadata: {
151         scrumLead:         this.msInstanceAddForm.value['scrumLead'],
152         scrumLeadId:       this.msInstanceAddForm.value['scrumLeadId'],
153         systemsEngineer:   this.msInstanceAddForm.value['systemsEngineer'],
154         systemsEngineerId: this.msInstanceAddForm.value['systemsEngineerId'],
155         developer:         this.msInstanceAddForm.value['developer'],
156         developerId:       this.msInstanceAddForm.value['developerId'],
157         pstDueDate:        this.msInstanceAddForm.value['pstDueDate'],
158         pstDueIteration:   this.msInstanceAddForm.value['pstDueIteration'],
159         eteDueDate:        this.msInstanceAddForm.value['eteDueDate'],
160         eteDueIteration:   this.msInstanceAddForm.value['eteDueIteration'],
161         labels:            labels,
162         notes:             this.msInstanceAddForm.value['notes']
163       },
164       user: this.username
165     }
166
167     this.handler.emit(this.msInstanceToAdd) //return request body back to parent
168   }
169 }
170
171 export interface AddMsInstance {
172   name: string,
173   release: string,
174   metadata: {
175     scrumLead: string,
176     scrumLeadId: string,
177     systemsEngineer: string,
178     systemsEngineerId: string,
179     developer: string,
180     developerId: string,
181     pstDueDate: any,
182     pstDueIteration: string,
183     eteDueDate: any,
184     eteDueIteration: string,
185     labels: string[],
186     notes: string
187   }
188   user: string
189 }