Commit 4365022c authored by Baptiste Toulemonde's avatar Baptiste Toulemonde
Browse files

refactoring and clean code

parent 59457de0
......@@ -59,9 +59,8 @@ export class SedaService {
if (currentNode && nameNode) {
let i: number;
let currentChild: SedaData;
let sedaNode: SedaData;
if (nameNode === currentNode.Name) {
sedaNode = currentNode;
return currentNode;
} else {
// Use a for loop instead of forEach to avoid nested functions
// Otherwise "return" will not work properly
......@@ -72,12 +71,12 @@ export class SedaService {
const result = this.getSedaNode(currentChild, nameNode);
// Return the result if the node has been found
if (result) {
sedaNode = result;
return result;
}
}
}
// The node has not been found and we have no more options
return sedaNode;
return;
}
}
}
......@@ -85,10 +84,10 @@ export class SedaService {
getSedaNodeRecursively(currentNode: SedaData, nameNode: string): SedaData {
let i: number;
let currentChild: SedaData;
let sedaData: SedaData;
let resultNode: SedaData;
if (currentNode) {
if (nameNode === currentNode.Name) {
sedaData = currentNode;
resultNode = currentNode;
} else {
// Use a for loop instead of forEach to avoid nested functions
// Otherwise "return" will not work properly
......@@ -99,16 +98,13 @@ export class SedaService {
const result = this.getSedaNodeRecursively(currentChild, nameNode);
// Return the result if the node has been found
if (result) {
sedaData = result;
resultNode = result;
}
}
} else {
// The node has not been found and we have no more options
console.log('No SEDA nodes could be found for ', nameNode);
return sedaData;
}
}
}
return resultNode;
}
// Get the seda node based on collection name and a node name.
......@@ -116,19 +112,19 @@ export class SedaService {
// the function will search the whole file and return a single metadata based on
// a node name and a collection name;
getSedaNodeCollection(sedaNode: SedaData, nodeName: string, collectionName: string): SedaData {
let sedaData: SedaData;
let resultNode: SedaData;
if (sedaNode) {
if (sedaNode.Collection === collectionName && sedaNode.Name === nodeName) {
sedaData = sedaNode;
resultNode = sedaNode;
}
for (const child of sedaNode.Children) {
const nodeFound = this.getSedaNodeCollection(child, nodeName, collectionName);
if (nodeFound) {
sedaData = nodeFound;
resultNode = nodeFound;
}
}
}
return sedaData;
return resultNode;
}
// For all correspondent values beetween seda and tree elements,
......@@ -151,7 +147,7 @@ export class SedaService {
if (!clickedNode.cardinality) {
return '1';
} else {
return cardlinalityValues.find(c => c.value === clickedNode.cardinality).value;
return cardlinalityValues.find(c => c.value == clickedNode.cardinality).value;
}
}
......@@ -161,7 +157,7 @@ export class SedaService {
*/
getAttributes(sedaNode: SedaData, collection: string): SedaData[] {
// if (!sedaNode) return;
return sedaNode.Children.filter(children => children.Element === 'Attribute'
return sedaNode.Children.filter(children => children.Element == 'Attribute'
&& sedaNode.Collection === collection);
}
......@@ -204,7 +200,7 @@ export class SedaService {
if (nodeName === sedaNode.Name) {
return sedaNode;
}
const childFound = sedaNode.Children.find(c => c.Name === nodeName);
const childFound = sedaNode.Children.find(c => c.Name == nodeName);
return childFound ? childFound : null;
}
......
......@@ -225,7 +225,7 @@ export class FileTreeMetadataComponent {
this.sedaLanguage = value;
},
(error) => {
console.log(error);
console.error(error);
}
);
this._fileServiceSubscriptionNodeChange = this.fileService.nodeChange.subscribe(node => {
......@@ -395,7 +395,6 @@ export class FileTreeMetadataComponent {
}
setNodeValue(metadata: MetadataHeaders, newValue: string) {
console.log(metadata.cardinalite + 'new Value ' + newValue);
if (newValue != null) {
const updatedValue = newValue.length > 0 ? newValue : null;
if (this.clickedNode.name === metadata.nomDuChamp) {
......@@ -430,7 +429,6 @@ export class FileTreeMetadataComponent {
onAddNode() {
if (this.clickedNode.name === 'DescriptiveMetadata') {
console.log('Yes');
let elements: SedaData[];
elements.push({
Name: 'ArchiveUnit',
......@@ -477,7 +475,6 @@ export class FileTreeMetadataComponent {
popData.cancelLabel = this.popupAnnuler;
const popUpAnswer = await this.fileService.openPopup(popData) as AttributeData[];
console.log('The answer for edit attributte was ', popUpAnswer);
if (popUpAnswer) {
......@@ -664,7 +661,6 @@ export class FileTreeMetadataComponent {
onChange(val: any, $event: MatCheckboxChange) {
console.log('onChange file tree metadata go dans methode setNodeValue' + val + ' et event ' + $event);
// @ts-ignore
this.setNodeValue(val, $event);
......@@ -677,7 +673,7 @@ export class FileTreeMetadataComponent {
this.sedaLanguage = value;
},
(error) => {
console.log(error);
console.error(error);
}
);
}
......
......@@ -133,7 +133,6 @@ export class FileTreeMetadataService {
}
this.allowedSedaCardinalities.next(allowedCardList);
this.selectedCardinalities.next(this.findCardinalities(clickedNode, sedaChild, data));
console.log('Data on fillDataTable', data, 'with selected cards :', this.selectedCardinalities.getValue());
return data;
}
......@@ -163,7 +162,6 @@ export class FileTreeMetadataService {
// If the clicked node has the same name was the seda node, the node is already found
if (sedaNode.Name === fileNode.name) {
allowedCardinalityListResult = this.allowedCardinality.get(sedaNode.Cardinality);
return allowedCardinalityListResult;
}
if (sedaNode.Children.length > 0) {
// Search the sedaNode children to find the correnpondent cardinality list
......@@ -173,39 +171,26 @@ export class FileTreeMetadataService {
// the seda cardinality wont include the cardinality retrieved by node's rng file.
// In this case, the condition will return the rng file cardinality list
// instead of node's cardinality list in accordance with the SEDA specification.
// if (child.Cardinality !== sedaNode.Cardinality){
// allowedCardinalityListResult = this.allowedCardinality.get(clickedNode.cardinality);
// return allowedCardinalityListResult;
// }
allowedCardinalityListResult = this.allowedCardinality.get(child.Cardinality);
resultList.push(allowedCardinalityListResult);
this.allowedSedaCardinalities.next(resultList);
return allowedCardinalityListResult;
}
}
} else {
// console.error("Final CARDINALITY LIST (NO seda children found) : ", allowedCardinalityListResult, " for ", sedaNode.Name);
for (const [card, cardlist] of this.allowedCardinality) {
if (card === fileNode.cardinality) {
!fileNode.cardinality ? allowedCardinalityListResult.push('1') : allowedCardinalityListResult = cardlist;
// result = cardlist;
resultList.push(cardlist);
this.allowedSedaCardinalities.next(resultList);
// console.error("Final CARDINALITY LIST : ", allowedCardinalityListResult)
return allowedCardinalityListResult;
}
}
}
this.allowedSedaCardinalities.next(resultList);
if (allowedCardinalityListResult.length < 1) {
// console.error("Card not found for : ", clickedNode.name, "..assuming attribute cardinality :", clickedNode.cardinality);
allowedCardinalityListResult = this.allowedCardinality.get(fileNode.cardinality);
// !clickedNode.cardinality ? result.push("1") : result = this.allowedCardinality[clickedNode.cardinality];
return allowedCardinalityListResult;
}
return allowedCardinalityListResult;
}
findCardinalities(clickedNode: FileNode, sedaNode: SedaData, data: MetadataHeaders[]): string[] {
......
......@@ -275,6 +275,82 @@ export class FileTreeComponent implements OnDestroy {
this.loggingService.showSuccess(this.notificationAddmetadonneeSOne + ' ' + names + ' ' + this.notificationAddmetadonneeSTwo);
}
}
insertItemIterate(parent: FileNode, elementsToAddFromSeda: SedaData [] , node?: FileNode, insertItemDuplicate?: boolean) {
for (const element of elementsToAddFromSeda) {
// 1. Define a new file node, its id and seda data;
const newNode = {} as FileNode;
const newId = window.crypto.getRandomValues(new Uint32Array(10))[0];
const sedaChild = element;
// 1.2. New node type is defined acording to the seda element type
sedaChild.Element === SedaElementConstants.attribute ?
newNode.type = TypeConstants.attribute :
newNode.type = TypeConstants.element;
// 1.3. Fill the missing new node data
if (insertItemDuplicate) {
newNode.cardinality = node.cardinality;
newNode.value = node.value;
newNode.documentation = node.documentation;
newNode.type = node.type;
} else {
newNode.cardinality = Object.values(CardinalityConstants).find(c => c.valueOf() === sedaChild.Cardinality);
}
newNode.name = element.Name;
newNode.id = newId;
newNode.level = parent.level + 1;
newNode.dataType = DataTypeConstants[sedaChild.Type as keyof typeof DataTypeConstants];
newNode.parentId = parent.id;
newNode.parent = parent;
newNode.children = [];
newNode.sedaData = sedaChild;
console.log('Parent node name: ' + parent.name);
console.log('New node : ', newNode);
// 1.4. Update parent->children relashionship
parent.children.push(newNode);
this.parentNodeMap.set(newNode, parent);
console.log('Seda children and file children: ', parent.sedaData.Children, parent.children);
// 2. Insert all children of complex elements based on SEDA definition
if (sedaChild.Element === SedaElementConstants.complex) {
const childrenOfComplexElement: string[] = [];
sedaChild.Children.forEach((child: { Cardinality: any; Name: string; }) => {
if (child.Cardinality === SedaCardinalityConstants.one ||
child.Cardinality === SedaCardinalityConstants.oreOrMore) {
childrenOfComplexElement.push(child.Name);
}
});
this.insertItem(newNode, childrenOfComplexElement);
}
if (insertItemDuplicate) {
this.insertAttributes(newNode, null, node, insertItemDuplicate);
} else {
// 3. Insert all olbigatory attributes of the added node, if there is
if (sedaChild.Children.some((child: { Element: any; }) => child.Element === SedaElementConstants.attribute)) {
const attributes: FileNode[] = [];
sedaChild.Children.filter((c: { Element: any; }) => c.Element === SedaElementConstants.attribute).forEach((child: { Name: string; Element: any; Cardinality: any; }) => {
const isAttributeAlreadyIncluded = newNode.children.some(nodeChild => nodeChild.name.includes(child.Name));
// If the added node contains an obligatory attribute,
// on its seda definition and the attribute is not already part of the node,
// we then, build an attribute node based on the seda atribute defintion
if (child.Element === SedaElementConstants.attribute &&
child.Cardinality === SedaCardinalityConstants.one &&
!isAttributeAlreadyIncluded) {
const childAttribute = {} as FileNode;
childAttribute.name = child.Name;
childAttribute.cardinality = child.Cardinality === SedaCardinalityConstants.one ? '1' : null;
childAttribute.sedaData = sedaChild;
attributes.push(childAttribute);
}
});
this.insertAttributes(newNode, attributes);
}
}
}
}
/** Add an item (or a list of items) in the Tree */
insertItem(parent: FileNode, elementsToAdd: string[], node?: FileNode, insertItemDuplicate?: boolean) {
......@@ -289,80 +365,7 @@ export class FileTreeComponent implements OnDestroy {
}
if (parent.children && elementsToAddFromSeda) {
for (const element of elementsToAddFromSeda) {
// 1. Define a new file node, its id and seda data;
const newNode = {} as FileNode;
const newId = window.crypto.getRandomValues(new Uint32Array(10))[0];
const sedaChild = element;
// 1.2. New node type is defined acording to the seda element type
sedaChild.Element === SedaElementConstants.attribute ?
newNode.type = TypeConstants.attribute :
newNode.type = TypeConstants.element;
// 1.3. Fill the missing new node data
if (insertItemDuplicate) {
newNode.cardinality = node.cardinality;
newNode.value = node.value;
newNode.documentation = node.documentation;
newNode.type = node.type;
} else {
newNode.cardinality = Object.values(CardinalityConstants).find(c => c.valueOf() === sedaChild.Cardinality);
}
newNode.name = element.Name;
newNode.id = newId;
newNode.level = parent.level + 1;
newNode.dataType = DataTypeConstants[sedaChild.Type as keyof typeof DataTypeConstants];
newNode.parentId = parent.id;
newNode.parent = parent;
newNode.children = [];
newNode.sedaData = sedaChild;
console.log('Parent node name: ' + parent.name);
console.log('New node : ', newNode);
// 1.4. Update parent->children relashionship
parent.children.push(newNode);
this.parentNodeMap.set(newNode, parent);
console.log('Seda children and file children: ', parent.sedaData.Children, parent.children);
// 2. Insert all children of complex elements based on SEDA definition
if (sedaChild.Element === SedaElementConstants.complex) {
const childrenOfComplexElement: string[] = [];
sedaChild.Children.forEach((child: { Cardinality: any; Name: string; }) => {
if (child.Cardinality === SedaCardinalityConstants.one ||
child.Cardinality === SedaCardinalityConstants.oreOrMore) {
childrenOfComplexElement.push(child.Name);
}
});
this.insertItem(newNode, childrenOfComplexElement);
}
if (insertItemDuplicate) {
this.insertAttributes(newNode, null, node, insertItemDuplicate);
} else {
// 3. Insert all olbigatory attributes of the added node, if there is
if (sedaChild.Children.some((child: { Element: any; }) => child.Element === SedaElementConstants.attribute)) {
const attributes: FileNode[] = [];
sedaChild.Children.filter((c: { Element: any; }) => c.Element === SedaElementConstants.attribute).forEach((child: { Name: string; Element: any; Cardinality: any; }) => {
const isAttributeAlreadyIncluded = newNode.children.some(nodeChild => nodeChild.name.includes(child.Name));
// If the added node contains an obligatory attribute,
// on its seda definition and the attribute is not already part of the node,
// we then, build an attribute node based on the seda atribute defintion
if (child.Element === SedaElementConstants.attribute &&
child.Cardinality === SedaCardinalityConstants.one &&
!isAttributeAlreadyIncluded) {
const childAttribute = {} as FileNode;
childAttribute.name = child.Name;
childAttribute.cardinality = child.Cardinality === SedaCardinalityConstants.one ? '1' : null;
childAttribute.sedaData = sedaChild;
attributes.push(childAttribute);
}
});
this.insertAttributes(newNode, attributes);
}
}
}
this.insertItemIterate(parent, elementsToAddFromSeda, node, insertItemDuplicate);
// 4. Order elements according to seda definition
const sedaChildrenName: string[] = [];
parent.sedaData.Children.forEach((child: { Name: string; }) => {
......
......@@ -35,7 +35,7 @@ same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL-C license and that you accept its terms.
*/
import {Component, Input, OnDestroy, OnInit, Pipe, PipeTransform, ViewChild} from '@angular/core';
import {Component, Input, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {MatDialog} from '@angular/material/dialog';
import {MatTableDataSource} from '@angular/material/table';
import {ActivatedRoute, Router} from '@angular/router';
......@@ -168,7 +168,6 @@ export class ListProfileComponent extends SidenavPage<ProfileDescription> implem
return this.profileService.retrievedProfiles.subscribe((profileList: ProfileDescription[]) => {
if (profileList) {
this.retrievedProfiles = profileList;
console.log('Profiles: ', this.retrievedProfiles);
this.profilesChargees = true;
this.ngxLoader.stopLoader('table-profiles');
}
......@@ -217,7 +216,6 @@ export class ListProfileComponent extends SidenavPage<ProfileDescription> implem
formData.append('file', fileToUpload, fileToUpload.name);
this._uploadProfileSub = this.profileService.uploadProfile(formData).subscribe( (response: any) => {
if (response) {
console.log('File submited! Reponse is : ', response);
this.router.navigateByUrl(this.pastisConfig.pastisPathPrefix + (this.isStandalone ? '' : this.startupService.getTenantIdentifier() ) + this.pastisConfig.pastisNewProfile, { state: response });
}
......@@ -242,11 +240,9 @@ export class ListProfileComponent extends SidenavPage<ProfileDescription> implem
);
this.subscription2$ = dialogRef.afterClosed().subscribe((result) => {
if (result.success) {
console.log(result.action + ' PA ou PUA ?');
if (result.action === 'PA' || result.action === 'PUA') {
this.profileService.createProfile(this.pastisConfig.createProfileByTypeUrl, result.action).subscribe((response: ProfileResponse) => {
if (response) {
console.log('File submited! Reponse is : ', response);
this.router.navigateByUrl(this.pastisConfig.pastisPathPrefix + (this.isStandalone ? '' : this.startupService.getTenantIdentifier() ) + this.pastisConfig.pastisNewProfile, {state: response});
}
});
......@@ -310,9 +306,7 @@ export class ListProfileComponent extends SidenavPage<ProfileDescription> implem
const fileReader = new FileReader();
fileReader.readAsText(fileToUpload, 'UTF-8');
fileReader.onload = () => {
// console.log(fileReader.result.toString());
jsonObj = (JSON.parse(fileReader.result.toString()));
console.log(jsonObj.controlSchema);
profileDescription.controlSchema = jsonObj.controlSchema;
archivalProfileUnit = this.noticeService.profileDescriptionToPuaProfile(profileDescription);
this.profileService.updateProfilePua(archivalProfileUnit).subscribe(() => {
......@@ -321,7 +315,7 @@ export class ListProfileComponent extends SidenavPage<ProfileDescription> implem
});
};
fileReader.onerror = (error) => {
console.log(error);
console.error(error);
};
}
}
......@@ -329,34 +323,13 @@ export class ListProfileComponent extends SidenavPage<ProfileDescription> implem
}
changeExpand(element: ProfileDescription) {
if (element.type == 'PA' && !element.path && element.status === 'ACTIVE') {
if (element.type === 'PA' && !element.path && element.status === 'ACTIVE') {
this.expanded = !this.expanded;
}
if (element.type == 'PUA' && element.status == 'ACTIVE'
&& (element.controlSchema == '{}' || !element.controlSchema)) {
if (element.type === 'PUA' && element.status === 'ACTIVE'
&& (element.controlSchema === '{}' || !element.controlSchema)) {
this.expanded = !this.expanded;
}
}
}
@Pipe({name: 'filterByType'})
export class FilterByTypePipe implements PipeTransform {
transform(listOfProfiles: ProfileDescription[], typeToFilter: string): ProfileDescription[] {
if (!listOfProfiles) { return null; }
if (!typeToFilter) { return listOfProfiles; }
if (typeToFilter == 'ALL') { return listOfProfiles; }
return listOfProfiles.filter(profile => profile.type == typeToFilter);
}
}
@Pipe({name: 'filterByStringName'})
export class FilterByStringNamePipe implements PipeTransform {
constructor() {}
private listOfProfiles: ProfileDescription[];
transform(listOfProfiles: ProfileDescription[], nameToFilter: string): ProfileDescription[] {
if (!listOfProfiles) { return null; }
if (!nameToFilter) { return listOfProfiles; }
this.listOfProfiles = listOfProfiles.filter(profile => profile.identifier.toLowerCase().indexOf(nameToFilter.toLowerCase()) >= 0);
return this.listOfProfiles;
}
}
import { Pipe, PipeTransform } from '@angular/core';
import { ProfileDescription } from '../../../models/profile-description.model';
@Pipe({name: 'filterByStringName'})
export class FilterByStringNamePipe implements PipeTransform {
constructor() {}
private listOfProfiles: ProfileDescription[];
transform(listOfProfiles: ProfileDescription[], nameToFilter: string): ProfileDescription[] {
if (!listOfProfiles) { return null; }
if (!nameToFilter) { return listOfProfiles; }
this.listOfProfiles = listOfProfiles.filter(profile => profile.identifier.toLowerCase().indexOf(nameToFilter.toLowerCase()) >= 0);
return this.listOfProfiles;
}
}
\ No newline at end of file
import { Pipe, PipeTransform } from '@angular/core';
import { ProfileDescription } from '../../../models/profile-description.model';
@Pipe({name: 'filterByType'})
export class FilterByTypePipe implements PipeTransform {
transform(listOfProfiles: ProfileDescription[], typeToFilter: string): ProfileDescription[] {
if (!listOfProfiles) { return null; }
if (!typeToFilter) { return listOfProfiles; }
if (typeToFilter === 'ALL') { return listOfProfiles; }
return listOfProfiles.filter(profile => profile.type === typeToFilter);
}
}
\ No newline at end of file
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {Component, EventEmitter, Input, Output} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import {Observable, of} from 'rxjs';
import {catchError} from 'rxjs/operators';
import {diff} from 'ui-frontend-common';
......@@ -14,7 +15,7 @@ import {ProfileDescription} from '../../../../models/profile-description.model';
templateUrl: './profile-information-tab.component.html',
styleUrls: ['./profile-information-tab.component.scss']
})
export class ProfileInformationTabComponent implements OnInit {
export class ProfileInformationTabComponent {
@Input()
set inputProfile(profileDescription: ProfileDescription) {
......@@ -41,7 +42,8 @@ export class ProfileInformationTabComponent implements OnInit {
constructor(private formBuilder: FormBuilder,
private profileService: ProfileService, private loggingService: NotificationService) {
private profileService: ProfileService, private loggingService: NotificationService,
private translateService: TranslateService) {
this.form = this.formBuilder.group({
identifier: [null, Validators.required],
id: [null, Validators.required],
......@@ -93,16 +95,12 @@ export class ProfileInformationTabComponent implements OnInit {
}
prepareSubmit(inputProfile: ProfileDescription): Observable<ProfileDescription> {
console.log(JSON.stringify(inputProfile) + ' inputProfile');
console.log(this.form.getRawValue());
// let diffValue = diff(this.form.getRawValue(), this.previousValue());
if (inputProfile.type == 'PA') {
if (inputProfile.type === 'PA') {
this.profile = Object.assign(this.form.value, this.profile);
console.log(JSON.stringify(this.profile));
return this.profileService.updateProfilePa(this.profile).pipe(catchError(() => of(null)));
} else {
this.archivalProfileUnit = Object.assign( this.form.value, this.archivalProfileUnit);
......@@ -116,14 +114,8 @@ export class ProfileInformationTabComponent implements OnInit {
this.pending = !this.pending;
this.inputProfile = this._inputProfile;
console.log(JSON.stringify(result));
this.loggingService.showSuccess('La modification du profil a bien été effectué');
this.profileService.refreshListProfiles();
this.close.emit(true);