From 07a51ab6c05d216b2345a6e8e06df2c1caff2f57 Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Fri, 17 Sep 2010 11:13:28 +0000
Subject: [PATCH] [LMS-1761] changed comment in templates used for batch
 update; use '<DELETE>' to mark deletion of value in sample batch update (not
 working for properties yet)

SVN: 17889
---
 .../openbis/generic/server/CommonServer.java  | 107 ++++++++++--------
 .../UpdatedSampleParserObjectFactory.java     |  68 ++++++++++-
 .../UpdatedSampleParserObjectFactoryTest.java |   3 +-
 3 files changed, 125 insertions(+), 53 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java
index 7464d42f06b..0b18247b6b7 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java
@@ -186,7 +186,7 @@ import ch.systemsx.cisd.openbis.generic.shared.util.EntityHelper;
 import ch.systemsx.cisd.openbis.generic.shared.util.HibernateUtils;
 
 public final class CommonServer extends AbstractCommonServer<ICommonServerForInternalUse> implements
-         ICommonServerForInternalUse
+        ICommonServerForInternalUse
 {
     private final LastModificationState lastModificationState;
 
@@ -220,7 +220,7 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
     {
         return new CommonServerLogger(getSessionManager(), context);
     }
-    
+
     //
     // ISystemAuthenticator
     //
@@ -233,19 +233,20 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
         role.setDatabaseInstance(getDAOFactory().getHomeDatabaseInstance());
         role.setRole(RoleCode.ADMIN);
         systemUser.addRoleAssignment(role);
-        String sessionToken = sessionManager.tryToOpenSession(systemUser.getUserId(), new IPrincipalProvider()
-            {
-                public Principal tryToGetPrincipal(String userID)
-                {
-                    return new Principal(systemUser.getUserId(), systemUser.getFirstName(),
-                            systemUser.getLastName(), systemUser.getEmail(), true);
-                }
-            });
+        String sessionToken =
+                sessionManager.tryToOpenSession(systemUser.getUserId(), new IPrincipalProvider()
+                    {
+                        public Principal tryToGetPrincipal(String userID)
+                        {
+                            return new Principal(systemUser.getUserId(), systemUser.getFirstName(),
+                                    systemUser.getLastName(), systemUser.getEmail(), true);
+                        }
+                    });
         Session session = sessionManager.getSession(sessionToken);
         session.setPerson(systemUser);
         return tryGetSession(sessionToken);
     }
-    
+
     //
     // IGenericServer
     //
@@ -429,8 +430,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
         {
             IHibernateSearchDAO searchDAO = getDAOFactory().getHibernateSearchDAO();
             final Collection<Long> sampleIds =
-                    searchDAO.searchForEntityIds(criteria,
-                            DtoConverters.convertEntityKind(EntityKind.SAMPLE));
+                    searchDAO.searchForEntityIds(criteria, DtoConverters
+                            .convertEntityKind(EntityKind.SAMPLE));
             final ISampleLister sampleLister = businessObjectFactory.createSampleLister(session);
             return sampleLister.list(new ListOrSearchSampleCriteria(sampleIds));
         } catch (final DataAccessException ex)
@@ -644,8 +645,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
         Session session = getSession(sessionToken);
 
         IEntityTypePropertyTypeBO etptBO =
-                businessObjectFactory.createEntityTypePropertyTypeBO(session,
-                        DtoConverters.convertEntityKind(entityKind));
+                businessObjectFactory.createEntityTypePropertyTypeBO(session, DtoConverters
+                        .convertEntityKind(entityKind));
         etptBO.loadAssignment(propertyTypeCode, entityTypeCode);
         etptBO.updateLoadedAssignment(isMandatory, defaultValue, section, previousETPTOrdinal);
     }
@@ -657,8 +658,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
         Session session = getSession(sessionToken);
 
         IEntityTypePropertyTypeBO etptBO =
-                businessObjectFactory.createEntityTypePropertyTypeBO(session,
-                        DtoConverters.convertEntityKind(entityKind));
+                businessObjectFactory.createEntityTypePropertyTypeBO(session, DtoConverters
+                        .convertEntityKind(entityKind));
         etptBO.loadAssignment(propertyTypeCode, entityTypeCode);
         etptBO.deleteLoadedAssignment();
     }
