From b1589a571fcc82d9a41aed0bd04958f24242ee68 Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Tue, 27 Oct 2009 21:25:09 +0000
Subject: [PATCH] [LMS-1194] improved DAO implementation, fixed bugs

SVN: 13101
---
 .../property_type/SectionSelectionWidget.java |  7 ++++
 .../business/bo/EntityTypePropertyTypeBO.java |  6 +--
 .../dataaccess/db/EntityPropertyTypeDAO.java  | 40 +++++++++----------
 .../dataaccess/db/VocabularyTermDAO.java      |  5 +++
 .../generic/shared/dto/TableNames.java        |  2 -
 5 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/property_type/SectionSelectionWidget.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/property_type/SectionSelectionWidget.java
index b249294bc39..e129765b298 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/property_type/SectionSelectionWidget.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/property_type/SectionSelectionWidget.java
@@ -27,6 +27,7 @@ import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
 import ch.systemsx.cisd.openbis.generic.client.web.client.ICommonClientServiceAsync;
 import ch.systemsx.cisd.openbis.generic.client.web.client.application.Dict;
 import ch.systemsx.cisd.openbis.generic.client.web.client.application.IViewContext;
+import ch.systemsx.cisd.openbis.generic.client.web.client.application.util.StringUtils;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.EntityTypePropertyType;
 
 /**
@@ -70,4 +71,10 @@ public final class SectionSelectionWidget extends SimpleComboBox<String>
         }
         add(sections);
     }
+
+    @Override
+    public String getSimpleValue()
+    {
+        return StringUtils.trimToNull(super.getSimpleValue());
+    }
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityTypePropertyTypeBO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityTypePropertyTypeBO.java
index 8895309c37b..c0a4b11b871 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityTypePropertyTypeBO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityTypePropertyTypeBO.java
@@ -153,12 +153,11 @@ public class EntityTypePropertyTypeBO extends AbstractBusinessObject implements
         if (assignment.getOrdinal().equals(currentOrdinal) == false)
         {
             increaseOrdinals(assignment.getEntityType(), currentOrdinal, 1);
-            assignment.setOrdinal(currentOrdinal);
         }
+        assignment.setOrdinal(currentOrdinal);
         assignment.setSection(section);
-        assignment.setMandatory(isMandatory);
         // fill missing property values if we change from optional to mandatory
-        if (isMandatory)
+        if (isMandatory && (assignment.isMandatory() == false))
         {
             final EntityTypePE entityType = assignment.getEntityType();
             final PropertyTypePE propertyType = assignment.getPropertyType();
@@ -173,6 +172,7 @@ public class EntityTypePropertyTypeBO extends AbstractBusinessObject implements
             addPropertyWithDefaultValue(entityType, propertyType, defaultValue, entities,
                     errorMsgTemplate);
         }
+        assignment.setMandatory(isMandatory);
         validateAndSave();
     }
 
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityPropertyTypeDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityPropertyTypeDAO.java
index dc77a8bafee..faad74b2ae4 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityPropertyTypeDAO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityPropertyTypeDAO.java
@@ -44,7 +44,6 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.PropertyTypePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.VocabularyPE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.VocabularyTermWithStats;
 import ch.systemsx.cisd.openbis.generic.shared.dto.properties.EntityKind;
-import ch.systemsx.cisd.openbis.generic.shared.util.HibernateUtils;
 
 /**
  * The unique {@link IEntityPropertyTypeDAO} implementation.
@@ -230,6 +229,7 @@ final class EntityPropertyTypeDAO extends AbstractDAO implements IEntityProperty
                         .format(
                                 "from %s props join fetch props.entity where props.vocabularyTerm.code = ?",
                                 entityKind.getEntityPropertyClass().getSimpleName());
+        //
         List<EntityPropertyPE> properties =
                 cast(getHibernateTemplate().find(query, toArray(vocabularyTermCode)));
         if (operationLog.isDebugEnabled())
@@ -257,28 +257,26 @@ final class EntityPropertyTypeDAO extends AbstractDAO implements IEntityProperty
 
     public void increaseOrdinals(EntityTypePE entityType, Long fromOrdinal, int increment)
     {
-        // TODO 2009-10-26, Piotr Buczek: try to rewrite in HQL
-        Long entityTypeId = HibernateUtils.getId(entityType);
-        String entityTypeIdColumnName = "";
-        String entityTypePropertyTypeTableName = entityKind.name() + "_type_property_types";
-        switch (entityKind)
+        assert entityType != null : "Unspecified entity type.";
+        assert fromOrdinal != null : "Unspecified ordinal.";
+
+        final HibernateTemplate hibernateTemplate = getHibernateTemplate();
+
+        String query =
+                String.format("UPDATE %s etpt SET etpt.ordinal = etpt.ordinal + ? "
+                        + "WHERE etpt.entityTypeInternal = ? AND etpt.ordinal >= ?", entityKind
+                        .getEntityTypePropertyTypeAssignmentClass().getSimpleName());
+        final int updatedRows =
+                hibernateTemplate.bulkUpdate(query, toArray(new Long(increment), entityType,
+                        fromOrdinal));
+        hibernateTemplate.flush();
+
+        if (operationLog.isInfoEnabled())
         {
-            case DATA_SET:
-                entityTypeIdColumnName = "dsty_id";
-                break;
-            case EXPERIMENT:
-                entityTypeIdColumnName = "exty_id";
-                break;
-            case SAMPLE:
-                entityTypeIdColumnName = "saty_id";
-                break;
-            case MATERIAL:
-                entityTypeIdColumnName = "maty_id";
-                break;
+            operationLog.debug(String.format(
+                    "%d etpt(s) updated for entity type '%s' with ordinal increased by %d.",
+                    updatedRows, entityType.getCode(), increment));
         }
-        executeUpdate("UPDATE " + entityTypePropertyTypeTableName + " SET ordinal = ordinal + ?"
-                + " WHERE " + entityTypeIdColumnName + " = ? AND ordinal >= ?", increment,
-                entityTypeId, fromOrdinal);
     }
 
     public final void validateAndSaveUpdatedEntity(EntityTypePropertyTypePE entity)
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/VocabularyTermDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/VocabularyTermDAO.java
index 545dbc9bc91..b73b8bb09f1 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/VocabularyTermDAO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/VocabularyTermDAO.java
@@ -60,6 +60,11 @@ final class VocabularyTermDAO extends AbstractGenericEntityDAO<VocabularyTermPE>
     public void increaseVocabularyTermOrdinals(VocabularyPE vocabulary, Long fromOrdinal,
             int increment)
     {
+        assert vocabulary != null : "Unspecified vocabulary.";
+        assert fromOrdinal != null : "Unspecified ordinal.";
+
+        // We could use HQL like in EntityPropertyTypeDAO.increaseOrdinals() instead of SQL
+        // but the connection between terms and vocabulary would need to be bidirectional.
         Long vocabularyId = HibernateUtils.getId(vocabulary);
         executeUpdate("UPDATE " + TableNames.CONTROLLED_VOCABULARY_TERM_TABLE
                 + " SET ordinal = ordinal + ?" + " WHERE covo_id = ? AND ordinal >= ?", increment,
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/TableNames.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/TableNames.java
index dc496d40c0a..cd4906bae97 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/TableNames.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/TableNames.java
@@ -28,8 +28,6 @@ public final class TableNames
 
     public static final String CONTROLLED_VOCABULARY_TERM_TABLE = "controlled_vocabulary_terms";
 
-    public static final Object CONTROLLED_VOCABULARY_TERMS_TABLE = "controlled_vocabulary_terms";
-
     public static final String DATA_SET_RELATIONSHIPS_TABLE = "data_set_relationships";
 
     public static final String DATA_SET_TYPES_TABLE = "data_set_types";
-- 
GitLab