From f1559d804b4627ea7fc07fd85a7c2b27495e5fe7 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Mon, 26 Oct 2009 16:14:22 +0000
Subject: [PATCH] LMS-1206 First improvement.

SVN: 13071
---
 .../application/InfoBoxCallbackListener.java  |  2 +-
 .../server/business/bo/VocabularyBO.java      | 27 ++++++++++++++++++-
 .../generic/shared/dto/VocabularyPE.java      |  4 +--
 .../server/business/bo/VocabularyBOTest.java  |  2 +-
 4 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/InfoBoxCallbackListener.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/InfoBoxCallbackListener.java
index 440453f98c3..799af461dac 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/InfoBoxCallbackListener.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/InfoBoxCallbackListener.java
@@ -42,6 +42,6 @@ public class InfoBoxCallbackListener<T> extends CallbackListenerAdapter<T>
             final AbstractAsyncCallback<T> callback, final String failureMessage,
             final Throwable throwable)
     {
-        infoBox.displayError(failureMessage);
+        infoBox.displayError(failureMessage.replace("\n", "<br>"));
     }
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBO.java
index bd0d4c4959e..9d31ce62d4a 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBO.java
@@ -23,6 +23,7 @@ import java.util.List;
 import java.util.Set;
 
 import org.springframework.dao.DataAccessException;
+import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.dao.DataRetrievalFailureException;
 
 import ch.systemsx.cisd.common.collections.IKeyExtractor;
@@ -55,6 +56,7 @@ import ch.systemsx.cisd.openbis.generic.shared.util.HibernateUtils;
  */
 public class VocabularyBO extends AbstractBusinessObject implements IVocabularyBO
 {
+    private static final int MAX_NUMBER_OF_INVAID_TERMS_IN_ERROR_MESSAGE = 10;
     private VocabularyPE vocabularyPE;
 
     public VocabularyBO(final IDAOFactory daoFactory, final Session session)
@@ -209,9 +211,32 @@ public class VocabularyBO extends AbstractBusinessObject implements IVocabularyB
         assert vocabularyPE != null : "Unspecified vocabulary";
         try
         {
+            StringBuilder builder = new StringBuilder();
+            int numberOfInvalidTerms = 0;
             for (VocabularyTermPE term : vocabularyPE.getTerms())
             {
-                getVocabularyTermDAO().validate(term);
+                try
+                {
+                    getVocabularyTermDAO().validate(term);
+                } catch (DataIntegrityViolationException ex)
+                {
+                    numberOfInvalidTerms++;
+                    if (numberOfInvalidTerms <= MAX_NUMBER_OF_INVAID_TERMS_IN_ERROR_MESSAGE)
+                    {
+                        builder.append('\n').append(ex.getMessage());
+                    }
+                }
+            }
+            if (builder.length() > 0)
+            {
+                builder.insert(0, "Invalid terms:");
+                int additionalTerms = numberOfInvalidTerms - MAX_NUMBER_OF_INVAID_TERMS_IN_ERROR_MESSAGE;
+                if (additionalTerms > 0)
+                {
+                    builder.append("\n").append("and ").append(additionalTerms);
+                    builder.append(" more invalid terms.");
+                }
+                throw new UserFailureException("Invalid terms:" + builder);
             }
             getVocabularyDAO().createOrUpdateVocabulary(vocabularyPE);
         } catch (final DataAccessException e)
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/VocabularyPE.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/VocabularyPE.java
index 8e1cc5e887b..80912fcdd83 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/VocabularyPE.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/VocabularyPE.java
@@ -19,7 +19,7 @@ package ch.systemsx.cisd.openbis.generic.shared.dto;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Date;
-import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -79,7 +79,7 @@ public class VocabularyPE extends HibernateAbstractRegistrationHolder implements
 
     private String description;
 
-    private Set<VocabularyTermPE> terms = new HashSet<VocabularyTermPE>();
+    private Set<VocabularyTermPE> terms = new LinkedHashSet<VocabularyTermPE>();
 
     private boolean managedInternally;
 
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBOTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBOTest.java
index bcc3194dcd1..39e71f14e95 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBOTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBOTest.java
@@ -206,7 +206,7 @@ public final class VocabularyBOTest extends AbstractBOTest
                     one(daoFactory).getHomeDatabaseInstance();
                     will(returnValue(ManagerTestTool.EXAMPLE_DATABASE_INSTANCE));
 
-                    one(vocabularyTermDAO).validate(with(aNonNull(VocabularyTermPE.class)));
+                    exactly(3).of(vocabularyTermDAO).validate(with(aNonNull(VocabularyTermPE.class)));
                     will(throwException(new DataIntegrityViolationException(null)));
                 }
             });
-- 
GitLab