@@ -670,8 +671,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
         Session session = getSession(sessionToken);
 
         IEntityTypePropertyTypeBO etptBO =
-                businessObjectFactory.createEntityTypePropertyTypeBO(session,
-                        DtoConverters.convertEntityKind(entityKind));
+                businessObjectFactory.createEntityTypePropertyTypeBO(session, DtoConverters
+                        .convertEntityKind(entityKind));
         return etptBO.countAssignmentValues(propertyTypeCode, entityTypeCode);
     }
 
@@ -783,8 +784,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
             IHibernateSearchDAO searchDAO = getDAOFactory().getHibernateSearchDAO();
 
             final Collection<Long> datasetIds =
-                    searchDAO.searchForEntityIds(criteria,
-                            DtoConverters.convertEntityKind(EntityKind.DATA_SET));
+                    searchDAO.searchForEntityIds(criteria, DtoConverters
+                            .convertEntityKind(EntityKind.DATA_SET));
             final IDatasetLister datasetLister = createDatasetLister(session);
             return datasetLister.listByDatasetIds(datasetIds);
         } catch (final DataAccessException ex)
@@ -930,8 +931,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
             getDAOFactory().getFileFormatTypeDAO().createOrUpdate(fileFormatType);
         } catch (final DataAccessException ex)
         {
-            DataAccessExceptionTranslator.throwException(ex,
-                    String.format("File format type '%s' ", fileFormatType.getCode()), null);
+            DataAccessExceptionTranslator.throwException(ex, String.format(
+                    "File format type '%s' ", fileFormatType.getCode()), null);
         }
     }
 
@@ -1199,9 +1200,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
         {
             IExperimentBO experimentBO = businessObjectFactory.createExperimentBO(session);
             experimentBO.loadDataByTechId(experimentId);
-            return AttachmentTranslator.translate(
-                    listHolderAttachments(session, experimentBO.getExperiment()),
-                    session.getBaseIndexURL());
+            return AttachmentTranslator.translate(listHolderAttachments(session, experimentBO
+                    .getExperiment()), session.getBaseIndexURL());
         } catch (final DataAccessException ex)
         {
             throw createUserFailureException(ex);
@@ -1215,9 +1215,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
         {
             ISampleBO sampleBO = businessObjectFactory.createSampleBO(session);
             sampleBO.loadDataByTechId(sampleId);
-            return AttachmentTranslator
-                    .translate(listHolderAttachments(session, sampleBO.getSample()),
-                            session.getBaseIndexURL());
+            return AttachmentTranslator.translate(listHolderAttachments(session, sampleBO
+                    .getSample()), session.getBaseIndexURL());
         } catch (final DataAccessException ex)
         {
             throw createUserFailureException(ex);
@@ -1231,9 +1230,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
         {
             IProjectBO projectBO = businessObjectFactory.createProjectBO(session);
             projectBO.loadDataByTechId(projectId);
-            return AttachmentTranslator.translate(
-                    listHolderAttachments(session, projectBO.getProject()),
-                    session.getBaseIndexURL());
+            return AttachmentTranslator.translate(listHolderAttachments(session, projectBO
+                    .getProject()), session.getBaseIndexURL());
         } catch (final DataAccessException ex)
         {
             throw createUserFailureException(ex);
@@ -1376,8 +1374,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
     {
         if (entityOrNull == null)
         {
-            throw UserFailureException.fromTemplate("There is no %s with permId '%s'.",
-                    kind.getDescription(), permId);
+            throw UserFailureException.fromTemplate("There is no %s with permId '%s'.", kind
+                    .getDescription(), permId);
         }
         return createInformationHolder(kind, entityOrNull);
     }
@@ -1480,9 +1478,10 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
                 } catch (DataIntegrityViolationException ex)
                 {
                     throw new UserFailureException(
-                            String.format(
-                                    "File format type '%s' is being used. Use 'Data Set Search' to find all connected data sets.",
-                                    code));
+                            String
+                                    .format(
+                                            "File format type '%s' is being used. Use 'Data Set Search' to find all connected data sets.",
+                                            code));
                 }
             }
         }
