-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathprocess-form.component.ts
More file actions
228 lines (207 loc) · 6.72 KB
/
Copy pathprocess-form.component.ts
File metadata and controls
228 lines (207 loc) · 6.72 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import {
Component,
Input,
OnInit,
} from '@angular/core';
import {
FormsModule,
NgForm,
} from '@angular/forms';
import {
NavigationExtras,
Router,
RouterLink,
} from '@angular/router';
import { ScriptDataService } from '@dspace/core/data/processes/script-data.service';
import { RemoteData } from '@dspace/core/data/remote-data';
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
import { Process } from '@dspace/core/processes/process.model';
import { ProcessParameter } from '@dspace/core/processes/process-parameter.model';
import { getFirstCompletedRemoteData } from '@dspace/core/shared/operators';
import { Script } from '@dspace/core/shared/scripts/script.model';
import { ScriptParameter } from '@dspace/core/shared/scripts/script-parameter.model';
import {
hasValue,
isEmpty,
} from '@dspace/shared/utils/empty.util';
import {
TranslateModule,
TranslateService,
} from '@ngx-translate/core';
import { BtnDisabledDirective } from '../../shared/btn-disabled.directive';
import { getProcessListRoute } from '../process-page-routing.paths';
import { ProcessParametersComponent } from './process-parameters/process-parameters.component';
import { ScriptHelpComponent } from './script-help/script-help.component';
import { ScriptsSelectComponent } from './scripts-select/scripts-select.component';
/**
* Component to create a new script
*/
@Component({
selector: 'ds-process-form',
templateUrl: './process-form.component.html',
styleUrls: ['./process-form.component.scss'],
imports: [
BtnDisabledDirective,
FormsModule,
ProcessParametersComponent,
RouterLink,
ScriptHelpComponent,
ScriptsSelectComponent,
TranslateModule,
],
})
export class ProcessFormComponent implements OnInit {
/**
* The currently selected script
*/
@Input() public selectedScript: Script = undefined;
/**
* The process to create
*/
@Input() public process: Process = undefined;
/**
* The parameter values to use to start the process
*/
@Input() public parameters: ProcessParameter[] = [];
/**
* Optional files that are used as parameter values
*/
public files: File[] = [];
/**
* Message key for the header of the form
*/
@Input() public headerKey: string;
/**
* Contains the missing parameters on submission
*/
public missingParameters = [];
/**
* Indicates whether the form has been submitted
* Used to surface validation errors on an interrupted submission
*/
public submitted = false;
/**
* Indicates whether a script has been selected
*/
get isScriptSelected(): boolean {
return hasValue(this.selectedScript);
}
constructor(
private scriptService: ScriptDataService,
private notificationsService: NotificationsService,
private translationService: TranslateService,
private router: Router) {
}
ngOnInit(): void {
this.process = new Process();
}
/**
* Validates the form, sets the parameters to correct values and invokes the script with the correct parameters
* @param form
*/
submitForm(form: NgForm) {
this.submitted = true;
if (isEmpty(this.parameters)) {
this.parameters = [];
}
if (!this.isScriptSelected || !this.validateForm(form) || this.isRequiredMissing()) {
return;
}
const stringParameters: ProcessParameter[] = this.parameters.map((parameter: ProcessParameter) => {
return {
name: parameter.name,
value: this.checkValue(parameter),
};
},
);
this.scriptService.invoke(this.selectedScript.id, stringParameters, this.files)
.pipe(getFirstCompletedRemoteData())
.subscribe((rd: RemoteData<Process>) => {
if (rd.hasSucceeded) {
const title = this.translationService.get('process.new.notification.success.title');
const content = this.translationService.get('process.new.notification.success.content');
this.notificationsService.success(title, content);
this.sendBack(rd.payload);
} else {
const title = this.translationService.get('process.new.notification.error.title');
const content = this.translationService.get('process.new.notification.error.content');
this.notificationsService.error(title, content);
}
});
}
/**
* Checks whether the parameter values are files
* Replaces file parameters by strings and stores the files in a separate list
* @param processParameter The parameter value to check
*/
private checkValue(processParameter: ProcessParameter): string {
if (typeof processParameter.value === 'object') {
this.files = [...this.files, processParameter.value];
return processParameter.value.name;
}
return processParameter.value;
}
/**
* Validates the form
* Returns false if the form is invalid
* Returns true if the form is valid
* @param form The NgForm object to validate
*/
private validateForm(form: NgForm) {
let valid = true;
Object.keys(form.controls).forEach((key) => {
if (form.controls[key].invalid) {
form.controls[key].markAsDirty();
valid = false;
}
});
return valid;
}
private isRequiredMissing() {
this.missingParameters = [];
if (!this.isScriptSelected || isEmpty(this.selectedScript.parameters)) {
return false;
}
const setParams: string[] = this.parameters
.map((param) => param.name);
const requiredParams: ScriptParameter[] = this.selectedScript.parameters.filter((param) => param.mandatory);
for (const rp of requiredParams) {
if (!setParams.includes(rp.name)) {
this.missingParameters.push(rp.name);
}
}
return this.missingParameters.length > 0;
}
/**
* Redirect the user to the processes overview page with the new process' ID,
* so it can be highlighted in the overview table.
* @param newProcess The newly created process
* @private
*/
private sendBack(newProcess: Process) {
const extras: NavigationExtras = {
queryParams: { new_process_id: newProcess.processId },
};
void this.router.navigate([getProcessListRoute()], extras);
}
updateScript($event: Script) {
this.selectedScript = $event;
this.parameters = undefined;
}
get generatedProcessName() {
const paramsString = this.parameters?.map((p: ProcessParameter) => {
const value = this.parseValue(p.value);
return isEmpty(value) ? p.name : `${p.name} ${value}`;
}).join(' ') || '';
return isEmpty(paramsString) ? this.selectedScript.name : `${this.selectedScript.name} ${paramsString}`;
}
private parseValue(value: any) {
if (typeof value === 'boolean') {
return undefined;
}
if (value instanceof File) {
return value.name;
}
return value?.toString();
}
}