1111
1212package org .kitodo .production .forms .createprocess ;
1313
14+ import static org .kitodo .api .validation .State .ERROR ;
1415import static org .kitodo .constants .StringConstants .CREATE ;
1516
1617import java .io .IOException ;
1920import java .util .Arrays ;
2021import java .util .Collection ;
2122import java .util .Collections ;
23+ import java .util .HashMap ;
2224import java .util .LinkedList ;
2325import java .util .List ;
2426import java .util .Locale ;
4345import org .kitodo .api .dataformat .Workpiece ;
4446import org .kitodo .api .externaldatamanagement .ImportConfigurationType ;
4547import org .kitodo .api .schemaconverter .MetadataFormat ;
48+ import org .kitodo .api .validation .ValidationResult ;
4649import org .kitodo .constants .StringConstants ;
4750import org .kitodo .data .database .beans .Client ;
4851import org .kitodo .data .database .beans .ImportConfiguration ;
@@ -114,6 +117,7 @@ public class CreateProcessForm extends BaseForm implements MetadataTreeTableInte
114117 private String xmlString ;
115118 private String filename ;
116119 protected int numberOfEadElements ;
120+ private HashMap <String , ValidationResult > validationResultHashMap = new HashMap <>();
117121
118122 public CreateProcessForm () {
119123 priorityList = ServiceManager .getUserService ().getCurrentMetadataLanguage ();
@@ -367,6 +371,8 @@ public String createNewProcess() {
367371 Helper .setErrorMessage ("rulesetNotFound" , new Object [] {rulesetFile }, logger , e );
368372 } catch (IOException | ProcessGenerationException e ) {
369373 logger .error (e .getLocalizedMessage (), e );
374+ } catch (DAOException e ) {
375+ Helper .setErrorMessage ("Error validating process metadata" , e );
370376 }
371377 return this .stayOnCurrentPage ;
372378 }
@@ -378,7 +384,7 @@ public String createNewProcess() {
378384 */
379385 public String createNewProcessAndContinue () {
380386 String destination = createNewProcess ();
381- if (!destination .equals (processListPath )) {
387+ if (!processListPath .equals (destination )) {
382388 return destination ;
383389 }
384390 Process parentProcess = titleRecordLinkTab .getTitleRecordProcess ();
@@ -390,7 +396,8 @@ public String createNewProcessAndContinue() {
390396 + "&faces-redirect=true" ;
391397 }
392398
393- private boolean canCreateProcess () throws IOException {
399+ private boolean canCreateProcess () throws IOException , DAOException {
400+ validationResultHashMap = new HashMap <>();
394401 if (Objects .nonNull (titleRecordLinkTab .getTitleRecordProcess ())) {
395402 if ((Objects .isNull (titleRecordLinkTab .getSelectedInsertionPosition ())
396403 || titleRecordLinkTab .getSelectedInsertionPosition ().isEmpty ())) {
@@ -404,9 +411,60 @@ private boolean canCreateProcess() throws IOException {
404411 return false ;
405412 }
406413 }
414+ // validate process and potential ancestors
415+ for (TempProcess tempProcess : processes ) {
416+ ValidationResult result = ServiceManager .getMetadataValidationService ().validate (
417+ tempProcess .getWorkpiece (), rulesetManagement , false );
418+ if (ERROR .equals (result .getState ())) {
419+ validationResultHashMap .put (getCatalogId (tempProcess ), result );
420+ }
421+ }
422+ // validate potential process children
423+ for (TempProcess tempProcess : childProcesses ) {
424+ ValidationResult result = ServiceManager .getMetadataValidationService ().validate (
425+ tempProcess .getWorkpiece (), rulesetManagement , false );
426+ if (ERROR .equals (result .getState ())) {
427+ validationResultHashMap .put (getCatalogId (tempProcess ), result );
428+ }
429+ }
430+ if (!validationResultHashMap .isEmpty ()) {
431+ Helper .setErrorMessage ("dataEditor.validation.state.error" );
432+ for (Map .Entry <String , ValidationResult > resultEntry : validationResultHashMap .entrySet ()) {
433+ if (processes .size () > 1 || childProcesses .size () > 1 ) {
434+ Helper .setErrorMessage (Helper .getTranslation ("process" ) + ": " + resultEntry .getKey ());
435+ }
436+ for (String message : resultEntry .getValue ().getResultMessages ()) {
437+ Helper .setErrorMessage (" - " + message );
438+ }
439+ }
440+ PrimeFaces .current ().ajax ().update ("editForm:processFromTemplateTabView:processHierarchyContent" );
441+ return false ;
442+ }
407443 return true ;
408444 }
409445
446+ /**
447+ * Get CSS style classes for UI button representing given TempProcess "process"
448+ * in import masks "Process hierarchy" panel as whitespace separated string of class names.
449+ * Always contains class name 'carousel-button'.
450+ * Also contains class name
451+ * - 'selected' if given process is currently selected in the import mask
452+ * - 'validation-error' if ruleset based metadata validation failed for given process
453+ * @param process TempProcess for which CSS style classes are returned
454+ * @return String containing style classes, separated by whitespaces, for given process
455+ */
456+ public String getProcessButtonStyleClass (TempProcess process ) {
457+ String styleClass = "carousel-button" ;
458+ if (currentProcess .equals (process )) {
459+ styleClass = styleClass + " selected" ;
460+ }
461+ String catalogId = getCatalogId (process );
462+ if (!validationResultHashMap .isEmpty () && validationResultHashMap .containsKey (catalogId )) {
463+ styleClass = styleClass + " validation-error" ;
464+ }
465+ return styleClass ;
466+ }
467+
410468 private String parentTypeIfForbidden () throws IOException {
411469 URI metadataFileUri = ServiceManager .getProcessService ()
412470 .getMetadataFileUri (titleRecordLinkTab .getTitleRecordProcess ());
0 commit comments