@@ -1511,11 +1510,12 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
             if (types.size() != 1)
             {
                 section =
-                        String.format(
-                                "[%s]\n%s%s\n",
-                                entityType.getCode(),
-                                firstSection ? "# Comments must be located after the type declaration ('[TYPE]').\n"
-                                        : "", section);
+                        String
+                                .format(
+                                        "[%s]\n%s%s\n",
+                                        entityType.getCode(),
+                                        firstSection ? "# Comments must be located after the type declaration ('[TYPE]').\n"
+                                                : "", section);
             }
             sb.append(section);
             firstSection = false;
@@ -1523,6 +1523,11 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
         return sb.toString();
     }
 
+    private static final String UPDATE_TEMPLATE_COMMENT =
+            "# If one doesn't want to modify values in a column the column can be removed completely from the file.\n"
+                    + "# Empty value in a column also means that the value stored in openBIS shouldn't be changed.\n "
+                    + "# To delete a value/connection from openBIS one needs to put '<DELETE>' into the corresponding cell.\n";
+
     private String createTemplateForType(EntityKind entityKind, boolean autoGenerate,
             EntityTypePE entityType, boolean addComments, boolean withExperiments,
             BatchOperationKind operationKind)
@@ -1561,16 +1566,22 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
             }
             sb.append(column);
         }
-        if (entityKind.equals(EntityKind.SAMPLE) && addComments)
+        if (addComments)
         {
-
             switch (operationKind)
             {
                 case REGISTRATION:
-                    sb.insert(0, NewSample.SAMPLE_REGISTRATION_TEMPLATE_COMMENT);
+                    if (entityKind.equals(EntityKind.SAMPLE))
+                    {
+                        sb.insert(0, NewSample.SAMPLE_REGISTRATION_TEMPLATE_COMMENT);
+                    }
                     break;
                 case UPDATE:
-                    sb.insert(0, UpdatedSample.SAMPLE_UPDATE_TEMPLATE_COMMENT);
+                    sb.insert(0, UPDATE_TEMPLATE_COMMENT);
+                    if (entityKind.equals(EntityKind.SAMPLE))
+                    {
+                        sb.insert(0, UpdatedSample.SAMPLE_UPDATE_TEMPLATE_COMMENT);
+                    }
                     break;
             }
         }
@@ -1702,8 +1713,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
         IExternalDataTable externalDataTable =
                 businessObjectFactory.createExternalDataTable(session);
         Map<String, String> parameterBindings = new HashMap<String, String>();
