Skip to content

Commit

Permalink
Merge pull request 'vocabulary_performance' (#17) from vocabulary_per…
Browse files Browse the repository at this point in the history
  • Loading branch information
rsehr committed Nov 6, 2024
2 parents 9ce9695 + e775898 commit 8894185
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ public EntityConfig getConfiguration() {
configuration = new EntityConfig(config, true);
sourceVocabulary = vocabularyAPIManager.vocabularies().get(configuration.getSourceVocabularyId());
} catch (RuntimeException e) {
Helper.setFehlerMeldung(e.getMessage());
Helper.setFehlerMeldung(e);
}
}
return configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.apache.commons.lang.StringUtils;

import io.goobi.vocabulary.exchange.FieldDefinition;
import io.goobi.vocabulary.exchange.FieldInstance;
import io.goobi.vocabulary.exchange.TranslationInstance;
import io.goobi.vocabulary.exchange.VocabularyRecord;
import io.goobi.workflow.api.vocabulary.VocabularyAPIManager;
import io.goobi.workflow.api.vocabulary.helper.ExtendedVocabulary;
import io.goobi.workflow.api.vocabulary.helper.ExtendedVocabularyRecord;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -192,45 +194,39 @@ public void setVocabulary(String name, String id) {

if ("vocabularyList".equals(fieldType)) {

List<ExtendedVocabularyRecord> recordList = vocabularyAPIManager.vocabularyRecords()
.list(currentVocabulary.getId())
.all()
.request()
List<VocabularyRecord> recordList = vocabularyAPIManager.vocabularyRecords()
.listPlain(currentVocabulary.getId())
.getContent();
recordList.sort((r1, r2) -> r1.getMainValue().compareToIgnoreCase(r2.getMainValue()));

vocabularyList = new ArrayList<>(recordList.size());

for (ExtendedVocabularyRecord vr : recordList) {
for (VocabularyRecord vr : recordList) {
VocabularyEntry ve = new VocabularyEntry();
ve.setId(vr.getId());
ve.setMainValue(vr.getMainValue());
ve.setEntryUrl(vr.getURI());
vr.getFields()
.stream()
.filter(f -> f.getDefinitionId().equals(mainFieldId)) // Should be a single one
.flatMap(f -> f.getValues().stream()) // TODO: Ignore multi-values (for now)
.flatMap(v -> v.getTranslations().stream())
.forEach(t -> {
if (t.getLanguage() != null) { // In case a switch on null leads to errors
switch (t.getLanguage()) {
case "eng":
ve.setLabelEn(t.getValue());
break;
case "fre":
ve.setLabelFr(t.getValue());
break;
case "ger":
ve.setLabelDe(t.getValue());
break;
default:
throw new IllegalArgumentException("Unknown language \"" + t.getLanguage() + "\"");
}

ve.setEntryUrl(vr.get_links().get("self").getHref());

for (FieldInstance efi : vr.getFields()) {
if (efi.getDefinitionId().equals(mainFieldId)) {
List<TranslationInstance> translations = efi.getValues().get(0).getTranslations();

for (TranslationInstance ti : translations) {
if ("eng".equals(ti.getLanguage())) {
ve.setLabelEn(ti.getValue());
} else if ("ger".equals(ti.getLanguage())) {
ve.setLabelDe(ti.getValue());
} else if ("fre".equals(ti.getLanguage())) {
ve.setLabelFr(ti.getValue());
}
});
}
}
}
vocabularyList.add(ve);
}

vocabularyList.sort((r1, r2) -> r1.getLabelEn().compareToIgnoreCase(r2.getLabelEn()));
}

}

public void adMetadataField(MetadataField metadataField) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.lang.StringUtils;

import io.goobi.vocabulary.exchange.FieldDefinition;
import io.goobi.vocabulary.exchange.FieldInstance;
import io.goobi.vocabulary.exchange.TranslationInstance;
import io.goobi.vocabulary.exchange.Vocabulary;
import io.goobi.vocabulary.exchange.VocabularyRecord;
import io.goobi.workflow.api.vocabulary.VocabularyAPIManager;
import io.goobi.workflow.api.vocabulary.helper.ExtendedFieldInstance;
import io.goobi.workflow.api.vocabulary.helper.ExtendedVocabularyRecord;
import io.goobi.workflow.api.vocabulary.helper.ExtendedVocabulary;
import lombok.Getter;
import lombok.Setter;

Expand Down Expand Up @@ -130,20 +134,27 @@ public EntityConfig(XMLConfiguration config, boolean extendedConfiguration) {
ConfiguredField metadataField = extractField(field);
newType.addMetadataField(metadataField);
}

for (HierarchicalConfiguration field : type.configurationsAt("/relations/relation")) {
long id = field.getLong("@id", 0L);
String name = field.getString("@name");
boolean reverse = field.getBoolean("@reverse", false);
String destinationEntity = field.getString("@destinationEntity");
String sourceEntity = entityName;
if (id != 0) {
List<ExtendedVocabularyRecord> records = vocabularyAPIManager.vocabularyRecords()
.list(id)
.all()
.request()
ExtendedVocabulary vocabulary = vocabularyAPIManager.vocabularies().get(id);
List<FieldDefinition> fieldDefinitions =
vocabularyAPIManager.vocabularySchemas().get(vocabulary.getSchemaId()).getDefinitions();

long relationshipTypeId = extractFieldId(vocabulary, fieldDefinitions, "Relationship type");
long reverseRelationshipTypeId = extractFieldId(vocabulary, fieldDefinitions, "Reverse relationship");
long dateBeginningAllowedId = extractFieldId(vocabulary, fieldDefinitions, "Date beginning allowed");
long dateEndAllowedId = extractFieldId(vocabulary, fieldDefinitions, "Date end allowed");
long additionalTextFieldAllowedId = extractFieldId(vocabulary, fieldDefinitions, "Additional text field allowed");

List<VocabularyRecord> records = vocabularyAPIManager.vocabularyRecords()
.listPlain(id)
.getContent();
for (ExtendedVocabularyRecord rec : records) {
for (VocabularyRecord rec : records) {
RelationshipType relationType = new RelationshipType();
relationType.setReversed(reverse);
relationType.setSourceType(sourceEntity);
Expand All @@ -152,49 +163,67 @@ public EntityConfig(XMLConfiguration config, boolean extendedConfiguration) {
relationType.setVocabularyName(name);

relationType.setVocabularyUrl(rec.get_links().get("vocabulary").getHref());
for (ExtendedFieldInstance efi : rec.getExtendedFields()) {
switch (efi.getDefinition().getName()) {
case "Relationship type":
if (reverse) {
relationType.setReversedRelationshipNameEn(efi.getFieldValue("eng"));
relationType.setReversedRelationshipNameDe(efi.getFieldValue("ger"));
relationType.setReversedRelationshipNameFr(efi.getFieldValue("fre"));
} else {
relationType.setRelationshipNameEn(efi.getFieldValue("eng"));
relationType.setRelationshipNameDe(efi.getFieldValue("ger"));
relationType.setRelationshipNameFr(efi.getFieldValue("fre"));
for (FieldInstance efi : rec.getFields()) {

if (efi.getDefinitionId().equals(relationshipTypeId)) {

List<TranslationInstance> translations = efi.getValues().get(0).getTranslations();
if (reverse) {
for (TranslationInstance ti : translations) {
if ("eng".equals(ti.getLanguage())) {
relationType.setReversedRelationshipNameEn(ti.getValue());
} else if ("ger".equals(ti.getLanguage())) {
relationType.setReversedRelationshipNameDe(ti.getValue());
} else if ("fre".equals(ti.getLanguage())) {
relationType.setReversedRelationshipNameFr(ti.getValue());
}
}

break;
case "Reverse relationship":
if (reverse) {
relationType.setRelationshipNameEn(efi.getFieldValue("eng"));
relationType.setRelationshipNameDe(efi.getFieldValue("ger"));
relationType.setRelationshipNameFr(efi.getFieldValue("fre"));
} else {
relationType.setReversedRelationshipNameEn(efi.getFieldValue("eng"));
relationType.setReversedRelationshipNameDe(efi.getFieldValue("ger"));
relationType.setReversedRelationshipNameFr(efi.getFieldValue("fre"));
} else {
for (TranslationInstance ti : translations) {
if ("eng".equals(ti.getLanguage())) {
relationType.setRelationshipNameEn(ti.getValue());
} else if ("ger".equals(ti.getLanguage())) {
relationType.setRelationshipNameDe(ti.getValue());
} else if ("fre".equals(ti.getLanguage())) {
relationType.setRelationshipNameFr(ti.getValue());
}
}

break;
case "Date beginning allowed":
if ("yes".equals(efi.getFieldValue())) {
relationType.setDisplayStartDate(true);
}
} else if (efi.getDefinitionId().equals(reverseRelationshipTypeId)) {
List<TranslationInstance> translations = efi.getValues().get(0).getTranslations();
if (!reverse) {
for (TranslationInstance ti : translations) {
if ("eng".equals(ti.getLanguage())) {
relationType.setReversedRelationshipNameEn(ti.getValue());
} else if ("ger".equals(ti.getLanguage())) {
relationType.setReversedRelationshipNameDe(ti.getValue());
} else if ("fre".equals(ti.getLanguage())) {
relationType.setReversedRelationshipNameFr(ti.getValue());
}
}
break;
case "Date end allowed":
if ("yes".equals(efi.getFieldValue())) {
relationType.setDisplayEndDate(true);
} else {
for (TranslationInstance ti : translations) {
if ("eng".equals(ti.getLanguage())) {
relationType.setRelationshipNameEn(ti.getValue());
} else if ("ger".equals(ti.getLanguage())) {
relationType.setRelationshipNameDe(ti.getValue());
} else if ("fre".equals(ti.getLanguage())) {
relationType.setRelationshipNameFr(ti.getValue());
}
}
break;
case "Additional text field allowed":
if ("yes".equals(efi.getFieldValue())) {
relationType.setDisplayAdditionalData(true);
}
break;
default:
}

} else if (efi.getDefinitionId().equals(dateBeginningAllowedId)
&& "yes".equals(efi.getValues().get(0).getTranslations().get(0).getValue())) {
relationType.setDisplayStartDate(true);

} else if (efi.getDefinitionId().equals(dateEndAllowedId)
&& "yes".equals(efi.getValues().get(0).getTranslations().get(0).getValue())) {
relationType.setDisplayEndDate(true);

} else if (efi.getDefinitionId().equals(additionalTextFieldAllowedId)
&& "yes".equals(efi.getValues().get(0).getTranslations().get(0).getValue())) {
relationType.setDisplayAdditionalData(true);
}

}
Expand All @@ -208,6 +237,16 @@ public EntityConfig(XMLConfiguration config, boolean extendedConfiguration) {
}

}

}

private long extractFieldId(Vocabulary vocabulary, List<FieldDefinition> fieldDefinitions, String fieldName) {
return fieldDefinitions.stream()
.filter(d -> fieldName.equals(d.getName()))
.map(FieldDefinition::getId)
.findFirst()
.orElseThrow(() -> new IllegalStateException("Vocabulary \"" + vocabulary.getName() + "\" [" + vocabulary.getId()
+ "] does not contain required field \"" + fieldName + "\""));
}

private ConfiguredField extractField(HierarchicalConfiguration field) {
Expand Down

0 comments on commit 8894185

Please sign in to comment.