From 72b79946e07e7dbca7444f291b585c059bd08766 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Thu, 18 Aug 2011 07:20:28 +0000
Subject: [PATCH] Refactoring PropertyTypeAssignmentGrid to TypedTableGrid:
 Introducing column IDs and provider.

SVN: 22560
---
 .../PropertyTypeAssignmentGridColumnIDs.java  |  40 ++++
 .../EntityTypePropertyTypeProvider.java       | 182 ++++++++++++++++++
 2 files changed, 222 insertions(+)
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/dto/PropertyTypeAssignmentGridColumnIDs.java
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/EntityTypePropertyTypeProvider.java

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/dto/PropertyTypeAssignmentGridColumnIDs.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/dto/PropertyTypeAssignmentGridColumnIDs.java
new file mode 100644
index 00000000000..1de3bd62b1c
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/dto/PropertyTypeAssignmentGridColumnIDs.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2011 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.generic.client.web.client.dto;
+
+import ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.property_type.PropertyTypeAssignmentGrid;
+
+/**
+ * IDs of columns of {@link PropertyTypeAssignmentGrid}.
+ *
+ * @author Franz-Josef Elmer
+ */
+public class PropertyTypeAssignmentGridColumnIDs
+{
+    public static final String PROPERTY_TYPE_CODE = "PROPERTY_TYPE_CODE";
+    public static final String LABEL = "LABEL";
+    public static final String DESCRIPTION = "DESCRIPTION";
+    public static final String ASSIGNED_TO = "ASSIGNED_TO";
+    public static final String TYPE_OF = "TYPE_OF";
+    public static final String IS_MANDATORY = "IS_MANDATORY";
+    public static final String DATA_TYPE = "DATA_TYPE";
+    public static final String ORDINAL = "ORDINAL";
+    public static final String SECTION = "SECTION";
+    public static final String IS_DYNAMIC = "IS_DYNAMIC";
+    public static final String IS_MANAGED = "IS_MANAGED";
+    public static final String SCRIPT = "SCRIPT";
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/EntityTypePropertyTypeProvider.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/EntityTypePropertyTypeProvider.java
new file mode 100644
index 00000000000..a299c475ca4
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/EntityTypePropertyTypeProvider.java
@@ -0,0 +1,182 @@
+/*
+ * Copyright 2011 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.generic.client.web.server.resultset;
+
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.ASSIGNED_TO;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.DATA_TYPE;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.DESCRIPTION;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.IS_DYNAMIC;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.IS_MANAGED;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.IS_MANDATORY;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.LABEL;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.ORDINAL;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.PROPERTY_TYPE_CODE;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.SCRIPT;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.SECTION;
+import static ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyTypeAssignmentGridColumnIDs.TYPE_OF;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import ch.systemsx.cisd.openbis.generic.shared.ICommonServer;
+import ch.systemsx.cisd.openbis.generic.shared.basic.SimpleYesNoRenderer;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetTypePropertyType;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataTypeCode;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.EntityTypePropertyType;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ExperimentTypePropertyType;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.MaterialType;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.MaterialTypePropertyType;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.PropertyType;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleTypePropertyType;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Script;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.TypedTableModel;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Vocabulary;
+import ch.systemsx.cisd.openbis.generic.shared.util.TypedTableModelBuilder;
+
+/**
+ * Provider of instances of {@link EntityTypePropertyType}.
+ *
+ * @author Franz-Josef Elmer
+ */
+public class EntityTypePropertyTypeProvider extends AbstractCommonTableModelProvider<EntityTypePropertyType<?>>
+{
+
+    public EntityTypePropertyTypeProvider(ICommonServer commonServer, String sessionToken)
+    {
+        super(commonServer, sessionToken);
+    }
+
+    @Override
+    protected TypedTableModel<EntityTypePropertyType<?>> createTableModel()
+    {
+        List<PropertyType> propertyTypes = commonServer.listPropertyTypes(sessionToken, true);
+        List<EntityTypePropertyType<?>> entityTypePropertyTypes = extractAssignments(propertyTypes);
+        TypedTableModelBuilder<EntityTypePropertyType<?>> builder = new TypedTableModelBuilder<EntityTypePropertyType<?>>();
+        builder.addColumn(PROPERTY_TYPE_CODE).withDefaultWidth(200);
+        builder.addColumn(LABEL).hideByDefault();
+        builder.addColumn(DESCRIPTION).hideByDefault();
+        builder.addColumn(ASSIGNED_TO).withDefaultWidth(200);
+        builder.addColumn(TYPE_OF);
+        builder.addColumn(IS_MANDATORY);
+        builder.addColumn(DATA_TYPE).withDefaultWidth(200);
+        builder.addColumn(ORDINAL).withDefaultWidth(100).hideByDefault();
+        builder.addColumn(SECTION);
+        builder.addColumn(IS_DYNAMIC);
+        builder.addColumn(IS_MANAGED);
+        builder.addColumn(SCRIPT);
+        for (EntityTypePropertyType<?> etpt : entityTypePropertyTypes)
+        {
+            builder.addRow(etpt);
+            PropertyType propertyType = etpt.getPropertyType();
+            builder.column(PROPERTY_TYPE_CODE).addString(propertyType.getCode());
+            builder.column(LABEL).addString(propertyType.getLabel());
+            builder.column(DESCRIPTION).addString(propertyType.getDescription());
+            builder.column(ASSIGNED_TO).addString(etpt.getEntityType().getCode());
+            builder.column(TYPE_OF).addString(etpt.getEntityKind().getDescription());
+            builder.column(IS_MANDATORY).addString(SimpleYesNoRenderer.render(etpt.isMandatory()));
+            builder.column(DATA_TYPE).addString(renderDataType(propertyType));
+            builder.column(ORDINAL).addInteger(etpt.getOrdinal());
+            builder.column(SECTION).addString(etpt.getSection());
+            builder.column(IS_DYNAMIC).addString(SimpleYesNoRenderer.render(etpt.isDynamic()));
+            builder.column(IS_MANAGED).addString(SimpleYesNoRenderer.render(etpt.isManaged()));
+            Script script = etpt.getScript();
+            if (script != null)
+            {
+                builder.column(SCRIPT).addString(script.getName());
+            }
+        }
+        return builder.getModel();
+    }
+
+    private static String renderDataType(PropertyType entity)
+    {
+        DataTypeCode dataType = entity.getDataType().getCode();
+        switch (dataType)
+        {
+            case BOOLEAN:
+                return "True / False";
+            case CONTROLLEDVOCABULARY:
+                return "Vocabulary: " + tryGetVocabularyCode(entity);
+            case INTEGER:
+                return "Integer Number";
+            case MATERIAL:
+                String materialTypeCode = tryGetMaterialTypeCode(entity);
+                if (materialTypeCode == null)
+                {
+                    return "Material of Any Type";
+                } else
+                {
+                    return "Material of Type: " + materialTypeCode;
+                }
+            case REAL:
+                return "Float Number";
+            case TIMESTAMP:
+                return "Date and Time";
+            case VARCHAR:
+                return "Text";
+            default:
+                return dataType.name();
+        }
+    }
+
+    private static String tryGetVocabularyCode(PropertyType entity)
+    {
+        Vocabulary vocabulary = entity.getVocabulary();
+        return vocabulary != null ? vocabulary.getCode() : null;
+    }
+
+    private static String tryGetMaterialTypeCode(PropertyType entity)
+    {
+        MaterialType materialType = entity.getMaterialType();
+        return materialType != null ? materialType.getCode() : null;
+    }
+
+    private static List<EntityTypePropertyType<?>> extractAssignments(
+            List<PropertyType> listPropertyTypes)
+    {
+        List<EntityTypePropertyType<?>> result = new ArrayList<EntityTypePropertyType<?>>();
+        for (PropertyType propertyType : listPropertyTypes)
+        {
+            extractAssignments(result, propertyType);
+        }
+        Collections.sort(result);
+        return result;
+    }
+
+    private static void extractAssignments(List<EntityTypePropertyType<?>> result,
+            final PropertyType propertyType)
+    {
+        for (ExperimentTypePropertyType etpt : propertyType.getExperimentTypePropertyTypes())
+        {
+            result.add(etpt);
+        }
+        for (SampleTypePropertyType etpt : propertyType.getSampleTypePropertyTypes())
+        {
+            result.add(etpt);
+        }
+        for (MaterialTypePropertyType etpt : propertyType.getMaterialTypePropertyTypes())
+        {
+            result.add(etpt);
+        }
+        for (DataSetTypePropertyType etpt : propertyType.getDataSetTypePropertyTypes())
+        {
+            result.add(etpt);
+        }
+    }
+
+}
-- 
GitLab