src/app/apps/apps-add/properties/apps-bulk-import-properties.component.ts
Applications Bulk Import Properties Provide a form to import applications by properties
selector | app-apps-bulk-import-properties |
styleUrls | ../styles.scss |
templateUrl | ./apps-bulk-import-properties.component.html |
Properties |
|
Methods |
constructor(appsService: AppsService, notificationService: NotificationService, blockerService: BlockerService, fb: FormBuilder, router: Router)
|
||||||||||||||||||||||||
Constructor
Parameters :
|
fileChange | ||||||||
fileChange(contents: )
|
||||||||
Parse and load a file to the properties control Produce an exception when the user cancel the file dialog
Parameters :
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Will cleanup any {@link Subscription}s to prevent memory leaks.
Returns :
void
|
prepareBulkImportRequest | ||||||||||||
prepareBulkImportRequest(force: , importProps: string)
|
||||||||||||
Prepare Bulk Import Params
Parameters :
Returns :
BulkImportParams
|
submit |
submit()
|
Bulk Import Apps. Submit the parameters
Returns :
void
|
form |
form:
|
Type : FormGroup
|
Fom Group |
Private ngUnsubscribe$ |
ngUnsubscribe$:
|
Type : Subject<any>
|
Unubscribe |
submitted |
submitted:
|
Default value : false
|
Form Submitted |
import { Component, OnDestroy } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Subject } from 'rxjs';
import { finalize, takeUntil } from 'rxjs/operators';
import { AppsService } from '../../apps.service';
import { NotificationService } from '../../../shared/services/notification.service';
import { BulkImportParams } from '../../components/apps.interface';
import { AppsAddValidator } from '../apps-add.validator';
import { BlockerService } from '../../../shared/components/blocker/blocker.service';
/**
* Applications Bulk Import Properties
* Provide a form to import applications by properties
*
* @author Damien Vitrac
*/
@Component({
selector: 'app-apps-bulk-import-properties',
styleUrls: ['./../styles.scss'],
templateUrl: './apps-bulk-import-properties.component.html'
})
export class AppsBulkImportPropertiesComponent implements OnDestroy {
/**
* Unubscribe
*/
private ngUnsubscribe$: Subject<any> = new Subject();
/**
* Fom Group
*/
form: FormGroup;
/**
* Form Submitted
*/
submitted = false;
/**
* Constructor
*
* @param {AppsService} appsService
* @param {NotificationService} notificationService
* @param {BlockerService} blockerService
* @param {FormBuilder} fb
* @param {Router} router
*/
constructor(private appsService: AppsService,
private notificationService: NotificationService,
private blockerService: BlockerService,
private fb: FormBuilder,
private router: Router) {
this.form = fb.group({
'properties': new FormControl('', [Validators.required, AppsAddValidator.properties]),
'file': new FormControl(''),
'force': new FormControl(false)
});
}
/**
* Will cleanup any {@link Subscription}s to prevent
* memory leaks.
*/
ngOnDestroy() {
this.ngUnsubscribe$.next();
this.ngUnsubscribe$.complete();
}
/**
* Parse and load a file to the properties control
* Produce an exception when the user cancel the file dialog
*
* @param {Blob} contents File
*/
fileChange(contents) {
try {
const reader = new FileReader();
reader.onloadend = (e) => {
this.form.get('properties').setValue(reader.result);
this.form.get('file').setValue('');
};
reader.readAsText(contents.target.files[0]);
} catch (e) {
}
}
/**
* Prepare Bulk Import Params
* @param force
* @param {string} importProps
*/
prepareBulkImportRequest(force, importProps: string): BulkImportParams {
return {
force: force,
properties: importProps.split('\n'),
uri: ''
};
}
/**
* Bulk Import Apps.
* Submit the parameters
*/
submit() {
this.submitted = true;
if (!this.form.valid) {
this.notificationService.error('Some field(s) are missing or invalid.');
} else {
this.blockerService.lock();
const reqImportBulkApps = this.prepareBulkImportRequest(
this.form.get('force').value,
this.form.get('properties').value.toString()
);
this.appsService.bulkImportApps(reqImportBulkApps)
.pipe(takeUntil(this.ngUnsubscribe$), finalize(() => this.blockerService.unlock()))
.subscribe(() => {
this.notificationService.success('Apps Imported.');
this.router.navigate(['apps']);
}, () => {
this.notificationService.error('An error occurred while importing Apps. ' +
'Please check the server logs for more details.');
});
}
}
}
<app-page-head class="step2">
<app-page-head-back [defaultUrl]="'/apps/add'" [isNotRegex]="'^(\/apps\/add\/)'"></app-page-head-back>
<app-page-head-title><strong>Bulk import application</strong> coordinates from a properties file</app-page-head-title>
</app-page-head>
<form [formGroup]="form" role="form" (ngSubmit)="submit()" novalidate dataflowLayoutType type="large">
<div dataflowLayoutType type="medium">
<div class="dataflow-alert dataflow-alert-info">
Enter the <strong>list of properties</strong> into the text area field below. <br>
Alternatively, you can also <strong>select a file</strong> in your local file system, which is used to populate
the text area field.
</div>
<div class="form-group" [class.has-error]="form.get('properties').invalid && submitted">
<label for="propertiesInput" class="control-label">Apps as Properties: <em class="required">*</em></label>
<textarea id="propertiesInput" name="properties" autofocus
formControlName="properties"
rows="5"
[dataflowFocus]="true"
class="form-control" placeholder="Example:
task.timestamp=maven://o.s.cloud.task.app:timestamp-task:1.2.3.RELEASE
task.spark-client=maven://o.s.cloud.task.app:spark-client-task:1.2.3.RELEASE">
</textarea>
<span class="help-block" *ngIf="form.get('properties').invalid && submitted">
Please provide a valid properties where the keys are formatted as
<strong>type.name</strong> and the values are the URIs of the apps.
</span>
<div class="input-file">
<label class="btn-file btn btn-default" placement="top" delay="500"
tooltip="Please provide a text file containing properties. This will be used to populate the text area above.">
<input formControlName="file" id="propertiesFile" name="propertiesFile" type="file"
(change)="fileChange($event)"/>
Import a local file
</label>
</div>
</div>
<div class="form-group">
<label class="checkbox-inline">
<input formControlName="force" id="forceInput" name="force" type="checkbox"/>
Force<span class="checkbox-description">, the applications will be imported and installed
even if it already exists but only if not being used already.</span>
</label>
</div>
</div>
<app-page-actions id="footer-actions">
<button name="cancel" type="button" class="btn btn-default" routerLink="/apps">Cancel</button>
<button name="submit" type="submit" class="btn btn-primary">
Import the application(s)
</button>
</app-page-actions>
</form>