From a1facf47c26b59575414ae43b1a503b53c0fac5d Mon Sep 17 00:00:00 2001
From: kaloyane <kaloyane>
Date: Mon, 25 Jul 2011 16:10:02 +0000
Subject: [PATCH] [LMS-2401] Sanofi: make library template a managed property
 (and thus hide it from the standard view)

SVN: 22261
---
 .../sanofi-managed-property.py                |  82 +++++++++++++
 .../managed_property/sanofi-upgrade.sql       | 109 ++++++++++++++++++
 2 files changed, 191 insertions(+)
 create mode 100644 sanofi/resource/managed_property/sanofi-managed-property.py
 create mode 100644 sanofi/resource/managed_property/sanofi-upgrade.sql

diff --git a/sanofi/resource/managed_property/sanofi-managed-property.py b/sanofi/resource/managed_property/sanofi-managed-property.py
new file mode 100644
index 00000000000..2f5e059eee8
--- /dev/null
+++ b/sanofi/resource/managed_property/sanofi-managed-property.py
@@ -0,0 +1,82 @@
+from java.text import DecimalFormat
+from ch.systemsx.cisd.common.geometry import ConversionUtils
+
+def parseLines(value):
+    lines = []
+    if value:
+        lines = value.strip().split("\n")
+        
+    return [ line.split("\t") for line in lines ]
+
+def getMaxWidth(lines):
+  maxWidth = 0
+  for line in lines:
+    maxWidth = max(maxWidth, len(line))
+  return maxWidth
+
+def isControlWell(value):
+    return value in ["H", "L"]
+    
+def isCompoundWell(value):
+    try:
+        numberParser = DecimalFormat()
+        numberParser.parse(value)
+        return True
+    except:
+        return False
+
+def validate(lines):
+  for line in lines:
+      for value in line:
+          normalizedValue = value.strip().upper()
+          if normalizedValue:
+              if isControlWell(normalizedValue):
+                  pass
+              elif isCompoundWell(normalizedValue):
+                  pass
+              else:
+                  raise ValidationException("Invalid value found in template : " + normalizedValue)
+          
+
+def configureUI():
+    propValue = ""
+    if not property.isSpecialValue():
+        propValue = property.getValue()
+    lines = parseLines(propValue)
+    tableBuilder = createTableBuilder()
+    
+    if lines:
+        width = getMaxWidth(lines)
+        header = [ str(item) for item in range(1, width + 1) ]
+        header.insert(0, " ")  
+        tableBuilder.addFullHeader(header)
+
+        for i in range(0, len(lines)):
+            rowLetterCode = ConversionUtils.translateRowNumberIntoLetterCode(i+1)
+            row = [ rowLetterCode ] + lines[i]
+            tableBuilder.addFullRow(row)
+ 
+    property.setOwnTab(True)
+    uiDesc = property.getUiDescription()
+    uiDesc.useTableOutput(tableBuilder.getTableModel())
+
+    factory = inputWidgetFactory()
+    editAction = uiDesc.addTableAction("Edit").setDescription("Edit the library template. "
+                                   "Copy the template from Excel and paste it in the text are below.")
+    textArea = factory.createMultilineTextInputField("Text")
+    textArea.setValue(propValue)
+    editAction.addInputWidgets([ textArea ])
+    
+def updateFromUI(action):
+    """in the "Edit" action we simply replace the entire property content"""
+    if action.getName() == "Edit":
+        newValue = action.getInputWidgetDescriptions()[0].getValue()
+        lines = parseLines(newValue)
+        validate(lines)
+        if not newValue:
+            property.setValue("")
+        else:
+            property.setValue(newValue)
+    
+
+
diff --git a/sanofi/resource/managed_property/sanofi-upgrade.sql b/sanofi/resource/managed_property/sanofi-upgrade.sql
new file mode 100644
index 00000000000..9126e592af7
--- /dev/null
+++ b/sanofi/resource/managed_property/sanofi-upgrade.sql
@@ -0,0 +1,109 @@
+DELETE 
+   FROM scripts 
+   WHERE NAME = 'SANOFI_LIBRARY_TEMPLATE';
+
+INSERT
+  INTO scripts 
+      (id, dbin_id, name, description, pers_id_registerer, entity_kind, script_type, script) 
+  
+  VALUES
+     ( 
+       nextval('script_id_seq'), 
+       (select max(id) from database_instances),
+       'SANOFI_LIBRARY_TEMPLATE',
+       'Script for managed property library template',
+       (select id from persons where user_id='admin'),
+       'EXPERIMENT',
+       'MANAGED_PROPERTY',
+  E'
+from java.text import DecimalFormat
+from ch.systemsx.cisd.common.geometry import ConversionUtils
+
+def parseLines(value):
+    lines = []
+    if value:
+        lines = value.strip().split("\\n")
+        
+    return [ line.split("\\t") for line in lines ]
+
+def getMaxWidth(lines):
+  maxWidth = 0
+  for line in lines:
+    maxWidth = max(maxWidth, len(line))
+  return maxWidth
+
+def isControlWell(value):
+    return value in ["H", "L"]
+    
+def isCompoundWell(value):
+    try:
+        numberParser = DecimalFormat()
+        numberParser.parse(value)
+        return True
+    except:
+        return False
+
+def validate(lines):
+  for line in lines:
+      for value in line:
+          normalizedValue = value.strip().upper()
+          if normalizedValue:
+              if isControlWell(normalizedValue):
+                  pass
+              elif isCompoundWell(normalizedValue):
+                  pass
+              else:
+                  raise ValidationException("Invalid value found in template : " + normalizedValue)
+          
+
+def configureUI():
+    propValue = ""
+    if not property.isSpecialValue():
+        propValue = property.getValue()
+    lines = parseLines(propValue)
+    tableBuilder = createTableBuilder()
+    
+    if lines:
+        width = getMaxWidth(lines)
+        header = [ str(item) for item in range(1, width + 1) ]
+        header.insert(0, " ")  
+        tableBuilder.addFullHeader(header)
+
+        for i in range(0, len(lines)):
+            rowLetterCode = ConversionUtils.translateRowNumberIntoLetterCode(i+1)
+            row = [ rowLetterCode ] + lines[i]
+            tableBuilder.addFullRow(row)
+ 
+    property.setOwnTab(True)
+    uiDesc = property.getUiDescription()
+    uiDesc.useTableOutput(tableBuilder.getTableModel())
+
+    factory = inputWidgetFactory()
+    editAction = uiDesc.addTableAction("Edit").setDescription("Edit the library template. "
+                                   "Copy the template from Excel and paste it in the text are below.")
+    textArea = factory.createMultilineTextInputField("Text")
+    textArea.setValue(propValue)
+    editAction.addInputWidgets([ textArea ])
+    
+def updateFromUI(action):
+    """in the "Edit" action we simply replace the entire property content"""
+    if action.getName() == "Edit":
+        newValue = action.getInputWidgetDescriptions()[0].getValue()
+        lines = parseLines(newValue)
+        validate(lines)
+        if not newValue:
+            property.setValue("")
+        else:
+            property.setValue(newValue)
+    
+');
+  
+  
+UPDATE experiment_type_property_types 
+SET 
+   is_managed_internally=True,
+   script_id=(select id from scripts where name='SANOFI_LIBRARY_TEMPLATE')
+WHERE
+   prty_id = (select id from property_types where code='LIBRARY_TEMPLATE')
+ AND
+   exty_id = (select id from experiment_types where code='COMPOUND_HCS')
\ No newline at end of file
-- 
GitLab