From 6fd11cd78e66013c90f19e3c65cc6c94c9d544e1 Mon Sep 17 00:00:00 2001
From: Florent Poittevin <florent.poittevin@unige.ch>
Date: Wed, 10 Jul 2019 15:14:35 +0200
Subject: [PATCH] Add property name decorator

---
 .../deposit/components/form/form.component.ts | 29 ++++++++++---------
 .../components/form/form.component.ts         | 17 ++++++-----
 .../decorators/property-name.decorator.ts     |  5 ++++
 .../models/base-form-definition.model.ts      |  1 -
 4 files changed, 29 insertions(+), 23 deletions(-)
 create mode 100644 src/app/shared/decorators/property-name.decorator.ts

diff --git a/src/app/deposit/components/form/form.component.ts b/src/app/deposit/components/form/form.component.ts
index 13305d1cf..40239fac5 100644
--- a/src/app/deposit/components/form/form.component.ts
+++ b/src/app/deposit/components/form/form.component.ts
@@ -1,5 +1,6 @@
 import {ChangeDetectionStrategy, Component, Input} from "@angular/core";
 import {FormBuilder, Validators} from "@angular/forms";
+import {DateUtil} from "@app/core/utils/date.util";
 import {
   DepositsModel,
   LanguagesModel,
@@ -9,8 +10,8 @@ import {
   SubmissionPoliciesModel,
 } from "@app/generated-api";
 import {AbstractFormComponent} from "@app/shared/components/abstract-form/abstract-form.component";
+import {PropertyName} from "@app/shared/decorators/property-name.decorator";
 import {BaseFormDefinition} from "@app/shared/models/base-form-definition.model";
-import {DateUtil} from "@app/core/utils/date.util";
 import AccessModelEnum = DepositsModel.AccessModelEnum;
 
 @Component({
@@ -89,17 +90,17 @@ export class FormComponent extends AbstractFormComponent<DepositsModel> {
 }
 
 class FormComponentFormDefinition extends BaseFormDefinition {
-  organizationalUnitId: string = "organizationalUnitId";
-  title: string = "title";
-  description: string = "description";
-  languageId: string = "languageId";
-  publicationDate: string = "publicationDate";
-  collectionBegin: string = "collectionBegin";
-  collectionEnd: string = "collectionEnd";
-  author: string = "author";
-  accessLevel: string = "accessLevel";
-  hasEmbargo: string = "hasEmbargo";
-  licenseId: string = "licenseId";
-  submissionPolicyId: string = "submissionPolicyId";
-  preservationPolicyId: string = "preservationPolicyId";
+  @PropertyName() organizationalUnitId: string;
+  @PropertyName() title: string;
+  @PropertyName() description: string;
+  @PropertyName() languageId: string;
+  @PropertyName() publicationDate: string;
+  @PropertyName() collectionBegin: string;
+  @PropertyName() collectionEnd: string;
+  @PropertyName() author: string;
+  @PropertyName() accessLevel: string;
+  @PropertyName() hasEmbargo: string;
+  @PropertyName() licenseId: string;
+  @PropertyName() submissionPolicyId: string;
+  @PropertyName() preservationPolicyId: string;
 }
diff --git a/src/app/organizational-unit/components/form/form.component.ts b/src/app/organizational-unit/components/form/form.component.ts
index aadbe706a..dce6828e1 100644
--- a/src/app/organizational-unit/components/form/form.component.ts
+++ b/src/app/organizational-unit/components/form/form.component.ts
@@ -1,10 +1,11 @@
 import {ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges} from "@angular/core";
 import {FormBuilder, Validators} from "@angular/forms";
+import {DateUtil} from "@app/core/utils/date.util";
 import {OrganizationalUnitsModel, PreservationPoliciesModel, SubmissionPoliciesModel} from "@app/generated-api";
 import {AbstractFormComponent} from "@app/shared/components/abstract-form/abstract-form.component";
+import {PropertyName} from "@app/shared/decorators/property-name.decorator";
 import {ModelAttributeEnum} from "@app/shared/enums/model-attribute.enum";
 import {BaseFormDefinition} from "@app/shared/models/base-form-definition.model";
-import {DateUtil} from "@app/core/utils/date.util";
 import _ from "lodash";
 
 @Component({
@@ -72,11 +73,11 @@ export class FormComponent extends AbstractFormComponent<OrganizationalUnitsMode
 }
 
 class FormComponentFormDefinition extends BaseFormDefinition {
-  name: string = "name";
-  description: string = "description";
-  isEmpty: string = "isEmpty";
-  closingDate: string = "closingDate";
-  openingDate: string = "openingDate";
-  submissionPolicies: string = ModelAttributeEnum.submissionPolicies;
-  open: string = "open";
+  @PropertyName() name: string;
+  @PropertyName() description: string;
+  @PropertyName() isEmpty: string;
+  @PropertyName() closingDate: string;
+  @PropertyName() openingDate: string;
+  @PropertyName() submissionPolicies: string;
+  @PropertyName() open: string;
 }
diff --git a/src/app/shared/decorators/property-name.decorator.ts b/src/app/shared/decorators/property-name.decorator.ts
new file mode 100644
index 000000000..91681761c
--- /dev/null
+++ b/src/app/shared/decorators/property-name.decorator.ts
@@ -0,0 +1,5 @@
+export function PropertyName(prefix: string = "", suffix: string = ""): PropertyDecorator {
+  return (target: Object, propertyKey: string | symbol) => {
+    target[propertyKey] = prefix + propertyKey.toString() + suffix;
+  };
+}
diff --git a/src/app/shared/models/base-form-definition.model.ts b/src/app/shared/models/base-form-definition.model.ts
index ca26294af..3c9e8771a 100644
--- a/src/app/shared/models/base-form-definition.model.ts
+++ b/src/app/shared/models/base-form-definition.model.ts
@@ -1,3 +1,2 @@
 export abstract class BaseFormDefinition {
-
 }
-- 
GitLab