From 98557be4d9a1e1925ec3e41667fd0f30d0d8b11f Mon Sep 17 00:00:00 2001
From: Diamadis Tziotzios <dtziotzios@cite.gr>
Date: Mon, 18 Dec 2017 15:45:36 +0200
Subject: [PATCH] no message

---
 .../datasets_new/dataset-listing.component.ts |  4 +-
 .../src/app/dmps/dmp-listing.component.ts     | 23 +------
 .../src/app/models/criteria/BaseCriteria.ts   |  5 +-
 .../models/criteria/BaseCriteriaErrorModel.ts |  2 +-
 .../criteria/project/ProjectCriteria.ts       |  4 +-
 .../app/projects/project-listing.component.ts | 18 ++----
 .../autocomplete/autocomplete.component.ts    |  2 +-
 .../criteria/base/base-criteria.component.ts  | 60 -------------------
 .../dmp-criteria.component.ts                 |  2 -
 .../projects/projects-criteria.component.html | 37 ++++++------
 .../projects/projects-criteria.component.scss |  2 +-
 .../projects/projects-criteria.component.ts   | 34 +++--------
 dmp-frontend/src/assets/lang/en.json          |  7 ++-
 13 files changed, 50 insertions(+), 150 deletions(-)

diff --git a/dmp-frontend/src/app/datasets_new/dataset-listing.component.ts b/dmp-frontend/src/app/datasets_new/dataset-listing.component.ts
index 1688fc92a..960d081d1 100644
--- a/dmp-frontend/src/app/datasets_new/dataset-listing.component.ts
+++ b/dmp-frontend/src/app/datasets_new/dataset-listing.component.ts
@@ -97,7 +97,7 @@ export class DatasetDataSource extends DataSource<DatasetListingModel> {
         });
         const startIndex = this._paginator.pageIndex * this._paginator.pageSize;
         const request = new DataTableRequest(startIndex, this._paginator.pageSize);
-        request.criteria = this._criteria.getFormData();
+        request.criteria = this._criteria.criteria;
         return this._service.getPaged(request);
       })
       .catch((error: any) => {
@@ -106,7 +106,7 @@ export class DatasetDataSource extends DataSource<DatasetListingModel> {
           duration: 3000,
           extraClasses: ['snackbar-warning']
         });
-        this._criteria.onCallbackError(error);
+        //this._criteria.criteria.onCallbackError(error);
         return Observable.of(null);
       })
       .map(result => {
diff --git a/dmp-frontend/src/app/dmps/dmp-listing.component.ts b/dmp-frontend/src/app/dmps/dmp-listing.component.ts
index 8df801cb8..d29f9a090 100644
--- a/dmp-frontend/src/app/dmps/dmp-listing.component.ts
+++ b/dmp-frontend/src/app/dmps/dmp-listing.component.ts
@@ -42,11 +42,7 @@ export class DataManagementPlanListingComponent implements OnInit, AfterViewInit
   }
 
   ngAfterViewInit() {
-    setTimeout(() => {
-      this.criteria.setRefreshCallback(() => this.refresh());
-      this.criteria.setCriteria(this.getDefaultCriteria());
-      this.criteria.controlModified();
-    });
+    this.criteria.setRefreshCallback(() => this.refresh());
   }
 
   refresh() {
@@ -61,19 +57,6 @@ export class DataManagementPlanListingComponent implements OnInit, AfterViewInit
     const defaultCriteria = new DataManagementPlanCriteria();
     return defaultCriteria;
   }
-
-  // canShowOwner(): boolean {
-  //   const principal: Principal = this.authentication.current();
-  //   if (principal) {
-  //     const principalRoles = principal.appRoles;
-  //     for (let i = 0; i < principalRoles.length; i++) {
-  //       if (principalRoles[i] === Principal.AppRole.Admin || principalRoles[i] === Principal.AppRole.BudgetManager) {
-  //         return true;
-  //       }
-  //     }
-  //   }
-  //   return false;
-  // }
 }
 
 export class DataManagementPlanDataSource extends DataSource<DataManagementPlanListingModel> {
@@ -115,7 +98,7 @@ export class DataManagementPlanDataSource extends DataSource<DataManagementPlanL
         });
         const startIndex = this._paginator.pageIndex * this._paginator.pageSize;
         const request = new DataTableRequest(startIndex, this._paginator.pageSize);
