11package de .intranda .goobi .plugins ;
22
3- import java .io .FileOutputStream ;
4- import java .io .IOException ;
5- import java .io .OutputStream ;
3+ import java .io .*;
64import java .nio .file .Path ;
75import java .nio .file .Paths ;
86import java .text .SimpleDateFormat ;
97import java .util .ArrayList ;
108import java .util .List ;
119
10+ import io .goobi .workflow .api .vocabulary .APIException ;
11+ import io .goobi .workflow .api .vocabulary .helper .APIExceptionExtractor ;
1212import org .apache .commons .configuration .HierarchicalConfiguration ;
1313import org .apache .commons .lang3 .StringUtils ;
1414import org .apache .jena .datatypes .xsd .XSDDatatype ;
3232import de .sub .goobi .persistence .managers .ProcessManager ;
3333import de .sub .goobi .persistence .managers .ProjectManager ;
3434import de .sub .goobi .persistence .managers .PropertyManager ;
35+ import io .goobi .workflow .api .connection .HttpUtils ;
3536import io .goobi .workflow .api .vocabulary .VocabularyAPIManager ;
3637import io .goobi .workflow .api .vocabulary .helper .ExtendedVocabulary ;
3738import io .goobi .workflow .api .vocabulary .helper .ExtendedVocabularyRecord ;
4344import lombok .extern .log4j .Log4j2 ;
4445import net .xeoh .plugins .base .annotations .PluginImplementation ;
4546
47+ import javax .xml .transform .*;
48+ import javax .xml .transform .stream .StreamResult ;
49+ import javax .xml .transform .stream .StreamSource ;
50+
4651@ PluginImplementation
4752@ Log4j2
4853public class ArcheProjectExportAdministrationPlugin implements IAdministrationPlugin {
@@ -154,23 +159,7 @@ public void setProjectId(Integer inProjektAuswahl) {
154159 String vocabularyName = hc .getString ("/vocabulary/@name" );
155160 String labelField = hc .getString ("/vocabulary/@label" );
156161 String valueField = hc .getString ("/vocabulary/@value" );
157-
158- ExtendedVocabulary currentVocabulary = VocabularyAPIManager .getInstance ().vocabularies ().findByName (vocabularyName );
159-
160- List <ExtendedVocabularyRecord > recordList = VocabularyAPIManager .getInstance ()
161- .vocabularyRecords ()
162- .list (currentVocabulary .getId ())
163- .all ()
164- .request ()
165- .getContent ();
166-
167- for (ExtendedVocabularyRecord rec : recordList ) {
168- String label = rec .getFieldForDefinitionName (labelField ).get ().getFieldValue ();
169- String value = rec .getFieldForDefinitionName (valueField ).get ().getFieldValue ();
170-
171- pp .getPossibleValues ().add (new SelectItem (value , label ));
172- }
173- pp .setType (org .goobi .production .properties .Type .LIST );
162+ initializeProperty (vocabularyName , labelField , valueField , pp );
174163 }
175164
176165 for (HierarchicalConfiguration selectItem : hc .configurationsAt ("/select" )) {
@@ -180,6 +169,32 @@ public void setProjectId(Integer inProjektAuswahl) {
180169 }
181170 }
182171
172+ private static void initializeProperty (String vocabularyName , String labelField , String valueField , DisplayProperty pp ) {
173+ try {
174+ ExtendedVocabulary currentVocabulary = VocabularyAPIManager .getInstance ().vocabularies ().findByName (vocabularyName );
175+
176+ List <ExtendedVocabularyRecord > recordList = VocabularyAPIManager .getInstance ()
177+ .vocabularyRecords ()
178+ .list (currentVocabulary .getId ())
179+ .all ()
180+ .request ()
181+ .getContent ();
182+
183+ pp .getPossibleValues ().clear ();
184+ for (ExtendedVocabularyRecord rec : recordList ) {
185+ String label = rec .getFieldForDefinitionName (labelField ).get ().getFieldValue ();
186+ String value = rec .getFieldForDefinitionName (valueField ).get ().getFieldValue ();
187+
188+ pp .getPossibleValues ().add (new SelectItem (value , label ));
189+ }
190+ } catch (APIException e ) {
191+ APIExceptionExtractor extractor = new APIExceptionExtractor (e );
192+ String message = "Failed to load vocabulary \" " + vocabularyName + "\" records, Reason: \n " + extractor .getLocalizedMessage (Helper .getSessionLocale ());
193+ log .error (message , e );
194+ Helper .setFehlerMeldung (message , e .getMessage ());
195+ }
196+ }
197+
183198 public List <Project > getPossibleProjects () {
184199 if (possibleProjects == null ) {
185200 log .trace ("project list is not initialized, load them from database" );
@@ -457,4 +472,57 @@ private void writePropertyValue(String languageCode, Model model, Resource proje
457472 }
458473 }
459474 }
475+
476+ public void updateVocabulary (DisplayProperty property ) {
477+ HierarchicalConfiguration hc = archeConfiguration .getConfig ().configurationAt ("/project/property[@name='" + property .getName () + "']" );
478+ String vocabularyName = hc .getString ("/vocabulary/@name" );
479+ String labelField = hc .getString ("/vocabulary/@label" );
480+ String valueField = hc .getString ("/vocabulary/@value" );
481+
482+ try {
483+ long vocabularyId = VocabularyAPIManager .getInstance ().vocabularies ().findByName (vocabularyName ).getId ();
484+
485+ String skosURI = hc .getString ("/vocabulary/@url" );
486+ String xsltPath = hc .getString ("/vocabulary/@xslt" );
487+
488+ // if uri and xslt are configured
489+ if (StringUtils .isNotBlank (skosURI ) && StringUtils .isNotBlank (xsltPath )) {
490+
491+ // get data from uri
492+ String data = HttpUtils .getStringFromUrl (skosURI );
493+
494+ // if data is found
495+ if (StringUtils .isNotBlank (data )) {
496+ try {
497+ StreamSource input = new StreamSource (new StringReader (data ));
498+ StreamSource xslt = new StreamSource (xsltPath );
499+
500+ TransformerFactory transformerFactory = TransformerFactory .newInstance ();
501+ Transformer transformer = transformerFactory .newTransformer (xslt );
502+
503+ ByteArrayOutputStream csvOutputStream = new ByteArrayOutputStream ();
504+ transformer .transform (input , new StreamResult (csvOutputStream ));
505+ ByteArrayInputStream csvInputStream = new ByteArrayInputStream (csvOutputStream .toByteArray ());
506+ VocabularyAPIManager .getInstance ().vocabularies ().cleanImportCsv (vocabularyId , csvInputStream );
507+
508+ initializeProperty (vocabularyName , labelField , valueField , property );
509+ } catch (TransformerException e ) {
510+ String message = "Error while transforming vocabulary data from " + skosURI + " to CSV" ;
511+ log .error (message , e );
512+ Helper .setFehlerMeldung (message , e .getMessage ());
513+ } catch (APIException e ) {
514+ APIExceptionExtractor extractor = new APIExceptionExtractor (e );
515+ String message = "Failed to update vocabulary \" " + vocabularyName + "\" , Reason: \n " + extractor .getLocalizedMessage (Helper .getSessionLocale ());
516+ log .error (message , e );
517+ Helper .setFehlerMeldung (message , e .getMessage ());
518+ }
519+ }
520+ }
521+ } catch (APIException e ) {
522+ APIExceptionExtractor extractor = new APIExceptionExtractor (e );
523+ String message = "Failed to update vocabulary \" " + vocabularyName + "\" , Reason: \n " + extractor .getLocalizedMessage (Helper .getSessionLocale ());
524+ log .error (message , e );
525+ Helper .setFehlerMeldung (message , e .getMessage ());
526+ }
527+ }
460528}
0 commit comments