Skip to content

Commit

Permalink
story #12867 : New descriptive field of vocabulary on Ontology
Browse files Browse the repository at this point in the history
  • Loading branch information
Hassane Diaby committed Jun 17, 2024
1 parent 76af46f commit 213143c
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import fr.gouv.vitam.common.model.administration.OntologyModel;
import fr.gouv.vitam.common.model.administration.OntologyOrigin;
import fr.gouv.vitam.common.model.administration.OntologyType;
import fr.gouv.vitam.common.model.administration.StringSize;
import fr.gouv.vitam.common.model.administration.TypeDetail;
import fr.gouv.vitamui.commons.api.domain.DirectionDto;
import fr.gouv.vitamui.commons.api.domain.PaginatedValuesDto;
import fr.gouv.vitamui.commons.api.dtos.VitamUiOntologyDto;
Expand Down Expand Up @@ -245,6 +247,12 @@ public OntologyDto patch(VitamContext vitamContext, final Map<String, Object> pa
partialDto.forEach((key, value) -> {
if ("type".equals(key)) {
ontologyDto.setType(OntologyType.valueOf((String) value));
} else if ("stringSize".equals(key)) {
ontologyDto.setStringSize(
Optional.ofNullable(value).map(v -> StringSize.valueOf((String) v)).orElse(null)
);
} else if ("typeDetail".equals(key)) {
ontologyDto.setTypeDetail(TypeDetail.valueOf((String) value));
} else if (!"id".equals(key)) {
try {
BeanUtilsBean.getInstance().copyProperty(ontologyDto, key, value);
Expand All @@ -257,45 +265,38 @@ public OntologyDto patch(VitamContext vitamContext, final Map<String, Object> pa
try {
updateOntology(vitamContext, (String) partialDto.get("id"), ontologyDto);
return ontologyDto;
} catch (InvalidParseOperationException | AccessExternalClientException | IOException e) {
} catch (
InvalidParseOperationException | AccessExternalClientException | IOException | InternalServerException e
) {
throw new InternalServerException("Unable to patch agency", e);
}
}

private RequestResponse<?> updateOntology(
final VitamContext vitamContext,
final String id,
OntologyDto patchOntology
) throws InvalidParseOperationException, AccessExternalClientException, IOException {
private void updateOntology(final VitamContext vitamContext, final String id, OntologyDto patchOntology)
throws InvalidParseOperationException, AccessExternalClientException, IOException, InternalServerException {
if (vitamContext != null) {
LOGGER.info("Update Ontology EvIdAppSession : {} ", vitamContext.getApplicationSessionId());
}
final List<OntologyDto> ontologies = getAll(vitamContext);

ontologies
final OntologyDto matchedOntology = ontologies
.stream()
.filter(ontology -> id.equals(ontology.getId()))
.forEach(ontology -> this.patchFields(ontology, patchOntology));
.findFirst()
.orElseThrow(() -> new InternalServerException("No ontology matched for update"));

return ontologyService.importOntologies(vitamContext, converter.convertDtosToVitams(ontologies));
}
ontologies.remove(matchedOntology);

private void patchFields(OntologyDto ontologyToPatch, OntologyDto fieldsToApply) {
if (fieldsToApply.getShortName() != null) {
ontologyToPatch.setShortName(fieldsToApply.getShortName());
}
matchedOntology.setShortName(patchOntology.getShortName());
matchedOntology.setDescription(patchOntology.getDescription());
matchedOntology.setType(patchOntology.getType());
matchedOntology.setCollections(patchOntology.getCollections());
matchedOntology.setStringSize(patchOntology.getStringSize());
matchedOntology.setTypeDetail(patchOntology.getTypeDetail());

if (fieldsToApply.getDescription() != null) {
ontologyToPatch.setDescription(fieldsToApply.getDescription());
}

if (fieldsToApply.getType() != null) {
ontologyToPatch.setType(fieldsToApply.getType());
}
ontologies.add(patchOntology);

if (fieldsToApply.getCollections() != null) {
ontologyToPatch.setCollections(fieldsToApply.getCollections());
}
ontologyService.importOntologies(vitamContext, converter.convertDtosToVitams(ontologies));
}

public JsonNode findHistoryByIdentifier(VitamContext vitamContext, final String id) throws VitamClientException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
></i>
</div>

<div class="d-flex">
<div class="d-flex align-items-center">
<vitamui-common-input
class="w-100"
formControlName="shortName"
minlength="2"
maxlength="100"
required
[placeholder]="'ONTOLOGY.CREATE_DIALOG.NAME' | translate"
>
<ng-container *ngIf="form.get('shortName')?.touched">
Expand All @@ -47,11 +48,20 @@
</vitamui-common-input-error>
</ng-container>
</vitamui-common-input>
<i
class="vitamui-icon vitamui-icon-info align-middle primary ml-3"
vitamuiCommonToolTip="{{ 'ONTOLOGY.CREATE_DIALOG.NAME_TOOLTIP' | translate }}"
></i>
</div>

<div class="d-flex align-items-center pb-4">
<mat-form-field class="vitamui-mat-select w-100 pb-0">
<mat-select formControlName="type" [placeholder]="'ONTOLOGY.CREATE_DIALOG.TYPE' | translate" required>
<mat-select
formControlName="type"
[placeholder]="'ONTOLOGY.CREATE_DIALOG.INDEXING_MODE' | translate"
(selectionChange)="onIndexingModeChange($event.value)"
required
>
<mat-option *ngFor="let type of types" [value]="type.key">
{{ type.label }}
</mat-option>
Expand All @@ -60,14 +70,29 @@
</mat-form-field>
<i
class="vitamui-icon vitamui-icon-info align-middle primary ml-3"
vitamuiCommonToolTip="{{ 'ONTOLOGY.CREATE_DIALOG.TYPE_TOOLTIP' | translate }}"
vitamuiCommonToolTip="{{ 'ONTOLOGY.CREATE_DIALOG.INDEXING_MODE_TOOLTIP' | translate }}"
>
</i>
</div>

<div *ngIf="sizeFieldVisible" class="d-flex align-items-center pb-4">
<mat-form-field class="vitamui-mat-select w-100 pb-0">
<mat-select formControlName="stringSize" [placeholder]="'ONTOLOGY.CREATE_DIALOG.FIELD_SIZE' | translate">
<mat-option *ngFor="let size of sizes" [value]="size.key">
{{ size.label }}
</mat-option>
</mat-select>
<div class="select-arrow"><i class="material-icons">keyboard_arrow_down</i></div>
</mat-form-field>
<i
class="vitamui-icon vitamui-icon-info align-middle primary ml-3"
vitamuiCommonToolTip="{{ 'ONTOLOGY.CREATE_DIALOG.SIZE_TOOLTIP' | translate }}"
></i>
</div>

<div class="d-flex align-items-center pb-4">
<mat-form-field class="vitamui-mat-select w-100 pb-0">
<mat-select formControlName="collections" [placeholder]="'ONTOLOGY.CREATE_DIALOG.COLLECTION' | translate" multiple>
<mat-select formControlName="collections" [placeholder]="'ONTOLOGY.CREATE_DIALOG.COLLECTION' | translate" multiple required>
<mat-option *ngFor="let collection of collections" [value]="collection.key">
{{ collection.label }}
</mat-option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const expectedOntology = {
collections: ['ObjectGroup'],
description: 'Mon Ontologie',
origin: 'INTERNAL',
typeDetail: 'STRING',
stringSize: 'MEDIUM',
};

let component: OntologyCreateComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { Subscription } from 'rxjs';
import { ConfirmDialogService, Option } from 'vitamui-library';
import { OntologyService } from '../ontology.service';
import { OntologyCreateValidators } from './ontology-create.validators';
import { setTypeDetailAndStringSize } from '../../../../../vitamui-library/src/app/modules/models/ontology/ontology.utils';

@Component({
selector: 'app-ontology-create',
Expand All @@ -54,6 +55,7 @@ export class OntologyCreateComponent implements OnInit, OnDestroy {
hasError = true;
message: string;
isDisabledButton = false;
sizeFieldVisible = false;

// stepCount is the total number of steps and is used to calculate the advancement of the progress bar.
// We could get the number of steps using ViewChildren(StepComponent) but this triggers a
Expand All @@ -78,6 +80,12 @@ export class OntologyCreateComponent implements OnInit, OnDestroy {
{ key: 'ObjectGroup', label: "Groupe d'objet", info: '' },
];

sizes: Option[] = [
{ key: 'SHORT', label: 'Court', info: '' },
{ key: 'MEDIUM', label: 'Moyen', info: '' },
{ key: 'LARGE', label: 'Long', info: '' },
];

@ViewChild('fileSearch', { static: false }) fileSearch: any;

constructor(
Expand All @@ -91,10 +99,12 @@ export class OntologyCreateComponent implements OnInit, OnDestroy {

ngOnInit() {
this.form = this.formBuilder.group({
shortName: [null],
shortName: [null, Validators.required],
identifier: [null, [Validators.required, this.ontologyCreateValidator.patternID()], this.ontologyCreateValidator.uniqueID()],
type: [null, Validators.required],
collections: [null],
typeDetail: [null],
stringSize: [null],
collections: [null, Validators.required],
description: [null],
origin: ['INTERNAL'],
});
Expand All @@ -114,6 +124,11 @@ export class OntologyCreateComponent implements OnInit, OnDestroy {
}
}

onIndexingModeChange(key: string) {
this.sizeFieldVisible = key === 'TEXT';
setTypeDetailAndStringSize(key, this.form);
}

onSubmit() {
if (this.form.invalid) {
this.isDisabledButton = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
<form [formGroup]="form" class="side-form" (ngSubmit)="onSubmit()">
<div class="d-flex mt-3">
<vitamui-common-input formControlName="shortName" [placeholder]="'ONTOLOGY.TAB.INFORMATION.NAME' | translate" disabled>
<vitamui-common-input formControlName="identifier" [placeholder]="'ONTOLOGY.TAB.INFORMATION.IDENTIFIER' | translate" disabled>
</vitamui-common-input>
</div>

<div class="d-flex mt-3">
<vitamui-common-input formControlName="shortName" [placeholder]="'ONTOLOGY.TAB.INFORMATION.NAME' | translate" [disabled]="isInternal">
</vitamui-common-input>
</div>

<div class="d-flex">
<mat-form-field class="vitamui-mat-select w-100">
<mat-select formControlName="type" [disabled]="isInternal" [placeholder]="'ONTOLOGY.TAB.INFORMATION.TYPE' | translate">
<mat-select
formControlName="type"
[disabled]="isInternal"
[placeholder]="'ONTOLOGY.TAB.INFORMATION.TYPE' | translate"
(selectionChange)="onIndexingModeChange($event.value)"
>
<mat-option *ngFor="let type of types" [value]="type.key">
{{ type.label }}
</mat-option>
Expand All @@ -15,6 +25,22 @@
</mat-form-field>
</div>

<div *ngIf="sizeFieldVisible" class="d-flex mt-4">
<mat-form-field class="vitamui-mat-select w-100">
<mat-select
formControlName="stringSize"
[disabled]="isInternal"
[placeholder]="'ONTOLOGY.TAB.INFORMATION.FIELD_SIZE' | translate"
[required]="sizeFieldVisible"
>
<mat-option *ngFor="let size of sizes" [value]="size.key">
{{ size.label }}
</mat-option>
</mat-select>
<div class="select-arrow"><i class="material-icons">keyboard_arrow_down</i></div>
</mat-form-field>
</div>

<div class="d-flex mt-4">
<mat-form-field class="vitamui-mat-select w-100">
<mat-select
Expand Down Expand Up @@ -57,7 +83,7 @@
</mat-form-field>

<div class="actions mt-5">
<button type="submit" class="btn primary" [disabled]="unchanged() || submited">
<button type="submit" class="btn primary" [disabled]="unchanged() || form.invalid || submited">
{{ 'COMMON.SAVE' | translate }}
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ describe('OntologyInformationTabComponent', () => {
type: 'EXTERNAL',
collections: [''],
description: 'Mon Ontologie',
typeDetail: 'string',
stringSize: 'MEDIUM',
};

beforeEach(waitForAsync(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Observable, of } from 'rxjs';
import { catchError, filter, map, switchMap } from 'rxjs/operators';
import { Option, diff } from 'vitamui-library';
import { extend, isEmpty } from 'underscore';
import { Ontology } from 'vitamui-library';
import { OntologyService } from '../../ontology.service';
import { catchError, switchMap } from 'rxjs/operators';
import { diff, Ontology, Option } from 'vitamui-library';
import { RULE_TYPES } from '../../../rule/rules.constants';
import { OntologyService } from '../../ontology.service';
import { setTypeDetailAndStringSize } from '../../../../../../vitamui-library/src/app/modules/models/ontology/ontology.utils';

@Component({
selector: 'app-ontology-information-tab',
Expand All @@ -21,6 +20,8 @@ export class OntologyInformationTabComponent implements OnInit {

submited = false;

sizeFieldVisible = false;

// FIXME: Get list from common var ?
types: Option[] = [
{ key: 'DATE', label: 'Date', info: '' },
Expand All @@ -39,6 +40,12 @@ export class OntologyInformationTabComponent implements OnInit {
{ key: 'ObjectGroup', label: "Groupe d'objet", info: '' },
];

sizes: Option[] = [
{ key: 'SHORT', label: 'Court', info: '' },
{ key: 'MEDIUM', label: 'Moyen', info: '' },
{ key: 'LARGE', label: 'Long', info: '' },
];

@Input()
set inputOntology(ontology: Ontology) {
this._inputOntology = ontology;
Expand All @@ -53,6 +60,8 @@ export class OntologyInformationTabComponent implements OnInit {
this._inputOntology.collections = [];
}

this.sizeFieldVisible = this._inputOntology.type === 'TEXT';

this.resetForm(this.inputOntology);
this.updated.emit(false);
}
Expand Down Expand Up @@ -86,7 +95,9 @@ export class OntologyInformationTabComponent implements OnInit {
identifier: [null, Validators.required],
shortName: [{ value: null, disabled: true }, Validators.required],
type: [null, Validators.required],
collections: [null],
typeDetail: [null],
stringSize: [null],
collections: [null, Validators.required],
description: [null],
creationDate: [{ value: null, disabled: true }],
});
Expand All @@ -104,12 +115,22 @@ export class OntologyInformationTabComponent implements OnInit {
return unchanged;
}

onIndexingModeChange(key: string) {
if (!this.isInternal) {
this.sizeFieldVisible = key === 'TEXT';
}

setTypeDetailAndStringSize(key, this.form);
}

prepareSubmit(): Observable<Ontology> {
return of(diff(this.form.getRawValue(), this.previousValue())).pipe(
filter((formData) => !isEmpty(formData)),
map((formData) => extend({ id: this.previousValue().id, identifier: this.previousValue().identifier }, formData)),
switchMap((formData: { id: string; [key: string]: any }) => this.ontologyService.patch(formData).pipe(catchError(() => of(null)))),
);
const payload = {
id: this.previousValue().id,
identifier: this.previousValue().identifier,
...this.form.value,
};

return of(payload).pipe(switchMap((formData: any) => this.ontologyService.patch(formData).pipe(catchError(() => of(null)))));
}

onSubmit() {
Expand Down
Loading

0 comments on commit 213143c

Please sign in to comment.