-        request.criteria = this._criteria.getFormData();
+        request.criteria = this._criteria.criteria;
         return this._service.getPaged(request);
       })
       .catch((error: any) => {
@@ -124,7 +107,7 @@ export class DataManagementPlanDataSource extends DataSource<DataManagementPlanL
           duration: 3000,
           extraClasses: ['snackbar-warning']
         });
-        this._criteria.onCallbackError(error);
+        //this._criteria.onCallbackError(error);
         return Observable.of(null);
       })
       .map(result => {
diff --git a/dmp-frontend/src/app/models/criteria/BaseCriteria.ts b/dmp-frontend/src/app/models/criteria/BaseCriteria.ts
index f33ba1545..446922e5c 100644
--- a/dmp-frontend/src/app/models/criteria/BaseCriteria.ts
+++ b/dmp-frontend/src/app/models/criteria/BaseCriteria.ts
@@ -1,3 +1,6 @@
+import { FormGenerator } from "../../utilities/forms/FormGenerator";
+import { BaseCriteriaErrorModel } from "./BaseCriteriaErrorModel";
+
 export class BaseCriteria {
-	public Like: string;
+	public like: string;
 }
diff --git a/dmp-frontend/src/app/models/criteria/BaseCriteriaErrorModel.ts b/dmp-frontend/src/app/models/criteria/BaseCriteriaErrorModel.ts
index 48ff68405..b19407afe 100644
--- a/dmp-frontend/src/app/models/criteria/BaseCriteriaErrorModel.ts
+++ b/dmp-frontend/src/app/models/criteria/BaseCriteriaErrorModel.ts
@@ -1,3 +1,3 @@
 export class BaseCriteriaErrorModel {
-	public Like: String;
+	public like: String;
 }
diff --git a/dmp-frontend/src/app/models/criteria/project/ProjectCriteria.ts b/dmp-frontend/src/app/models/criteria/project/ProjectCriteria.ts
index 7b40640c4..4ae9e5616 100644
--- a/dmp-frontend/src/app/models/criteria/project/ProjectCriteria.ts
+++ b/dmp-frontend/src/app/models/criteria/project/ProjectCriteria.ts
@@ -1,6 +1,6 @@
 import { BaseCriteria } from "../BaseCriteria";
 
 export class ProjectCriteria extends BaseCriteria {
-	public PeriodStart: Date;
-	public PeriodEnd: Date;
+	public periodStart: Date;
+	public periodEnd: Date;
 }
diff --git a/dmp-frontend/src/app/projects/project-listing.component.ts b/dmp-frontend/src/app/projects/project-listing.component.ts
index 7c9479cea..4092c408f 100644
--- a/dmp-frontend/src/app/projects/project-listing.component.ts
+++ b/dmp-frontend/src/app/projects/project-listing.component.ts
@@ -20,7 +20,7 @@ import { SnackBarNotificationComponent } from "../shared/components/notificaiton
   providers: [ProjectService],
   encapsulation: ViewEncapsulation.None
 })