-        externalDataTable.processDatasets(serviceDescription.getKey(),
-                serviceDescription.getDatastoreCode(), datasetCodes, parameterBindings);
+        externalDataTable.processDatasets(serviceDescription.getKey(), serviceDescription
+                .getDatastoreCode(), datasetCodes, parameterBindings);
     }
 
     public void registerAuthorizationGroup(String sessionToken,
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/UpdatedSampleParserObjectFactory.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/UpdatedSampleParserObjectFactory.java
index a9fa808590c..e19e568b759 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/UpdatedSampleParserObjectFactory.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/UpdatedSampleParserObjectFactory.java
@@ -18,6 +18,7 @@ package ch.systemsx.cisd.openbis.plugin.generic.client.web.server.parser;
 
 import ch.systemsx.cisd.common.parser.IPropertyMapper;
 import ch.systemsx.cisd.common.parser.ParserException;
+import ch.systemsx.cisd.common.shared.basic.utils.StringUtils;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewSample;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleBatchUpdateDetails;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType;
@@ -30,17 +31,21 @@ import ch.systemsx.cisd.openbis.generic.shared.basic.dto.UpdatedSample;
  */
 final class UpdatedSampleParserObjectFactory extends NewSampleParserObjectFactory
 {
-    private final SampleBatchUpdateDetails batchUpdateDetails;
+    private final SampleBatchUpdateDetails basicBatchUpdateDetails;
 
     UpdatedSampleParserObjectFactory(final SampleType sampleType,
             final IPropertyMapper propertyMapper, boolean identifierExpectedInFile,
             boolean allowExperiments)
     {
         super(sampleType, propertyMapper, identifierExpectedInFile, allowExperiments);
-        this.batchUpdateDetails = createBatchUpdateDetails();
+        this.basicBatchUpdateDetails = createBasicBatchUpdateDetails();
     }
 
-    private SampleBatchUpdateDetails createBatchUpdateDetails()
+    /**
+     * Prepares details about which values should be updated in general taking into account only the
+     * information about availability of columns in the file.
+     */
+    private SampleBatchUpdateDetails createBasicBatchUpdateDetails()
     {
         boolean updateExperiment = isColumnAvailable(UpdatedSample.EXPERIMENT);
         boolean updateParent = isColumnAvailable(UpdatedSample.PARENT);
@@ -57,6 +62,61 @@ final class UpdatedSampleParserObjectFactory extends NewSampleParserObjectFactor
     public NewSample createObject(final String[] lineTokens) throws ParserException
     {
         final NewSample newSample = super.createObject(lineTokens);
-        return new UpdatedSample(newSample, batchUpdateDetails);
+        final SampleBatchUpdateDetails updateDetails = createBatchUpdateDetails(newSample);
+        cleanUp(newSample);
+        return new UpdatedSample(newSample, updateDetails);
     }
+
+    //
+
+    /**
+     * Returns details about which values should be updated for the specified sample. If a cell was
+     * left empty in the file the corresponding value will not be modified.
+     */
+    private SampleBatchUpdateDetails createBatchUpdateDetails(NewSample newSample)
+    {
+        boolean updateExperiment =
+                basicBatchUpdateDetails.isExperimentUpdateRequested()
+                        && isNotEmpty(newSample.getExperimentIdentifier());
+        boolean updateParent =
+                basicBatchUpdateDetails.isParentUpdateRequested()
+                        && isNotEmpty(newSample.getParentIdentifier());
+        boolean updateContainer =
+                basicBatchUpdateDetails.isContainerUpdateRequested()
+                        && isNotEmpty(newSample.getContainerIdentifier());
+
+        // TODO 2010-09-17, Piotr Buczek: properties
+        return new SampleBatchUpdateDetails(updateExperiment, updateParent, updateContainer,
+                basicBatchUpdateDetails.getPropertiesToUpdate());
+    }
+
+    /** Cleans the placeholders used to mark deletion of values in the specified sample. */
+    private void cleanUp(NewSample newSample)
+    {
+        if (shouldDelete(newSample.getExperimentIdentifier()))
+        {
+            newSample.setExperimentIdentifier(null);
+        }
+        if (shouldDelete(newSample.getParentIdentifier()))
+        {
+            newSample.setParentIdentifier(null);
+        }
+        if (shouldDelete(newSample.getContainerIdentifier()))
+        {
+            newSample.setContainerIdentifier(null);
+        }
+    }
+
+    private static final String DELETE = "<DELETE>";
+
+    private static boolean isNotEmpty(String value)
+    {
+        return StringUtils.isBlank(value) == false;
+    }
+
+    private static boolean shouldDelete(String value)
+    {
+        return DELETE.equals(value);
+    }
+
 }
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/UpdatedSampleParserObjectFactoryTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/UpdatedSampleParserObjectFactoryTest.java
index 96165eecf65..13da86e57eb 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/UpdatedSampleParserObjectFactoryTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/UpdatedSampleParserObjectFactoryTest.java
@@ -100,7 +100,8 @@ public final class UpdatedSampleParserObjectFactoryTest
         assertEquals(2, propertiesToUpdate.size());
         assertTrue(propertiesToUpdate.contains(PROPERTY_1));
         assertTrue(propertiesToUpdate.contains(PROPERTY_2));
-        assertTrue(batchUpdateDetails.isContainerUpdateRequested());
+        assertEquals(StringUtils.isBlank(lineTokens[2]) == false, batchUpdateDetails
+                .isContainerUpdateRequested());
         assertFalse(batchUpdateDetails.isParentUpdateRequested());
         assertFalse(batchUpdateDetails.isExperimentUpdateRequested());
     }
-- 
GitLab