/
create-link-resource.component.ts
158 lines (135 loc) · 6.35 KB
/
create-link-resource.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { Component, EventEmitter, Inject, Input, OnInit, Output, ViewChild } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import {
ApiResponseError,
Constants,
CreateFileValue,
CreateResource,
CreateTextValueAsString,
CreateValue,
KnoraApiConnection,
ReadResource,
ResourceClassAndPropertyDefinitions,
ResourceClassDefinition,
ResourcePropertyDefinition
} from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { DialogEvent } from 'src/app/main/dialog/dialog.component';
import { ErrorHandlerService } from 'src/app/main/services/error-handler.service';
import { SelectPropertiesComponent } from '../../resource-instance-form/select-properties/select-properties.component';
@Component({
selector: 'app-create-link-resource',
templateUrl: './create-link-resource.component.html',
styleUrls: ['./create-link-resource.component.scss']
})
export class CreateLinkResourceComponent implements OnInit {
@Input() parentResource: ReadResource;
@Input() propDef: string;
@Input() resourceClassDef: string;
@Input() currentOntoIri: string;
@Output() closeDialog: EventEmitter<ReadResource | DialogEvent> = new EventEmitter<ReadResource | DialogEvent>();
@ViewChild('selectProps') selectPropertiesComponent: SelectPropertiesComponent;
properties: ResourcePropertyDefinition[];
propertiesForm: FormGroup;
resourceClass: ResourceClassDefinition;
ontologyInfo: ResourceClassAndPropertyDefinitions;
fileValue: CreateFileValue;
hasFileValue: 'stillImage' | 'movingImage' | 'audio' | 'document' | 'text' | 'archive';
propertiesObj = {};
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _fb: FormBuilder,
private _errorHandler: ErrorHandlerService,
) { }
ngOnInit(): void {
this.propertiesForm = this._fb.group({});
this._dspApiConnection.v2.ontologyCache.getResourceClassDefinition(this.resourceClassDef).subscribe(
(onto: ResourceClassAndPropertyDefinitions) => {
this.ontologyInfo = onto;
this.resourceClass = onto.classes[this.resourceClassDef];
this.properties = onto.getPropertyDefinitionsByType(ResourcePropertyDefinition).filter(
prop =>
!prop.isLinkProperty &&
prop.isEditable &&
prop.id !== Constants.HasStillImageFileValue &&
prop.id !== Constants.HasDocumentFileValue &&
prop.id !== Constants.HasAudioFileValue &&
prop.id !== Constants.HasArchiveFileValue &&
prop.id !== Constants.HasMovingImageFileValue &&
prop.id !== Constants.HasTextFileValue
);
if (onto.properties[Constants.HasStillImageFileValue]) {
this.hasFileValue = 'stillImage';
} else if (onto.properties[Constants.HasDocumentFileValue]) {
this.hasFileValue = 'document';
} else if (onto.properties[Constants.HasAudioFileValue]) {
this.hasFileValue = 'audio';
} else if (onto.properties[Constants.HasArchiveFileValue]) {
this.hasFileValue = 'archive';
} else if (onto.properties[Constants.HasMovingImageFileValue]) {
this.hasFileValue = 'movingImage';
} else if (onto.properties[Constants.HasTextFileValue]) {
this.hasFileValue = 'text';
} else {
this.hasFileValue = undefined;
}
}
);
}
onSubmit() {
if (this.propertiesForm.valid) {
const createResource = new CreateResource();
const resLabelVal = <CreateTextValueAsString>this.selectPropertiesComponent.createValueComponent.getNewValue();
createResource.label = resLabelVal.text;
createResource.type = this.resourceClassDef;
// todo: find a better way to do this
createResource.attachedToProject = 'http://rdfh.ch/projects/' + this.resourceClassDef.split('/')[4];
this.selectPropertiesComponent.switchPropertiesComponent.forEach((child) => {
const createVal = child.createValueComponent.getNewValue();
const iri = child.property.id;
if (createVal instanceof CreateValue) {
if (this.propertiesObj[iri]) {
// if a key already exists, add the createVal to the array
this.propertiesObj[iri].push(createVal);
} else {
// if no key exists, add one and add the createVal as the first value of the array
this.propertiesObj[iri] = [createVal];
}
}
});
if (this.fileValue) {
switch (this.hasFileValue) {
case 'stillImage':
this.propertiesObj[Constants.HasStillImageFileValue] = [this.fileValue];
break;
case 'document':
this.propertiesObj[Constants.HasDocumentFileValue] = [this.fileValue];
break;
case 'audio':
this.propertiesObj[Constants.HasAudioFileValue] = [this.fileValue];
break;
case 'archive':
this.propertiesObj[Constants.HasArchiveFileValue] = [this.fileValue];
}
}
createResource.properties = this.propertiesObj;
this._dspApiConnection.v2.res.createResource(createResource).subscribe(
(res: ReadResource) => {
this.closeDialog.emit(res);
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
} else {
this.propertiesForm.markAllAsTouched();
}
}
onCancel() {
// emit DialogCanceled event
this.closeDialog.emit(DialogEvent.DialogCanceled);
}
setFileValue(file: CreateFileValue) {
this.fileValue = file;
}
}