-export class ProjectListingComponent implements OnInit, AfterViewInit {
+export class ProjectListingComponent implements OnInit {
 
   @ViewChild(MatPaginator) _paginator: MatPaginator;
   @ViewChild(MatSort) sort: MatSort;
@@ -39,19 +39,13 @@ export class ProjectListingComponent implements OnInit, AfterViewInit {
   }
 
   ngOnInit() {
-
-  }
-
-  ngAfterViewInit() {
-    setTimeout(() => {
-      this.criteria.setRefreshCallback(() => this.refresh());
-      this.criteria.setCriteria(this.getDefaultCriteria());
-      this.criteria.controlModified();
-    });
+    this.criteria.setCriteria(this.getDefaultCriteria());
+    this.refresh();
+    this.criteria.setRefreshCallback(() => this.refresh());
   }
 
   refresh() {
-    this.dataSource = new ProjectDataSource(this.projectService, this._paginator, this.sort, this.languageService, this.snackBar, this.criteria, );
+    this.dataSource = new ProjectDataSource(this.projectService, this._paginator, this.sort, this.languageService, this.snackBar, this.criteria );
   }
 
   rowClick(rowId: String) {
@@ -116,7 +110,7 @@ export class ProjectDataSource extends DataSource<ProjectListingModel> {
         });
         const startIndex = this._paginator.pageIndex * this._paginator.pageSize;
         const request = new DataTableRequest(startIndex, this._paginator.pageSize);
-        request.criteria = this._criteria.getFormData();
+        request.criteria = this._criteria.criteria;
         return this._service.getPaged(request);
       })
       .catch((error: any) => {
diff --git a/dmp-frontend/src/app/shared/components/autocomplete/autocomplete.component.ts b/dmp-frontend/src/app/shared/components/autocomplete/autocomplete.component.ts
index 0cebaeb85..c347d8a10 100644
--- a/dmp-frontend/src/app/shared/components/autocomplete/autocomplete.component.ts
+++ b/dmp-frontend/src/app/shared/components/autocomplete/autocomplete.component.ts
@@ -90,7 +90,7 @@ export class AutocompleteComponent implements OnInit {
 	inputOnChange(term: string) {
 		//this.form.patchValue({ value: null, description: '', text: '' });
 		//this.form.updateValueAndValidity();
-		this.configuration.criteria.Like = term;
+		this.configuration.criteria.like = term;
 		this.configuration.callback(this.configuration.criteria)
 			.map((res: any) => this.mapper(res))
 			.subscribe(
diff --git a/dmp-frontend/src/app/shared/components/criteria/base/base-criteria.component.ts b/dmp-frontend/src/app/shared/components/criteria/base/base-criteria.component.ts
index 2ed93d09f..f42c77ade 100644
--- a/dmp-frontend/src/app/shared/components/criteria/base/base-criteria.component.ts
+++ b/dmp-frontend/src/app/shared/components/criteria/base/base-criteria.component.ts
@@ -13,7 +13,6 @@ import { BaseCriteriaErrorModel } from '../../../../models/criteria/BaseCriteria
 export class BaseCriteriaComponent implements OnInit {
 
     public refreshCallback: Function = null;
-    public formGroup: FormGroup = null;
     public errorModel: BaseCriteriaErrorModel = new BaseCriteriaErrorModel();
 
     constructor(errorModel: BaseCriteriaErrorModel) {
@@ -23,69 +22,10 @@ export class BaseCriteriaComponent implements OnInit {
     ngOnInit() {
     }
 
-    controlModified(): void {
-        this.clearErrorModel();
-        if (!this.isFormValid()) { return; }
-        if (this.refreshCallback != null) { this.refreshCallback(); }
-    }
-
-    public isFormValid(): boolean {
-        this.touchAllFormFields(this.formGroup);
-        this.validateAllFormFields(this.formGroup);
-        return this.formGroup.valid;
-    }
-
-    public getFormData(): any {
-        return this.formGroup.value;
-    }
-
-    public getFormControl(controlName: string): AbstractControl {
-        return this.formGroup.get(controlName);
-    }
-
-    public disableFormFields(formControl: AbstractControl) {
-        formControl.disable();
-    }
-
-    public validateAllFormFields(formControl: AbstractControl) {
-        if (formControl instanceof FormControl) {
-            formControl.updateValueAndValidity({ emitEvent: false })
-        } else if (formControl instanceof FormGroup) {
-            Object.keys(formControl.controls).forEach(item => {
-                const control = formControl.get(item);
-                this.validateAllFormFields(control);
-            })
-        } else if (formControl instanceof FormArray) {
-            formControl.controls.forEach(item => {
-                this.validateAllFormFields(item);
-            })
-        }
-    }
-
-    public touchAllFormFields(formControl: AbstractControl) {
-        if (formControl instanceof FormControl) {
-            formControl.markAsTouched();
-        } else if (formControl instanceof FormGroup) {
-            Object.keys(formControl.controls).forEach(item => {
-                const control = formControl.get(item);
-                this.touchAllFormFields(control);
-            })
-        } else if (formControl instanceof FormArray) {
-            formControl.controls.forEach(item => {
-                this.touchAllFormFields(item);
-            })
-        }
-    }
-
     setRefreshCallback(callback: Function): void {
         this.refreshCallback = callback;
     }
 
-    onCallbackError(error: any) {
-        this.setErrorModel(error.error);
-        this.validateAllFormFields(this.formGroup);
-    }
-
     public setErrorModel(errorModel: BaseCriteriaErrorModel) {
         Object.keys(errorModel).forEach(item => {
             (<any>this.errorModel)[item] = (<any>errorModel)[item];
diff --git a/dmp-frontend/src/app/shared/components/criteria/data-management-plan/dmp-criteria.component.ts b/dmp-frontend/src/app/shared/components/criteria/data-management-plan/dmp-criteria.component.ts
index 01174ffc8..d6faa2076 100644
--- a/dmp-frontend/src/app/shared/components/criteria/data-management-plan/dmp-criteria.component.ts
+++ b/dmp-frontend/src/app/shared/components/criteria/data-management-plan/dmp-criteria.component.ts
@@ -31,12 +31,10 @@ export class DataManagementPlanCriteriaComponent extends BaseCriteriaComponent i
 	ngOnInit() {
 		super.ngOnInit();
 		if (this.criteria == null) { this.criteria = new DataManagementPlanCriteria(); }
-		if (this.formGroup == null) { this.formGroup = this.buildForm(); }
 	}
 
 	setCriteria(criteria: DataManagementPlanCriteria): void {
 		this.criteria = criteria;
-		this.formGroup = this.buildForm();
 	}
 
 	public fromJSONObject(item: any): DataManagementPlanCriteria {
diff --git a/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.html b/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.html
index f01d9a8b4..13acc6d4f 100644
--- a/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.html
+++ b/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.html
@@ -1,34 +1,31 @@
-<form class="form-criteria">
+<div class="project-criteria">
 	<mat-card class="mat-card">
 		<div class="row">
-			<div class="col-sm-6 col-md-2">
+			<div class="col-md-4">
 				<mat-form-field>
-					<input matInput
-						   (focus)="periodStartPicker.open()"
-						   (click)="periodStartPicker.open()"
-						   placeholder=" {{'CRITERIA.FORMS.PERIOD-FROM'| translate}}"
-						   [matDatepicker]="periodStartPicker"
-						   name="projectCriteriaPeriodStart"
-						   [ngModel]="this.criteria.PeriodStart" (ngModelChange)="controlModified()">
-					<mat-error *ngIf="getFormControl('periodStart').errors?.backendError">{{baseErrorModel['Criteria.PeriodStart']}}</mat-error>
+					<input matInput placeholder=" {{'CRITERIA.PROJECTS.LIKE'| translate}}" name="projectCriteriaLike" [(ngModel)]="criteria.like"
+					 (ngModelChange)="controlModified()">
+					<mat-error *ngIf="baseErrorModel?.like">{{baseErrorModel['Criteria.like']}}</mat-error>
+				</mat-form-field>
+			</div>
+			<div class="col-md-4">
+				<mat-form-field>
+					<input matInput (focus)="periodStartPicker.open()" (click)="periodStartPicker.open()" placeholder=" {{'CRITERIA.PROJECTS.PERIOD-FROM'| translate}}"
+					 [matDatepicker]="periodStartPicker" name="projectCriteriaPeriodStart" [(ngModel)]="criteria.periodStart" (ngModelChange)="controlModified()">
+					<mat-error *ngIf="baseErrorModel?.periodStart">{{baseErrorModel['Criteria.periodStart']}}</mat-error>
 					<mat-datepicker-toggle matSuffix [for]="periodStartPicker"></mat-datepicker-toggle>
 					<mat-datepicker #periodStartPicker></mat-datepicker>
 				</mat-form-field>
 			</div>
-			<div class="col-sm-6 col-md-2">
+			<div class="col-md-4">
 				<mat-form-field>
-					<input matInput
-						   (focus)="periodEndPicker.open()"
-						   (click)="periodEndPicker.open()"
-						   name="projectCriteriaPeriodEnd"
-						   placeholder=" {{'CRITERIA.FORMS.PERIOD-TO'| translate}}"
-						   [matDatepicker]="periodEndPicker"
-						   [ngModel]="this.criteria.PeriodEnd" (ngModelChange)="controlModified()">
-					<mat-error *ngIf="getFormControl('periodEnd').errors?.backendError">{{baseErrorModel['Criteria.PeriodEnd']}}</mat-error>
+					<input matInput (focus)="periodEndPicker.open()" (click)="periodEndPicker.open()" name="projectCriteriaPeriodEnd" placeholder=" {{'CRITERIA.PROJECTS.PERIOD-TO'| translate}}"
+					 [matDatepicker]="periodEndPicker" [(ngModel)]="criteria.periodEnd" (ngModelChange)="controlModified()">
+					<mat-error *ngIf="baseErrorModel?.periodEnd">{{baseErrorModel['Criteria.periodEnd']}}</mat-error>
 					<mat-datepicker-toggle matSuffix [for]="periodEndPicker"></mat-datepicker-toggle>
 					<mat-datepicker #periodEndPicker></mat-datepicker>
 				</mat-form-field>
 			</div>
 		</div>
 	</mat-card>
-</form>
\ No newline at end of file
+</div>
\ No newline at end of file
diff --git a/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.scss b/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.scss
index 263cc1548..128b8021f 100644
--- a/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.scss
+++ b/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.scss
@@ -1,4 +1,4 @@
-.form-criteria {
+.project-criteria {
 	mat-form-field {
 		padding-bottom: 5px;
 		width: 100%;
diff --git a/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.ts b/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.ts
index 59d8c7611..b2f025338 100644
--- a/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.ts
+++ b/dmp-frontend/src/app/shared/components/criteria/projects/projects-criteria.component.ts
@@ -31,38 +31,22 @@ export class ProjectCriteriaComponent extends BaseCriteriaComponent implements O
 	ngOnInit() {
 		super.ngOnInit();
 		if (this.criteria == null) { this.criteria = new ProjectCriteria(); }
-		if (this.formGroup == null) { this.formGroup = this.buildForm(); }
 	}
 
 	setCriteria(criteria: ProjectCriteria): void {
 		this.criteria = criteria;
-		this.formGroup = this.buildForm();
 	}
 
-	public fromJSONObject(item: any): ProjectCriteria {
-		this.criteria = new ProjectCriteria();
-		this.criteria.PeriodStart = new Date(item.PeriodStart);
-		this.criteria.PeriodEnd = new Date(item.PeriodEnd);
-		return this.criteria;
+	onCallbackError(error: any) {
+		this.setErrorModel(error.error);
 	}
 
-	buildForm(): FormGroup {
-		const context: ValidationContext = this.createValidationContext();
-
-		return this.formBuilder.group({
-			periodStart: [this.criteria.PeriodStart, context.getValidation('periodStart').validators],
-			periodEnd: [this.criteria.PeriodEnd, context.getValidation('periodEnd').validators],
-		});
-	}
-
-	createValidationContext(): ValidationContext {
-		const validationContext: ValidationContext = new ValidationContext();
-		const validationArray: Validation[] = new Array<Validation>();
-
-		validationArray.push({ key: 'periodStart', validators: [BackendErrorValidator(this.errorModel, 'Criteria.PeriodStart')] }); //must add 'Criteria.' because the criteria validator is inside the request validator
-		validationArray.push({ key: 'periodEnd', validators: [BackendErrorValidator(this.errorModel, 'Criteria.PeriodEnd')] });
-
-		validationContext.validation = validationArray;
-		return validationContext;
+	controlModified(): void {
+		this.clearErrorModel();
+		if (this.refreshCallback != null &&
+			(this.criteria.like == null || this.criteria.like.length == 0 || this.criteria.like.length > 2)
+		) {
+			this.refreshCallback();
+		}
 	}
 }
diff --git a/dmp-frontend/src/assets/lang/en.json b/dmp-frontend/src/assets/lang/en.json
index b35090fb8..b17dd6116 100644
--- a/dmp-frontend/src/assets/lang/en.json
+++ b/dmp-frontend/src/assets/lang/en.json
@@ -74,9 +74,10 @@
         }
     },
     "CRITERIA": {
-        "FORMS": {
-            "PERIOD-FROM": "Start",
-            "PERIOD-TO": "End"
+        "PROJECTS": {
+            "LIKE": "Search",
+            "PERIOD-FROM": "Project Start",
+            "PERIOD-TO": "Project End"
         }
     }
 }
\ No newline at end of file