diff --git a/README.md b/README.md index 3631d405f..7d8bdd6cd 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ # ISPyB Project [![Build Status](https://travis-ci.org/antolinos/ispyb.png)](https://travis-ci.org/antolinos/ispyb) -## Installing +1. [Installing](#installing) +2. [Versioning](#versioning) +3. [Database creation and update](#database-creation-and-update) +4. [Database schema](#database-schema) +5. [Graylog on Widlfly 8.2](#graylog) + +# Installing 1. Clone or fork the ISPyB repository and then clone it by typing: ``` @@ -147,3 +153,41 @@ Please do not forget to update the database schema : https://github.com/ispyb/ISPyB/blob/master/documentation/database/ISPyB_DataModel_5.mwb This schema can be updated using MySQLWorkbench (free tool from MySQL). + + +### Graylog + +Download [biz.paluch.logging](http://logging.paluch.biz) that provides logging to logstash using the Graylog Extended Logging Format (GELF) 1.0 and 1.1. + +Then create a custom handler on standalone.xml +``` + + + ... + + + + + + + + + + + ... + + + + + + + +``` + +I had some problems with the unvalid messages because of timestapPatter. It was fixed by using: +``` + +``` + + + diff --git a/configuration/documentation/ISPyB_AdminGuide.doc b/configuration/documentation/ISPyB_AdminGuide.doc deleted file mode 100644 index 7d126ce29..000000000 Binary files a/configuration/documentation/ISPyB_AdminGuide.doc and /dev/null differ diff --git a/configuration/documentation/ISPyB_DevelopersGuide.doc b/configuration/documentation/ISPyB_DevelopersGuide.doc deleted file mode 100644 index 8d05a3abc..000000000 Binary files a/configuration/documentation/ISPyB_DevelopersGuide.doc and /dev/null differ diff --git a/configuration/documentation/ISPyB_wildfly.doc b/configuration/documentation/ISPyB_wildfly.doc deleted file mode 100644 index f727d7aaf..000000000 Binary files a/configuration/documentation/ISPyB_wildfly.doc and /dev/null differ diff --git a/configuration/documentation/MariaDB_installation_CentOS.txt b/configuration/documentation/MariaDB_installation_CentOS.txt deleted file mode 100644 index e4906e6bd..000000000 --- a/configuration/documentation/MariaDB_installation_CentOS.txt +++ /dev/null @@ -1,131 +0,0 @@ -INSTALLATION of MaraiaDB and Connection on CentOS 7: --------------------------------------------------- - -0/ OS: CentOS Linux release 7.1.1503 (Core) x64 - [root@srvdb ~]# tail /etc/redhat-release - CentOS Linux release 7.1.1503 (Core) - -1/ Make sure that you have internet access: - [root@srvdb tmp]# wget http://google.fr - -2/ Adding the MariaDB YUM Repository: - [root@srvdb ~]# vi /etc/yum.repos.d/MariaDB.repo - [mariadb] - name = MariaDB - baseurl = http://yum.mariadb.org/10.1/centos7-amd64 - gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB - gpgcheck=1 - -3/ Installing MariaDB: - [root@srvdb ~]# yum install MariaDB-server MariaDB-client - -4/ Mariadb stat, start and Set to start on boot: - [root@srvdb ~]# systemctl status mariadb - [root@srvdb ~]# systemctl start mariadb - [root@srvdb ~]# systemctl enable mariadb - -5/ Login and View Existing Databases: - [root@srvdb ~]# mysql - -6/ First Installation of MariaDB: - [root@srvdb ~]# mysql_secure_installation - login: root - root password: PPPPPPPP - Remove anonymous user : Yes - disable root login remotely: Yes - remove test database and access to it: Yes - Reload privileges tables now: Yes - - [root@srvdb ~]# systemctl restart mariadb - -7/ Connection and set password: - [root@srvdb ~]# mysql -u root -p - Enter password: PPPPPPP - -8/ Importing ISPyB schema into MaraiDB - Create Database and User Account: - --------------------------------- - [root@srvdb ~]# mysql -u root -p - Enter password: PPPPPPPP - - MariaDB [(none)]> use mysql; - MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS `ispyb_config` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; - MariaDB [(mysql)]> GRANT ALL PRIVILEGES ON *.* TO 'ispyb_config'@'localhost' IDENTIFIED BY 'PPPPPPPP' WITH GRANT OPTION; - - MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS `ispyb_db` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; - MariaDB [(mysql)]> GRANT ALL PRIVILEGES ON *.* TO 'ispyb_db'@'localhost' IDENTIFIED BY 'PPPPPPPP' WITH GRANT OPTION; - - MariaDB [(mysql)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost'; - - MariaDB [(mysql)]> FLUSH PRIVILEGES; - - MariaDB [mysql]> quit; - Bye - - See - ---- - MariaDB [(mysql)]> SELECT User,Host,Password FROM mysql.user; - +--------------+---------------+-------------------------------------------+ - | User | Host | Password | - +--------------+---------------+-------------------------------------------+ - | root | localhost | *DCDD362A8D61A6EF15DBC1FCA1D97E0B0F6F134B | - | root | 127.0.0.1 | *DCDD362A8D61A6EF15DBC1FCA1D97E0B0F6F134B | - | root | ::1 | *DCDD362A8D61A6EF15DBC1FCA1D97E0B0F6F134B | - | ispyb_db | 127.0.0.1 | *B659BF6BE0C776E1949529F99ADEE9D7E9061468 | - | ispyb_config | 127.0.0.1 | *6560351E4A775F27F05DD50F9E6C06BD79008276 | - | ispyb_config | localhost | *6560351E4A775F27F05DD50F9E6C06BD79008276 | - | ispyb_db | localhost | *B659BF6BE0C776E1949529F99ADEE9D7E9061468 | - +--------------+---------------+-------------------------------------------+ - 16 rows in set (0.00 sec) - - -9/ Log into new data: - [root@srvdb ~]# mysql -u ispyb_config -p - Enter password: PPPPPPPPP - -10/ Solve case sensitive issue in linux by declaring case sensitive attribute in the '/etc/my.conf' file - [root@srvdb ~]# vi /etc/my.cnf - ... - #added by IC to tell MySQL that table name comparaison should not be case sensitive - lower_case_table_names=1 - - # Disabling symbolic-links is recommended to prevent assorted security risks - symbolic-links=0 - -11/ Import ispyb_config and ispyb_db into the MaraiDB: - Before doing that you need to export ispyb schema and data from your MySQL database for instance into 'export_ispyb_config.sql' and 'export_ispyb_db.sql' files. - - [root@srvdb ~]# systemctl restart mariadb - [root@srvdb ~]# mysql -u ispyb_config -p --database=ispyb_config < /path_to/export_ispyb_config.sql - Enter password: PPPPPPPP - - [root@srvdb ~]# mysql -u ispyb_db -p --database=ispyb_db < /path_to/export_ispyb_db.sql - Enter password: PPPPPPPP - -12/ Test of MaraiDB: - In the ISPyB server: - 1/ Stop the ISPyB server (Wildfly server) - [root@ispyb ~]# systemctl stop wildfly - - 2/ Update the url and credentials to connect to the MaraiDB - [root@ispyb ~]# vi %WILDFLY_HOME%/standalone/configuration/standalone.xml - - - jdbc:mysql://pydevserv.esrf.fr:3306/pydb - mysql-connector-java-5.1.21.jar - - UUUUUUUU - PPPPPPPP - - - - - - - - 3/ Start the ISpyB server - [root@ispyb ~]# systemctl start wildfly - - 4/ Log into the ISPyB web site and test it. - -That's. diff --git a/configuration/documentation/QuickAdaptToWildfly.docx b/configuration/documentation/QuickAdaptToWildfly.docx deleted file mode 100644 index 1f99b727a..000000000 Binary files a/configuration/documentation/QuickAdaptToWildfly.docx and /dev/null differ diff --git a/configuration/documentation/database/ISPyB_DataModel.mwb b/configuration/documentation/database/ISPyB_DataModel.mwb deleted file mode 100644 index a855d898f..000000000 Binary files a/configuration/documentation/database/ISPyB_DataModel.mwb and /dev/null differ diff --git a/configuration/documentation/database/ISPyB_DataModel_4-0-5.mwb b/configuration/documentation/database/ISPyB_DataModel_4-0-5.mwb deleted file mode 100644 index 64fe90bf8..000000000 Binary files a/configuration/documentation/database/ISPyB_DataModel_4-0-5.mwb and /dev/null differ diff --git a/configuration/documentation/database/ISPyB_DataModel_4-0-5.pdf b/configuration/documentation/database/ISPyB_DataModel_4-0-5.pdf deleted file mode 100644 index 907289133..000000000 Binary files a/configuration/documentation/database/ISPyB_DataModel_4-0-5.pdf and /dev/null differ diff --git a/configuration/documentation/database/ISPyB_DataModel_4-0-5.png b/configuration/documentation/database/ISPyB_DataModel_4-0-5.png deleted file mode 100644 index 7d7c64982..000000000 Binary files a/configuration/documentation/database/ISPyB_DataModel_4-0-5.png and /dev/null differ diff --git a/configuration/documentation/database/ISPyB_DataModel_BX.png b/configuration/documentation/database/ISPyB_DataModel_BX.png deleted file mode 100644 index d0f63bc27..000000000 Binary files a/configuration/documentation/database/ISPyB_DataModel_BX.png and /dev/null differ diff --git a/configuration/documentation/database/ISPyB_DataModel_mx.png b/configuration/documentation/database/ISPyB_DataModel_mx.png deleted file mode 100644 index 5822d1aea..000000000 Binary files a/configuration/documentation/database/ISPyB_DataModel_mx.png and /dev/null differ diff --git a/configuration/documentation/database/ISPyB_DataModel_mx_autoproc.png b/configuration/documentation/database/ISPyB_DataModel_mx_autoproc.png deleted file mode 100644 index 89a51ebac..000000000 Binary files a/configuration/documentation/database/ISPyB_DataModel_mx_autoproc.png and /dev/null differ diff --git a/configuration/documentation/settings.xml.example b/configuration/documentation/settings.xml.example deleted file mode 100644 index e4996843c..000000000 --- a/configuration/documentation/settings.xml.example +++ /dev/null @@ -1,304 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - esrf - true - http - proxy.esrf.fr - 3128 - localhost - - - - - - - - - - - - - - - - - - - - - - - ESRF - - - - ispyb - ****** - C:/java/appServers/wildfly-8.2.0.Final - - - - - - - - - - - ESRF - - - diff --git a/configuration/standalone.xml b/configuration/standalone.xml index caaea833d..ed7ca4b1f 100644 --- a/configuration/standalone.xml +++ b/configuration/standalone.xml @@ -83,7 +83,7 @@ - + @@ -106,6 +106,26 @@ + + + + + + + + + + + + + + + + + + + + @@ -153,20 +173,28 @@ + + jdbc:mysql://pydevserv.esrf.fr:3308/pyconfig + mysql-connector-java-5.1.21.jar + + **** + **** + + jdbc:mysql://pydevserv.esrf.fr:3308/pydb mysql-connector-java-5.1.21.jar - UUUUUUU - PPPPPP + **** + **** jdbc:mysql://pydevserv.esrf.fr:3308/pydb mysql-connector-java-5.1.21.jar - UUUUUUU - PPPPPP + **** + **** @@ -403,18 +431,6 @@ - - - true - - - - true - - - - true - true @@ -427,23 +443,7 @@ true - - - true - - - - true - - - - true - - - - true - - + @@ -499,38 +499,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -564,6 +532,7 @@ + @@ -624,12 +593,5 @@ - - - - - - - - + diff --git a/configuration/documentation/standalone.xml.example b/configuration/standalone.xml.simple similarity index 90% rename from configuration/documentation/standalone.xml.example rename to configuration/standalone.xml.simple index ed7ca4b1f..8b8e63f37 100644 --- a/configuration/documentation/standalone.xml.example +++ b/configuration/standalone.xml.simple @@ -83,7 +83,7 @@ - + @@ -144,15 +144,11 @@ - - - - @@ -173,28 +169,28 @@ - - jdbc:mysql://pydevserv.esrf.fr:3308/pyconfig + + jdbc:mysql://ispydb-prod.esrf.fr:3306/pyconfig mysql-connector-java-5.1.21.jar - **** - **** + pxuser + ***** - jdbc:mysql://pydevserv.esrf.fr:3308/pydb + jdbc:mysql://ispydb-prod.esrf.fr:3306/pydb mysql-connector-java-5.1.21.jar - **** - **** + pxuser + ***** - jdbc:mysql://pydevserv.esrf.fr:3308/pydb + jdbc:mysql://ispydb-prod.esrf.fr:3306/pydb mysql-connector-java-5.1.21.jar - **** - **** + pxuser + ***** @@ -423,41 +419,9 @@ - - - - - - - - - - - true - - - - true - - - - true - - - + - - - - - - - - - - - @@ -469,7 +433,7 @@ - + @@ -499,6 +463,14 @@ + + + + + + + + @@ -532,7 +504,6 @@ - @@ -541,7 +512,7 @@ - + @@ -593,5 +564,4 @@ - diff --git a/documentation/ISPyB_DevelopersGuide.doc b/documentation/ISPyB_DevelopersGuide.doc index f5ca5375e..bf73a7a97 100644 Binary files a/documentation/ISPyB_DevelopersGuide.doc and b/documentation/ISPyB_DevelopersGuide.doc differ diff --git a/documentation/~$PyB_DevelopersGuide.doc b/documentation/~$PyB_DevelopersGuide.doc deleted file mode 100644 index 37c256e44..000000000 Binary files a/documentation/~$PyB_DevelopersGuide.doc and /dev/null differ diff --git a/documentation/~WRL0004.tmp b/documentation/~WRL0004.tmp deleted file mode 100644 index 20ca96603..000000000 Binary files a/documentation/~WRL0004.tmp and /dev/null differ diff --git a/ispyb-bcr-ear/pom.xml b/ispyb-bcr-ear/pom.xml index c072129af..b727e8d4f 100644 --- a/ispyb-bcr-ear/pom.xml +++ b/ispyb-bcr-ear/pom.xml @@ -1,112 +1,90 @@ - - 4.0.0 - - ispyb - ispyb-parent - 5.2.0 - - ispyb-bcr-ear - ear - - - - - - ispyb - ispyb-ejb3 - ejb - 5.2.0 - provided - - - - ispyb - ispyb-bcr - 5.0.3 - war - - - - org.apache.httpcomponents - httpclient - 4.4 - - - - - bcr - - - src/main/resources - - - - - - org.apache.maven.plugins - maven-ear-plugin - ${version.ear.plugin} - - - - 6 - true - - lib - - - - - ispyb - ispyb-ejb3 - / - - - - - ispyb - ispyb-bcr - /bcr/bcr - - - - no-version - - - - - org.wildfly.plugins - wildfly-maven-plugin - ${version.wildfly.maven.plugin} - - bcr.ear - false - - - - - + + 4.0.0 + + ispyb + ispyb-parent + 5.3.4 + + ispyb-bcr-ear + ear + + + + + ispyb + ispyb-ejb3 + ejb + 5.3.4 + provided + + + + ispyb + ispyb-bcr + 5.0.3 + war + + + + org.apache.httpcomponents + httpclient + 4.4 + + + + + bcr + + + src/main/resources + + + + + + org.apache.maven.plugins + maven-ear-plugin + ${version.ear.plugin} + + + + 6 + true + + lib + + + + + ispyb + ispyb-ejb3 + / + + + + + ispyb + ispyb-bcr + /bcr/bcr + + + + no-version + + + + + org.wildfly.plugins + wildfly-maven-plugin + ${version.wildfly.maven.plugin} + + bcr.ear + false + + + + + diff --git a/ispyb-bcr/pom.xml b/ispyb-bcr/pom.xml index 915e82467..b8c7744a4 100644 --- a/ispyb-bcr/pom.xml +++ b/ispyb-bcr/pom.xml @@ -31,7 +31,7 @@ ispyb ispyb-ejb3 - 5.2.0 + 5.3.4 provided @@ -133,7 +133,7 @@ commons-collections commons-collections - 3.2.1 + 3.2.2 com.sun.mail @@ -200,12 +200,17 @@ org.hibernate hibernate-core - 4.0.0.Final + 5.0.10.Final + provided antlr antlr + + dom4j + dom4j + diff --git a/ispyb-ear/pom.xml b/ispyb-ear/pom.xml index 7ce68d16a..54fb40673 100644 --- a/ispyb-ear/pom.xml +++ b/ispyb-ear/pom.xml @@ -4,7 +4,7 @@ ispyb ispyb-parent - 5.2.0 + 5.3.4 ispyb-ear ear @@ -13,8 +13,9 @@ ispyb ispyb-ejb3 ejb - 5.2.0 + 5.3.4 + ispyb ispyb-ui @@ -25,8 +26,9 @@ ispyb ispyb-ws war - 5.2.0 + 5.3.4 + @@ -58,6 +60,7 @@ ispyb-ejb3 / + ispyb ispyb-ws @@ -69,7 +72,7 @@ ispyb-ui /ispyb - + no-version diff --git a/ispyb-ejb/pom.xml b/ispyb-ejb/pom.xml index 3f45a8c6f..a97cabb3c 100644 --- a/ispyb-ejb/pom.xml +++ b/ispyb-ejb/pom.xml @@ -4,7 +4,7 @@ ispyb ispyb-parent - 5.2.0 + 5.3.4 ispyb-ejb3 jar @@ -52,15 +52,6 @@ cdi-api provided - - - - - - - - @@ -69,16 +60,20 @@ provided - org.hibernate hibernate-core - 4.0.0.Final + 5.0.10.Final + provided antlr antlr + + dom4j + dom4j + @@ -90,13 +85,18 @@ org.jboss.ejb3 jboss-ejb3-ext-api - 2.1.0 + 2.2.0.Final org.jboss.as jboss-as-security 7.1.1.Final + + org.jboss.ws + jbossws-spi + 2.3.1.Final + org.apache.axis @@ -108,11 +108,6 @@ httpclient 4.4 - - org.jboss.ws - jbossws-spi - 2.3.1.Final - jhdf jhdf diff --git a/ispyb-ejb/src/main/java/ispyb/common/util/Constants.java b/ispyb-ejb/src/main/java/ispyb/common/util/Constants.java index 5f9495e1f..f7be66d07 100644 --- a/ispyb-ejb/src/main/java/ispyb/common/util/Constants.java +++ b/ispyb-ejb/src/main/java/ispyb/common/util/Constants.java @@ -875,7 +875,7 @@ public static String getSAXSBeamline() { // public static final String[] LIST_EXPERIMENT_KIND_ESRF = { "Default", "MXPressE", "MXPressO", "MXPressE_SAD", "MXScore", "MXPressM","OSC", "SAD", // "MAD", "Fixed", "Ligand binding", "Refinement", "MAD - Inverse Beam", "SAD - Inverse Beam" }; - public static final String[] LIST_EXPERIMENT_KIND_ESRF = { "Default", "MXPressE", "MXPressO", "MXPressI", "MXPressE_SAD", "MXScore", "MXPressM", "MXPressP" }; + public static final String[] LIST_EXPERIMENT_KIND_ESRF = { "Default", "MXPressE", "MXPressO", "MXPressI", "MXPressE_SAD", "MXScore", "MXPressM", "MXPressP", "MXPressP_SAD" }; public static final String[] LIST_EXPERIMENT_KIND = (SITE_IS_ESRF()) ? LIST_EXPERIMENT_KIND_ESRF : LIST_EXPERIMENT_KIND_MAXIV; diff --git a/ispyb-ejb/src/main/java/ispyb/common/util/beamlines/ESRFBeamlineEnum.java b/ispyb-ejb/src/main/java/ispyb/common/util/beamlines/ESRFBeamlineEnum.java index b99a02f86..13a97882c 100644 --- a/ispyb-ejb/src/main/java/ispyb/common/util/beamlines/ESRFBeamlineEnum.java +++ b/ispyb-ejb/src/main/java/ispyb/common/util/beamlines/ESRFBeamlineEnum.java @@ -30,37 +30,53 @@ public enum ESRFBeamlineEnum { ID14_1("ID14-1", "id14eh1", new String[] { "ID14 1" }, "+33476882323", - null, false, false), ID14_2("ID14-2", "id14eh2", new String[] { "ID14 2" }, + null, false, false), + ID14_2("ID14-2", "id14eh2", new String[] { "ID14 2" }, "+33476882565", - null, false, false), ID14_3("ID14-3", "id14eh3", new String[] { "ID14 3" }, + null, false, false), + ID14_3("ID14-3", "id14eh3", new String[] { "ID14 3" }, "+33476882786", - null, false, false), ID14_4("ID14-4", "id14eh4", new String[] { "ID14 4" }, + null, false, false), + ID14_4("ID14-4", "id14eh4", new String[] { "ID14 4" }, "+33476882322", - null, false, false), ID23_1("ID23-1", "id23eh1", new String[] { "ID23 1" }, + null, false, false), + ID23_1("ID23-1", "id23eh1", new String[] { "ID23 1" }, "+33476882261", - new String[] { "x_geo_corr.cbf", "y_geo_corr.cbf" }, false, true), ID23_2("ID23-2", "id23eh2", new String[] { "ID23 2" }, + new String[] { "x_geo_corr.cbf", "y_geo_corr.cbf" }, false, true), + ID23_2("ID23-2", "id23eh2", new String[] { "ID23 2" }, "+33476882590", - new String[] { "x_geo_corr.cbf", "y_geo_corr.cbf" }, false, true), ID29("ID29", "id29", new String[] { "ID29" }, + new String[] { "x_geo_corr.cbf", "y_geo_corr.cbf" }, false, true), + ID29("ID29", "id29", new String[] { "ID29" }, "+33476882805", - new String[] { "x_geo_corr.cbf", "y_geo_corr.cbf" }, false, true), ID30A1("ID30A-1", "id30a1", new String[] { "ID30A1" }, + new String[] { "x_geo_corr.cbf", "y_geo_corr.cbf" }, false, true), + ID30A1("ID30A-1", "id30a1", new String[] { "ID30A1" }, "+3347688****", - null, true, true), ID30A2("ID30A-2", "id30a2", new String[] { "ID30A2" }, + null, true, true), + ID30A2("ID30A-2", "id30a2", new String[] { "ID30A2" }, "+3347688****", - null, true, true), ID30A3("ID30A-3", "id30a3", new String[] { "ID30A3" }, + null, true, true), + ID30A3("ID30A-3", "id30a3", new String[] { "ID30A3" }, "+3347688****", - null, true, true), ID30B("ID30B", "id30b", null, + null, true, true), + ID30B("ID30B", "id30b", null, "+3347688****", - null, true, true), BM14("BM14", "bm14", new String[] { "BM14U" }, + null, true, true), + BM14("BM14", "bm14", new String[] { "BM14U" }, "+33476882703", - null, false, true), BM16("BM16", "bm16", null, + null, false, true), + BM16("BM16", "bm16", null, "+33476882614", - null, false, false), BM29("BM29", "bm29", null, + null, false, false), + BM29("BM29", "bm29", null, "+33476882628", - null, false, true), BM30A("BM30A", "bm30a", null, + null, false, true), + BM30A("BM30A", "bm30a", null, "+33476882787", - null, false, true), ID19("ID19", "id19", null, + null, false, true), + ID19("ID19", "id19", null, "+33476882700", - null, false, true); + null, false, true), + CM01("CM01", "cm01", null,"+3347688****", null, false, true); private ESRFBeamlineEnum(String beamlineName, String directoryName, String[] associatedName, String phoneNumber, String[] correctionFiles, boolean emailNotification, boolean inActivity) { diff --git a/ispyb-ejb/src/main/java/ispyb/common/util/export/ExiPdfRtfExporter.java b/ispyb-ejb/src/main/java/ispyb/common/util/export/ExiPdfRtfExporter.java index ce9035ac5..8a4dabd52 100644 --- a/ispyb-ejb/src/main/java/ispyb/common/util/export/ExiPdfRtfExporter.java +++ b/ispyb-ejb/src/main/java/ispyb/common/util/export/ExiPdfRtfExporter.java @@ -26,13 +26,17 @@ import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import org.apache.log4j.Logger; +import org.jboss.util.collection.ReverseListIterator; import com.lowagie.text.BadElementException; import com.lowagie.text.Cell; @@ -54,11 +58,12 @@ import ispyb.common.util.PathUtils; import ispyb.server.common.services.sessions.Session3Service; import ispyb.server.common.util.ejb.Ejb3ServiceLocator; +import ispyb.server.mx.services.autoproc.SpaceGroup3Service; import ispyb.server.mx.services.collections.DataCollection3Service; import ispyb.server.mx.services.collections.Image3Service; import ispyb.server.mx.services.collections.IspybCrystalClass3Service; -import ispyb.server.mx.vos.collections.DataCollection3VO; -import ispyb.server.mx.vos.collections.DataCollectionGroup3VO; +import ispyb.server.mx.services.ws.rest.datacollectiongroup.DataCollectionGroupRestWsService; +import ispyb.server.mx.vos.autoproc.SpaceGroup3VO; import ispyb.server.mx.vos.collections.IspybCrystalClass3VO; import ispyb.server.mx.vos.collections.Session3VO; @@ -108,7 +113,11 @@ public class ExiPdfRtfExporter { public final static Font FONT_DOC = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.BLACK); + public final static Font FONT_DOC_SMALL = new Font(Font.HELVETICA, 6, Font.NORMAL, Color.BLACK); + public final static Font FONT_DOC_BLUE = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.BLUE); + public final static Font FONT_DOC_ORANGE = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.ORANGE); + public final static Font FONT_DOC_RED = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.RED); public final static Font FONT_DOC_ITALIC = new Font(Font.HELVETICA, 8, Font.ITALIC, Color.BLACK); @@ -121,15 +130,22 @@ public class ExiPdfRtfExporter { public final static Font FONT_DOC_EXPONENT_BLUE = new Font(Font.HELVETICA, 11, Font.NORMAL, Color.BLUE); public final static Font FONT_DOC_BOLD = new Font(Font.HELVETICA, 8, Font.BOLD); + + public final static Font FONT_DOC_SMALL_BOLD = new Font(Font.HELVETICA, 6, Font.BOLD); + + public final static Font FONT_DOC_SMALL_CENTERED = new Font(Font.HELVETICA, 6, Font.BOLD); + public final static Font FONT_DOC_PARAM_TITLE = new Font(Font.HELVETICA, 8, Font.NORMAL, LIGHT_GREY_COLOR); public final static Font FONT_INDEXING_NOTDONE = new Font(Font.HELVETICA, 8, Font.NORMAL, LIGHT_GREY_COLOR); - public final static Font FONT_INDEXING_FAILED = new Font(Font.HELVETICA, 8, Font.NORMAL, RED_COLOR); + public final static Font FONT_INDEXING_FAILED = new Font(Font.HELVETICA, 6, Font.BOLD, RED_COLOR); - public final static Font FONT_INDEXING_SUCCESS = new Font(Font.HELVETICA, 8, Font.NORMAL, GREEN_COLOR); + public final static Font FONT_INDEXING_SUCCESS = new Font(Font.HELVETICA, 6, Font.BOLD, GREEN_COLOR); - public final static int NB_COL_DATACOLLECTION = 8; + public final static int NB_COL_DATACOLLECTION = 7; + + public final static int NB_COL_DATA_ANALYSIS = 12; public final static int SIZE_FONT = 8; @@ -154,14 +170,21 @@ public class ExiPdfRtfExporter { public final static float CRYSTAL_IMAGE_HEIGHT = 174; - public final static float IMAGE_HEIGHT = 120; + //public final static float IMAGE_HEIGHT = 120; + public final static float IMAGE_HEIGHT = 80; + public final static float IMAGE_HEIGHT_SMALL = 50; + public final static float IMAGE_HEIGHT_SNAPSHOT = 60; // public final static float CRYSTAL_IMAGE_WIDTH = 160; // public final static float CRYSTAL_IMAGE_HEIGHT = 99; public final static float DIFF_IMAGE_WIDTH = 174; public final static float DIFF_IMAGE_HEIGHT = 174; + + public final static double MIN_RMERGE = 10; + //proposalId + int proposalId; // proposal String proposalDesc; @@ -187,9 +210,15 @@ public class ExiPdfRtfExporter { private DataCollection3Service dcService; + private DataCollectionGroupRestWsService dcGroupService; + private Image3Service imageService; + + private SpaceGroup3Service spacegroupService; + + private Map spgMap = new HashMap (); - public ExiPdfRtfExporter(String proposalDesc, Integer sessionId, + public ExiPdfRtfExporter(int proposalId, String proposalDesc, Integer sessionId, List> dataCollections, Integer nbRowsMax) throws Exception { this.proposalDesc = proposalDesc; this.sessionId = sessionId; @@ -208,10 +237,22 @@ private void init() throws Exception { .getLocalService(Session3Service.class); dcService = (DataCollection3Service) ejb3ServiceLocator .getLocalService(DataCollection3Service.class); + dcGroupService = (DataCollectionGroupRestWsService) ejb3ServiceLocator + .getLocalService(DataCollectionGroupRestWsService.class); imageService = (Image3Service) ejb3ServiceLocator .getLocalService(Image3Service.class); + spacegroupService = (SpaceGroup3Service) ejb3ServiceLocator + .getLocalService(SpaceGroup3Service.class); slv = sessionService.findByPk(sessionId, false/*withDataCollectionGroup*/, false/*withEnergyScan*/, false/*withXFESpectrum*/); + + List spaceGroups = spacegroupService.findAll(); + + for (Iterator iterator = spaceGroups.iterator(); iterator.hasNext();) { + SpaceGroup3VO spg = (SpaceGroup3VO) iterator.next(); + spgMap.put(spg.getSpaceGroupName(), spg.getSpaceGroupNumber()); + } + LOG.info("spgMap=" + spgMap.toString()); } @@ -267,6 +308,50 @@ public ByteArrayOutputStream exportDataCollectionReport(boolean rtfFormat) throw } + public ByteArrayOutputStream exportDataCollectionAnalysisReport(boolean rtfFormat) throws Exception { + + this.init(); + // create simple doc and write to a ByteArrayOutputStream + Document document = new Document(PageSize.A4.rotate(), 20, 20, 20, 20); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + if (!rtfFormat) { + PdfWriter.getInstance(document, baos); + } else { + RtfWriter2.getInstance(document, baos); + } + + // ============================= + // Header + footer + // ============================= + + setHeader(document); + setFooter(document); + document.open(); + + // =============================== + // Body + // =============================== + // Crystallographer added only for IFX proposal in case of MXPress + // experiment + setCrystallographer(document); + // Session comments + setSessionComments(document); + // session title& info + setSessionTable(document); + + // ====================== + // Data Collection analysis table + // ====================== + document.add(new Paragraph(" ")); + setDataAnalysisTable(document); + + // ====================== + // End of file + // ====================== + document.close(); + return baos; + + } /** * sets the header in the specified document * @@ -407,38 +492,72 @@ private void setSessionTable(Document document) throws Exception { * @throws Exception */ private void setDataCollectionTable(Document document) throws Exception { + document.add(new Paragraph("Data Collections:", FONT_TITLE)); document.add(new Paragraph(" ")); + if (dataCollections.isEmpty()) { document.add(new Paragraph("There is no data collection in this report", FONT_DOC)); } else { - document.add(new Paragraph(" ")); + document.add(new Paragraph(" ")); + // need the list of DCgroups for crystal class summary Map mapDataCollectionGroupIdCClass = new HashMap(); // DataCollection Rows - Iterator> it = dataCollections.iterator(); + //Iterator> it = dataCollections.iterator(); + Iterator> it2 = new ReverseListIterator >(dataCollections); + int i = 0; - while (it.hasNext() && i < nbRowsMax) { - Map dataCollectionMapData = it.next(); + while (it2.hasNext() && i < nbRowsMax) { + Map dataCollectionMapData = it2.next(); LOG.info("dcMap=" + dataCollectionMapData.toString()); setDataCollectionMapData(document, dataCollectionMapData); - if (getCellParam(dataCollectionMapData, "DataCollectionGroup_crystalClass") != null) { - String dcgId = getCellParam(dataCollectionMapData, "DataCollectionGroup_dataCollectionGroupId"); + if (getCellParam(dataCollectionMapData, "DataCollectionGroup_crystalClass", null) != null) { + String dcgId = getCellParam(dataCollectionMapData, "DataCollectionGroup_dataCollectionGroupId", null); if (!mapDataCollectionGroupIdCClass.containsKey(dcgId)){ - mapDataCollectionGroupIdCClass.put(dcgId, getCellParam(dataCollectionMapData, "DataCollectionGroup_crystalClass")); + mapDataCollectionGroupIdCClass.put(dcgId, getCellParam(dataCollectionMapData, "DataCollectionGroup_crystalClass", null)); } } i++; } - setCrystalClassSummary(document, mapDataCollectionGroupIdCClass); + //setCrystalClassSummary(document, mapDataCollectionGroupIdCClass); document.add(new Paragraph(" ")); } document.add(new Paragraph(" ")); } + + /** + * set the dataCollection table + * + * @param document + * @throws Exception + */ + private void setDataAnalysisTable(Document document) throws Exception { + document.add(new Paragraph("Data Collections & Analysis results:", FONT_TITLE)); + document.add(new Paragraph(" ")); + if (dataCollections.isEmpty()) { + document.add(new Paragraph("There is no data collection in this report", FONT_DOC)); + } else { + //document.add(new Paragraph(" ")); + + // DataCollection Rows + //Iterator> it = dataCollections.iterator(); + Iterator> it2 = new ReverseListIterator >(dataCollections); + + int i = 0; + while (it2.hasNext() && i < nbRowsMax) { + Map dataCollectionMapData = it2.next(); + LOG.info("dcMap=" + dataCollectionMapData.toString()); + setDataAnalysisMapData(document, dataCollectionMapData); + i++; + } + document.add(new Paragraph(" ")); + } + } /** * set a line for a specified dataCollection in the dataCollection table @@ -454,40 +573,43 @@ private void setDataCollectionTable(Document document) throws Exception { private void setDataCollectionMapData(Document document, Map dataCollectionMapItem) throws Exception { // 1st row - String parag = getCellParam(dataCollectionMapItem, "DataCollectionGroup_experimentType") - + " " + getCellParam(dataCollectionMapItem, "DataCollection_startTime"); + String parag = getCellParam(dataCollectionMapItem, "DataCollectionGroup_experimentType", null) + + " " + getCellParam(dataCollectionMapItem, "DataCollection_startTime", null); Paragraph p = new Paragraph(parag, FONT_DOC_BLUE); document.add(p); //row2 - parag = getCellParam(dataCollectionMapItem,"DataCollection_imageDirectory"); + parag = getCellParam(dataCollectionMapItem,"DataCollection_imageDirectory", null); document.add(new Paragraph(parag, FONT_DOC_ITALIC)); //row3 Table table = new Table(NB_COL_DATACOLLECTION); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.getDefaultCell().setBorderWidth(0); + table.setBorder(0); + table.setCellsFitPage(true); + table.setWidth(90); // 1st Cell - parag = "Workflow: \n" - + "Protein: \n" - + "Sample: \n" - + "Prefix: \n" - + "Run #: \n" - + "Images: \n" - + "Transmission: \n"; + parag = "Workflow:\n" + + "Protein:\n" + + "Sample:\n" + + "Prefix:\n" + + "Run #:\n" + + "Images:\n" + + "Transmission:\n"; LOG.info("parag=" + parag); p = new Paragraph(parag, FONT_DOC); table.addCell(p); // Cell2 - parag = getCellParam(dataCollectionMapItem, "Workflow_workflowType") + "\n" - + getCellParam(dataCollectionMapItem, "Protein_acronym") + "\n" - + getCellParam(dataCollectionMapItem, "BLSample_name") + "\n" - + getCellParam(dataCollectionMapItem, "DataCollection_imagePrefix") + "\n" - + getCellParam(dataCollectionMapItem, "DataCollection_dataCollectionNumber") + "\n" - + getCellParam(dataCollectionMapItem, "Protein_acronym") + "\n" - + getCellParam(dataCollectionMapItem, "DataCollection_transmission") + "\n"; + parag = getCellParam(dataCollectionMapItem, "Workflow_workflowType", null) + "\n" + + getCellParam(dataCollectionMapItem, "Protein_acronym", null) + "\n" + + getCellParam(dataCollectionMapItem, "BLSample_name", null) + "\n" + + getCellParam(dataCollectionMapItem, "DataCollection_imagePrefix", null) + "\n" + + getCellParam(dataCollectionMapItem, "DataCollection_dataCollectionNumber", null) + "\n" + + getCellParam(dataCollectionMapItem, "DataCollection_numberOfImages", null) + "\n" + + getCellParam(dataCollectionMapItem, "transmission", df2) + "%\n"; LOG.info("parag=" + parag); p = new Paragraph(parag, FONT_DOC_BOLD); table.addCell(p); @@ -506,45 +628,37 @@ private void setDataCollectionMapData(Document document, Map dat // Cell 4 - parag = getCellParam(dataCollectionMapItem, "DataCollection_resolutionAtCorner")+ "\n" - + getCellParam(dataCollectionMapItem, "DataCollection_wavelength") + "\n" - + getCellParam(dataCollectionMapItem, "DataCollection_axisRange") + "\n" - + getCellParam(dataCollectionMapItem, "DataCollection_omegaStart") + "\n" - + getCellParam(dataCollectionMapItem, "exposureTime") + "\n" - + getCellParam(dataCollectionMapItem, "DataCollection_flux") + "\n" - + getCellParam(dataCollectionMapItem, "DataCollection_flux_end") + "\n" ; + parag = getCellParam(dataCollectionMapItem, "DataCollection_resolution", df2) + Constants.ANGSTROM + + " ("+ getCellParam(dataCollectionMapItem, "DataCollection_resolutionAtCorner", df2) + Constants.ANGSTROM + ") \n" + + getCellParam(dataCollectionMapItem, "DataCollection_wavelength", df3) + Constants.ANGSTROM + "\n" + + getCellParam(dataCollectionMapItem, "DataCollection_axisRange", df2) + Constants.DEGREE + "\n" + + getCellParam(dataCollectionMapItem, "DataCollection_omegaStart", df2) + Constants.DEGREE + "\n" + + getCellParam(dataCollectionMapItem, "DataCollection_exposureTime", df2) + "s \n" + + getCellParam(dataCollectionMapItem, "DataCollection_flux", null) + "ph/sec \n" + + getCellParam(dataCollectionMapItem, "DataCollection_flux_end", null) + "ph/sec \n" ; table.addCell(new Paragraph(parag, FONT_DOC_BOLD)); - - - - // 5 Cell add cell containing autoproc results - table.addCell(" "); - // 6 Cell : thumbnail + // 5 Cell : thumbnail - if (!getCellParam(dataCollectionMapItem, "lastImageId").isEmpty()) { - String thumbnailPath = (imageService.findByPk(new Integer(getCellParam(dataCollectionMapItem, "lastImageId")))).getJpegThumbnailFileFullPath(); - Cell cellThumbnail = getCellImage(thumbnailPath); + if (!getCellParam(dataCollectionMapItem, "lastImageId", null).isEmpty()) { + String thumbnailPath = (imageService.findByPk(new Integer(getCellParam(dataCollectionMapItem, "lastImageId", null)))).getJpegThumbnailFileFullPath(); + Cell cellThumbnail = getCellImage(thumbnailPath, IMAGE_HEIGHT); cellThumbnail.setBorderWidth(0); table.addCell(cellThumbnail); } else { table.addCell(" "); } - - //cellThumbnail.setRowspan(nbRows); - - - // 7 Cell : snapshot - Cell cellSnapshot = getCellImage(dataCollectionMapItem,"DataCollection_xtalSnapshotFullPath1"); - //cellSnapshot.setRowspan(nbRows); + + // 6 Cell : snapshot + Cell cellSnapshot = getCellImage(dataCollectionMapItem,"DataCollection_xtalSnapshotFullPath1", IMAGE_HEIGHT_SNAPSHOT); cellSnapshot.setBorderWidth(0); table.addCell(cellSnapshot); - // 8 Cell : graph or other plot - if (!getCellParam(dataCollectionMapItem, "DataCollection_dataCollectionId").isEmpty()) { - String plotPath = (dcService.findByPk(new Integer(getCellParam(dataCollectionMapItem, "DataCollection_dataCollectionId")), false, false)).getImageQualityIndicatorsPlotPath(); - Cell cellGraph = getCellImage(plotPath); + // 7 Cell : graph or other plot + if (!getCellParam(dataCollectionMapItem, "DataCollection_dataCollectionId", null).isEmpty()) { + String plotPath = (dcService.findByPk(new Integer(getCellParam(dataCollectionMapItem, "DataCollection_dataCollectionId", null)), false, false)).getImageQualityIndicatorsPlotPath(); + Cell cellGraph = getCellImage(plotPath, IMAGE_HEIGHT); cellGraph.setBorderWidth(0); table.addCell(cellGraph); } else { @@ -562,6 +676,230 @@ private void setDataCollectionMapData(Document document, Map dat return; } + /** + * set a line for a specified dataCollection in the dataCollection table + * + * @param document + * @param table + * @param col + * @param session + * @param df2 + * @param df3 + * @throws Exception + */ + private void setDataAnalysisMapData(Document document, Map dataCollectionMapItem) throws Exception { + + //row 1 + String parag = getCellParam(dataCollectionMapItem, "DataCollectionGroup_experimentType", null) + + " " + getCellParam(dataCollectionMapItem, "DataCollection_startTime", null); + Paragraph p = new Paragraph(parag, FONT_DOC_BLUE); + //document.add(p); + + Table table = new Table(NB_COL_DATA_ANALYSIS); + table.setWidth(100); + table.setCellsFitPage(true); + table.setBorder(0); + + table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); + table.getDefaultCell().setBorderWidth(0); + + // 1st Cell + parag = "Protein: \n\n" + + "Prefix: \n\n" + + "Images: \n\n" ; + + LOG.info("parag=" + parag); + p = new Paragraph(parag, FONT_DOC_SMALL); + table.addCell(p); + + // 2st Cell + parag = getCellParam(dataCollectionMapItem, "Protein_acronym", null)+ "\n\n" + + getCellParam(dataCollectionMapItem, "DataCollection_imagePrefix", null) + "\n\n" + + getCellParam(dataCollectionMapItem, "DataCollection_numberOfImages", null) + "\n\n" ; + + LOG.info("parag=" + parag); + p = new Paragraph(parag, FONT_DOC_SMALL_BOLD); + table.addCell(p); + + // Cell 3 + parag = "Type: \n" + + "Res. (corner): \n" + + "Wavelength: \n" ; + LOG.info("parag=" + parag); + p = new Paragraph(parag, FONT_DOC_SMALL); + table.addCell(p); + + // Cell 4 + parag = getCellParam(dataCollectionMapItem, "Workflow_workflowType", null) + "\n" + + getCellParam(dataCollectionMapItem, "DataCollection_resolution", df2) + Constants.ANGSTROM + + "("+ getCellParam(dataCollectionMapItem, "DataCollection_resolutionAtCorner", df2) + Constants.ANGSTROM + ") \n" + + getCellParam(dataCollectionMapItem, "DataCollection_wavelength", df3) + Constants.ANGSTROM + "\n" ; + + p = new Paragraph(parag, FONT_DOC_SMALL_BOLD); + table.addCell(p); + + // Cell 5 : image quality indicator plot + if (!getCellParam(dataCollectionMapItem, "DataCollection_dataCollectionId", null).isEmpty()) { + String plotPath = (dcService.findByPk(new Integer(getCellParam(dataCollectionMapItem, "DataCollection_dataCollectionId", null)), false, false)).getImageQualityIndicatorsPlotPath(); + Cell cellGraph = getCellImage(plotPath, IMAGE_HEIGHT); + cellGraph.setBorderWidth(0); + table.addCell(cellGraph); + } else { + table.addCell(" "); + } + + // Cell 6 7 8 9 10 11 indexed/strategy or completeness + Boolean indexing = getBoolean(dataCollectionMapItem, "ScreeningOutput_indexingSuccess"); + Boolean strategy = getBoolean(dataCollectionMapItem, "ScreeningOutput_strategySuccess"); + String autoprocSpaceGroup = getCellParam(dataCollectionMapItem, "AutoProc_spaceGroup", null); + boolean existAutoProcSpaceGroup = (autoprocSpaceGroup != null && !autoprocSpaceGroup.isEmpty() ) + || ( dataCollectionMapItem.get("AutoProc_spaceGroups") != null && dataCollectionMapItem.get("Autoprocessing_cell_a") != null); + + p = new Paragraph(); + String [] bestRmerge = null; + + if (existAutoProcSpaceGroup && extractBestAutoproc(dataCollectionMapItem) != null){ + // Cell 6 + bestRmerge = extractBestAutoproc(dataCollectionMapItem); + parag = bestRmerge[0] + "\n" + + "Overall\n" + + "Inner\n" + + "Outer\n"; + p = new Paragraph(parag, FONT_DOC_SMALL); + table.addCell(p); + + // Cell 7 + p = new Paragraph("Completeness\n", FONT_DOC_SMALL); + Chunk chu = getCompletenessChunk(bestRmerge[16]); + p.add(chu); + p.add("\n"); + chu = getCompletenessChunk(bestRmerge[2]); + p.add(chu); + p.add("\n"); + chu = getCompletenessChunk(bestRmerge[12]); + p.add(chu); + + table.addCell(p); + + // Cell 8 + parag = "Res. \n" + + bestRmerge[17]+ "\n " + + bestRmerge[3] + "\n" + + bestRmerge[13] + "\n" ; + p = new Paragraph(parag, FONT_DOC_SMALL); + table.addCell(p); + + // Cell 9 + parag = "Rmerge \n" + + bestRmerge[15] + "\n" + + bestRmerge[1] + "\n" + + bestRmerge[11] + "\n"; + p = new Paragraph(parag, FONT_DOC_SMALL); + table.addCell(p); + + // cell parameters of innerShell + // Cell 10 a alpha + parag = "a \n" + bestRmerge[4] + + "\n alpha \n" + bestRmerge[7] ; + p = new Paragraph(parag, FONT_DOC_SMALL_CENTERED); + p.setAlignment(Element.ALIGN_CENTER); + table.addCell(p); + + // Cell 11 b beta + parag = "b \n" + bestRmerge[5] + + "\n beta \n" + bestRmerge[8] ; + p = new Paragraph(parag, FONT_DOC_SMALL_CENTERED); + p.setAlignment(Element.ALIGN_CENTER); + table.addCell(p); + + // Cell 12 + parag = "c \n" + bestRmerge[6] + + "\n gamma \n" + bestRmerge[9] ; + + p = new Paragraph(parag, FONT_DOC_SMALL_CENTERED); + p.setAlignment(Element.ALIGN_CENTER); + table.addCell(p); + + } else if (indexing != null && strategy != null){ + // Cell 6 + parag = "\nIndexed: \n " + + "\nStrategy: \n"; + p = new Paragraph(parag, FONT_DOC_SMALL); + table.addCell(p); + + // Cell 7 + p = new Paragraph(); + Chunk chu2 = new Chunk( "KO", FONT_INDEXING_FAILED); + if (indexing.booleanValue() ){ + chu2 = new Chunk( "OK", FONT_INDEXING_SUCCESS); + } + p.add(chu2); + + chu2 = new Chunk( "KO", FONT_INDEXING_FAILED); + if (strategy.booleanValue() ){ + chu2 = new Chunk( "OK", FONT_INDEXING_SUCCESS); + } + p.add("\n"); + p.add(chu2); + table.addCell(p); + + // Cell 8 + parag = "Space group: \n" + + "Mosaicity: \n" ; + p = new Paragraph(parag, FONT_DOC_SMALL); + table.addCell(p); + + // Cell 9 + parag = getCellParam(dataCollectionMapItem, "ScreeningOutputLattice_spaceGroup", null) + "\n" + + getCellParam(dataCollectionMapItem, "ScreeningOutput_mosaicity", null)+ "\n" ; + p = new Paragraph(parag, FONT_DOC_SMALL_BOLD); + table.addCell(p); + + // Cell 10 + parag = "a \n" + getCellParam(dataCollectionMapItem, "ScreeningOutputLattice_unitCell_a", null) + + "\n alpha \n" + getCellParam(dataCollectionMapItem, "ScreeningOutputLattice_unitCell_alpha", null) ; + p = new Paragraph(parag, FONT_DOC_SMALL_CENTERED); + p.setAlignment(Element.ALIGN_CENTER); + table.addCell(p); + + // Cell 11 + parag = "b \n" + getCellParam(dataCollectionMapItem, "ScreeningOutputLattice_unitCell_b", null) + + "\n beta \n" + getCellParam(dataCollectionMapItem, "ScreeningOutputLattice_unitCell_beta", null) ; + p = new Paragraph(parag, FONT_DOC_SMALL_CENTERED); + p.setAlignment(Element.ALIGN_CENTER); + table.addCell(p); + + // Cell 12 + parag = "c \n" + getCellParam(dataCollectionMapItem, "ScreeningOutputLattice_unitCell_c", null) + + "\n gamma \n" + getCellParam(dataCollectionMapItem, "ScreeningOutputLattice_unitCell_gama", null) ; + + p = new Paragraph(parag, FONT_DOC_SMALL_CENTERED); + p.setAlignment(Element.ALIGN_CENTER); + table.addCell(p); + + } else { + table.addCell(" "); + table.addCell(" "); + table.addCell(" "); + table.addCell(" "); + table.addCell(" "); + table.addCell(" "); + table.addCell(" "); + } + // to avoid splitting table + Table containingTable = new Table(1); + containingTable.setCellsFitPage(true); + containingTable.setBorder(0); + containingTable.getDefaultCell().setBorderWidth(0); + + Cell cell = new Cell(table); + containingTable.addCell(cell); + + document.add(containingTable); + + return; + } + /** * get the value or replace by blank if null to fill a cell paragraph * @@ -569,15 +907,35 @@ private void setDataCollectionMapData(Document document, Map dat * @param dataCollectionMap * @throws Exception */ - private String getCellParam(Map dataCollectionMap, String param) throws Exception { + private String getCellParam(Map dataCollectionMap, String param, DecimalFormat df) throws Exception { String paramValue = ""; - if (dataCollectionMap.get(param) != null) { - paramValue = dataCollectionMap.get(param).toString(); + if (dataCollectionMap.get(param) != null) { + if (df == null){ + paramValue = dataCollectionMap.get(param).toString(); + } else { + paramValue = df.format(dataCollectionMap.get(param)).toString(); + } } return paramValue; } + + /** + * get the value as boolean to fill a cell and return a null value if no value or not an integer + * + * @param param + * @param dataCollectionMap + * @throws Exception + */ + private Boolean getBoolean(Map dataCollectionMap, String param) throws Exception { + + Boolean cellBool = null; + if (dataCollectionMap.get(param) != null) { + cellBool = new Boolean (dataCollectionMap.get(param).toString()); + } + return cellBool; + } /** * returns a simple cell witha given value inside @@ -600,23 +958,11 @@ private Cell getCellValue(String value) { * @return * @throws Exception */ - private Cell getCellImage(Map dataCollectionMapItem, String imageParam) throws Exception { + private Cell getCellImage(Map dataCollectionMapItem, String imageParam, float image_size) throws Exception { if (dataCollectionMapItem.get(imageParam) != null && !(dataCollectionMapItem.get(imageParam).toString()).equals("") ) { - String image = dataCollectionMapItem.get(imageParam).toString(); - image = PathUtils.getPath(image); - try { - Image jpg1 = Image.getInstance(image); - jpg1.scaleAbsolute(jpg1.getWidth() * IMAGE_HEIGHT / jpg1.getHeight(), IMAGE_HEIGHT); - Cell cell = new Cell(jpg1); - cell.setLeading(0); - cell.setBorderWidth(0); - cell.setHorizontalAlignment(Element.ALIGN_CENTER); - cell.setVerticalAlignment(Element.ALIGN_CENTER); - return cell; - } catch (IOException e) { - return new Cell(new Paragraph(image + " not found", FONT_DOC)); - } + String imagePath = dataCollectionMapItem.get(imageParam).toString(); + return this.getCellImage(imagePath, image_size); } return new Cell(new Paragraph("", FONT_DOC)); } @@ -628,13 +974,13 @@ private Cell getCellImage(Map dataCollectionMapItem, String imag * @return * @throws Exception */ - private Cell getCellImage(String imagePath) throws Exception { + private Cell getCellImage(String imagePath, float image_size) throws Exception { if (imagePath != null ) { String image = PathUtils.getPath(imagePath); try { Image jpg1 = Image.getInstance(image); - jpg1.scaleAbsolute(jpg1.getWidth() * IMAGE_HEIGHT / jpg1.getHeight(), IMAGE_HEIGHT); + jpg1.scaleAbsolute(jpg1.getWidth() * image_size / jpg1.getHeight(), image_size); Cell cell = new Cell(jpg1); cell.setLeading(0); cell.setBorderWidth(0); @@ -761,5 +1107,143 @@ private List getListOfNbCrystalPerClass(List list return listOfNbCrystalPerClass; } + private String[] extractBestAutoproc(Map dataCollectionMapItem) throws Exception { + + String [] bestRmerge = null; + String listString = (String)dataCollectionMapItem.get("completenessList"); + + if (dataCollectionMapItem.get("completenessList") != null && !listString.isEmpty() && dataCollectionMapItem.get("AutoProc_spaceGroups") != null) { + + listString.trim(); + List completenessList = new ArrayList(Arrays.asList((listString.split(",")))); + LOG.debug("completenessList = " + completenessList.toString()); + List spaceGroupsList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("AutoProc_spaceGroups")).trim().split(","))); + LOG.debug("spaceGroupsList = " + spaceGroupsList.size() + spaceGroupsList.toString()); + List resolutionsLimitLowList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("resolutionsLimitLow")).trim().split(","))); + LOG.debug("resolutionsLimitLowList = " + resolutionsLimitLowList.size() + resolutionsLimitLowList.toString()); + List resolutionsLimitHighList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("resolutionsLimitHigh")).trim().split(","))); + LOG.debug("resolutionsLimitHighList = " + resolutionsLimitHighList.toString()); + List rmergesList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("rMerges")).trim().split(","))); + LOG.debug("rmergesList = " + rmergesList.size() + rmergesList.toString() ); + List scalingStatisticsTypesList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("scalingStatisticsTypes")).trim().split(","))); + LOG.debug("scalingStatisticsTypesList = " + scalingStatisticsTypesList.size() + scalingStatisticsTypesList.toString()); + List anomalousList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("Autoprocessing_anomalous")).trim().split(","))); + LOG.debug("anomalousList = " + anomalousList.size() + anomalousList.toString()); + + bestRmerge = new String[18]; + int i = 0; + Double rmergeMin = 1000.000; + int indexRmergeMin = 0; + Set indexSet = new HashSet(); + + for (Iterator iterator = scalingStatisticsTypesList.iterator(); iterator.hasNext();) { + String type = (String) iterator.next(); + // select also no anom + if (type.contains("innerShell") && (new Integer(anomalousList.get(i).trim())).intValue() < 1) { + double rm = new Double(rmergesList.get(i)).doubleValue(); + LOG.debug("rm = " + rm); + if (rm > 0 && rm < MIN_RMERGE) { + indexSet.add(i); + LOG.debug("index kept: " + i); + } else if (rm > 0 && rm < rmergeMin) { + rmergeMin = rm; + indexRmergeMin = i; + } + } + i=i+1; + } + + // select higher symmetry + if (!indexSet.isEmpty()) { + String spgTemp; + int spgNb = 0; + for (Iterator iterator = indexSet.iterator(); iterator.hasNext();) { + Integer index = (Integer) iterator.next(); + spgTemp = spaceGroupsList.get(index).trim(); + LOG.debug("index : " + index + " spgtemp: " + spgTemp); + if (spgMap.get(spgTemp)!= null && spgNb <= spgMap.get(spgTemp).intValue() ) { + spgNb = spgMap.get(spgTemp).intValue(); + LOG.debug("index : " + index + " spgNb: " + spgMap.get(spgTemp)); + indexRmergeMin = index; + } + } + } + + bestRmerge[0] = spaceGroupsList.get(indexRmergeMin); + bestRmerge[1] = rmergesList.get(indexRmergeMin); + bestRmerge[2]= completenessList.get(indexRmergeMin); + bestRmerge[3] = resolutionsLimitLowList.get(indexRmergeMin) + "/" + resolutionsLimitHighList.get(indexRmergeMin); + + List tmpList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("Autoprocessing_cell_a")).trim().split(","))); + bestRmerge[4] = tmpList.get(indexRmergeMin); + tmpList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("Autoprocessing_cell_b")).trim().split(","))); + bestRmerge[5] = tmpList.get(indexRmergeMin); + tmpList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("Autoprocessing_cell_c")).trim().split(","))); + bestRmerge[6] = tmpList.get(indexRmergeMin); + tmpList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("Autoprocessing_cell_alpha")).trim().split(","))); + bestRmerge[7] = tmpList.get(indexRmergeMin); + tmpList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("Autoprocessing_cell_beta")).trim().split(","))); + bestRmerge[8] = tmpList.get(indexRmergeMin); + tmpList = new ArrayList(Arrays.asList(((String)dataCollectionMapItem.get("Autoprocessing_cell_gamma")).trim().split(","))); + bestRmerge[9] = tmpList.get(indexRmergeMin); + + //outer + int outerIndex = -1; + if (scalingStatisticsTypesList.get(indexRmergeMin+1).contains("outerShell") ){ + outerIndex = indexRmergeMin+1; + } else if (indexRmergeMin+2 < scalingStatisticsTypesList.size() && scalingStatisticsTypesList.get(indexRmergeMin+2).contains("outerShell") ){ + outerIndex = indexRmergeMin+2; + } + if (outerIndex > -1) { + bestRmerge[10] = spaceGroupsList.get(outerIndex); + bestRmerge[11] = rmergesList.get(outerIndex); + bestRmerge[12]= completenessList.get(outerIndex); + bestRmerge[13] = resolutionsLimitLowList.get(outerIndex) + "/" + resolutionsLimitHighList.get(outerIndex); + } + + //overall + int overallIndex=-1; + if (indexRmergeMin-1 >= 0 && scalingStatisticsTypesList.get(indexRmergeMin-1).contains("overall")) { + overallIndex = indexRmergeMin-1; + } else if (indexRmergeMin-2 >= 0 && scalingStatisticsTypesList.get(indexRmergeMin-2).contains("overall")) { + overallIndex = indexRmergeMin-2; + } else if (indexRmergeMin+2 < scalingStatisticsTypesList.size() && scalingStatisticsTypesList.get(indexRmergeMin+2).contains("overall")) { + overallIndex = indexRmergeMin+2; + } else if (indexRmergeMin+3 < scalingStatisticsTypesList.size() && scalingStatisticsTypesList.get(indexRmergeMin+3).contains("overall")) { + overallIndex = indexRmergeMin+3; + } + + if (overallIndex > -1) { + bestRmerge[14] = spaceGroupsList.get(overallIndex); + bestRmerge[15] = rmergesList.get(overallIndex); + bestRmerge[16]= completenessList.get(overallIndex); + //TODO format to 2 figures after . + bestRmerge[17] = resolutionsLimitLowList.get(overallIndex) + "/" + resolutionsLimitHighList.get(overallIndex); + } + + LOG.info("bestRmerge = " + bestRmerge[0] + "- " + bestRmerge[1]+ "- " + bestRmerge[2]+ "- " + bestRmerge[3]); + } + + return bestRmerge; + } + + private Chunk getCompletenessChunk(String completeness) { + + Chunk chu = new Chunk( " ", FONT_DOC_SMALL_BOLD); + + if (completeness != null && !completeness.isEmpty()) { + + chu = new Chunk( completeness, FONT_DOC_SMALL_BOLD); + chu.setBackground(BLUE_COLOR); + if (completeness != null && new Double(completeness) < 90 ) { + if (new Double(completeness) < 50) { + chu.setBackground(RED_COLOR); + } else { + chu.setBackground(LIGHT_YELLOW_COLOR); + } + } + } + return chu; + } } diff --git a/ispyb-ejb/src/main/java/ispyb/common/util/upload/ISPyBParser.java b/ispyb-ejb/src/main/java/ispyb/common/util/upload/ISPyBParser.java index f6a25a0cd..7038cc588 100644 --- a/ispyb-ejb/src/main/java/ispyb/common/util/upload/ISPyBParser.java +++ b/ispyb-ejb/src/main/java/ispyb/common/util/upload/ISPyBParser.java @@ -73,6 +73,8 @@ public class ISPyBParser extends XLSParser { */ private HSSFWorkbook mWorkbook = null; + + private final String PWD = "ispyb"; private static final Ejb3ServiceLocator ejb3ServiceLocator = Ejb3ServiceLocator.getInstance(); @@ -515,7 +517,7 @@ public void populateExistingShipment(String templateFileName, String populatedTe workbook.setSheetName(s, dewarCode + "_" + puckNumber); HSSFSheet sheet = workbook.getSheetAt(s); - sheet.setProtect(false); + sheet.protectSheet(PWD); // Dewar Code HSSFRow row = sheet.getRow(dewarRow); @@ -599,7 +601,8 @@ public void populateExistingShipment(String templateFileName, String populatedTe cell = row.createCell(shippingDateCol); cell.setCellValue(new HSSFRichTextString(shippingDate)); - sheet.setProtect(true); + //sheet.setProtect(true); + sheet.protectSheet(PWD); puckNumber++; } } diff --git a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/BiosaxsServices.java b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/BiosaxsServices.java index 830edc54b..acd62828d 100644 --- a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/BiosaxsServices.java +++ b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/BiosaxsServices.java @@ -491,6 +491,7 @@ public Integer createHPLC(String proposalCode, String proposalNumber, String nam } Experiment3VO experiment = new Experiment3VO(); + experiment = this.experiment3Service.initPlates(experiment); experiment.setProposalId(proposal.getProposalId()); experiment.setName(name); experiment.setSessionId(sessionId); diff --git a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/experiment/Experiment3Service.java b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/experiment/Experiment3Service.java index 29d3ae30b..0dbc0ce7c 100644 --- a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/experiment/Experiment3Service.java +++ b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/experiment/Experiment3Service.java @@ -72,5 +72,8 @@ public interface Experiment3Service { public abstract void saveStructure(Structure3VO structure3vo); public List> getExperimentDescription(Integer experimentId); + + public Experiment3VO initPlates(Experiment3VO vo); + } \ No newline at end of file diff --git a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/experiment/Experiment3ServiceBean.java b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/experiment/Experiment3ServiceBean.java index 084d8769c..ca577d69d 100644 --- a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/experiment/Experiment3ServiceBean.java +++ b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/experiment/Experiment3ServiceBean.java @@ -1,348 +1,357 @@ -/******************************************************************************* - * This file is part of ISPyB. - * - * ISPyB is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ISPyB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with ISPyB. If not, see . - * - * Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Brenchereau, M. Bodin, A. De Maria Antolinos - ******************************************************************************************************************************/ - -package ispyb.server.biosaxs.services.core.experiment; - - -import ispyb.server.biosaxs.services.core.ExperimentScope; -import ispyb.server.biosaxs.services.core.proposal.SaxsProposal3Service; -import ispyb.server.biosaxs.services.sql.SQLQueryKeeper; -import ispyb.server.biosaxs.vos.assembly.Macromolecule3VO; -import ispyb.server.biosaxs.vos.assembly.Stoichiometry3VO; -import ispyb.server.biosaxs.vos.assembly.Structure3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Buffer3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Experiment3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Measurement3VO; -import ispyb.server.biosaxs.vos.datacollection.MeasurementTodataCollection3VO; -import ispyb.server.biosaxs.vos.datacollection.SaxsDataCollection3VO; -import ispyb.server.biosaxs.vos.utils.comparator.SaxsDataCollectionComparator; -import ispyb.server.biosaxs.vos.utils.parser.RobotXMLParser; -import ispyb.server.mx.services.ws.rest.WsServiceBean; - -import java.io.File; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collections; -import java.util.Comparator; -import java.util.GregorianCalendar; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import javax.ejb.EJB; -import javax.ejb.Stateless; -import javax.persistence.EntityManager; -import javax.persistence.NoResultException; -import javax.persistence.PersistenceContext; -import javax.persistence.TypedQuery; - -import org.apache.log4j.Logger; -import org.hibernate.SQLQuery; -import org.hibernate.Session; -import org.hibernate.transform.AliasToEntityMapResultTransformer; - - - -@Stateless -public class Experiment3ServiceBean extends WsServiceBean implements Experiment3Service, Experiment3ServiceLocal { - - private final static Logger log = Logger.getLogger(Experiment3ServiceBean.class); - - - /** - * QUERIES - */ - private String GetExperimentDescriptionByExperimentId = getViewExperimentDescriptionTableQuery() + " where experimentId = :experimentId"; - - - - @PersistenceContext(unitName = "ispyb_db") - private EntityManager entityManager; - - @EJB - private SaxsProposal3Service saxsProposal3Service; - - @Override - public void persist(Experiment3VO transientInstance) { - try { - entityManager.persist(transientInstance); - } catch (RuntimeException re) { - throw re; - } - } - - @Override - public Experiment3VO merge(Experiment3VO detachedInstance) { - try { - Experiment3VO result = entityManager.merge(detachedInstance); - return result; - } catch (RuntimeException re) { - throw re; - } - } - - private String getViewExperimentDescriptionTableQuery(){ - return this.getQueryFromResourceFile("/queries/biosaxs/Experiment3ServiceBean/getViewExperimentDescriptionTableQuery.sql"); - } - - - @Override - public List findByProposalId(int proposalId, ExperimentScope scope) { - StringBuilder ejbQLQuery = Experiment3ServiceBean.getQueryByScope(scope); - ejbQLQuery.append("WHERE experiment.proposalId = :proposalId"); - TypedQuery query = entityManager.createQuery(ejbQLQuery.toString(), Experiment3VO.class).setParameter("proposalId", proposalId); - return query.getResultList(); - } - - @Override - public Experiment3VO findById(Integer experimentId, ExperimentScope scope, Integer proposalId) { - try { - StringBuilder ejbQLQuery = Experiment3ServiceBean.getQueryByScope(scope); - ejbQLQuery.append("WHERE experiment.experimentId = :experimentId "); - // if coming from manager account, proposalId can be null - if (proposalId != null) { - ejbQLQuery.append(" and experiment.proposalId = :proposalId"); - } - TypedQuery query = entityManager.createQuery(ejbQLQuery.toString(), Experiment3VO.class); - query.setParameter("experimentId", experimentId); - if (proposalId != null) { - query.setParameter("proposalId", proposalId); - } - return query.getSingleResult(); - } catch (NoResultException e) { - return null; - } - } - - public static StringBuilder getQueryByScope(ExperimentScope scope){ - StringBuilder ejbQLQuery = new StringBuilder(); - ejbQLQuery.append("SELECT DISTINCT(experiment) FROM Experiment3VO experiment "); - - switch (scope) { - case MINIMAL: - break; - case MEDIUM: - ejbQLQuery.append("LEFT JOIN FETCH experiment.samples samples "); - ejbQLQuery.append("LEFT JOIN FETCH samples.macromolecule3VO macromolecule "); - ejbQLQuery.append("LEFT JOIN FETCH macromolecule.stoichiometry st "); -// ejbQLQuery.append("LEFT JOIN FETCH st.macromolecule3VO "); - ejbQLQuery.append("LEFT JOIN FETCH macromolecule.structure3VOs "); - ejbQLQuery.append("LEFT JOIN FETCH samples.measurements specimens "); - ejbQLQuery.append("LEFT JOIN FETCH experiment.samplePlate3VOs samplePlates "); - ejbQLQuery.append("LEFT JOIN FETCH samplePlates.plategroup3VO "); - ejbQLQuery.append("LEFT JOIN FETCH samplePlates.sampleplateposition3VOs "); - - ejbQLQuery.append("LEFT JOIN FETCH specimens.merge3VOs merges "); - ejbQLQuery.append("LEFT JOIN FETCH specimens.run3VO "); - break; - case PREPARE_EXPERIMENT: - ejbQLQuery.append("LEFT JOIN FETCH experiment.samples samples "); - ejbQLQuery.append("LEFT JOIN FETCH samples.macromolecule3VO "); - ejbQLQuery.append("LEFT JOIN FETCH samples.measurements specimens "); - ejbQLQuery.append("LEFT JOIN FETCH experiment.samplePlate3VOs samplePlates "); - ejbQLQuery.append("LEFT JOIN FETCH samplePlates.plategroup3VO "); - ejbQLQuery.append("LEFT JOIN FETCH samplePlates.sampleplateposition3VOs "); - ejbQLQuery.append("LEFT JOIN FETCH experiment.dataCollections dataCollections "); - ejbQLQuery.append("LEFT JOIN FETCH dataCollections.measurementtodatacollection3VOs "); - break; - default: - break; - } - return ejbQLQuery; - } - - - @Override - public Experiment3VO findByMeasurementId(int measurementId){ - StringBuilder ejbQLQuery = Experiment3ServiceBean.getQueryByScope(ExperimentScope.MEDIUM); - ejbQLQuery.append(" wHERE specimens.measurementId = " + measurementId); - TypedQuery query = entityManager.createQuery(ejbQLQuery.toString(), Experiment3VO.class); - return query.getSingleResult(); - } - - - @Override - public List test(String ejbQL) { - try { - TypedQuery query = entityManager.createQuery(ejbQL, Experiment3VO.class); - return query.getResultList(); - } catch (RuntimeException re) { - log.error("get failed", re); - throw re; - } - } - - @Override - public Experiment3VO findById(Integer experimentId, ExperimentScope scope) { - StringBuilder ejbQLQuery = Experiment3ServiceBean.getQueryByScope(scope); - ejbQLQuery.append("WHERE experiment.experimentId = :experimentId"); - TypedQuery query = entityManager.createQuery(ejbQLQuery.toString(), Experiment3VO.class); - query.setParameter("experimentId", experimentId); - List results = query.getResultList(); - if (results.isEmpty()) { - return null; // handle no-results case - } else { - return results.get(0); - } - } - - - @Override - public List> getExperimentDescription(Integer experimentId) { - Session session = (Session) this.entityManager.getDelegate(); - SQLQuery query = session.createSQLQuery(GetExperimentDescriptionByExperimentId); - query.setParameter("experimentId", experimentId); - return executeSQLQuery(query); - } - - - - /** - * It returns the xml as String of the robot.xml file - * This xml format is used on BsxCube for load sample changer experiments - * - */ - @Override - public String toRobotXML(Integer experimentId, int proposalId, final SaxsDataCollectionComparator... multipleOptions) { - this.setPriorities(experimentId, proposalId, multipleOptions); - return this.toRobotXML(experimentId, proposalId); - } - - @Override - public String toRobotXML(Integer experimentId, int proposalId) { - Experiment3VO experiment = this.findById(experimentId, ExperimentScope.MEDIUM); - List buffers = saxsProposal3Service.findBuffersByProposalId(proposalId); - return RobotXMLParser.toRobotXML(experiment, experiment.getSamplePlate3VOs(), buffers); - } - - - - @Override - public void remove(Experiment3VO experiment) { - Experiment3VO result = entityManager.merge(experiment); - this.entityManager.remove(result); - } - - /** - * Sets the order for each data collection based on SaxsDataCollectionComparator - */ - @Override - public Experiment3VO setPriorities(int experimentId, int proposalId, SaxsDataCollectionComparator[] multipleOptions) { - Experiment3VO experiment = this.findById(experimentId, ExperimentScope.MEDIUM, proposalId); - List dataCollectionList = experiment.getDataCollectionList(); - /** Sort data collections by the priority order **/ - Collections.sort(dataCollectionList, SaxsDataCollectionComparator.compare(experiment, SaxsDataCollectionComparator.getComparator(multipleOptions))); - - int priority = 1; - for (int i = 0; i < dataCollectionList.size(); i++) { - SaxsDataCollection3VO datacollection = dataCollectionList.get(i); - List measurementsToDatacollection = this.getMeasurementToDataCollectionOrdered(datacollection); - for (MeasurementTodataCollection3VO m : measurementsToDatacollection) { - Measurement3VO measurement = experiment.getMeasurementById(m.getMeasurementId()); - measurement.setPriority(priority); - measurement = this.entityManager.merge(measurement); - priority ++; - } - } - return experiment; - - } - /** Sort two measurements by their dataCollectionOrder field **/ - private static Comparator MeasurementTodataCollectionComparatorOrder = new Comparator(){ - public int compare(MeasurementTodataCollection3VO o1, MeasurementTodataCollection3VO o2) { - return o1.getDataCollectionOrder() - o2.getDataCollectionOrder(); - } - }; - - /** Sort a data collection by its dataCollectionOrder field **/ - private List getMeasurementToDataCollectionOrdered(SaxsDataCollection3VO dataCollection){ - Set measurements = dataCollection.getMeasurementtodatacollection3VOs(); - List list = Arrays.asList(measurements.toArray(new MeasurementTodataCollection3VO[measurements.size()])); - Collections.sort(list, MeasurementTodataCollectionComparatorOrder); - return list; - } - - @Override - public void saveStructure(Integer macromoleculeId, String fileName, String filePath, String type, String symmetry, String multiplicity) { - Structure3VO structure = new Structure3VO(); - structure.setMacromoleculeId(macromoleculeId); - structure.setFilePath(filePath); - structure.setName(fileName); - structure.setType(type); - structure.setSymmetry(symmetry); - structure.setMultiplicity(multiplicity); - structure.setCreationDate(GregorianCalendar.getInstance().getTime()); - entityManager.merge(structure); - } - - @Override - public void removeStructure(int structureId) { - Structure3VO structure = this.entityManager.find(Structure3VO.class, structureId); - this.entityManager.remove(structure); - } - - @Override - public Structure3VO findStructureById(int structureId) { - return this.entityManager.find(Structure3VO.class, structureId); - } - - @Override - public void removeStoichiometry(int stoichiometryId) { - Stoichiometry3VO st = this.entityManager.find(Stoichiometry3VO.class, stoichiometryId); - this.entityManager.remove(st); - } - - @Override - public void saveStoichiometry(int macromoleculeId, int hostmacromoleculeId, String ratio, String comments) { - Stoichiometry3VO stoi = new Stoichiometry3VO(); - stoi.setMacromolecule3VOByHostMacromoleculeId(macromoleculeId); - stoi.setHostmacromoleculeId(hostmacromoleculeId); - stoi.setRatio(ratio); - this.entityManager.merge(stoi); - } - -// @Override -// public Macromolecule3VO findMacromoleculeById(Integer macromoleculeId) { -// StringBuilder ejbQLQuery = new StringBuilder(); -// ejbQLQuery.append("SELECT DISTINCT(macromolecule) FROM Macromolecule3VO macromolecule "); -// ejbQLQuery.append("LEFT JOIN FETCH macromolecule.stoichiometry st "); -// ejbQLQuery.append("LEFT JOIN FETCH st.macromolecule3VO "); -// ejbQLQuery.append("LEFT JOIN FETCH macromolecule.structure3VOs "); -// ejbQLQuery.append("WHERE macromolecule.macromoleculeId = :macromoleculeId"); -// TypedQuery query = entityManager.createQuery(ejbQLQuery.toString(), Macromolecule3VO.class); -// query.setParameter("macromoleculeId", macromoleculeId); -// return query.getSingleResult(); -// } - - @Override - public Structure3VO findStructureByFilePathId(String filePath, int experimentId) { -// Experiment3VO experiment = this.findById(experimentId, ExperimentScope.MINIMAL); -// List macromolecules = saxsProposal3Service.findMacromoleculesByProposalId(experiment.getProposalId()); -// for (Macromolecule3VO macromolecule3vo : macromolecules) { -// -// } - return null; - } - - @Override - public void saveStructure(Structure3VO structure3vo) { - this.entityManager.merge(structure3vo); - - } -} +/******************************************************************************* + * This file is part of ISPyB. + * + * ISPyB is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ISPyB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with ISPyB. If not, see . + * + * Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Brenchereau, M. Bodin, A. De Maria Antolinos + ******************************************************************************************************************************/ + +package ispyb.server.biosaxs.services.core.experiment; + + +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.ejb.EJB; +import javax.ejb.Stateless; +import javax.naming.NamingException; +import javax.persistence.EntityManager; +import javax.persistence.NoResultException; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; + +import org.apache.log4j.Logger; +import org.hibernate.SQLQuery; +import org.hibernate.Session; + +import ispyb.server.biosaxs.services.core.ExperimentScope; +import ispyb.server.biosaxs.services.core.plateType.PlateType3Service; +import ispyb.server.biosaxs.services.core.proposal.SaxsProposal3Service; +import ispyb.server.biosaxs.vos.assembly.Stoichiometry3VO; +import ispyb.server.biosaxs.vos.assembly.Structure3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Buffer3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Experiment3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Measurement3VO; +import ispyb.server.biosaxs.vos.datacollection.MeasurementTodataCollection3VO; +import ispyb.server.biosaxs.vos.datacollection.SaxsDataCollection3VO; +import ispyb.server.biosaxs.vos.utils.comparator.SaxsDataCollectionComparator; +import ispyb.server.biosaxs.vos.utils.parser.RobotXMLParser; +import ispyb.server.mx.services.ws.rest.WsServiceBean; + + + +@Stateless +public class Experiment3ServiceBean extends WsServiceBean implements Experiment3Service, Experiment3ServiceLocal { + + private final static Logger log = Logger.getLogger(Experiment3ServiceBean.class); + + + /** + * QUERIES + */ + private String GetExperimentDescriptionByExperimentId = getViewExperimentDescriptionTableQuery() + " where experimentId = :experimentId"; + + + + @PersistenceContext(unitName = "ispyb_db") + private EntityManager entityManager; + + @EJB + private SaxsProposal3Service saxsProposal3Service; + + @EJB + private PlateType3Service plateType3Service; + + @Override + public void persist(Experiment3VO transientInstance) { + try { + entityManager.persist(transientInstance); + } catch (RuntimeException re) { + throw re; + } + } + + @Override + public Experiment3VO merge(Experiment3VO detachedInstance) { + try { + Experiment3VO result = entityManager.merge(detachedInstance); + return result; + } catch (RuntimeException re) { + throw re; + } + } + + private String getViewExperimentDescriptionTableQuery(){ + return this.getQueryFromResourceFile("/queries/biosaxs/Experiment3ServiceBean/getViewExperimentDescriptionTableQuery.sql"); + } + + + @Override + public List findByProposalId(int proposalId, ExperimentScope scope) { + StringBuilder ejbQLQuery = Experiment3ServiceBean.getQueryByScope(scope); + ejbQLQuery.append("WHERE experiment.proposalId = :proposalId"); + TypedQuery query = entityManager.createQuery(ejbQLQuery.toString(), Experiment3VO.class).setParameter("proposalId", proposalId); + return query.getResultList(); + } + + @Override + public Experiment3VO findById(Integer experimentId, ExperimentScope scope, Integer proposalId) { + try { + StringBuilder ejbQLQuery = Experiment3ServiceBean.getQueryByScope(scope); + ejbQLQuery.append("WHERE experiment.experimentId = :experimentId "); + // if coming from manager account, proposalId can be null + if (proposalId != null) { + ejbQLQuery.append(" and experiment.proposalId = :proposalId"); + } + TypedQuery query = entityManager.createQuery(ejbQLQuery.toString(), Experiment3VO.class); + query.setParameter("experimentId", experimentId); + if (proposalId != null) { + query.setParameter("proposalId", proposalId); + } + return query.getSingleResult(); + } catch (NoResultException e) { + return null; + } + } + + public Experiment3VO initPlates(Experiment3VO vo){ + try { + vo.setPlatetype3VOs(this.plateType3Service.findAll()); + } catch (Exception e) { + e.printStackTrace(); + } + return vo; + } + + public static StringBuilder getQueryByScope(ExperimentScope scope){ + StringBuilder ejbQLQuery = new StringBuilder(); + ejbQLQuery.append("SELECT DISTINCT(experiment) FROM Experiment3VO experiment "); + + switch (scope) { + case MINIMAL: + break; + case MEDIUM: + ejbQLQuery.append("LEFT JOIN FETCH experiment.samples samples "); + ejbQLQuery.append("LEFT JOIN FETCH samples.macromolecule3VO macromolecule "); + ejbQLQuery.append("LEFT JOIN FETCH macromolecule.stoichiometry st "); +// ejbQLQuery.append("LEFT JOIN FETCH st.macromolecule3VO "); + ejbQLQuery.append("LEFT JOIN FETCH macromolecule.structure3VOs "); + ejbQLQuery.append("LEFT JOIN FETCH samples.measurements specimens "); + ejbQLQuery.append("LEFT JOIN FETCH experiment.samplePlate3VOs samplePlates "); + ejbQLQuery.append("LEFT JOIN FETCH samplePlates.plategroup3VO "); + ejbQLQuery.append("LEFT JOIN FETCH samplePlates.sampleplateposition3VOs "); + + ejbQLQuery.append("LEFT JOIN FETCH specimens.merge3VOs merges "); + ejbQLQuery.append("LEFT JOIN FETCH specimens.run3VO "); + break; + case PREPARE_EXPERIMENT: + ejbQLQuery.append("LEFT JOIN FETCH experiment.samples samples "); + ejbQLQuery.append("LEFT JOIN FETCH samples.macromolecule3VO "); + ejbQLQuery.append("LEFT JOIN FETCH samples.measurements specimens "); + ejbQLQuery.append("LEFT JOIN FETCH experiment.samplePlate3VOs samplePlates "); + ejbQLQuery.append("LEFT JOIN FETCH samplePlates.plategroup3VO "); + ejbQLQuery.append("LEFT JOIN FETCH samplePlates.sampleplateposition3VOs "); + ejbQLQuery.append("LEFT JOIN FETCH experiment.dataCollections dataCollections "); + ejbQLQuery.append("LEFT JOIN FETCH dataCollections.measurementtodatacollection3VOs "); + break; + default: + break; + } + return ejbQLQuery; + } + + + @Override + public Experiment3VO findByMeasurementId(int measurementId){ + StringBuilder ejbQLQuery = Experiment3ServiceBean.getQueryByScope(ExperimentScope.MEDIUM); + ejbQLQuery.append(" wHERE specimens.measurementId = " + measurementId); + TypedQuery query = entityManager.createQuery(ejbQLQuery.toString(), Experiment3VO.class); + return query.getSingleResult(); + } + + + @Override + public List test(String ejbQL) { + try { + TypedQuery query = entityManager.createQuery(ejbQL, Experiment3VO.class); + return query.getResultList(); + } catch (RuntimeException re) { + log.error("get failed", re); + throw re; + } + } + + @Override + public Experiment3VO findById(Integer experimentId, ExperimentScope scope) { + StringBuilder ejbQLQuery = Experiment3ServiceBean.getQueryByScope(scope); + ejbQLQuery.append("WHERE experiment.experimentId = :experimentId"); + TypedQuery query = entityManager.createQuery(ejbQLQuery.toString(), Experiment3VO.class); + query.setParameter("experimentId", experimentId); + List results = query.getResultList(); + if (results.isEmpty()) { + return null; // handle no-results case + } else { + return results.get(0); + } + } + + + @Override + public List> getExperimentDescription(Integer experimentId) { + Session session = (Session) this.entityManager.getDelegate(); + SQLQuery query = session.createSQLQuery(GetExperimentDescriptionByExperimentId); + query.setParameter("experimentId", experimentId); + return executeSQLQuery(query); + } + + + + /** + * It returns the xml as String of the robot.xml file + * This xml format is used on BsxCube for load sample changer experiments + * + */ + @Override + public String toRobotXML(Integer experimentId, int proposalId, final SaxsDataCollectionComparator... multipleOptions) { + this.setPriorities(experimentId, proposalId, multipleOptions); + return this.toRobotXML(experimentId, proposalId); + } + + @Override + public String toRobotXML(Integer experimentId, int proposalId) { + Experiment3VO experiment = this.findById(experimentId, ExperimentScope.MEDIUM); + List buffers = saxsProposal3Service.findBuffersByProposalId(proposalId); + return RobotXMLParser.toRobotXML(experiment, experiment.getSamplePlate3VOs(), buffers); + } + + + + @Override + public void remove(Experiment3VO experiment) { + Experiment3VO result = entityManager.merge(experiment); + this.entityManager.remove(result); + } + + /** + * Sets the order for each data collection based on SaxsDataCollectionComparator + */ + @Override + public Experiment3VO setPriorities(int experimentId, int proposalId, SaxsDataCollectionComparator[] multipleOptions) { + Experiment3VO experiment = this.findById(experimentId, ExperimentScope.MEDIUM, proposalId); + List dataCollectionList = experiment.getDataCollectionList(); + /** Sort data collections by the priority order **/ + Collections.sort(dataCollectionList, SaxsDataCollectionComparator.compare(experiment, SaxsDataCollectionComparator.getComparator(multipleOptions))); + + int priority = 1; + for (int i = 0; i < dataCollectionList.size(); i++) { + SaxsDataCollection3VO datacollection = dataCollectionList.get(i); + List measurementsToDatacollection = this.getMeasurementToDataCollectionOrdered(datacollection); + for (MeasurementTodataCollection3VO m : measurementsToDatacollection) { + Measurement3VO measurement = experiment.getMeasurementById(m.getMeasurementId()); + measurement.setPriority(priority); + measurement = this.entityManager.merge(measurement); + priority ++; + } + } + return experiment; + + } + /** Sort two measurements by their dataCollectionOrder field **/ + private static Comparator MeasurementTodataCollectionComparatorOrder = new Comparator(){ + public int compare(MeasurementTodataCollection3VO o1, MeasurementTodataCollection3VO o2) { + return o1.getDataCollectionOrder() - o2.getDataCollectionOrder(); + } + }; + + /** Sort a data collection by its dataCollectionOrder field **/ + private List getMeasurementToDataCollectionOrdered(SaxsDataCollection3VO dataCollection){ + Set measurements = dataCollection.getMeasurementtodatacollection3VOs(); + List list = Arrays.asList(measurements.toArray(new MeasurementTodataCollection3VO[measurements.size()])); + Collections.sort(list, MeasurementTodataCollectionComparatorOrder); + return list; + } + + @Override + public void saveStructure(Integer macromoleculeId, String fileName, String filePath, String type, String symmetry, String multiplicity) { + Structure3VO structure = new Structure3VO(); + structure.setMacromoleculeId(macromoleculeId); + structure.setFilePath(filePath); + structure.setName(fileName); + structure.setType(type); + structure.setSymmetry(symmetry); + structure.setMultiplicity(multiplicity); + structure.setCreationDate(GregorianCalendar.getInstance().getTime()); + entityManager.merge(structure); + } + + @Override + public void removeStructure(int structureId) { + Structure3VO structure = this.entityManager.find(Structure3VO.class, structureId); + this.entityManager.remove(structure); + } + + @Override + public Structure3VO findStructureById(int structureId) { + return this.entityManager.find(Structure3VO.class, structureId); + } + + @Override + public void removeStoichiometry(int stoichiometryId) { + Stoichiometry3VO st = this.entityManager.find(Stoichiometry3VO.class, stoichiometryId); + this.entityManager.remove(st); + } + + @Override + public void saveStoichiometry(int macromoleculeId, int hostmacromoleculeId, String ratio, String comments) { + Stoichiometry3VO stoi = new Stoichiometry3VO(); + stoi.setMacromolecule3VOByHostMacromoleculeId(macromoleculeId); + stoi.setHostmacromoleculeId(hostmacromoleculeId); + stoi.setRatio(ratio); + this.entityManager.merge(stoi); + } + +// @Override +// public Macromolecule3VO findMacromoleculeById(Integer macromoleculeId) { +// StringBuilder ejbQLQuery = new StringBuilder(); +// ejbQLQuery.append("SELECT DISTINCT(macromolecule) FROM Macromolecule3VO macromolecule "); +// ejbQLQuery.append("LEFT JOIN FETCH macromolecule.stoichiometry st "); +// ejbQLQuery.append("LEFT JOIN FETCH st.macromolecule3VO "); +// ejbQLQuery.append("LEFT JOIN FETCH macromolecule.structure3VOs "); +// ejbQLQuery.append("WHERE macromolecule.macromoleculeId = :macromoleculeId"); +// TypedQuery query = entityManager.createQuery(ejbQLQuery.toString(), Macromolecule3VO.class); +// query.setParameter("macromoleculeId", macromoleculeId); +// return query.getSingleResult(); +// } + + @Override + public Structure3VO findStructureByFilePathId(String filePath, int experimentId) { +// Experiment3VO experiment = this.findById(experimentId, ExperimentScope.MINIMAL); +// List macromolecules = saxsProposal3Service.findMacromoleculesByProposalId(experiment.getProposalId()); +// for (Macromolecule3VO macromolecule3vo : macromolecules) { +// +// } + return null; + } + + @Override + public void saveStructure(Structure3VO structure3vo) { + this.entityManager.merge(structure3vo); + + } +} diff --git a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/robot/Robot3ServiceBean.java b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/robot/Robot3ServiceBean.java index b05cecc55..60a952618 100644 --- a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/robot/Robot3ServiceBean.java +++ b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/core/robot/Robot3ServiceBean.java @@ -1,1045 +1,1045 @@ -/******************************************************************************* - * This file is part of ISPyB. - * - * ISPyB is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ISPyB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with ISPyB. If not, see . - * - * Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Brenchereau, M. Bodin, A. De Maria Antolinos - ******************************************************************************************************************************/ - -package ispyb.server.biosaxs.services.core.robot; - -import ispyb.common.util.Constants; -import ispyb.server.biosaxs.services.core.ExperimentScope; -import ispyb.server.biosaxs.services.core.experiment.Experiment3ServiceLocal; -import ispyb.server.biosaxs.services.core.measurement.Measurement3ServiceLocal; -import ispyb.server.biosaxs.services.core.measurementToDataCollection.MeasurementToDatacollection3ServiceLocal; -import ispyb.server.biosaxs.services.core.plateType.PlateType3ServiceLocal; -import ispyb.server.biosaxs.services.core.proposal.SaxsProposal3ServiceLocal; -import ispyb.server.biosaxs.services.core.samplePlate.Sampleplate3ServiceLocal; -import ispyb.server.biosaxs.services.core.specimen.Specimen3Service; -import ispyb.server.biosaxs.vos.assembly.Macromolecule3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Buffer3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Experiment3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Measurement3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Specimen3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.plate.PlateGroup3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.plate.Platetype3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplate3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplateposition3VO; -import ispyb.server.biosaxs.vos.datacollection.MeasurementTodataCollection3VO; -import ispyb.server.biosaxs.vos.datacollection.SaxsDataCollection3VO; - -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; - -import javax.ejb.EJB; -import javax.ejb.Stateless; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -import org.apache.log4j.Logger; - -import com.google.gson.GsonBuilder; - -@Stateless -public class Robot3ServiceBean implements Robot3Service, Robot3ServiceLocal { - - private final static Logger log = Logger.getLogger(Robot3ServiceBean.class); - - /** The specimen3 service local. */ - @EJB - private Measurement3ServiceLocal measurement3ServiceLocal; - - /** The experiment3 service local. */ - @EJB - private Experiment3ServiceLocal experiment3ServiceLocal; - - /** The macromolecule3 service. */ - @EJB - private SaxsProposal3ServiceLocal proposal3Service; - - /** The sample plate3 service. */ - @EJB - private Sampleplate3ServiceLocal samplePlate3Service; - - /** The plate type3 service. */ - @EJB - private PlateType3ServiceLocal plateType3Service; - - /** The measurement to data collection3 service. */ - @EJB - private MeasurementToDatacollection3ServiceLocal measurementToDataCollection3Service; - - @EJB - private Specimen3Service specimen3Service; - - /** The entity manager. */ - @PersistenceContext(unitName = "ispyb_db") - private EntityManager entityManager; - - /** The now. */ - protected static Calendar NOW; - - /** - * Gets the now. - * - * @return the now - */ - private Date getNow() { - NOW = GregorianCalendar.getInstance(); - return NOW.getTime(); - } - - /** - * Gets the buffers. It returns a hashmap - * - * @param samples - * the samples - * @return the buffers - */ - private HashMap parseBuffers(ArrayList> samples, int proposalId) { - List buffersProposal = proposal3Service.findBuffersByProposalId(proposalId); - Robot3ServiceBean.log.info("----------------- parseBuffers ------------ "); - HashMap bufferNameToBuffer3VO = new HashMap(); - - /** Store the name of the macromolecules **/ - HashMap bufferNameToBufferAcronym = new HashMap(); - - /** - * It is possible that users make a mistake and for the same specimen in the same position they set a different - * buffer's name - */ - HashMap bufferPositionToBufferAcronym = new HashMap(); - - for (HashMap sample : samples) { - if (sample.get("type").equals("Buffer")) { - Buffer3VO buffer = new Buffer3VO(); - buffer.setName(sample.get("macromolecule").trim()); - buffer.setAcronym(sample.get("macromolecule").trim()); - buffer.setComment(sample.get("comments")); - - Robot3ServiceBean.log.info("sample: " + sample.toString()); - Robot3ServiceBean.log.info("buffername: " + sample.get("buffername")); - - String positionCode = this.getSamplePositionKey(sample); - if (bufferPositionToBufferAcronym.get(positionCode) == null) { - bufferNameToBufferAcronym.put(sample.get("buffername").trim(), sample.get("macromolecule").trim()); - bufferPositionToBufferAcronym.put(positionCode, sample.get("macromolecule").trim()); - } else { - Robot3ServiceBean.log.info("Buffers detected in the same position with different macromolecule name"); - log.warn("Buffers detected in the same position with different macromolecule name"); - bufferNameToBufferAcronym.put(sample.get("buffername").trim(), - bufferPositionToBufferAcronym.get(positionCode)); - } - } - } - - for (String bufferName : bufferNameToBufferAcronym.keySet()) { - Buffer3VO bufferExists = this - .getBufferByAcronym(buffersProposal, bufferNameToBufferAcronym.get(bufferName)); - if (bufferExists != null) { - bufferNameToBuffer3VO.put(bufferName, bufferExists); - } else { - Buffer3VO buffer = new Buffer3VO(); - buffer.setAcronym(bufferNameToBufferAcronym.get(bufferName)); - buffer.setName(bufferNameToBufferAcronym.get(bufferName)); - bufferNameToBuffer3VO.put(bufferName, buffer); - } - } - - return bufferNameToBuffer3VO; - } - - /** If the acronym exists in the list the macromolecule is returned **/ - private Macromolecule3VO getMacromoleculeByAcronym(List macromolecules, String acronym) { - for (Macromolecule3VO macromolecule : macromolecules) { - if (macromolecule.getAcronym().toLowerCase().equals(acronym.toLowerCase())) { - return macromolecule; - } - } - return null; - } - - /** If the acronym exists in the list the buffer is returned **/ - private Buffer3VO getBufferByAcronym(List buffers, String acronym) { - for (Buffer3VO buffer : buffers) { - if (buffer.getAcronym().toLowerCase().equals(acronym.toLowerCase())) { - return buffer; - } - } - return null; - } - - /** - * Gets the macromolecules. If there are two with the same name it means that they are the same macromolecules - * - * @param samples - * the samples - * @return the macromolecules - */ - private HashMap parseMacromolecules(ArrayList> samples, - int proposalId) { - List macromoleculesProposalList = proposal3Service.findMacromoleculesByProposalId(proposalId); - Robot3ServiceBean.log.info("----------------- parseMacromolecules ------------ "); - HashMap acronymToMacromolecule3VO = new HashMap(); - /** Store the name of the macromolecules **/ - HashSet macromoleculeAcronyms = new HashSet(); - for (HashMap sample : samples) { - if (sample.get("type").equals("Sample")) { - /** If not macromolecule is defined we use the code **/ - if (sample.get("macromolecule") == null) { - macromoleculeAcronyms.add(sample.get("code").trim()); - } else { - macromoleculeAcronyms.add(sample.get("macromolecule").trim()); - } - } - } - - for (String acronym : macromoleculeAcronyms) { - Macromolecule3VO macromoleculeExists = this.getMacromoleculeByAcronym(macromoleculesProposalList, acronym); - if (macromoleculeExists != null) { - acronymToMacromolecule3VO.put(acronym, macromoleculeExists); - } else { - Macromolecule3VO macromolecule = new Macromolecule3VO(); - macromolecule.setAcronym(acronym); - macromolecule.setName(acronym); - acronymToMacromolecule3VO.put(acronym, macromolecule); - } - } - return acronymToMacromolecule3VO; - } - - private String getSamplePositionKey(HashMap sample) { - return sample.get("plate") + "_" + sample.get("row") + "_" + sample.get("well"); - } - - /** - * Gets the specimens. - * - * @param samples - * the samples - * @param acronymToMacromolecule3VO - * the macromolecules - * @param bufferNameToBuffer3VO - * the buffers - * @param plateIndexToSamplePlate3VO - * the plates - * @return HashMap where String is bufferName for the buffers and position for the Samples - */ - private HashMap getSpecimens(ArrayList> samples, - HashMap acronymToMacromolecule3VO, - HashMap bufferNameToBuffer3VO, HashMap plateIndexToSamplePlate3VO) { - - HashMap specimenIdToSpecimen3VO = new HashMap(); - - /** Looking for positions **/ - for (HashMap sample : samples) { - // String positionCode = sample.get("plate") + "_" + sample.get("row") + "_" + sample.get("well"); - String positionCode = this.getSamplePositionKey(sample); - Specimen3VO specimen = null; - - if (specimenIdToSpecimen3VO.get(positionCode) == null) { - /** Creating new Sample for the experiment **/ - specimen = new Specimen3VO(); - /** Buffer **/ - Buffer3VO buffer = bufferNameToBuffer3VO.get(sample.get("buffername").trim()); - if (buffer == null) { - log.error("Buffer not found with buffer name: " + sample.get("buffername")); - } - specimen.setBufferId(buffer.getBufferId()); - - /** Macromolecule **/ - if (sample.get("type").equals("Sample")) { - Macromolecule3VO macromolecule3VO = acronymToMacromolecule3VO.get(sample.get("macromolecule")); - specimen.setMacromolecule3VO(macromolecule3VO); - } - - /** Position **/ - if (plateIndexToSamplePlate3VO != null) { - Sampleplate3VO plate = plateIndexToSamplePlate3VO.get(sample.get("plate")); - if (plate != null) { - Sampleplateposition3VO position = new Sampleplateposition3VO(); - position.setSamplePlateId(plate.getSamplePlateId()); - position.setRowNumber(Integer.parseInt(sample.get("row"))); - position.setColumnNumber(Integer.parseInt(sample.get("well"))); - position = entityManager.merge(position); - specimen.setSampleplateposition3VO(position); - } - } - /** Specimen properties **/ - specimen.setConcentration(sample.get("concentration")); - specimen.setVolume(sample.get("volume")); -// specimen.setCode(sample.get("macromolecule")); - } else { - /** Updating volume to load **/ - specimen = specimenIdToSpecimen3VO.get(positionCode); - specimen.setVolume(String.valueOf(Integer.parseInt(specimen.getVolume()) - + Integer.parseInt(sample.get("volume")))); - } - - /** it is a buffer we keep the code of the buffername **/ - if (specimen.getMacromolecule3VO() == null) { - specimenIdToSpecimen3VO.put(sample.get("buffername"), specimen); - } else { - specimenIdToSpecimen3VO.put(positionCode, specimen); - } - } - return specimenIdToSpecimen3VO; - } - - /** - * Gets the plates. This should be changed depending on the bm configuration - * - * @param samples - * the samples - * @param storageTemperature - * the storage temperature - * @return the plates - */ -// private HashMap parsePlates(ArrayList> samples, String storageTemperature) { -// /** There is always three Plates **/ -// HashMap platePositionToSamplePlate3VO = new HashMap(); -// -// /** Plate 1 **/ -// Platetype3VO type = this.plateType3Service.findById(1); -// Sampleplate3VO samplePlate_1 = new Sampleplate3VO(); -// samplePlate_1.setStorageTemperature(storageTemperature); -// samplePlate_1.setName(type.getName()); -// samplePlate_1.setSlotPositionRow("1"); -// samplePlate_1.setSlotPositionColumn("1"); -// samplePlate_1.setPlatetype3VO(type); -// platePositionToSamplePlate3VO.put("1", samplePlate_1); -// -// /** Plate 2 **/ -// Platetype3VO type_2 = this.plateType3Service.findById(2); -// Sampleplate3VO samplePlate_2 = new Sampleplate3VO(); -// samplePlate_2.setStorageTemperature(storageTemperature); -// samplePlate_2.setName(type_2.getName()); -// samplePlate_2.setPlatetype3VO(type_2); -// samplePlate_2.setSlotPositionRow("1"); -// samplePlate_2.setSlotPositionColumn("2"); -// platePositionToSamplePlate3VO.put("2", samplePlate_2); -// -// /** Plate 3 **/ -// Platetype3VO type_3 = this.plateType3Service.findById(4); -// Sampleplate3VO samplePlate_3 = new Sampleplate3VO(); -// samplePlate_3.setStorageTemperature(storageTemperature); -// samplePlate_3.setName(type_3.getName()); -// samplePlate_3.setSlotPositionRow("1"); -// samplePlate_3.setSlotPositionColumn("3"); -// samplePlate_3.setPlatetype3VO(type_3); -// platePositionToSamplePlate3VO.put("3", samplePlate_3); -// -// return platePositionToSamplePlate3VO; -// } - - /** - * Parses the specimen. - * - * @param sample - * the sample - * @return the specimen3 vo - */ - private Measurement3VO parseSpecimen(HashMap sample) { - String waittime = sample.get("waittime"); - - /** - * Problem is that from BsxCube there is not any parameters for volume, volume in this case means volumeToLoad - * however from WUI there are both for indicating in buffers specially the volume and the volumeToLoad - * **/ - String volumeToLoad = sample.get("volume"); - if (sample.containsKey("volumeToLoad")) { - volumeToLoad = sample.get("volumeToLoad"); - } - String comments = sample.get("comments"); - String transmission = sample.get("transmission"); - String viscosity = sample.get("viscosity"); - String flow = sample.get("flow"); - String code = sample.get("code"); - - Measurement3VO measurement = new Measurement3VO(); -// measurement.setCode(code); - measurement.setWaitTime(waittime); - measurement.setVolumeToLoad(volumeToLoad); - measurement.setComment(comments); - measurement.setTransmission(transmission); - measurement.setViscosity(viscosity); - measurement.setFlow(Boolean.valueOf(flow)); - - return measurement; - } - - /** - * Creates the measurements. - * - * @param samples - * the samples - * @param sample3vOs - * the sample3v os - * @param experiment - * the experiment - * @param buffers - * the buffers - * @param mode - * the mode - * @param extraFlowTime - * the extra flow time - * @return the list - * @throws Exception - * the exception - */ - public List createMeasurements(ArrayList> samples, - HashMap sample3vOs, Experiment3VO experiment, HashMap buffers, - String mode, String extraFlowTime) throws Exception { - List dataCollections = new ArrayList(); - - /** Creating for measurements **/ - for (HashMap sample : samples) { - String positionCode = sample.get("plate") + "_" + sample.get("row") + "_" + sample.get("well"); - if (sample.get("type").equals("Sample")) { - - Specimen3VO specimen = sample3vOs.get(positionCode); - String exposureTemperature = sample.get("SEUtemperature"); - Measurement3VO measurement = this.parseSpecimen(sample); - measurement.setSpecimenId(specimen.getSpecimenId()); - measurement.setExtraFlowTime(extraFlowTime); - measurement.setExposureTemperature(exposureTemperature); - -// try{ - /** Code should be runNumber otherwise set as acronym + concentration **/ -// if (sample.get("code") != null){ -// measurement.setCode(sample.get("code")); -// } -// else{ -// measurement.setCode(specimen.getMacromolecule3VO().getAcronym() + "_" + specimen.getConcentration()); -// } -// } -// catch(Exception exp){ -// exp.printStackTrace(); -// System.out.println("It has not been possible to set the code to the measurement"); -// } - - /** Buffer **/ - Specimen3VO buffer = sample3vOs.get(sample.get("buffername")); - - if (buffer == null) { - throw new Exception("Buffer not found: " + sample.get("buffername")); - } - - /** MODE: Before and After **/ - if (mode.equals("BeforeAndAfter")) { - for (HashMap sampleBuffer : samples) { - if (sampleBuffer.get("type").equals("Buffer")) { - if (sampleBuffer.get("buffername").equals(sample.get("buffername"))) { - - /** Measurement Before **/ - Measurement3VO measurementBufferBefore = this.parseSpecimen(sampleBuffer); - measurementBufferBefore.setSpecimenId(buffer.getSpecimenId()); - measurementBufferBefore.setExtraFlowTime(extraFlowTime); - measurementBufferBefore.setExposureTemperature(exposureTemperature); - - /** Copying parameters of the sample measurement **/ - /** Update, buffer's parameter may be different **/ - measurementBufferBefore.setVolumeToLoad(measurement.getVolumeToLoad()); - measurementBufferBefore.setTransmission(measurement.getTransmission()); - measurementBufferBefore.setFlow(measurement.getFlow()); - measurementBufferBefore.setViscosity(measurement.getViscosity()); - measurementBufferBefore.setWaitTime(measurement.getWaitTime()); - - /** But not exposure temperature **/ - measurementBufferBefore.setExposureTemperature(measurement.getExposureTemperature()); - - measurementBufferBefore = this.measurement3ServiceLocal.merge(measurementBufferBefore); - buffer.getMeasurements().add(measurementBufferBefore); - - /** Sample **/ - measurement = this.measurement3ServiceLocal.merge(measurement); - specimen.getMeasurements().add(measurement); - - /** After **/ - Measurement3VO specimenBufferAfter = this.parseSpecimen(sampleBuffer); - specimenBufferAfter.setSpecimenId(buffer.getSpecimenId()); - specimenBufferAfter.setExtraFlowTime(extraFlowTime); - specimenBufferAfter.setExposureTemperature(exposureTemperature); - /** Specimen After Code **/ - // specimenBufferAfter.setCode(experiment.getCodeSpecimen(buffer, specimenBufferAfter)); - - /** Copying parameters of the sample measurement **/ - specimenBufferAfter.setVolumeToLoad(measurement.getVolumeToLoad()); - specimenBufferAfter.setTransmission(measurement.getTransmission()); - specimenBufferAfter.setFlow(measurement.getFlow()); - specimenBufferAfter.setViscosity(measurement.getViscosity()); - specimenBufferAfter.setWaitTime(measurement.getWaitTime()); - - specimenBufferAfter.setExposureTemperature(measurement.getExposureTemperature()); - - specimenBufferAfter = this.measurement3ServiceLocal.merge(specimenBufferAfter); - buffer.getMeasurements().add(specimenBufferAfter); - - /** Creating Data Collections **/ - List dataCollectionSpecimens = new ArrayList(); - dataCollectionSpecimens.add(measurementBufferBefore); - dataCollectionSpecimens.add(measurement); - dataCollectionSpecimens.add(specimenBufferAfter); - dataCollections.add(this.createDataCollection(dataCollectionSpecimens, experiment)); - } - } - } - } - - if (mode.equals("After")) { - for (HashMap sampleBuffer : samples) { - if (sampleBuffer.get("type").equals("Buffer")) { - if (sampleBuffer.get("buffername").equals(sample.get("buffername"))) { - - /** Sample **/ - measurement = this.measurement3ServiceLocal.merge(measurement); - - /** After **/ - Measurement3VO measurementBufferAfter = this.parseSpecimen(sampleBuffer); - measurementBufferAfter.setSpecimenId(buffer.getSpecimenId()); - measurementBufferAfter.setExtraFlowTime(extraFlowTime); - measurementBufferAfter.setExposureTemperature(exposureTemperature); - - /** Specimen code **/ - // measurementBufferAfter.setCode(experiment.getCodeSpecimen(buffer, - // measurementBufferAfter)); - measurementBufferAfter = this.measurement3ServiceLocal.merge(measurementBufferAfter); - - /** Creating Data Collections **/ - List dataCollectionSpecimens = new ArrayList(); - dataCollectionSpecimens.add(measurement); - dataCollections.add(this.createDataCollection(dataCollectionSpecimens, experiment)); - } - } - } - } - - if (mode.equals("Before")) { - for (HashMap sampleBuffer : samples) { - if (sampleBuffer.get("type").equals("Buffer")) { - if (sampleBuffer.get("buffername").equals(sample.get("buffername"))) { - Measurement3VO specimenBufferBefore = this.parseSpecimen(sampleBuffer); - specimenBufferBefore.setSpecimenId(buffer.getSpecimenId()); - specimenBufferBefore.setExtraFlowTime(extraFlowTime); - specimenBufferBefore.setExposureTemperature(exposureTemperature); - - /** Specimen Before **/ - // specimenBufferBefore.setCode(experiment.getCodeSpecimen(buffer, - // specimenBufferBefore)); - specimenBufferBefore = this.measurement3ServiceLocal.merge(specimenBufferBefore); - buffer.getMeasurements().add(specimenBufferBefore); - - /** Sample **/ - measurement = this.measurement3ServiceLocal.merge(measurement); - specimen.getMeasurements().add(measurement); - - /** Creating Data Collections **/ - List dataCollectionSpecimens = new ArrayList(); - dataCollectionSpecimens.add(specimenBufferBefore); - dataCollectionSpecimens.add(measurement); - dataCollections.add(this.createDataCollection(dataCollectionSpecimens, experiment)); - } - } - } - } - - if (mode.equals("None")) { - /** Single sample with no data collection **/ - measurement = this.measurement3ServiceLocal.merge(measurement); - specimen.getMeasurements().add(measurement); - - } - } - } - return dataCollections; - } - - /** - * Creates the data collection. - * - * @param specimens - * the specimens - * @param experiment - * the experiment - * @return the saxs data collection3 vo - */ - private SaxsDataCollection3VO createDataCollection(List specimens, Experiment3VO experiment) { - SaxsDataCollection3VO dc = new SaxsDataCollection3VO(); - dc.setExperimentId(experiment.getExperimentId()); - dc = this.entityManager.merge(dc); - - int order = 1; - for (Measurement3VO specimen3vo : specimens) { - MeasurementTodataCollection3VO mc = new MeasurementTodataCollection3VO(); - mc.setDataCollectionId(dc.getDataCollectionId()); - mc.setMeasurementId(specimen3vo.getMeasurementId()); - mc.setDataCollectionOrder(order); - dc.getMeasurementtodatacollection3VOs().add(this.measurementToDataCollection3Service.merge(mc)); - order++; - - } - return dc; - } - - private Specimen3VO isAlreadyCreatedByPosition(Specimen3VO specimen, List specimens) { - if (specimen.getSampleplateposition3VO() != null) { - /** Checking for position of the specimen **/ - for (Specimen3VO existingSpecimen : specimens) { - if (existingSpecimen.getSampleplateposition3VO() != null) { - if (existingSpecimen.getSampleplateposition3VO().getSamplePlateId() - .equals(specimen.getSampleplateposition3VO().getSamplePlateId())) { - if (existingSpecimen.getSampleplateposition3VO().getColumnNumber() == specimen - .getSampleplateposition3VO().getColumnNumber()) { - if (existingSpecimen.getSampleplateposition3VO().getRowNumber() == specimen - .getSampleplateposition3VO().getRowNumber()) { - return existingSpecimen; - } - } - } - } - } - } - return null; - } - - private Experiment3VO createExperimentFromRobotParams(Experiment3VO experiment, - ArrayList> samples, int proposalId, String mode, String storageTemperature, - String extraFlowTime, Boolean optimize) throws Exception { - - try { - ArrayList bufferList = new ArrayList(); - HashMap bufferNameToBuffer3VO = parseBuffers(samples, proposalId); - HashMap bufferIdToBuffer3VO = new HashMap(); - for (String key : bufferNameToBuffer3VO.keySet()) { - Buffer3VO buffer3VO = bufferNameToBuffer3VO.get(key); - buffer3VO.setProposalId(proposalId); - buffer3VO = this.proposal3Service.merge(buffer3VO); - bufferNameToBuffer3VO.put(key, buffer3VO); - bufferIdToBuffer3VO.put(buffer3VO.getBufferId(), buffer3VO); - bufferList.add(buffer3VO); - } - - HashMap acronymToMacromolecule3VO = parseMacromolecules(samples, proposalId); - log.debug("----------------- MACROMOLECULES ------------"); - log.debug(acronymToMacromolecule3VO.toString()); - for (String key : acronymToMacromolecule3VO.keySet()) { - Macromolecule3VO macromolecule3VO = acronymToMacromolecule3VO.get(key); - macromolecule3VO.setProposalId(proposalId); - macromolecule3VO = this.proposal3Service.merge(macromolecule3VO); - acronymToMacromolecule3VO.put(key, macromolecule3VO); - } - - /** There is only one plate group **/ - PlateGroup3VO plateGroup = new PlateGroup3VO(); - plateGroup.setName("BsxCube Group 1"); - plateGroup.setStorageTemperature(storageTemperature); - plateGroup = this.samplePlate3Service.merge(plateGroup); - - /** Plate index is the position in the plate in the sample changer: 1,2,3 for bm29 **/ -// HashMap plateIndexToSamplePlate3VO = parsePlates(samples, storageTemperature); - HashMap plateIndexToSamplePlate3VO = getDefaultSampleChangerPlateconfiguration(storageTemperature); - System.out.println(new GsonBuilder().excludeFieldsWithModifiers(Modifier.PRIVATE).create().toJson(plateIndexToSamplePlate3VO)); - - /** Creating plates **/ - for (String key : plateIndexToSamplePlate3VO.keySet()) { - Sampleplate3VO plate = plateIndexToSamplePlate3VO.get(key); - plate.setExperimentId(experiment.getExperimentId()); - plate.setStorageTemperature(storageTemperature); - plate.setPlategroup3VO(plateGroup); - plate = this.samplePlate3Service.merge(plate); - plateIndexToSamplePlate3VO.put(key, plate); - experiment.getSamplePlate3VOs().add(plate); - } - - log.debug("----------------- buffers ------------"); - log.debug(bufferNameToBuffer3VO.toString()); - - log.debug("----------------- samples ------------"); - log.debug(samples.toString()); - - HashMap specimens = getSpecimens(samples, acronymToMacromolecule3VO, - bufferNameToBuffer3VO, plateIndexToSamplePlate3VO); - log.debug("----------------- specimens ------------"); - log.debug(specimens.toString()); - - HashMap sampleBuffersVOs = new HashMap(); - - List specimensCreated = new ArrayList(); - /** - * Key: For buffers is the buffer name For samples is the position - */ - for (String key : specimens.keySet()) { - Specimen3VO specimen = specimens.get(key); - if (specimen.getSampleplateposition3VO() != null) { - specimen.setSampleplateposition3VO(entityManager.merge(specimen.getSampleplateposition3VO())); - } - specimen.setExperimentId(experiment.getExperimentId()); - - /** - * Feature when samples contains macromolecule and buffername is the backgroundId Before doing the merge - * we have to check that this is not the case: BUFFERNAME PLATE ROW WELL BUFFER 1 1 1 1 BBSA 2 1 1 1 - * BBSA - * **/ - Specimen3VO existingSpecimenAtSamePosition = isAlreadyCreatedByPosition(specimen, specimensCreated); - if (existingSpecimenAtSamePosition == null) { - specimen = specimen3Service.merge(specimen); - specimensCreated.add(specimen); - experiment.getSamples().add(specimen); - } else { - specimen = existingSpecimenAtSamePosition; - } - - specimens.put(key, specimen); - if (specimen.getMacromolecule3VO() == null) { - sampleBuffersVOs.put(bufferIdToBuffer3VO.get(specimen.getBufferId()).getAcronym(), specimen); - } - } - - List dataCollectionList = createMeasurements(samples, specimens, experiment, - bufferNameToBuffer3VO, mode, extraFlowTime); - - /** Set the priorities but also optimize the buffers **/ - if (optimize) { - // measurement3ServiceLocal.optimizeAndPrioritize(experiment, dataCollections, 1); - measurement3ServiceLocal.resetAllPriorities(experiment, dataCollectionList); - measurement3ServiceLocal.optimizeDatacollectionByRemovingDuplicatedBuffers(experiment, - dataCollectionList, 1); - } - return experiment; - } catch (Exception e) { - throw e; - } - - } - - /** - * createExperimentFromRobotParams - * - * Create the macromolecule, buffer, plates, samples, measurement and executes the prioritizer in order to optimize - * the buffers duplicated When a data collection is: buffer1 sample1 buffer1 buffer1 sample2 buffer1 - * - * Because there are two buffer1 together the prioritizer will remove one like: buffer1 sample1 buffer1 sample2 - * buffer1 - * - * @param samples - * ArrayList> containing the hashmaps with all the values required to generate an - * experiment samples = "[{'code': 'Buff1', 'plate': '2', 'enable': True, 'title': 'P2-1:9', - * 'transmission': 20.0, 'viscosity': 'Low', 'recuperate': False, 'SEUtemperature': 4.0, 'flow': False, - * 'comments': 'test', 'volume': 20, 'buffername': 'testot', 'typen': 0, 'waittime': 0.0, - * 'concentration': 0.0, 'type': 'Buffer', 'well': '9', 'row': 1}] - * - * Coming form WUI we add a new parameter called volumeToLoad in order to distinguish both specially for - * buffer measurements - * - * @param proposalId - * the proposal id - * @param mode - * the mode: "BeforeAndAfter", "None", "Before" and "After" - * @param storageTemperature - * the storage temperature - * @param extraFlowTime - * the extra flow time - * @return the experiment3VO - * - * TODO: Mode should replaced by enumeration - */ - @Override - public Experiment3VO createExperimentFromRobotParams(ArrayList> samples, Integer sessionId, - int proposalId, String mode, String storageTemperature, String extraFlowTime, String type, - String sourceFilePath, String name, String comments) throws Exception { - return this.createExperimentFromRobotParams(samples, sessionId, proposalId, mode, storageTemperature, - extraFlowTime, type, sourceFilePath, name, true, comments); - } - - @Override - public Experiment3VO createExperimentFromRobotParams(ArrayList> samples, Integer sessionId, - int proposalId, String mode, String storageTemperature, String extraFlowTime, String type, - String sourceFilePath, String name, Boolean optimize, String comments) throws Exception { - - Experiment3VO experiment = new Experiment3VO(); - experiment.setType(type); - - experiment.setSourceFilePath(sourceFilePath); - experiment.setCreationDate(getNow()); - experiment.setName(name); - experiment.setProposalId(proposalId); - experiment.setSessionId(sessionId); - experiment.setComments(comments); - experiment = this.experiment3ServiceLocal.merge(experiment); - - if (sourceFilePath != null) { - /** Modifying the path if contains __ID__ then we replace __ID__ by the real id of the experiment **/ - experiment.setSourceFilePath(experiment.getSourceFilePath().replace("__ID__", - String.valueOf(experiment.getExperimentId()))); - experiment = this.experiment3ServiceLocal.merge(experiment); - } - - /** Triming all values **/ - for (HashMap hashMap : samples) { - for (String key : hashMap.keySet()) { - /** It may be null when creating template and plate = null **/ - if (hashMap.get(key) != null) { - hashMap.put(key, hashMap.get(key).trim()); - } - } - } - - /** Dealing with macromolecule **/ - for (HashMap hashMap : samples) { - if (!hashMap.containsKey("macromolecule")) { - hashMap.put("macromolecule", hashMap.get("code")); - } - } - - experiment = this.createExperimentFromRobotParams(experiment, samples, proposalId, mode, storageTemperature, - extraFlowTime, optimize); - return experiment; - } - - @Override - public Experiment3VO addMeasurementsToExperiment(int experimentId, int proposalId, - ArrayList> samples) throws Exception { - System.out.println("addMeasurementsToExperiment"); - this.log.info("addMeasurementsToExperiment"); -// Experiment3VO experiment = this.experiment3ServiceLocal.findById(experimentId, ExperimentScope.PREPARE_EXPERIMENT, proposalId); - Experiment3VO experiment = this.experiment3ServiceLocal.findById(experimentId, ExperimentScope.MINIMAL, proposalId); - System.out.println("PREPARE_EXPERIMENT"); - this.log.info("PREPARE_EXPERIMENT"); - /** Triming all values **/ - for (HashMap hashMap : samples) { - for (String key : hashMap.keySet()) { - /** It may be null when creating template and plate = null **/ - if (hashMap.get(key) != null) { - hashMap.put(key, hashMap.get(key).trim()); - } - } - } - - HashMap buffers = this.parseBuffers(samples, proposalId); - for (String key : buffers.keySet()) { - System.out.println(buffers.get(key)); - } - - HashMap macromolecules = this.parseMacromolecules(samples, proposalId); - for (String key : macromolecules.keySet()) { - System.out.println(macromolecules.get(key)); - } - - HashMap specimens = this.getSpecimens(samples, macromolecules, buffers); - for (String key : specimens.keySet()) { - System.out.println(specimens.get(key)); - } - - for (String key : specimens.keySet()) { - Specimen3VO sample = specimens.get(key); - sample.setExperimentId(experiment.getExperimentId()); - sample.setSampleplateposition3VO(null); - sample = specimen3Service.merge(sample); - experiment.getSamples().add(sample); - specimens.put(key, sample); - } - this.createMeasurements(samples, specimens, experiment, buffers, "BeforeAndAfter", "0"); - experiment = this.experiment3ServiceLocal.findById(experimentId, ExperimentScope.MEDIUM, proposalId); - return experiment; - } - - private HashMap getSpecimens(ArrayList> samples, - HashMap macromolecules, HashMap buffers) { - - HashMap sample3VOs = new HashMap(); - - /** Looking for positions **/ - for (HashMap sample : samples) { - String positionCode = sample.get("plate") + "_" + sample.get("row") + "_" + sample.get("well"); - Specimen3VO sample3VO = null; - - if (sample3VOs.get(positionCode) == null) { - /** Creating new Sample for the experiment **/ - sample3VO = new Specimen3VO(); - - /** Buffer **/ - Buffer3VO buffer = buffers.get(sample.get("buffername").trim()); - if (buffer == null) { - log.error("Buffer not found with buffer name: " + sample.get("buffername")); - } - sample3VO.setBufferId(buffer.getBufferId()); - - /** Macromolecule **/ - if (sample.get("type").equals("Sample")) { - Macromolecule3VO macromolecule3VO = macromolecules.get(sample.get("macromolecule")); - sample3VO.setMacromolecule3VO(macromolecule3VO); - } - - /** Sample properties **/ - sample3VO.setConcentration(sample.get("concentration")); - sample3VO.setVolume(sample.get("volume")); -// sample3VO.setCode(sample.get("macromolecule")); - - } else { - /** Updating volume to load **/ - sample3VO = sample3VOs.get(positionCode); - sample3VO.setVolume(String.valueOf(Integer.parseInt(sample3VO.getVolume()) - + Integer.parseInt(sample.get("volume")))); - } - - /** it is a buffer we keep the code of the buffername **/ - if (sample3VO.getMacromolecule3VO() == null) { - sample3VOs.put(sample.get("buffername"), sample3VO); - } else { - sample3VOs.put(positionCode, sample3VO); - } - - } - return sample3VOs; - } - - - /** - * Gets the plates. This should be changed depending on the bm configuration - * - * @param samples - * the samples - * @param storageTemperature - * the storage temperature - * @return the plates - */ - @Override - public HashMap getDefaultSampleChangerPlateconfiguration( String storageTemperature) { - /** There is always three Plates **/ - HashMap platePositionToSamplePlate3VO = new HashMap(); - - /** Plate 1 **/ - Platetype3VO type = this.plateType3Service.findById(1); - Sampleplate3VO samplePlate_1 = new Sampleplate3VO(); - samplePlate_1.setStorageTemperature(storageTemperature); - samplePlate_1.setName(type.getName()); - samplePlate_1.setSlotPositionRow("1"); - samplePlate_1.setSlotPositionColumn("1"); - samplePlate_1.setPlatetype3VO(type); - platePositionToSamplePlate3VO.put("1", samplePlate_1); - - /** Plate 2 **/ - Platetype3VO type_2 = this.plateType3Service.findById(2); - Sampleplate3VO samplePlate_2 = new Sampleplate3VO(); - samplePlate_2.setStorageTemperature(storageTemperature); - samplePlate_2.setName(type_2.getName()); - samplePlate_2.setPlatetype3VO(type_2); - samplePlate_2.setSlotPositionRow("1"); - samplePlate_2.setSlotPositionColumn("2"); - platePositionToSamplePlate3VO.put("2", samplePlate_2); - - /** Plate 3 **/ - Platetype3VO type_3 = this.plateType3Service.findById(4); - Sampleplate3VO samplePlate_3 = new Sampleplate3VO(); - samplePlate_3.setStorageTemperature(storageTemperature); - samplePlate_3.setName(type_3.getName()); - samplePlate_3.setSlotPositionRow("1"); - samplePlate_3.setSlotPositionColumn("3"); - samplePlate_3.setPlatetype3VO(type_3); - platePositionToSamplePlate3VO.put("3", samplePlate_3); - - /** At EMBL Layout is vertical instead of horizontal **/ - if (Constants.SITE_IS_EMBL()){ - samplePlate_1.setSlotPositionRow("1"); - samplePlate_1.setSlotPositionColumn("1"); - samplePlate_2.setSlotPositionRow("2"); - samplePlate_2.setSlotPositionColumn("1"); - samplePlate_3.setSlotPositionRow("3"); - samplePlate_3.setSlotPositionColumn("1"); - } - return platePositionToSamplePlate3VO; - } - - public ArrayList createOrUpdateMacromolecule(ArrayList> samples, int proposalId) throws Exception { - Robot3ServiceBean.log.info("createOrUpdateMacromolecule"); - - ArrayList macromoleculeList = new ArrayList(); - - /** Triming all values **/ - for (HashMap hashMap : samples) { - for (String key : hashMap.keySet()) { - /** It may be null when creating template and plate = null **/ - if (hashMap.get(key) != null) { - hashMap.put(key, hashMap.get(key).trim()); - } - } - } - - HashMap macromolecules = this.parseMacromolecules(samples, proposalId); - for (String key : macromolecules.keySet()) { - Robot3ServiceBean.log.info("createOrUpdateMacromolecule: " + macromolecules.get(key)); - } - - /** Macromolecule **/ - for (HashMap sample : samples) { - if (sample.get("type").equals("Sample")) { - Macromolecule3VO macromolecule3VO = macromolecules.get(sample.get("macromolecule")); - Robot3ServiceBean.log.info("createOrUpdateMacromolecule macromolecule.acronym = " + macromolecule3VO.getAcronym()); - if (macromolecule3VO != null) { - macromolecule3VO.setProposalId(proposalId); - macromolecule3VO = this.proposal3Service.merge(macromolecule3VO); - macromoleculeList.add(macromolecule3VO); - } - } - } - Robot3ServiceBean.log.info("createOrUpdateMacromolecule size = " + macromoleculeList.size()); - - return macromoleculeList; - } - - public ArrayList createOrUpdateBuffer(ArrayList> samples, int proposalId) throws Exception { - Robot3ServiceBean.log.info("createOrUpdateBuffer"); - - ArrayList bufferList = new ArrayList(); - - /** Triming all values **/ - for (HashMap hashMap : samples) { - for (String key : hashMap.keySet()) { - /** It may be null when creating template and plate = null **/ - if (hashMap.get(key) != null) { - hashMap.put(key, hashMap.get(key).trim()); - } - } - } - - HashMap buffers = this.parseBuffers(samples, proposalId); - for (String key : buffers.keySet()) { - Robot3ServiceBean.log.info("createOrUpdateBuffer: key= " + key + " | value= " + buffers.get(key)); - } - - /** Buffer **/ - for (HashMap sample : samples) { - Buffer3VO buffer3VO = buffers.get(sample.get("buffername").trim()); - if (buffer3VO == null) { - log.error("Buffer not found with buffer name: " + sample.get("buffername")); - } - - if (buffer3VO != null) { - buffer3VO.setProposalId(proposalId); - buffer3VO = this.proposal3Service.merge(buffer3VO); - bufferList.add(buffer3VO); - } - } - Robot3ServiceBean.log.info("createOrUpdateBuffer size = " + bufferList.size()); - - return bufferList; - } -} +/******************************************************************************* + * This file is part of ISPyB. + * + * ISPyB is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ISPyB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with ISPyB. If not, see . + * + * Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Brenchereau, M. Bodin, A. De Maria Antolinos + ******************************************************************************************************************************/ + +package ispyb.server.biosaxs.services.core.robot; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; + +import javax.ejb.EJB; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.apache.log4j.Logger; + +import com.google.gson.GsonBuilder; + +import ispyb.common.util.Constants; +import ispyb.server.biosaxs.services.core.ExperimentScope; +import ispyb.server.biosaxs.services.core.experiment.Experiment3ServiceLocal; +import ispyb.server.biosaxs.services.core.measurement.Measurement3ServiceLocal; +import ispyb.server.biosaxs.services.core.measurementToDataCollection.MeasurementToDatacollection3ServiceLocal; +import ispyb.server.biosaxs.services.core.plateType.PlateType3ServiceLocal; +import ispyb.server.biosaxs.services.core.proposal.SaxsProposal3ServiceLocal; +import ispyb.server.biosaxs.services.core.samplePlate.Sampleplate3ServiceLocal; +import ispyb.server.biosaxs.services.core.specimen.Specimen3Service; +import ispyb.server.biosaxs.vos.assembly.Macromolecule3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Buffer3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Experiment3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Measurement3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Specimen3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.plate.PlateGroup3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.plate.Platetype3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplate3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplateposition3VO; +import ispyb.server.biosaxs.vos.datacollection.MeasurementTodataCollection3VO; +import ispyb.server.biosaxs.vos.datacollection.SaxsDataCollection3VO; + +@Stateless +public class Robot3ServiceBean implements Robot3Service, Robot3ServiceLocal { + + private final static Logger log = Logger.getLogger(Robot3ServiceBean.class); + + /** The specimen3 service local. */ + @EJB + private Measurement3ServiceLocal measurement3ServiceLocal; + + /** The experiment3 service local. */ + @EJB + private Experiment3ServiceLocal experiment3ServiceLocal; + + /** The macromolecule3 service. */ + @EJB + private SaxsProposal3ServiceLocal proposal3Service; + + /** The sample plate3 service. */ + @EJB + private Sampleplate3ServiceLocal samplePlate3Service; + + /** The plate type3 service. */ + @EJB + private PlateType3ServiceLocal plateType3Service; + + /** The measurement to data collection3 service. */ + @EJB + private MeasurementToDatacollection3ServiceLocal measurementToDataCollection3Service; + + @EJB + private Specimen3Service specimen3Service; + + /** The entity manager. */ + @PersistenceContext(unitName = "ispyb_db") + private EntityManager entityManager; + + /** The now. */ + protected static Calendar NOW; + + /** + * Gets the now. + * + * @return the now + */ + private Date getNow() { + NOW = GregorianCalendar.getInstance(); + return NOW.getTime(); + } + + /** + * Gets the buffers. It returns a hashmap + * + * @param samples + * the samples + * @return the buffers + */ + private HashMap parseBuffers(ArrayList> samples, int proposalId) { + List buffersProposal = proposal3Service.findBuffersByProposalId(proposalId); + Robot3ServiceBean.log.info("----------------- parseBuffers ------------ "); + HashMap bufferNameToBuffer3VO = new HashMap(); + + /** Store the name of the macromolecules **/ + HashMap bufferNameToBufferAcronym = new HashMap(); + + /** + * It is possible that users make a mistake and for the same specimen in the same position they set a different + * buffer's name + */ + HashMap bufferPositionToBufferAcronym = new HashMap(); + + for (HashMap sample : samples) { + if (sample.get("type").equals("Buffer")) { + Buffer3VO buffer = new Buffer3VO(); + buffer.setName(sample.get("macromolecule").trim()); + buffer.setAcronym(sample.get("macromolecule").trim()); + buffer.setComment(sample.get("comments")); + + Robot3ServiceBean.log.info("sample: " + sample.toString()); + Robot3ServiceBean.log.info("buffername: " + sample.get("buffername")); + + String positionCode = this.getSamplePositionKey(sample); + if (bufferPositionToBufferAcronym.get(positionCode) == null) { + bufferNameToBufferAcronym.put(sample.get("buffername").trim(), sample.get("macromolecule").trim()); + bufferPositionToBufferAcronym.put(positionCode, sample.get("macromolecule").trim()); + } else { + Robot3ServiceBean.log.info("Buffers detected in the same position with different macromolecule name"); + log.warn("Buffers detected in the same position with different macromolecule name"); + bufferNameToBufferAcronym.put(sample.get("buffername").trim(), + bufferPositionToBufferAcronym.get(positionCode)); + } + } + } + + for (String bufferName : bufferNameToBufferAcronym.keySet()) { + Buffer3VO bufferExists = this + .getBufferByAcronym(buffersProposal, bufferNameToBufferAcronym.get(bufferName)); + if (bufferExists != null) { + bufferNameToBuffer3VO.put(bufferName, bufferExists); + } else { + Buffer3VO buffer = new Buffer3VO(); + buffer.setAcronym(bufferNameToBufferAcronym.get(bufferName)); + buffer.setName(bufferNameToBufferAcronym.get(bufferName)); + bufferNameToBuffer3VO.put(bufferName, buffer); + } + } + + return bufferNameToBuffer3VO; + } + + /** If the acronym exists in the list the macromolecule is returned **/ + private Macromolecule3VO getMacromoleculeByAcronym(List macromolecules, String acronym) { + for (Macromolecule3VO macromolecule : macromolecules) { + if (macromolecule.getAcronym().toLowerCase().equals(acronym.toLowerCase())) { + return macromolecule; + } + } + return null; + } + + /** If the acronym exists in the list the buffer is returned **/ + private Buffer3VO getBufferByAcronym(List buffers, String acronym) { + for (Buffer3VO buffer : buffers) { + if (buffer.getAcronym().toLowerCase().equals(acronym.toLowerCase())) { + return buffer; + } + } + return null; + } + + /** + * Gets the macromolecules. If there are two with the same name it means that they are the same macromolecules + * + * @param samples + * the samples + * @return the macromolecules + */ + private HashMap parseMacromolecules(ArrayList> samples, + int proposalId) { + List macromoleculesProposalList = proposal3Service.findMacromoleculesByProposalId(proposalId); + Robot3ServiceBean.log.info("----------------- parseMacromolecules ------------ "); + HashMap acronymToMacromolecule3VO = new HashMap(); + /** Store the name of the macromolecules **/ + HashSet macromoleculeAcronyms = new HashSet(); + for (HashMap sample : samples) { + if (sample.get("type").equals("Sample")) { + /** If not macromolecule is defined we use the code **/ + if (sample.get("macromolecule") == null) { + macromoleculeAcronyms.add(sample.get("code").trim()); + } else { + macromoleculeAcronyms.add(sample.get("macromolecule").trim()); + } + } + } + + for (String acronym : macromoleculeAcronyms) { + Macromolecule3VO macromoleculeExists = this.getMacromoleculeByAcronym(macromoleculesProposalList, acronym); + if (macromoleculeExists != null) { + acronymToMacromolecule3VO.put(acronym, macromoleculeExists); + } else { + Macromolecule3VO macromolecule = new Macromolecule3VO(); + macromolecule.setAcronym(acronym); + macromolecule.setName(acronym); + acronymToMacromolecule3VO.put(acronym, macromolecule); + } + } + return acronymToMacromolecule3VO; + } + + private String getSamplePositionKey(HashMap sample) { + return sample.get("plate") + "_" + sample.get("row") + "_" + sample.get("well"); + } + + /** + * Gets the specimens. + * + * @param samples + * the samples + * @param acronymToMacromolecule3VO + * the macromolecules + * @param bufferNameToBuffer3VO + * the buffers + * @param plateIndexToSamplePlate3VO + * the plates + * @return HashMap where String is bufferName for the buffers and position for the Samples + */ + private HashMap getSpecimens(ArrayList> samples, + HashMap acronymToMacromolecule3VO, + HashMap bufferNameToBuffer3VO, HashMap plateIndexToSamplePlate3VO) { + + HashMap specimenIdToSpecimen3VO = new HashMap(); + + /** Looking for positions **/ + for (HashMap sample : samples) { + // String positionCode = sample.get("plate") + "_" + sample.get("row") + "_" + sample.get("well"); + String positionCode = this.getSamplePositionKey(sample); + Specimen3VO specimen = null; + + if (specimenIdToSpecimen3VO.get(positionCode) == null) { + /** Creating new Sample for the experiment **/ + specimen = new Specimen3VO(); + /** Buffer **/ + Buffer3VO buffer = bufferNameToBuffer3VO.get(sample.get("buffername").trim()); + if (buffer == null) { + log.error("Buffer not found with buffer name: " + sample.get("buffername")); + } + specimen.setBufferId(buffer.getBufferId()); + + /** Macromolecule **/ + if (sample.get("type").equals("Sample")) { + Macromolecule3VO macromolecule3VO = acronymToMacromolecule3VO.get(sample.get("macromolecule")); + specimen.setMacromolecule3VO(macromolecule3VO); + } + + /** Position **/ + if (plateIndexToSamplePlate3VO != null) { + Sampleplate3VO plate = plateIndexToSamplePlate3VO.get(sample.get("plate")); + if (plate != null) { + Sampleplateposition3VO position = new Sampleplateposition3VO(); + position.setSamplePlateId(plate.getSamplePlateId()); + position.setRowNumber(Integer.parseInt(sample.get("row"))); + position.setColumnNumber(Integer.parseInt(sample.get("well"))); + position = entityManager.merge(position); + specimen.setSampleplateposition3VO(position); + } + } + /** Specimen properties **/ + specimen.setConcentration(sample.get("concentration")); + specimen.setVolume(sample.get("volume")); +// specimen.setCode(sample.get("macromolecule")); + } else { + /** Updating volume to load **/ + specimen = specimenIdToSpecimen3VO.get(positionCode); + specimen.setVolume(String.valueOf(Integer.parseInt(specimen.getVolume()) + + Integer.parseInt(sample.get("volume")))); + } + + /** it is a buffer we keep the code of the buffername **/ + if (specimen.getMacromolecule3VO() == null) { + specimenIdToSpecimen3VO.put(sample.get("buffername"), specimen); + } else { + specimenIdToSpecimen3VO.put(positionCode, specimen); + } + } + return specimenIdToSpecimen3VO; + } + + /** + * Gets the plates. This should be changed depending on the bm configuration + * + * @param samples + * the samples + * @param storageTemperature + * the storage temperature + * @return the plates + */ +// private HashMap parsePlates(ArrayList> samples, String storageTemperature) { +// /** There is always three Plates **/ +// HashMap platePositionToSamplePlate3VO = new HashMap(); +// +// /** Plate 1 **/ +// Platetype3VO type = this.plateType3Service.findById(1); +// Sampleplate3VO samplePlate_1 = new Sampleplate3VO(); +// samplePlate_1.setStorageTemperature(storageTemperature); +// samplePlate_1.setName(type.getName()); +// samplePlate_1.setSlotPositionRow("1"); +// samplePlate_1.setSlotPositionColumn("1"); +// samplePlate_1.setPlatetype3VO(type); +// platePositionToSamplePlate3VO.put("1", samplePlate_1); +// +// /** Plate 2 **/ +// Platetype3VO type_2 = this.plateType3Service.findById(2); +// Sampleplate3VO samplePlate_2 = new Sampleplate3VO(); +// samplePlate_2.setStorageTemperature(storageTemperature); +// samplePlate_2.setName(type_2.getName()); +// samplePlate_2.setPlatetype3VO(type_2); +// samplePlate_2.setSlotPositionRow("1"); +// samplePlate_2.setSlotPositionColumn("2"); +// platePositionToSamplePlate3VO.put("2", samplePlate_2); +// +// /** Plate 3 **/ +// Platetype3VO type_3 = this.plateType3Service.findById(4); +// Sampleplate3VO samplePlate_3 = new Sampleplate3VO(); +// samplePlate_3.setStorageTemperature(storageTemperature); +// samplePlate_3.setName(type_3.getName()); +// samplePlate_3.setSlotPositionRow("1"); +// samplePlate_3.setSlotPositionColumn("3"); +// samplePlate_3.setPlatetype3VO(type_3); +// platePositionToSamplePlate3VO.put("3", samplePlate_3); +// +// return platePositionToSamplePlate3VO; +// } + + /** + * Parses the specimen. + * + * @param sample + * the sample + * @return the specimen3 vo + */ + private Measurement3VO parseSpecimen(HashMap sample) { + String waittime = sample.get("waittime"); + + /** + * Problem is that from BsxCube there is not any parameters for volume, volume in this case means volumeToLoad + * however from WUI there are both for indicating in buffers specially the volume and the volumeToLoad + * **/ + String volumeToLoad = sample.get("volume"); + if (sample.containsKey("volumeToLoad")) { + volumeToLoad = sample.get("volumeToLoad"); + } + String comments = sample.get("comments"); + String transmission = sample.get("transmission"); + String viscosity = sample.get("viscosity"); + String flow = sample.get("flow"); + String code = sample.get("code"); + + Measurement3VO measurement = new Measurement3VO(); +// measurement.setCode(code); + measurement.setWaitTime(waittime); + measurement.setVolumeToLoad(volumeToLoad); + measurement.setComment(comments); + measurement.setTransmission(transmission); + measurement.setViscosity(viscosity); + measurement.setFlow(Boolean.valueOf(flow)); + + return measurement; + } + + /** + * Creates the measurements. + * + * @param samples + * the samples + * @param sample3vOs + * the sample3v os + * @param experiment + * the experiment + * @param buffers + * the buffers + * @param mode + * the mode + * @param extraFlowTime + * the extra flow time + * @return the list + * @throws Exception + * the exception + */ + public List createMeasurements(ArrayList> samples, + HashMap sample3vOs, Experiment3VO experiment, HashMap buffers, + String mode, String extraFlowTime) throws Exception { + List dataCollections = new ArrayList(); + + /** Creating for measurements **/ + for (HashMap sample : samples) { + String positionCode = sample.get("plate") + "_" + sample.get("row") + "_" + sample.get("well"); + if (sample.get("type").equals("Sample")) { + + Specimen3VO specimen = sample3vOs.get(positionCode); + String exposureTemperature = sample.get("SEUtemperature"); + Measurement3VO measurement = this.parseSpecimen(sample); + measurement.setSpecimenId(specimen.getSpecimenId()); + measurement.setExtraFlowTime(extraFlowTime); + measurement.setExposureTemperature(exposureTemperature); + +// try{ + /** Code should be runNumber otherwise set as acronym + concentration **/ +// if (sample.get("code") != null){ +// measurement.setCode(sample.get("code")); +// } +// else{ +// measurement.setCode(specimen.getMacromolecule3VO().getAcronym() + "_" + specimen.getConcentration()); +// } +// } +// catch(Exception exp){ +// exp.printStackTrace(); +// System.out.println("It has not been possible to set the code to the measurement"); +// } + + /** Buffer **/ + Specimen3VO buffer = sample3vOs.get(sample.get("buffername")); + + if (buffer == null) { + throw new Exception("Buffer not found: " + sample.get("buffername")); + } + + /** MODE: Before and After **/ + if (mode.equals("BeforeAndAfter")) { + for (HashMap sampleBuffer : samples) { + if (sampleBuffer.get("type").equals("Buffer")) { + if (sampleBuffer.get("buffername").equals(sample.get("buffername"))) { + + /** Measurement Before **/ + Measurement3VO measurementBufferBefore = this.parseSpecimen(sampleBuffer); + measurementBufferBefore.setSpecimenId(buffer.getSpecimenId()); + measurementBufferBefore.setExtraFlowTime(extraFlowTime); + measurementBufferBefore.setExposureTemperature(exposureTemperature); + + /** Copying parameters of the sample measurement **/ + /** Update, buffer's parameter may be different **/ + measurementBufferBefore.setVolumeToLoad(measurement.getVolumeToLoad()); + measurementBufferBefore.setTransmission(measurement.getTransmission()); + measurementBufferBefore.setFlow(measurement.getFlow()); + measurementBufferBefore.setViscosity(measurement.getViscosity()); + measurementBufferBefore.setWaitTime(measurement.getWaitTime()); + + /** But not exposure temperature **/ + measurementBufferBefore.setExposureTemperature(measurement.getExposureTemperature()); + + measurementBufferBefore = this.measurement3ServiceLocal.merge(measurementBufferBefore); + buffer.getMeasurements().add(measurementBufferBefore); + + /** Sample **/ + measurement = this.measurement3ServiceLocal.merge(measurement); + specimen.getMeasurements().add(measurement); + + /** After **/ + Measurement3VO specimenBufferAfter = this.parseSpecimen(sampleBuffer); + specimenBufferAfter.setSpecimenId(buffer.getSpecimenId()); + specimenBufferAfter.setExtraFlowTime(extraFlowTime); + specimenBufferAfter.setExposureTemperature(exposureTemperature); + /** Specimen After Code **/ + // specimenBufferAfter.setCode(experiment.getCodeSpecimen(buffer, specimenBufferAfter)); + + /** Copying parameters of the sample measurement **/ + specimenBufferAfter.setVolumeToLoad(measurement.getVolumeToLoad()); + specimenBufferAfter.setTransmission(measurement.getTransmission()); + specimenBufferAfter.setFlow(measurement.getFlow()); + specimenBufferAfter.setViscosity(measurement.getViscosity()); + specimenBufferAfter.setWaitTime(measurement.getWaitTime()); + + specimenBufferAfter.setExposureTemperature(measurement.getExposureTemperature()); + + specimenBufferAfter = this.measurement3ServiceLocal.merge(specimenBufferAfter); + buffer.getMeasurements().add(specimenBufferAfter); + + /** Creating Data Collections **/ + List dataCollectionSpecimens = new ArrayList(); + dataCollectionSpecimens.add(measurementBufferBefore); + dataCollectionSpecimens.add(measurement); + dataCollectionSpecimens.add(specimenBufferAfter); + dataCollections.add(this.createDataCollection(dataCollectionSpecimens, experiment)); + } + } + } + } + + if (mode.equals("After")) { + for (HashMap sampleBuffer : samples) { + if (sampleBuffer.get("type").equals("Buffer")) { + if (sampleBuffer.get("buffername").equals(sample.get("buffername"))) { + + /** Sample **/ + measurement = this.measurement3ServiceLocal.merge(measurement); + + /** After **/ + Measurement3VO measurementBufferAfter = this.parseSpecimen(sampleBuffer); + measurementBufferAfter.setSpecimenId(buffer.getSpecimenId()); + measurementBufferAfter.setExtraFlowTime(extraFlowTime); + measurementBufferAfter.setExposureTemperature(exposureTemperature); + + /** Specimen code **/ + // measurementBufferAfter.setCode(experiment.getCodeSpecimen(buffer, + // measurementBufferAfter)); + measurementBufferAfter = this.measurement3ServiceLocal.merge(measurementBufferAfter); + + /** Creating Data Collections **/ + List dataCollectionSpecimens = new ArrayList(); + dataCollectionSpecimens.add(measurement); + dataCollections.add(this.createDataCollection(dataCollectionSpecimens, experiment)); + } + } + } + } + + if (mode.equals("Before")) { + for (HashMap sampleBuffer : samples) { + if (sampleBuffer.get("type").equals("Buffer")) { + if (sampleBuffer.get("buffername").equals(sample.get("buffername"))) { + Measurement3VO specimenBufferBefore = this.parseSpecimen(sampleBuffer); + specimenBufferBefore.setSpecimenId(buffer.getSpecimenId()); + specimenBufferBefore.setExtraFlowTime(extraFlowTime); + specimenBufferBefore.setExposureTemperature(exposureTemperature); + + /** Specimen Before **/ + // specimenBufferBefore.setCode(experiment.getCodeSpecimen(buffer, + // specimenBufferBefore)); + specimenBufferBefore = this.measurement3ServiceLocal.merge(specimenBufferBefore); + buffer.getMeasurements().add(specimenBufferBefore); + + /** Sample **/ + measurement = this.measurement3ServiceLocal.merge(measurement); + specimen.getMeasurements().add(measurement); + + /** Creating Data Collections **/ + List dataCollectionSpecimens = new ArrayList(); + dataCollectionSpecimens.add(specimenBufferBefore); + dataCollectionSpecimens.add(measurement); + dataCollections.add(this.createDataCollection(dataCollectionSpecimens, experiment)); + } + } + } + } + + if (mode.equals("None")) { + /** Single sample with no data collection **/ + measurement = this.measurement3ServiceLocal.merge(measurement); + specimen.getMeasurements().add(measurement); + + } + } + } + return dataCollections; + } + + /** + * Creates the data collection. + * + * @param specimens + * the specimens + * @param experiment + * the experiment + * @return the saxs data collection3 vo + */ + private SaxsDataCollection3VO createDataCollection(List specimens, Experiment3VO experiment) { + SaxsDataCollection3VO dc = new SaxsDataCollection3VO(); + dc.setExperimentId(experiment.getExperimentId()); + dc = this.entityManager.merge(dc); + + int order = 1; + for (Measurement3VO specimen3vo : specimens) { + MeasurementTodataCollection3VO mc = new MeasurementTodataCollection3VO(); + mc.setDataCollectionId(dc.getDataCollectionId()); + mc.setMeasurementId(specimen3vo.getMeasurementId()); + mc.setDataCollectionOrder(order); + dc.getMeasurementtodatacollection3VOs().add(this.measurementToDataCollection3Service.merge(mc)); + order++; + + } + return dc; + } + + private Specimen3VO isAlreadyCreatedByPosition(Specimen3VO specimen, List specimens) { + if (specimen.getSampleplateposition3VO() != null) { + /** Checking for position of the specimen **/ + for (Specimen3VO existingSpecimen : specimens) { + if (existingSpecimen.getSampleplateposition3VO() != null) { + if (existingSpecimen.getSampleplateposition3VO().getSamplePlateId() + .equals(specimen.getSampleplateposition3VO().getSamplePlateId())) { + if (existingSpecimen.getSampleplateposition3VO().getColumnNumber() == specimen + .getSampleplateposition3VO().getColumnNumber()) { + if (existingSpecimen.getSampleplateposition3VO().getRowNumber() == specimen + .getSampleplateposition3VO().getRowNumber()) { + return existingSpecimen; + } + } + } + } + } + } + return null; + } + + private Experiment3VO createExperimentFromRobotParams(Experiment3VO experiment, + ArrayList> samples, int proposalId, String mode, String storageTemperature, + String extraFlowTime, Boolean optimize) throws Exception { + + try { + ArrayList bufferList = new ArrayList(); + HashMap bufferNameToBuffer3VO = parseBuffers(samples, proposalId); + HashMap bufferIdToBuffer3VO = new HashMap(); + for (String key : bufferNameToBuffer3VO.keySet()) { + Buffer3VO buffer3VO = bufferNameToBuffer3VO.get(key); + buffer3VO.setProposalId(proposalId); + buffer3VO = this.proposal3Service.merge(buffer3VO); + bufferNameToBuffer3VO.put(key, buffer3VO); + bufferIdToBuffer3VO.put(buffer3VO.getBufferId(), buffer3VO); + bufferList.add(buffer3VO); + } + + HashMap acronymToMacromolecule3VO = parseMacromolecules(samples, proposalId); + log.debug("----------------- MACROMOLECULES ------------"); + log.debug(acronymToMacromolecule3VO.toString()); + for (String key : acronymToMacromolecule3VO.keySet()) { + Macromolecule3VO macromolecule3VO = acronymToMacromolecule3VO.get(key); + macromolecule3VO.setProposalId(proposalId); + macromolecule3VO = this.proposal3Service.merge(macromolecule3VO); + acronymToMacromolecule3VO.put(key, macromolecule3VO); + } + + /** There is only one plate group **/ + PlateGroup3VO plateGroup = new PlateGroup3VO(); + plateGroup.setName("BsxCube Group 1"); + plateGroup.setStorageTemperature(storageTemperature); + plateGroup = this.samplePlate3Service.merge(plateGroup); + + /** Plate index is the position in the plate in the sample changer: 1,2,3 for bm29 **/ +// HashMap plateIndexToSamplePlate3VO = parsePlates(samples, storageTemperature); + HashMap plateIndexToSamplePlate3VO = getDefaultSampleChangerPlateconfiguration(storageTemperature); + System.out.println(new GsonBuilder().excludeFieldsWithModifiers(Modifier.PRIVATE).create().toJson(plateIndexToSamplePlate3VO)); + + /** Creating plates **/ + for (String key : plateIndexToSamplePlate3VO.keySet()) { + Sampleplate3VO plate = plateIndexToSamplePlate3VO.get(key); + plate.setExperimentId(experiment.getExperimentId()); + plate.setStorageTemperature(storageTemperature); + plate.setPlategroup3VO(plateGroup); + plate = this.samplePlate3Service.merge(plate); + plateIndexToSamplePlate3VO.put(key, plate); + experiment.getSamplePlate3VOs().add(plate); + } + + log.debug("----------------- buffers ------------"); + log.debug(bufferNameToBuffer3VO.toString()); + + log.debug("----------------- samples ------------"); + log.debug(samples.toString()); + + HashMap specimens = getSpecimens(samples, acronymToMacromolecule3VO, + bufferNameToBuffer3VO, plateIndexToSamplePlate3VO); + log.debug("----------------- specimens ------------"); + log.debug(specimens.toString()); + + HashMap sampleBuffersVOs = new HashMap(); + + List specimensCreated = new ArrayList(); + /** + * Key: For buffers is the buffer name For samples is the position + */ + for (String key : specimens.keySet()) { + Specimen3VO specimen = specimens.get(key); + if (specimen.getSampleplateposition3VO() != null) { + specimen.setSampleplateposition3VO(entityManager.merge(specimen.getSampleplateposition3VO())); + } + specimen.setExperimentId(experiment.getExperimentId()); + + /** + * Feature when samples contains macromolecule and buffername is the backgroundId Before doing the merge + * we have to check that this is not the case: BUFFERNAME PLATE ROW WELL BUFFER 1 1 1 1 BBSA 2 1 1 1 + * BBSA + * **/ + Specimen3VO existingSpecimenAtSamePosition = isAlreadyCreatedByPosition(specimen, specimensCreated); + if (existingSpecimenAtSamePosition == null) { + specimen = specimen3Service.merge(specimen); + specimensCreated.add(specimen); + experiment.getSamples().add(specimen); + } else { + specimen = existingSpecimenAtSamePosition; + } + + specimens.put(key, specimen); + if (specimen.getMacromolecule3VO() == null) { + sampleBuffersVOs.put(bufferIdToBuffer3VO.get(specimen.getBufferId()).getAcronym(), specimen); + } + } + + List dataCollectionList = createMeasurements(samples, specimens, experiment, + bufferNameToBuffer3VO, mode, extraFlowTime); + + /** Set the priorities but also optimize the buffers **/ + if (optimize) { + // measurement3ServiceLocal.optimizeAndPrioritize(experiment, dataCollections, 1); + measurement3ServiceLocal.resetAllPriorities(experiment, dataCollectionList); + measurement3ServiceLocal.optimizeDatacollectionByRemovingDuplicatedBuffers(experiment, + dataCollectionList, 1); + } + return experiment; + } catch (Exception e) { + throw e; + } + + } + + /** + * createExperimentFromRobotParams + * + * Create the macromolecule, buffer, plates, samples, measurement and executes the prioritizer in order to optimize + * the buffers duplicated When a data collection is: buffer1 sample1 buffer1 buffer1 sample2 buffer1 + * + * Because there are two buffer1 together the prioritizer will remove one like: buffer1 sample1 buffer1 sample2 + * buffer1 + * + * @param samples + * ArrayList> containing the hashmaps with all the values required to generate an + * experiment samples = "[{'code': 'Buff1', 'plate': '2', 'enable': True, 'title': 'P2-1:9', + * 'transmission': 20.0, 'viscosity': 'Low', 'recuperate': False, 'SEUtemperature': 4.0, 'flow': False, + * 'comments': 'test', 'volume': 20, 'buffername': 'testot', 'typen': 0, 'waittime': 0.0, + * 'concentration': 0.0, 'type': 'Buffer', 'well': '9', 'row': 1}] + * + * Coming form WUI we add a new parameter called volumeToLoad in order to distinguish both specially for + * buffer measurements + * + * @param proposalId + * the proposal id + * @param mode + * the mode: "BeforeAndAfter", "None", "Before" and "After" + * @param storageTemperature + * the storage temperature + * @param extraFlowTime + * the extra flow time + * @return the experiment3VO + * + * TODO: Mode should replaced by enumeration + */ + @Override + public Experiment3VO createExperimentFromRobotParams(ArrayList> samples, Integer sessionId, + int proposalId, String mode, String storageTemperature, String extraFlowTime, String type, + String sourceFilePath, String name, String comments) throws Exception { + return this.createExperimentFromRobotParams(samples, sessionId, proposalId, mode, storageTemperature, + extraFlowTime, type, sourceFilePath, name, true, comments); + } + + @Override + public Experiment3VO createExperimentFromRobotParams(ArrayList> samples, Integer sessionId, + int proposalId, String mode, String storageTemperature, String extraFlowTime, String type, + String sourceFilePath, String name, Boolean optimize, String comments) throws Exception { + + Experiment3VO experiment = new Experiment3VO(); + experiment = this.experiment3ServiceLocal.initPlates(experiment); + experiment.setType(type); + experiment.setSourceFilePath(sourceFilePath); + experiment.setCreationDate(getNow()); + experiment.setName(name); + experiment.setProposalId(proposalId); + experiment.setSessionId(sessionId); + experiment.setComments(comments); + experiment = this.experiment3ServiceLocal.merge(experiment); + + if (sourceFilePath != null) { + /** Modifying the path if contains __ID__ then we replace __ID__ by the real id of the experiment **/ + experiment.setSourceFilePath(experiment.getSourceFilePath().replace("__ID__", + String.valueOf(experiment.getExperimentId()))); + experiment = this.experiment3ServiceLocal.merge(experiment); + } + + /** Triming all values **/ + for (HashMap hashMap : samples) { + for (String key : hashMap.keySet()) { + /** It may be null when creating template and plate = null **/ + if (hashMap.get(key) != null) { + hashMap.put(key, hashMap.get(key).trim()); + } + } + } + + /** Dealing with macromolecule **/ + for (HashMap hashMap : samples) { + if (!hashMap.containsKey("macromolecule")) { + hashMap.put("macromolecule", hashMap.get("code")); + } + } + + experiment = this.createExperimentFromRobotParams(experiment, samples, proposalId, mode, storageTemperature, + extraFlowTime, optimize); + return experiment; + } + + @Override + public Experiment3VO addMeasurementsToExperiment(int experimentId, int proposalId, + ArrayList> samples) throws Exception { + System.out.println("addMeasurementsToExperiment"); + this.log.info("addMeasurementsToExperiment"); +// Experiment3VO experiment = this.experiment3ServiceLocal.findById(experimentId, ExperimentScope.PREPARE_EXPERIMENT, proposalId); + Experiment3VO experiment = this.experiment3ServiceLocal.findById(experimentId, ExperimentScope.MINIMAL, proposalId); + System.out.println("PREPARE_EXPERIMENT"); + this.log.info("PREPARE_EXPERIMENT"); + /** Triming all values **/ + for (HashMap hashMap : samples) { + for (String key : hashMap.keySet()) { + /** It may be null when creating template and plate = null **/ + if (hashMap.get(key) != null) { + hashMap.put(key, hashMap.get(key).trim()); + } + } + } + + HashMap buffers = this.parseBuffers(samples, proposalId); + for (String key : buffers.keySet()) { + System.out.println(buffers.get(key)); + } + + HashMap macromolecules = this.parseMacromolecules(samples, proposalId); + for (String key : macromolecules.keySet()) { + System.out.println(macromolecules.get(key)); + } + + HashMap specimens = this.getSpecimens(samples, macromolecules, buffers); + for (String key : specimens.keySet()) { + System.out.println(specimens.get(key)); + } + + for (String key : specimens.keySet()) { + Specimen3VO sample = specimens.get(key); + sample.setExperimentId(experiment.getExperimentId()); + sample.setSampleplateposition3VO(null); + sample = specimen3Service.merge(sample); + experiment.getSamples().add(sample); + specimens.put(key, sample); + } + this.createMeasurements(samples, specimens, experiment, buffers, "BeforeAndAfter", "0"); + experiment = this.experiment3ServiceLocal.findById(experimentId, ExperimentScope.MEDIUM, proposalId); + return experiment; + } + + private HashMap getSpecimens(ArrayList> samples, + HashMap macromolecules, HashMap buffers) { + + HashMap sample3VOs = new HashMap(); + + /** Looking for positions **/ + for (HashMap sample : samples) { + String positionCode = sample.get("plate") + "_" + sample.get("row") + "_" + sample.get("well"); + Specimen3VO sample3VO = null; + + if (sample3VOs.get(positionCode) == null) { + /** Creating new Sample for the experiment **/ + sample3VO = new Specimen3VO(); + + /** Buffer **/ + Buffer3VO buffer = buffers.get(sample.get("buffername").trim()); + if (buffer == null) { + log.error("Buffer not found with buffer name: " + sample.get("buffername")); + } + sample3VO.setBufferId(buffer.getBufferId()); + + /** Macromolecule **/ + if (sample.get("type").equals("Sample")) { + Macromolecule3VO macromolecule3VO = macromolecules.get(sample.get("macromolecule")); + sample3VO.setMacromolecule3VO(macromolecule3VO); + } + + /** Sample properties **/ + sample3VO.setConcentration(sample.get("concentration")); + sample3VO.setVolume(sample.get("volume")); +// sample3VO.setCode(sample.get("macromolecule")); + + } else { + /** Updating volume to load **/ + sample3VO = sample3VOs.get(positionCode); + sample3VO.setVolume(String.valueOf(Integer.parseInt(sample3VO.getVolume()) + + Integer.parseInt(sample.get("volume")))); + } + + /** it is a buffer we keep the code of the buffername **/ + if (sample3VO.getMacromolecule3VO() == null) { + sample3VOs.put(sample.get("buffername"), sample3VO); + } else { + sample3VOs.put(positionCode, sample3VO); + } + + } + return sample3VOs; + } + + + /** + * Gets the plates. This should be changed depending on the bm configuration + * + * @param samples + * the samples + * @param storageTemperature + * the storage temperature + * @return the plates + */ + @Override + public HashMap getDefaultSampleChangerPlateconfiguration( String storageTemperature) { + /** There is always three Plates **/ + HashMap platePositionToSamplePlate3VO = new HashMap(); + + /** Plate 1 **/ + Platetype3VO type = this.plateType3Service.findById(1); + Sampleplate3VO samplePlate_1 = new Sampleplate3VO(); + samplePlate_1.setStorageTemperature(storageTemperature); + samplePlate_1.setName(type.getName()); + samplePlate_1.setSlotPositionRow("1"); + samplePlate_1.setSlotPositionColumn("1"); + samplePlate_1.setPlatetype3VO(type); + platePositionToSamplePlate3VO.put("1", samplePlate_1); + + /** Plate 2 **/ + Platetype3VO type_2 = this.plateType3Service.findById(2); + Sampleplate3VO samplePlate_2 = new Sampleplate3VO(); + samplePlate_2.setStorageTemperature(storageTemperature); + samplePlate_2.setName(type_2.getName()); + samplePlate_2.setPlatetype3VO(type_2); + samplePlate_2.setSlotPositionRow("1"); + samplePlate_2.setSlotPositionColumn("2"); + platePositionToSamplePlate3VO.put("2", samplePlate_2); + + /** Plate 3 **/ + Platetype3VO type_3 = this.plateType3Service.findById(4); + Sampleplate3VO samplePlate_3 = new Sampleplate3VO(); + samplePlate_3.setStorageTemperature(storageTemperature); + samplePlate_3.setName(type_3.getName()); + samplePlate_3.setSlotPositionRow("1"); + samplePlate_3.setSlotPositionColumn("3"); + samplePlate_3.setPlatetype3VO(type_3); + platePositionToSamplePlate3VO.put("3", samplePlate_3); + + /** At EMBL Layout is vertical instead of horizontal **/ + if (Constants.SITE_IS_EMBL()){ + samplePlate_1.setSlotPositionRow("1"); + samplePlate_1.setSlotPositionColumn("1"); + samplePlate_2.setSlotPositionRow("2"); + samplePlate_2.setSlotPositionColumn("1"); + samplePlate_3.setSlotPositionRow("3"); + samplePlate_3.setSlotPositionColumn("1"); + } + return platePositionToSamplePlate3VO; + } + + public ArrayList createOrUpdateMacromolecule(ArrayList> samples, int proposalId) throws Exception { + Robot3ServiceBean.log.info("createOrUpdateMacromolecule"); + + ArrayList macromoleculeList = new ArrayList(); + + /** Triming all values **/ + for (HashMap hashMap : samples) { + for (String key : hashMap.keySet()) { + /** It may be null when creating template and plate = null **/ + if (hashMap.get(key) != null) { + hashMap.put(key, hashMap.get(key).trim()); + } + } + } + + HashMap macromolecules = this.parseMacromolecules(samples, proposalId); + for (String key : macromolecules.keySet()) { + Robot3ServiceBean.log.info("createOrUpdateMacromolecule: " + macromolecules.get(key)); + } + + /** Macromolecule **/ + for (HashMap sample : samples) { + if (sample.get("type").equals("Sample")) { + Macromolecule3VO macromolecule3VO = macromolecules.get(sample.get("macromolecule")); + Robot3ServiceBean.log.info("createOrUpdateMacromolecule macromolecule.acronym = " + macromolecule3VO.getAcronym()); + if (macromolecule3VO != null) { + macromolecule3VO.setProposalId(proposalId); + macromolecule3VO = this.proposal3Service.merge(macromolecule3VO); + macromoleculeList.add(macromolecule3VO); + } + } + } + Robot3ServiceBean.log.info("createOrUpdateMacromolecule size = " + macromoleculeList.size()); + + return macromoleculeList; + } + + public ArrayList createOrUpdateBuffer(ArrayList> samples, int proposalId) throws Exception { + Robot3ServiceBean.log.info("createOrUpdateBuffer"); + + ArrayList bufferList = new ArrayList(); + + /** Triming all values **/ + for (HashMap hashMap : samples) { + for (String key : hashMap.keySet()) { + /** It may be null when creating template and plate = null **/ + if (hashMap.get(key) != null) { + hashMap.put(key, hashMap.get(key).trim()); + } + } + } + + HashMap buffers = this.parseBuffers(samples, proposalId); + for (String key : buffers.keySet()) { + Robot3ServiceBean.log.info("createOrUpdateBuffer: key= " + key + " | value= " + buffers.get(key)); + } + + /** Buffer **/ + for (HashMap sample : samples) { + Buffer3VO buffer3VO = buffers.get(sample.get("buffername").trim()); + if (buffer3VO == null) { + log.error("Buffer not found with buffer name: " + sample.get("buffername")); + } + + if (buffer3VO != null) { + buffer3VO.setProposalId(proposalId); + buffer3VO = this.proposal3Service.merge(buffer3VO); + bufferList.add(buffer3VO); + } + } + Robot3ServiceBean.log.info("createOrUpdateBuffer size = " + bufferList.size()); + + return bufferList; + } +} diff --git a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/webservice/ATSASPipeline3ServiceBean.java b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/webservice/ATSASPipeline3ServiceBean.java index 63e32f735..8b6150279 100644 --- a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/webservice/ATSASPipeline3ServiceBean.java +++ b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/services/webservice/ATSASPipeline3ServiceBean.java @@ -1,1064 +1,1064 @@ -/******************************************************************************* - * This file is part of ISPyB. - * - * ISPyB is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ISPyB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with ISPyB. If not, see . - * - * Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Brenchereau, M. Bodin, A. De Maria Antolinos - ******************************************************************************************************************************/ - -package ispyb.server.biosaxs.services.webservice; - -import ispyb.common.util.Constants; -import ispyb.server.biosaxs.services.core.ExperimentScope; -import ispyb.server.biosaxs.services.core.analysis.abInitioModelling.AbInitioModelling3Service; -import ispyb.server.biosaxs.services.core.analysis.advanced.AdvancedAnalysis3Service; -import ispyb.server.biosaxs.services.core.analysis.primaryDataProcessing.PrimaryDataProcessing3Service; -import ispyb.server.biosaxs.services.core.experiment.Experiment3Service; -import ispyb.server.biosaxs.services.core.measurement.Measurement3Service; -import ispyb.server.biosaxs.services.core.measurementToDataCollection.MeasurementToDataCollection3Service; -import ispyb.server.biosaxs.services.core.proposal.SaxsProposal3Service; -import ispyb.server.biosaxs.services.core.robot.Robot3Service; -import ispyb.server.biosaxs.services.core.samplePlate.Sampleplate3Service; -import ispyb.server.biosaxs.services.core.specimen.Specimen3Service; -import ispyb.server.biosaxs.vos.advanced.FitStructureToExperimentalData3VO; -import ispyb.server.biosaxs.vos.advanced.MixtureToStructure; -import ispyb.server.biosaxs.vos.advanced.RigidBodyModeling3VO; -import ispyb.server.biosaxs.vos.advanced.Superposition3VO; -import ispyb.server.biosaxs.vos.assembly.Macromolecule3VO; -import ispyb.server.biosaxs.vos.assembly.Structure3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Buffer3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Experiment3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Measurement3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.Specimen3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.plate.PlateGroup3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplate3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplateposition3VO; -import ispyb.server.biosaxs.vos.datacollection.Framelist3VO; -import ispyb.server.biosaxs.vos.datacollection.MeasurementTodataCollection3VO; -import ispyb.server.biosaxs.vos.datacollection.Merge3VO; -import ispyb.server.biosaxs.vos.datacollection.Model3VO; -import ispyb.server.biosaxs.vos.datacollection.SaxsDataCollection3VO; -import ispyb.server.biosaxs.vos.datacollection.Subtraction3VO; -import ispyb.server.common.services.proposals.Proposal3Service; -import ispyb.server.common.services.sessions.Session3Service; -import ispyb.server.common.util.LoggerFormatter; -import ispyb.server.common.vos.proposals.Proposal3VO; -import ispyb.server.common.vos.proposals.ProposalWS3VO; -import ispyb.server.mx.vos.collections.Session3VO; -import ispyb.server.mx.vos.collections.SessionWS3VO; - -import java.io.File; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import javax.ejb.EJB; -import javax.ejb.Stateless; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -import org.apache.log4j.Logger; - -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.reflect.TypeToken; - -@Stateless -public class ATSASPipeline3ServiceBean implements ATSASPipeline3Service, DesySampleChangerConnectorServiceLocal { - private final static Logger LOG = Logger.getLogger("ATSASPipeline3ServiceBean"); - - /** The entity manager. */ - @PersistenceContext(unitName = "ispyb_db") - private EntityManager entityManager; - - @EJB - private Proposal3Service proposalService; - - @EJB - private SaxsProposal3Service saxsProposal3Service; - - @EJB - private AbInitioModelling3Service AbInitioModelling3Service; - - @EJB - private Experiment3Service experiment3Service; - - @EJB - private Measurement3Service measurement3Service; - - @EJB - private Specimen3Service specimen3Service; - - @EJB - private Sampleplate3Service sampleplate3Service; - - @EJB - private Robot3Service robot3Service; - - @EJB - private MeasurementToDataCollection3Service measurementToDataCollection3Service; - - @EJB - private PrimaryDataProcessing3Service primaryDataProcessing3Service; - - @EJB - private AdvancedAnalysis3Service advancedAnalysis3Service; - - private long now; - - @EJB - private Session3Service sessionService; - - /** The now. */ - protected static Calendar NOW; - - private Date getNow(){ - NOW = GregorianCalendar.getInstance(); - return NOW.getTime(); - } - - private Integer getSessionIdForExperiment(ProposalWS3VO proposal) throws Exception { - Calendar cal = Calendar.getInstance(); - Date today = cal.getTime(); - cal.add(Calendar.HOUR_OF_DAY, 24); - // by default the session created contains 3 shifts of 8hours each. - Date endTime = cal.getTime(); - - Integer sessionId = null; - - SessionWS3VO[] sessionTab = sessionService.findForWSByProposalCodeAndNumber(proposal.getCode(), - proposal.getNumber(), Constants.getSAXSBeamline()); - - if (sessionTab == null || sessionTab.length == 0) { - // No session found we have to create one to attach the experiment data to it - Session3VO ses = new Session3VO(); - ses.setBeamlineName(Constants.getSAXSBeamline()); - ses.setProposalVO(proposalService.findByPk(proposal.getProposalId())); - ses.setStartDate(today); - ses.setEndDate(endTime); - ses.setComments("session created by ISPyBB"); - ses.setScheduled((byte) 0); - ses.setNbShifts(3); - ses.setUsedFlag((byte) 1); - ses.setTimeStamp(today); - ses.setLastUpdate(endTime); - ses = sessionService.create(ses); - sessionId = ses.getSessionId(); - } else { - sessionId = sessionTab[0].getSessionId(); - } - - return sessionId; - } - - @Override - public Experiment3VO createEmptyExperiment(String proposalCode, String proposalNumber, String name) throws Exception { - ProposalWS3VO proposal = proposalService.findForWSByCodeAndNumber(proposalCode, proposalNumber); - - Experiment3VO experiment = new Experiment3VO(); - experiment.setName(name); - experiment.setType("STATIC"); - try{ - experiment.setSessionId(getSessionIdForExperiment(proposal)); - } - catch(Exception exp){ - /** Non-blocking exception **/ - exp.printStackTrace(); - } - experiment.setCreationDate(GregorianCalendar.getInstance().getTime()); - - - experiment.setProposalId(proposal.getProposalId()); - experiment = this.experiment3Service.merge(experiment); - HashMap plates = robot3Service.getDefaultSampleChangerPlateconfiguration("20"); - - PlateGroup3VO plateGroup = new PlateGroup3VO(); - plateGroup.setStorageTemperature("20"); - plateGroup = sampleplate3Service.merge(plateGroup); - - for (String key : plates.keySet()) { - Sampleplate3VO plate = plates.get(key); - plate.setExperimentId(experiment.getExperimentId()); - plate.setPlategroup3VO(plateGroup); - plate = sampleplate3Service.merge(plate); - sampleplate3Service.merge(plate); - } - - /** Creating a session **/ - Integer sessionId = null; - - - return experiment; - } - - @Override - public Measurement3VO appendMeasurementToExperiment(String experimentId, - String runNumber, String type, String plate, String row, String well, String name, String bufferName, - String concentration, String sEUtemperature, String viscosity, String volume, String volumeToLoad, String waitTime, String transmission, String comments) { - - // return experiment3Service.appendMeasurementToExperiment(experimentId, - // runNumber, type, plate, row, well, name, bufferName, concentration, - // sEUtemperature, viscosity, volume, volumeToLoad, waitTime, - // transmission, comments); - Experiment3VO experiment = experiment3Service.findById(Integer.parseInt(experimentId), ExperimentScope.MEDIUM); - System.out.println("Getting Experiment:" + experiment.getExperimentId()); - System.out.println("NAME:" + name); - System.out.println("proposal:" + experiment.getProposalId()); - - List macromolecules = new ArrayList(); - try { - List macromoleculeList = saxsProposal3Service.findMacromoleculesBy(name, experiment.getProposalId()); - System.out.println(macromoleculeList); - if (macromoleculeList != null) { - for (Macromolecule3VO macromolecule3vo : macromoleculeList) { - if (macromolecule3vo.getAcronym() != null) { - if (macromolecule3vo.getAcronym().toLowerCase().trim().equals(name.toLowerCase().trim())) { - macromolecules.add(macromolecule3vo); - System.out.println("Found"); - } - } - } - } - } catch (Exception e) { - e.printStackTrace(); - macromolecules = new ArrayList(); - } - - Specimen3VO specimen = new Specimen3VO(); - - /** - * - * We don't check location of the specimen for ATSAS - * http://forge.epn-campus.eu/issues/2095 - * - */ - Set plates = experiment.getSamplePlate3VOs(); - - - - - specimen.setConcentration(concentration); - if (type.toUpperCase().equals("SAMPLE")) { - Macromolecule3VO macromolecule = null; - if (macromolecules.size() == 0) { - if (name == null) { - name = ""; - } - Macromolecule3VO m = new Macromolecule3VO(experiment.getProposalId(), name, name, ""); - m.setCreationDate(GregorianCalendar.getInstance().getTime()); - macromolecule = entityManager.merge(m); - } else { - macromolecule = macromolecules.get(0); - } - specimen.setMacromolecule3VO(macromolecule); - } - - List buffers = saxsProposal3Service.findBuffersByProposalId(experiment.getProposalId()); - for (Buffer3VO buffer3vo : buffers) { - if (buffer3vo.getAcronym() != null) { - if (buffer3vo.getAcronym().equals(bufferName)) { - specimen.setBufferId(buffer3vo.getBufferId()); - } - } - } - /** If buffer not found **/ - if (specimen.getBufferId() == null) { - Buffer3VO buffer3VO = new Buffer3VO(); - if (bufferName == null) { - bufferName = ""; - } - buffer3VO.setAcronym(bufferName); - buffer3VO.setName(bufferName); - buffer3VO.setProposalId(experiment.getProposalId()); - buffer3VO = entityManager.merge(buffer3VO); - specimen.setBufferId(buffer3VO.getBufferId()); - } - - /** Sample plate position **/ - LOG.info("getSampleplateposition3VO: " + specimen.getSampleplateposition3VO()); - LOG.info("plates: " + plates.size()); - if (specimen.getSampleplateposition3VO() == null) { - /** this specimen is new because has no position **/ - for (Sampleplate3VO samplePlate : plates) { - if (samplePlate.getSlotPositionColumn().equals(plate)) { - Sampleplateposition3VO position = new Sampleplateposition3VO(); - position.setColumnNumber(Integer.parseInt(well)); - position.setRowNumber(Integer.parseInt(row)); - position.setSamplePlateId(samplePlate.getSamplePlateId()); - position = entityManager.merge(position); - specimen.setSampleplateposition3VO(position); - } - } - } - - specimen.setExperimentId(experiment.getExperimentId()); - specimen.setVolume(volume); - - specimen = entityManager.merge(specimen); - - Measurement3VO measurement = new Measurement3VO(); - measurement.setCode(runNumber); - measurement.setSpecimenId(specimen.getSpecimenId()); - measurement.setExposureTemperature(sEUtemperature); - measurement.setTransmission(transmission); - measurement.setComment(comments); - measurement.setViscosity(viscosity); - measurement.setVolumeToLoad(volumeToLoad); - measurement.setWaitTime(waitTime); - measurement.setPriority(experiment.getMeasurements().size() + 1); - measurement = entityManager.merge(measurement); - return measurement; - } - - public void updateAverage(int measurementId, int averagedCount, int framesCount, String oneDimensionalDataFilePathArray, String discardedCurves, String averageFilePath) { - List curveList = Arrays.asList(oneDimensionalDataFilePathArray.split(",")); - List mergeList = this.primaryDataProcessing3Service.findByMeasurementId(measurementId); - if (mergeList != null){ - if (mergeList.size() > 0){ - Merge3VO merge = mergeList.get(0); - if (Integer.parseInt(merge.getFramesCount()) >= framesCount){ - LOG.info("Same number of frames count. No update"); - return; - } - else{ - this.primaryDataProcessing3Service.addMerge(measurementId, averagedCount, framesCount, oneDimensionalDataFilePathArray, null, averageFilePath); - } - } - } - } - - @Override - public void addAveraged(String measurementId, String averaged, String discarded, String averageFile) { - this.addAveraged(measurementId, averaged, discarded, averageFile, null); - } - - @Override - public void addAveraged(String measurementId, String averaged, String discarded, String averageFile, String visitorFilePath) { - - Type listType = new TypeToken>>() { - }.getType(); - List> averageFrames = new Gson().fromJson(averaged, listType); - List> discardeFrames = new Gson().fromJson(discarded, listType); - - - ArrayList oneDimensionalFiles = new ArrayList(); - LOG.info("########### Averaged Frames ############# "); - for (HashMap frame : averageFrames) { - if (frame.containsKey("filePath")) { - oneDimensionalFiles.add(frame.get("filePath")); - LOG.info("\t" + frame.get("filePath")); - } - } - - LOG.info("########### Discarded Frames ############# "); - for (HashMap frame : discardeFrames) { - if (frame.containsKey("filePath")) { - oneDimensionalFiles.add(frame.get("filePath")); - LOG.info("\t" + frame.get("filePath")); - } - } - - /** Does it contains already a average **/ - List merges = primaryDataProcessing3Service.findByMeasurementId(Integer.parseInt(measurementId)); - - try{ - Measurement3VO measurement = this.measurement3Service.findById(Integer.parseInt(measurementId)); - measurement.setImageDirectory(visitorFilePath); - this.measurement3Service.merge(measurement); - } - catch(Exception exp){ - exp.printStackTrace(); - } - - LOG.info("------ Number of Averages by measurementId --------"); - LOG.info(merges.size()); - - if (merges.size() == 0) { - LOG.info("\t Creating a new average"); - /** Not any average found so we create a new one **/ - this.primaryDataProcessing3Service.addMerge(Integer.parseInt(measurementId), averageFrames.size(), oneDimensionalFiles.size(), - oneDimensionalFiles.toString().replace("[", "").replace("]", ""), null, averageFile); - } else { - LOG.info("\t Updating a new average"); - /** It is an update of the average **/ - - this.updateAverage(Integer.parseInt(measurementId), averageFrames.size(), oneDimensionalFiles.size(), - oneDimensionalFiles.toString().replace("[", "").replace("]", ""), null, averageFile); - - } - } - -// private void removeFrameListById(int frameListId) { -// Framelist3VO frameList = this.entityManager.find(Framelist3VO.class, frameListId); -// /** Looking for FrameToListObjects **/ -// -// String query = "select f from Frametolist3VO f where f.frameListId=:frameListId"; -// Query EJBQuery = this.entityManager.createQuery(query).setParameter("frameListId", frameListId); -// List frameToListList = (List)EJBQuery.getResultList(); -// for (Frametolist3VO frametolist3vo : frameToListList) { -// System.out.println("Found " + frametolist3vo.getFrameToListId()); -// } -// -// } - - /** - * True if a datacollection contains all the measurementIds otherwise false - * - * @param dataCollection - * @param measurementIds - * @return - */ - private Boolean containsSameMeasurements(SaxsDataCollection3VO dataCollection, List measurementIds) { - Iterator mIterator = dataCollection.getMeasurementtodatacollection3VOs().iterator(); - HashSet measurementDataCollectionSet = new HashSet(); - while (mIterator.hasNext()) { - MeasurementTodataCollection3VO measurementToDataCollection = mIterator.next(); - measurementDataCollectionSet.add(measurementToDataCollection.getMeasurementId()); - } - for (Integer measurementId : measurementIds) { - if (!measurementDataCollectionSet.contains(measurementId)) { - return false; - } - } - return true; - } - - private SaxsDataCollection3VO getDataCollectionByMeasurementsId(Experiment3VO experiment, List measurementIds) { - Iterator iterator = experiment.getDataCollections().iterator(); - while (iterator.hasNext()) { - SaxsDataCollection3VO dataCollection = iterator.next(); - if (this.containsSameMeasurements(dataCollection, measurementIds)) { - /** - * Returning the existing one - */ - return dataCollection; - } - } - return null; - } - /** - * Returns the measurement id corresponding to a sample or null if all measurementIds are buffers - * @param experiment - * @param measurementIds - * @return - */ - private Integer getSampleFromMeasurementList(Experiment3VO experiment, List measurementIds) { - for (Integer measurementId : measurementIds) { - Specimen3VO specimen = experiment.getSampleById(experiment.getMeasurementById(measurementId).getSpecimenId()); - if (specimen.getMacromolecule3VO() != null){ - return measurementId; - } - } - return null; - } - - /** - * return a existing datacollection if found containing the same - * measurements or create a new one - * - * @param experiment - * @param measurementIds - * @return - */ - private SaxsDataCollection3VO getSaxsDataCollectionByMeasurementIds(Experiment3VO experiment, List measurementIds) { -// SaxsDataCollection3VO datacollection = this.getDataCollectionByMeasurementsId(experiment, measurementIds); - SaxsDataCollection3VO datacollection = null; - LOG.info("Getting data collection"); - LOG.info("Getting data collection: " + measurementIds.toString()); - Integer sampleMeasurementId = this.getSampleFromMeasurementList(experiment, measurementIds); - LOG.info("sampleMeasurementId: " + sampleMeasurementId); - if (sampleMeasurementId == null){ - datacollection = this.getDataCollectionByMeasurementsId(experiment, measurementIds); - } - else{ - /** Look for a subtraction already containing this sample **/ - datacollection = experiment.getDataCollectionByMeasurementId(sampleMeasurementId); - - if (datacollection != null){ - /** Adding extra measurements **/ - Set measurementToDatacollectionList = datacollection.getMeasurementtodatacollection3VOs(); - Set measurementKeys = new HashSet(); - for (MeasurementTodataCollection3VO measurementToDatacollection : measurementToDatacollectionList) { - measurementKeys.add(measurementToDatacollection.getMeasurementId()); - } - LOG.info("measurementKeys:" + measurementKeys.toString()); - for (Integer measurementId : measurementIds) { - LOG.info("measurementKeys:" + measurementId + " " + measurementKeys.toString()); - if(!measurementKeys.contains(measurementId)){ - /** This measurement does not belong to the data collection yet **/ - MeasurementTodataCollection3VO measurementToDataCollection = new MeasurementTodataCollection3VO(); - measurementToDataCollection.setDataCollectionId(datacollection.getDataCollectionId()); - measurementToDataCollection.setMeasurementId(measurementId); - measurementToDataCollection.setDataCollectionOrder(datacollection.getMeasurementtodatacollection3VOs().size() + 1); - LOG.info("setDataCollectionOrder:" + datacollection.getMeasurementtodatacollection3VOs().size() + 1); - this.entityManager.merge(measurementToDataCollection); - } - } - } - } - - if (datacollection != null) { - return datacollection; - } else { - /** We create a new one **/ - SaxsDataCollection3VO dataCollection = new SaxsDataCollection3VO(); - dataCollection.setExperimentId(experiment.getExperimentId()); - dataCollection = entityManager.merge(dataCollection); - int dataCollectionOrder = 1; - for (Integer measurementId : measurementIds) { - MeasurementTodataCollection3VO measurementToDataCollection = new MeasurementTodataCollection3VO(); - measurementToDataCollection.setDataCollectionId(dataCollection.getDataCollectionId()); - measurementToDataCollection.setMeasurementId(measurementId); - measurementToDataCollection.setDataCollectionOrder(dataCollectionOrder); - this.entityManager.merge(measurementToDataCollection); - dataCollectionOrder++; - } - return dataCollection; - } - } - - private Framelist3VO createFrames(List sampleOneDimensionalFilesList){ - if (sampleOneDimensionalFilesList.size() > 0){ - return this.primaryDataProcessing3Service.addFrameList(sampleOneDimensionalFilesList); - } - return null; - } - - private void addSubtraction(Integer dataCollectionId, String rgStdev, String i0, String i0Stdev, String firstPointUsed, - String lastPointUsed, String quality, String isagregated, String rgGuinier, String rgGnom, String dmax, String total, - String volume, String sampleOneDimensionalFiles, String bufferOneDimensionalFiles, String sampleAverageFilePath, - String bestBufferFilePath, String subtractedFilePath, String experimentalDataPlotFilePath, String densityPlotFilePath, - String guinierPlotFilePath, String kratkyPlotFilePath, String gnomOutputFilePath, Subtraction3VO subtraction) { - - HashMap params = new HashMap(); - - long start = this.logInit("addSubtraction_CP_3.1", new Gson().toJson(params)); - - /** Getting the frames **/ - Type typeOfHash = new TypeToken>>() {}.getType(); - List> sampleOneDimensionalFilesListObject = new Gson().fromJson(sampleOneDimensionalFiles, typeOfHash); - List> bufferOneDimensionalFilesListObject = new Gson().fromJson(bufferOneDimensionalFiles, typeOfHash); - - List sampleOneDimensionalFilesList = new ArrayList(); - List bufferOneDimensionalFilesList = new ArrayList(); - - for (HashMap fileObject : sampleOneDimensionalFilesListObject) { - if (fileObject.get("filePath")!= null){ - sampleOneDimensionalFilesList.add(fileObject.get("filePath")); - } - } - - for (HashMap fileObject : bufferOneDimensionalFilesListObject) { - if (fileObject.get("filePath")!= null){ - bufferOneDimensionalFilesList.add(fileObject.get("filePath")); - } - } - - LOG.info(sampleOneDimensionalFilesList); - Framelist3VO sampleFiles = this.createFrames(sampleOneDimensionalFilesList); - this.logFinish("addSubtraction_CP_3.1", start); - - start = this.logInit("addSubtraction_CP_3.2", new Gson().toJson(params)); - Framelist3VO bufferFiles = this.createFrames(bufferOneDimensionalFilesList); - this.logFinish("addSubtraction_CP_3.2", start); - - - start = this.logInit("addSubtraction_CP_3.3", new Gson().toJson(params)); - subtraction.setDataCollectionId(dataCollectionId); - subtraction.setRg(rgGuinier); - subtraction.setRgStdev(rgStdev); - subtraction.setI0(i0); - subtraction.setI0stdev(i0Stdev); - subtraction.setFirstPointUsed(firstPointUsed); - subtraction.setLastPointUsed(lastPointUsed); - subtraction.setQuality(quality); - subtraction.setIsagregated(isagregated); - subtraction.setGnomFilePath(densityPlotFilePath); - subtraction.setRgGuinier(rgGuinier); - subtraction.setRgGnom(rgGnom); - subtraction.setDmax(dmax); - subtraction.setTotal(total); - subtraction.setVolume(volume); - subtraction.setKratkyFilePath(kratkyPlotFilePath); - subtraction.setScatteringFilePath(experimentalDataPlotFilePath); - subtraction.setGuinierFilePath(guinierPlotFilePath); - subtraction.setSubstractedFilePath(subtractedFilePath); - subtraction.setGnomFilePathOutput(gnomOutputFilePath); - subtraction.setCreationTime(getNow()); - - if (sampleFiles != null){ - subtraction.setSampleOneDimensionalFiles(sampleFiles); - } - if (bufferFiles != null){ - subtraction.setBufferOneDimensionalFiles(bufferFiles); - } - subtraction.setSampleAverageFilePath(sampleAverageFilePath); - subtraction.setBufferAverageFilePath(bestBufferFilePath); - - this.entityManager.merge(subtraction); - this.logFinish("addSubtraction_CP_3.3", start); - - } - - private void addSubtraction(int dataCollectionId, String rgStdev, String i0, String i0Stdev, String firstPointUsed, - String lastPointUsed, String quality, String isagregated, String rgGuinier, String rgGnom, String dmax, String total, - String volume, String sampleOneDimensionalFiles, String bufferOneDimensionalFiles, String sampleAverageFilePath, - String bestBufferFilePath, String subtractedFilePath, String experimentalDataPlotFilePath, String densityPlotFilePath, - String guinierPlotFilePath, String kratkyPlotFilePath, String gnomOutputFilePath) { - - Subtraction3VO subtraction = new Subtraction3VO(); - this.addSubtraction(dataCollectionId, rgStdev, i0, i0Stdev, firstPointUsed, lastPointUsed, quality, isagregated, rgGuinier, rgGnom, dmax, total, volume, sampleOneDimensionalFiles, bufferOneDimensionalFiles, sampleAverageFilePath, bestBufferFilePath, subtractedFilePath, experimentalDataPlotFilePath, densityPlotFilePath, guinierPlotFilePath, kratkyPlotFilePath, gnomOutputFilePath, subtraction); - - } - - - @Override - public void addSubtraction(String sampleMeasurementId, String rgStdev, String i0, String i0Stdev, String firstPointUsed, - String lastPointUsed, String quality, String isagregated, String rgGuinier, String rgGnom, String dmax, String total, - String volume, String sampleOneDimensionalFiles, String bufferOneDimensionalFiles, String sampleAverageFilePath, - String bestBufferFilePath, String subtractedFilePath, String experimentalDataPlotFilePath, String densityPlotFilePath, - String guinierPlotFilePath, String kratkyPlotFilePath, String gnomOutputFilePath) { - - - - HashMap params = new HashMap(); - long start = this.logInit("addSubtraction_CP_1", new Gson().toJson(params)); - if (sampleMeasurementId != null){ - // TODO: To be changed, performace problems!!! - Experiment3VO experiment = experiment3Service.findByMeasurementId(Integer.parseInt(sampleMeasurementId)); - this.logFinish("addSubtraction_CP_1", start); - - - if (experiment != null) { - start = this.logInit("addSubtraction_CP_2", new Gson().toJson(params)); - int dataCollectionId = experiment.getDataCollectionByMeasurementId(Integer.parseInt(sampleMeasurementId)).getDataCollectionId(); - this.logFinish("addSubtraction_CP_2", start); - - - start = this.logInit("addSubtraction_CP_3", new Gson().toJson(params)); - this.addSubtraction(dataCollectionId, rgStdev, i0, i0Stdev, firstPointUsed, lastPointUsed, quality, isagregated, rgGuinier, rgGnom, dmax, total, volume, sampleOneDimensionalFiles, bufferOneDimensionalFiles, sampleAverageFilePath, bestBufferFilePath, subtractedFilePath, experimentalDataPlotFilePath, densityPlotFilePath, guinierPlotFilePath, kratkyPlotFilePath, gnomOutputFilePath); - this.logFinish("addSubtraction_CP_3", start); - - } - } - } - - - /** - * Adds a new subtraction to several measurements To note: there are not any data collection yet - */ - public void addSubtraction( - String experimentId, - String runNumberList, - String rgStdev, - String i0, - String i0Stdev, - String firstPointUsed, - String lastPointUsed, - String quality, - String isagregated, - String rgGuinier, - String rgGnom, - String dmax, - String total, - String volume, - String sampleOneDimensionalFiles, - String bufferOneDimensionalFiles, - String sampleAverageFilePath, - String bestBufferFilePath, - String subtractedFilePath, - String experimentalDataPlotFilePath, - String densityPlotFilePath, - String guinierPlotFilePath, - String kratkyPlotFilePath, - String gnomOutputFilePath) throws Exception { - - Type typeOfList = new TypeToken>() {}.getType(); - List ids = new Gson().fromJson(runNumberList, typeOfList); - - /** Finding measurement by experimentId and run number **/ - List measurementIds = new ArrayList(); - for (String runNumber : ids) { - Measurement3VO measurement = measurement3Service.findMeasurementByCode(Integer.parseInt(experimentId), runNumber.toString()); - if (measurement != null) { - measurementIds.add(measurement.getMeasurementId()); - } else { - throw new Exception("No measurement found for runNumber: " + runNumber + " and experimentId: " + experimentId); - } - } - - System.out.println("Adding data collection and subtraction"); - - /** Adding data collection and subtraction **/ - if (measurementIds != null) { - if (measurementIds.size() > 0) { - Experiment3VO experiment = experiment3Service.findById(Integer.parseInt(experimentId), ExperimentScope.MEDIUM); - if (experiment != null) { - /** This method creates the data collection if it does not exist yet **/ - SaxsDataCollection3VO dataCollection = getSaxsDataCollectionByMeasurementIds(experiment, measurementIds); - - if (dataCollection.getSubstraction3VOs().size() == 0){ - this.addSubtraction(dataCollection.getDataCollectionId(), rgStdev, i0, i0Stdev, firstPointUsed, lastPointUsed, quality, isagregated, rgGuinier, rgGnom, dmax, total, volume, sampleOneDimensionalFiles, bufferOneDimensionalFiles, sampleAverageFilePath, bestBufferFilePath, subtractedFilePath, experimentalDataPlotFilePath, densityPlotFilePath, guinierPlotFilePath, kratkyPlotFilePath, gnomOutputFilePath); - } - else{ - /** - * Upgrading subtraction - */ - Subtraction3VO subtraction = (Subtraction3VO) Arrays.asList(dataCollection.getSubstraction3VOs().toArray()).get(0); - this.addSubtraction(dataCollection.getDataCollectionId(), rgStdev, i0, i0Stdev, firstPointUsed, lastPointUsed, quality, isagregated, rgGuinier, rgGnom, dmax, total, volume, sampleOneDimensionalFiles, bufferOneDimensionalFiles, sampleAverageFilePath, bestBufferFilePath, subtractedFilePath, experimentalDataPlotFilePath, densityPlotFilePath, guinierPlotFilePath, kratkyPlotFilePath, gnomOutputFilePath, subtraction); - } - } - } - } - } - - - - @Override - public void addAbinitioModelling(String experimentId, String runNumber, - ArrayList models3vo, Model3VO referenceModel, - Model3VO refinedModel) throws Exception { - - Measurement3VO measurement = measurement3Service.findMeasurementByCode(Integer.parseInt(experimentId), (runNumber)); - if (measurement != null){ - ArrayList measurementIds = new ArrayList(); - measurementIds.add(measurement.getMeasurementId()); - AbInitioModelling3Service.addAbinitioModeling(measurementIds, - models3vo, - referenceModel, - refinedModel); - - } - else{ - throw new Exception("No measurement found for " + experimentId + " and " + runNumber); - } - } - - @Override - public Model3VO getModel(String experimentId, String runNumber, String pdbfilepath) throws Exception { - Measurement3VO measurement = measurement3Service.findMeasurementByCode(Integer.parseInt(experimentId), (runNumber)); - if (measurement != null){ - return AbInitioModelling3Service.getModel(measurement.getMeasurementId(), pdbfilepath); - } - else{ - throw new Exception("Not measurement found for " + experimentId + " and " + runNumber); - } - } - - @Override - public Model3VO merge(Model3VO model) { - return this.entityManager.merge(model); - } - - @Override - public List getMacromoleculeByAcronym(String proposal, String acronym) { - List proposal3VOs = this.proposalService.findProposalByLoginName(proposal); - if (proposal3VOs != null){ - if (proposal3VOs.size() > 0){ - Proposal3VO proposal3VO = proposal3VOs.get(0); - List macromolecules = this.saxsProposal3Service.findMacromoleculesBy(acronym, proposal3VO.getProposalId()); - return macromolecules; - } - } - return new ArrayList(); - } - - public List getSubtractionByCode(String experimentId, String runNumber) { - Measurement3VO measurement = measurement3Service.findMeasurementByCode(Integer.valueOf(experimentId), runNumber); - System.out.println("measurement " + measurement); - if (measurement != null){ - return primaryDataProcessing3Service.getSubstractionsByMeasurementId(measurement.getMeasurementId()); - } - System.out.println("measurement not found "); - return null; - } - - public Macromolecule3VO getMacromoleculeByCode(String experimentId, String runNumber) { - Measurement3VO measurement = measurement3Service.findMeasurementByCode(Integer.valueOf(experimentId), runNumber); - if (measurement != null){ - Specimen3VO specimen = entityManager.find(Specimen3VO.class, measurement.getSpecimenId()); - if (specimen != null){ - return saxsProposal3Service.findMacromoleculesById(specimen.getMacromolecule3VO().getMacromoleculeId()); -// return experiment3Service.findMacromoleculeById(specimen.getMacromolecule3VO().getMacromoleculeId()); - } - } - return null; - } - - /** - * - * @param experimentId - * @param runNumber - * @param filePath {filePath:"/filepath.dat"} - * @return - */ - public Structure3VO getStructuresByCode(String experimentId, String runNumber, String filePath) { - Macromolecule3VO macromolecule = this.getMacromoleculeByCode(experimentId, runNumber); - for (Structure3VO structure : macromolecule.getStructure3VOs()) { - if (structure.getFilePath().trim().equals(filePath.trim())){ - return structure; - } - } - - return null; - } - - @Override - public void addMixture(String experimentId, String runNumber, String fitFilePath, String pdb) throws Exception { - List subtractions = this.getSubtractionByCode(experimentId, runNumber); - System.out.println("+++addMixture " + experimentId + " " + runNumber); - if (subtractions != null){ - if (subtractions.size() > 0){ - /** Getting last one are they should come by order **/ - Subtraction3VO subtraction = subtractions.get(subtractions.size() - 1); - - System.out.println("+++Subtraction Found"); - /** Getting Structure **/ - - Type ListType = new TypeToken>>() {}.getType(); - - /** Reading pdbs **/ - List> pdbFilePath = new Gson().fromJson(pdb, ListType); - - List mixtures = new ArrayList(); - if (pdbFilePath.size() > 0){ - for (HashMap pdbHash : pdbFilePath) { - MixtureToStructure mixture = new MixtureToStructure(); - - if (pdbHash.containsKey("filePath")){ - /** Finding the structue linked to such filePath */ - Structure3VO structure = this.getStructuresByCode(experimentId, runNumber, pdbHash.get("filePath")); - /** If structure does not exist we create a new one **/ - if (structure == null){ - structure = new Structure3VO(); - Macromolecule3VO macromolecule = this.getMacromoleculeByCode(experimentId, runNumber); - System.out.println("Macromolecule found"); - if (macromolecule != null){ - structure.setMacromoleculeId(macromolecule.getMacromoleculeId()); - structure.setFilePath(pdbHash.get("filePath")); - if (pdbHash.get("filePath") != null){ - String name = new File(pdbHash.get("filePath")).getName(); - structure.setName(name); - } - structure.setType("PDB"); - structure.setCreationDate(getNow()); - structure = this.entityManager.merge(structure); - System.out.println("Structure created"); - } - else{ - throw new Exception("Macromolecule not found for experimentId " + experimentId + " and runNumber " + runNumber ); - } - } - - /** Structure exists here **/ - mixture.setStructureId(structure.getStructureId()); - mixture.setCreationDate(getNow()); - - } - - if (pdbHash.containsKey("volumeFraction")){ - System.out.println("+++" +"Volume: " + pdbHash.get("volumeFraction")); - mixture.setVolumeFraction(pdbHash.get("volumeFraction")); - } - - mixtures.add(mixture); - } - - System.out.println( "+++Mixtures --- " + mixtures.size()); - System.out.println("+++" +fitFilePath + " --- " + fitFilePath); - - - /** Creating FitStructureToExperimentalData **/ - FitStructureToExperimentalData3VO fit = new FitStructureToExperimentalData3VO(); - fit.setSubtractionId(subtraction.getSubtractionId()); - fit.setCreationDate(getNow()); - fit.setFitFilePath(fitFilePath); - fit = entityManager.merge(fit); - System.out.println("+++Merging fit " + fit.getFitFilePath() + " " + fit.getFitStructureToExperimentalDataId()); - - /** Creating MixtureToStructures **/ - for (MixtureToStructure mixtureToStructure : mixtures) { - mixtureToStructure.setMixtureId(fit.getFitStructureToExperimentalDataId()); - mixtureToStructure = this.entityManager.merge(mixtureToStructure); - System.out.println("+++Merging mixturetoStructure " + mixtureToStructure.getVolumeFraction() + " " + mixtureToStructure.getStructureId() + " " + mixtureToStructure.getFitToStructureId()); - } - } - - /** Reading fitFilePath **/ -// Type HashType = new TypeToken>() {}.getType(); -// HashMap fitFilePathHash = new Gson().fromJson(fitFilePath, HashType); -// if (fitFilePathHash.containsKey("filePath")){ -// fitFilePath = fitFilePathHash.get("filePath"); -// } - - - } - else{ - throw new Exception("0 subtractions has been found for experimentId= " + experimentId + " and runNumber= " + runNumber); - } - - } - else{ - throw new Exception("No subtraction has been found for experimentId= " + experimentId + " and runNumber= " + runNumber); - } -// if (fitStructure == null){ -// fitStructure = new FitStructureToExperimentalData(); -// } -// -// fitStructure.setFitFilePath(fitFilePath); -//// fitStructure.setLogFilePath(logFile); -// fitStructure.setOutputFilePath(summaryFile); -//// fitStructure.setStructureId(Integer.parseInt(structureId)); -// fitStructure.setSubtractionId(Integer.parseInt(subtractionId)); -// fitStructure.setWorkflowId(Integer.parseInt(workflowId)); -// fitStructure.setComments("Finished at " + getNow()); -// fitStructure = advancedAnalysis3Service.merge(fitStructure); - - } - - @Override - public void addRigidBodyModeling(String experimentId, String runNumber, String fitFilePath, String rigidBodyModelFilePath, - String logFilePath, String curveConfigFilePath, String subunitConfigFilePath, String crosscorrConfigFilePath, - String contactConditionsFilePath, String masterSymmetry) throws Exception { - - List subtractions = this.getSubtractionByCode(experimentId, runNumber); - if (subtractions != null){ - if (subtractions.size() > 0){ - RigidBodyModeling3VO rigid = new RigidBodyModeling3VO(); - rigid.setSubtractionId(subtractions.get(subtractions.size() - 1).getSubtractionId()); - rigid.setFitFilePath(fitFilePath); - rigid.setRigidBodyModelFilePath(rigidBodyModelFilePath); - rigid.setLogFilePath(logFilePath); - rigid.setCurveConfigFilePath(curveConfigFilePath); - rigid.setSubUnitConfigFilePath(subunitConfigFilePath); - rigid.setCrossCorrConfigFilePath(crosscorrConfigFilePath); - rigid.setContactDescriptionFilePath(contactConditionsFilePath); - rigid.setSymmetry(masterSymmetry); - rigid.setCreationDate(getNow()); - this.entityManager.merge(rigid); - } - else{ - throw new Exception("No subtraction found for " + experimentId + " " + runNumber); - } - } - else{ - throw new Exception("No subtraction found for " + experimentId + " " + runNumber + "(null)"); - } - } - - @Override - public void addSuperposition(String experimentId, String runNumber, String abinitioModelPdbFilePath, String aprioriPdbFilePath, - String alignedPdbFilePath) throws Exception { - - List subtractions = this.getSubtractionByCode(experimentId, runNumber); - if (subtractions != null){ - Superposition3VO superposition = new Superposition3VO(); - superposition.setSubtractionId(subtractions.get(subtractions.size() - 1).getSubtractionId()); - superposition.setAbinitioModelPdbFilePath(abinitioModelPdbFilePath); - superposition.setAprioriPdbFilePath(aprioriPdbFilePath); - superposition.setAlignedPdbFilePath(alignedPdbFilePath); - superposition.setCreationDate(getNow()); - this.entityManager.merge(superposition); - } - else{ - throw new Exception("No subtraction found for " + experimentId + " " + runNumber + "(null)"); - } - - - } - - @Override - public HashMap getAprioriInformationByRunNumber(String proposal, String acronym) throws Exception { - HashMap info = new HashMap(); - List macromolecules = this.getMacromoleculeByAcronym(proposal, acronym); - if (macromolecules.size() == 1){ - Macromolecule3VO macromolecule = macromolecules.get(0); - if (macromolecule != null){ - info.put("ACRONYM", macromolecule.getAcronym()); - List> pdb = new ArrayList>(); - List> fasta = new ArrayList>(); - List> rbms = new ArrayList>(); - if (macromolecule.getStructure3VOs() != null){ - for (Structure3VO structure : macromolecule.getStructure3VOs()) { - if (structure.getType().equals("PDB")){ - HashMap pdbEntry = new HashMap(); - pdbEntry.put("FILEPATH", structure.getFilePath()); - pdbEntry.put("SYMMETRY", structure.getSymmetry()); - pdbEntry.put("MULTIPLICITY", structure.getMultiplicity()); - pdb.add(pdbEntry); - } - if (structure.getType().equals("SEQUENCE")){ - HashMap fastaEntry = new HashMap(); - fastaEntry.put("FILEPATH", structure.getFilePath()); - fasta.add(fastaEntry); - } - } - } - - HashMap rbm = new HashMap(); - rbm.put("FILEPATH", macromolecule.getContactsDescriptionFilePath()); - rbm.put("SYMMETRY", macromolecule.getSymmetry()); - rbms.add(rbm); - - info.put("PDB", pdb); - info.put("SEQUENCE", fasta); - info.put("RIGIDBODY", rbms); - return info; - } - else{ - throw new Exception("macromolecule not found"); - } - } - else{ - throw new Exception(macromolecules.size() + " macromolecules found"); - } - } - - - private void logFinish(String methodName, long id) { - LOG.debug("### [" + methodName.toUpperCase() + "] Execution time was " + (System.currentTimeMillis() - this.now) + " ms."); - LoggerFormatter.log(LOG, LoggerFormatter.Package.BIOSAXS_WS, methodName, id, System.currentTimeMillis(), - System.currentTimeMillis() - this.now); - - } - - protected long logInit(String methodName, String params) { - LOG.info("-----------------------"); - this.now = System.currentTimeMillis(); - LOG.info(methodName.toUpperCase()); - LoggerFormatter.log(LOG, LoggerFormatter.Package.BIOSAXS_WS, methodName, System.currentTimeMillis(), - System.currentTimeMillis(), params); - return this.now; - } - - @Override - public List getMacromoleculesByProposal(String code, String number) throws Exception { - ProposalWS3VO proposal = proposalService.findForWSByCodeAndNumber(code, number); - return this.saxsProposal3Service.findMacromoleculesByProposalId(proposal.getProposalId()); - } - - @Override - public List getBuffersByProposal(String code, String number) throws Exception { - ProposalWS3VO proposal = proposalService.findForWSByCodeAndNumber(code, number); - return this.saxsProposal3Service.findBuffersByProposalId(proposal.getProposalId()); - } -} +/******************************************************************************* + * This file is part of ISPyB. + * + * ISPyB is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ISPyB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with ISPyB. If not, see . + * + * Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Brenchereau, M. Bodin, A. De Maria Antolinos + ******************************************************************************************************************************/ + +package ispyb.server.biosaxs.services.webservice; + +import ispyb.common.util.Constants; +import ispyb.server.biosaxs.services.core.ExperimentScope; +import ispyb.server.biosaxs.services.core.analysis.abInitioModelling.AbInitioModelling3Service; +import ispyb.server.biosaxs.services.core.analysis.advanced.AdvancedAnalysis3Service; +import ispyb.server.biosaxs.services.core.analysis.primaryDataProcessing.PrimaryDataProcessing3Service; +import ispyb.server.biosaxs.services.core.experiment.Experiment3Service; +import ispyb.server.biosaxs.services.core.measurement.Measurement3Service; +import ispyb.server.biosaxs.services.core.measurementToDataCollection.MeasurementToDataCollection3Service; +import ispyb.server.biosaxs.services.core.proposal.SaxsProposal3Service; +import ispyb.server.biosaxs.services.core.robot.Robot3Service; +import ispyb.server.biosaxs.services.core.samplePlate.Sampleplate3Service; +import ispyb.server.biosaxs.services.core.specimen.Specimen3Service; +import ispyb.server.biosaxs.vos.advanced.FitStructureToExperimentalData3VO; +import ispyb.server.biosaxs.vos.advanced.MixtureToStructure; +import ispyb.server.biosaxs.vos.advanced.RigidBodyModeling3VO; +import ispyb.server.biosaxs.vos.advanced.Superposition3VO; +import ispyb.server.biosaxs.vos.assembly.Macromolecule3VO; +import ispyb.server.biosaxs.vos.assembly.Structure3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Buffer3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Experiment3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Measurement3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.Specimen3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.plate.PlateGroup3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplate3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplateposition3VO; +import ispyb.server.biosaxs.vos.datacollection.Framelist3VO; +import ispyb.server.biosaxs.vos.datacollection.MeasurementTodataCollection3VO; +import ispyb.server.biosaxs.vos.datacollection.Merge3VO; +import ispyb.server.biosaxs.vos.datacollection.Model3VO; +import ispyb.server.biosaxs.vos.datacollection.SaxsDataCollection3VO; +import ispyb.server.biosaxs.vos.datacollection.Subtraction3VO; +import ispyb.server.common.services.proposals.Proposal3Service; +import ispyb.server.common.services.sessions.Session3Service; +import ispyb.server.common.util.LoggerFormatter; +import ispyb.server.common.vos.proposals.Proposal3VO; +import ispyb.server.common.vos.proposals.ProposalWS3VO; +import ispyb.server.mx.vos.collections.Session3VO; +import ispyb.server.mx.vos.collections.SessionWS3VO; + +import java.io.File; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import javax.ejb.EJB; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.apache.log4j.Logger; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.reflect.TypeToken; + +@Stateless +public class ATSASPipeline3ServiceBean implements ATSASPipeline3Service, DesySampleChangerConnectorServiceLocal { + private final static Logger LOG = Logger.getLogger("ATSASPipeline3ServiceBean"); + + /** The entity manager. */ + @PersistenceContext(unitName = "ispyb_db") + private EntityManager entityManager; + + @EJB + private Proposal3Service proposalService; + + @EJB + private SaxsProposal3Service saxsProposal3Service; + + @EJB + private AbInitioModelling3Service AbInitioModelling3Service; + + @EJB + private Experiment3Service experiment3Service; + + @EJB + private Measurement3Service measurement3Service; + + @EJB + private Specimen3Service specimen3Service; + + @EJB + private Sampleplate3Service sampleplate3Service; + + @EJB + private Robot3Service robot3Service; + + @EJB + private MeasurementToDataCollection3Service measurementToDataCollection3Service; + + @EJB + private PrimaryDataProcessing3Service primaryDataProcessing3Service; + + @EJB + private AdvancedAnalysis3Service advancedAnalysis3Service; + + private long now; + + @EJB + private Session3Service sessionService; + + /** The now. */ + protected static Calendar NOW; + + private Date getNow(){ + NOW = GregorianCalendar.getInstance(); + return NOW.getTime(); + } + + private Integer getSessionIdForExperiment(ProposalWS3VO proposal) throws Exception { + Calendar cal = Calendar.getInstance(); + Date today = cal.getTime(); + cal.add(Calendar.HOUR_OF_DAY, 24); + // by default the session created contains 3 shifts of 8hours each. + Date endTime = cal.getTime(); + + Integer sessionId = null; + + SessionWS3VO[] sessionTab = sessionService.findForWSByProposalCodeAndNumber(proposal.getCode(), + proposal.getNumber(), Constants.getSAXSBeamline()); + + if (sessionTab == null || sessionTab.length == 0) { + // No session found we have to create one to attach the experiment data to it + Session3VO ses = new Session3VO(); + ses.setBeamlineName(Constants.getSAXSBeamline()); + ses.setProposalVO(proposalService.findByPk(proposal.getProposalId())); + ses.setStartDate(today); + ses.setEndDate(endTime); + ses.setComments("session created by ISPyBB"); + ses.setScheduled((byte) 0); + ses.setNbShifts(3); + ses.setUsedFlag((byte) 1); + ses.setTimeStamp(today); + ses.setLastUpdate(endTime); + ses = sessionService.create(ses); + sessionId = ses.getSessionId(); + } else { + sessionId = sessionTab[0].getSessionId(); + } + + return sessionId; + } + + @Override + public Experiment3VO createEmptyExperiment(String proposalCode, String proposalNumber, String name) throws Exception { + ProposalWS3VO proposal = proposalService.findForWSByCodeAndNumber(proposalCode, proposalNumber); + + Experiment3VO experiment = new Experiment3VO(); + experiment = this.experiment3Service.initPlates(experiment); + experiment.setName(name); + experiment.setType("STATIC"); + try{ + experiment.setSessionId(getSessionIdForExperiment(proposal)); + } + catch(Exception exp){ + /** Non-blocking exception **/ + exp.printStackTrace(); + } + experiment.setCreationDate(GregorianCalendar.getInstance().getTime()); + + + experiment.setProposalId(proposal.getProposalId()); + experiment = this.experiment3Service.merge(experiment); + HashMap plates = robot3Service.getDefaultSampleChangerPlateconfiguration("20"); + + PlateGroup3VO plateGroup = new PlateGroup3VO(); + plateGroup.setStorageTemperature("20"); + plateGroup = sampleplate3Service.merge(plateGroup); + + for (String key : plates.keySet()) { + Sampleplate3VO plate = plates.get(key); + plate.setExperimentId(experiment.getExperimentId()); + plate.setPlategroup3VO(plateGroup); + plate = sampleplate3Service.merge(plate); + sampleplate3Service.merge(plate); + } + + /** Creating a session **/ + Integer sessionId = null; + + + return experiment; + } + + @Override + public Measurement3VO appendMeasurementToExperiment(String experimentId, + String runNumber, String type, String plate, String row, String well, String name, String bufferName, + String concentration, String sEUtemperature, String viscosity, String volume, String volumeToLoad, String waitTime, String transmission, String comments) { + + // return experiment3Service.appendMeasurementToExperiment(experimentId, + // runNumber, type, plate, row, well, name, bufferName, concentration, + // sEUtemperature, viscosity, volume, volumeToLoad, waitTime, + // transmission, comments); + Experiment3VO experiment = experiment3Service.findById(Integer.parseInt(experimentId), ExperimentScope.MEDIUM); + System.out.println("Getting Experiment:" + experiment.getExperimentId()); + System.out.println("NAME:" + name); + System.out.println("proposal:" + experiment.getProposalId()); + + List macromolecules = new ArrayList(); + try { + List macromoleculeList = saxsProposal3Service.findMacromoleculesBy(name, experiment.getProposalId()); + System.out.println(macromoleculeList); + if (macromoleculeList != null) { + for (Macromolecule3VO macromolecule3vo : macromoleculeList) { + if (macromolecule3vo.getAcronym() != null) { + if (macromolecule3vo.getAcronym().toLowerCase().trim().equals(name.toLowerCase().trim())) { + macromolecules.add(macromolecule3vo); + System.out.println("Found"); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + macromolecules = new ArrayList(); + } + + Specimen3VO specimen = new Specimen3VO(); + + /** + * + * We don't check location of the specimen for ATSAS + * http://forge.epn-campus.eu/issues/2095 + * + */ + Set plates = experiment.getSamplePlate3VOs(); + + + + + specimen.setConcentration(concentration); + if (type.toUpperCase().equals("SAMPLE")) { + Macromolecule3VO macromolecule = null; + if (macromolecules.size() == 0) { + if (name == null) { + name = ""; + } + Macromolecule3VO m = new Macromolecule3VO(experiment.getProposalId(), name, name, ""); + m.setCreationDate(GregorianCalendar.getInstance().getTime()); + macromolecule = entityManager.merge(m); + } else { + macromolecule = macromolecules.get(0); + } + specimen.setMacromolecule3VO(macromolecule); + } + + List buffers = saxsProposal3Service.findBuffersByProposalId(experiment.getProposalId()); + for (Buffer3VO buffer3vo : buffers) { + if (buffer3vo.getAcronym() != null) { + if (buffer3vo.getAcronym().equals(bufferName)) { + specimen.setBufferId(buffer3vo.getBufferId()); + } + } + } + /** If buffer not found **/ + if (specimen.getBufferId() == null) { + Buffer3VO buffer3VO = new Buffer3VO(); + if (bufferName == null) { + bufferName = ""; + } + buffer3VO.setAcronym(bufferName); + buffer3VO.setName(bufferName); + buffer3VO.setProposalId(experiment.getProposalId()); + buffer3VO = entityManager.merge(buffer3VO); + specimen.setBufferId(buffer3VO.getBufferId()); + } + + /** Sample plate position **/ + LOG.info("getSampleplateposition3VO: " + specimen.getSampleplateposition3VO()); + LOG.info("plates: " + plates.size()); + if (specimen.getSampleplateposition3VO() == null) { + /** this specimen is new because has no position **/ + for (Sampleplate3VO samplePlate : plates) { + if (samplePlate.getSlotPositionColumn().equals(plate)) { + Sampleplateposition3VO position = new Sampleplateposition3VO(); + position.setColumnNumber(Integer.parseInt(well)); + position.setRowNumber(Integer.parseInt(row)); + position.setSamplePlateId(samplePlate.getSamplePlateId()); + position = entityManager.merge(position); + specimen.setSampleplateposition3VO(position); + } + } + } + + specimen.setExperimentId(experiment.getExperimentId()); + specimen.setVolume(volume); + + specimen = entityManager.merge(specimen); + + Measurement3VO measurement = new Measurement3VO(); + measurement.setCode(runNumber); + measurement.setSpecimenId(specimen.getSpecimenId()); + measurement.setExposureTemperature(sEUtemperature); + measurement.setTransmission(transmission); + measurement.setComment(comments); + measurement.setViscosity(viscosity); + measurement.setVolumeToLoad(volumeToLoad); + measurement.setWaitTime(waitTime); + measurement.setPriority(experiment.getMeasurements().size() + 1); + measurement = entityManager.merge(measurement); + return measurement; + } + + public void updateAverage(int measurementId, int averagedCount, int framesCount, String oneDimensionalDataFilePathArray, String discardedCurves, String averageFilePath) { + List curveList = Arrays.asList(oneDimensionalDataFilePathArray.split(",")); + List mergeList = this.primaryDataProcessing3Service.findByMeasurementId(measurementId); + if (mergeList != null){ + if (mergeList.size() > 0){ + Merge3VO merge = mergeList.get(0); + if (Integer.parseInt(merge.getFramesCount()) >= framesCount){ + LOG.info("Same number of frames count. No update"); + return; + } + else{ + this.primaryDataProcessing3Service.addMerge(measurementId, averagedCount, framesCount, oneDimensionalDataFilePathArray, null, averageFilePath); + } + } + } + } + + @Override + public void addAveraged(String measurementId, String averaged, String discarded, String averageFile) { + this.addAveraged(measurementId, averaged, discarded, averageFile, null); + } + + @Override + public void addAveraged(String measurementId, String averaged, String discarded, String averageFile, String visitorFilePath) { + + Type listType = new TypeToken>>() { + }.getType(); + List> averageFrames = new Gson().fromJson(averaged, listType); + List> discardeFrames = new Gson().fromJson(discarded, listType); + + + ArrayList oneDimensionalFiles = new ArrayList(); + LOG.info("########### Averaged Frames ############# "); + for (HashMap frame : averageFrames) { + if (frame.containsKey("filePath")) { + oneDimensionalFiles.add(frame.get("filePath")); + LOG.info("\t" + frame.get("filePath")); + } + } + + LOG.info("########### Discarded Frames ############# "); + for (HashMap frame : discardeFrames) { + if (frame.containsKey("filePath")) { + oneDimensionalFiles.add(frame.get("filePath")); + LOG.info("\t" + frame.get("filePath")); + } + } + + /** Does it contains already a average **/ + List merges = primaryDataProcessing3Service.findByMeasurementId(Integer.parseInt(measurementId)); + + try{ + Measurement3VO measurement = this.measurement3Service.findById(Integer.parseInt(measurementId)); + measurement.setImageDirectory(visitorFilePath); + this.measurement3Service.merge(measurement); + } + catch(Exception exp){ + exp.printStackTrace(); + } + + LOG.info("------ Number of Averages by measurementId --------"); + LOG.info(merges.size()); + + if (merges.size() == 0) { + LOG.info("\t Creating a new average"); + /** Not any average found so we create a new one **/ + this.primaryDataProcessing3Service.addMerge(Integer.parseInt(measurementId), averageFrames.size(), oneDimensionalFiles.size(), + oneDimensionalFiles.toString().replace("[", "").replace("]", ""), null, averageFile); + } else { + LOG.info("\t Updating a new average"); + /** It is an update of the average **/ + + this.updateAverage(Integer.parseInt(measurementId), averageFrames.size(), oneDimensionalFiles.size(), + oneDimensionalFiles.toString().replace("[", "").replace("]", ""), null, averageFile); + + } +} +// private void removeFrameListById(int frameListId) { +// Framelist3VO frameList = this.entityManager.find(Framelist3VO.class, frameListId); +// /** Looking for FrameToListObjects **/ +// +// String query = "select f from Frametolist3VO f where f.frameListId=:frameListId"; +// Query EJBQuery = this.entityManager.createQuery(query).setParameter("frameListId", frameListId); +// List frameToListList = (List)EJBQuery.getResultList(); +// for (Frametolist3VO frametolist3vo : frameToListList) { +// System.out.println("Found " + frametolist3vo.getFrameToListId()); +// } +// +// } + + /** + * True if a datacollection contains all the measurementIds otherwise false + * + * @param dataCollection + * @param measurementIds + * @return + */ + private Boolean containsSameMeasurements(SaxsDataCollection3VO dataCollection, List measurementIds) { + Iterator mIterator = dataCollection.getMeasurementtodatacollection3VOs().iterator(); + HashSet measurementDataCollectionSet = new HashSet(); + while (mIterator.hasNext()) { + MeasurementTodataCollection3VO measurementToDataCollection = mIterator.next(); + measurementDataCollectionSet.add(measurementToDataCollection.getMeasurementId()); + } + for (Integer measurementId : measurementIds) { + if (!measurementDataCollectionSet.contains(measurementId)) { + return false; + } + } + return true; + } + + private SaxsDataCollection3VO getDataCollectionByMeasurementsId(Experiment3VO experiment, List measurementIds) { + Iterator iterator = experiment.getDataCollections().iterator(); + while (iterator.hasNext()) { + SaxsDataCollection3VO dataCollection = iterator.next(); + if (this.containsSameMeasurements(dataCollection, measurementIds)) { + /** + * Returning the existing one + */ + return dataCollection; + } + } + return null; + } + /** + * Returns the measurement id corresponding to a sample or null if all measurementIds are buffers + * @param experiment + * @param measurementIds + * @return + */ + private Integer getSampleFromMeasurementList(Experiment3VO experiment, List measurementIds) { + for (Integer measurementId : measurementIds) { + Specimen3VO specimen = experiment.getSampleById(experiment.getMeasurementById(measurementId).getSpecimenId()); + if (specimen.getMacromolecule3VO() != null){ + return measurementId; + } + } + return null; + } + + /** + * return a existing datacollection if found containing the same + * measurements or create a new one + * + * @param experiment + * @param measurementIds + * @return + */ + private SaxsDataCollection3VO getSaxsDataCollectionByMeasurementIds(Experiment3VO experiment, List measurementIds) { +// SaxsDataCollection3VO datacollection = this.getDataCollectionByMeasurementsId(experiment, measurementIds); + SaxsDataCollection3VO datacollection = null; + LOG.info("Getting data collection"); + LOG.info("Getting data collection: " + measurementIds.toString()); + Integer sampleMeasurementId = this.getSampleFromMeasurementList(experiment, measurementIds); + LOG.info("sampleMeasurementId: " + sampleMeasurementId); + if (sampleMeasurementId == null){ + datacollection = this.getDataCollectionByMeasurementsId(experiment, measurementIds); + } + else{ + /** Look for a subtraction already containing this sample **/ + datacollection = experiment.getDataCollectionByMeasurementId(sampleMeasurementId); + + if (datacollection != null){ + /** Adding extra measurements **/ + Set measurementToDatacollectionList = datacollection.getMeasurementtodatacollection3VOs(); + Set measurementKeys = new HashSet(); + for (MeasurementTodataCollection3VO measurementToDatacollection : measurementToDatacollectionList) { + measurementKeys.add(measurementToDatacollection.getMeasurementId()); + } + LOG.info("measurementKeys:" + measurementKeys.toString()); + for (Integer measurementId : measurementIds) { + LOG.info("measurementKeys:" + measurementId + " " + measurementKeys.toString()); + if(!measurementKeys.contains(measurementId)){ + /** This measurement does not belong to the data collection yet **/ + MeasurementTodataCollection3VO measurementToDataCollection = new MeasurementTodataCollection3VO(); + measurementToDataCollection.setDataCollectionId(datacollection.getDataCollectionId()); + measurementToDataCollection.setMeasurementId(measurementId); + measurementToDataCollection.setDataCollectionOrder(datacollection.getMeasurementtodatacollection3VOs().size() + 1); + LOG.info("setDataCollectionOrder:" + datacollection.getMeasurementtodatacollection3VOs().size() + 1); + this.entityManager.merge(measurementToDataCollection); + } + } + } + } + + if (datacollection != null) { + return datacollection; + } else { + /** We create a new one **/ + SaxsDataCollection3VO dataCollection = new SaxsDataCollection3VO(); + dataCollection.setExperimentId(experiment.getExperimentId()); + dataCollection = entityManager.merge(dataCollection); + int dataCollectionOrder = 1; + for (Integer measurementId : measurementIds) { + MeasurementTodataCollection3VO measurementToDataCollection = new MeasurementTodataCollection3VO(); + measurementToDataCollection.setDataCollectionId(dataCollection.getDataCollectionId()); + measurementToDataCollection.setMeasurementId(measurementId); + measurementToDataCollection.setDataCollectionOrder(dataCollectionOrder); + this.entityManager.merge(measurementToDataCollection); + dataCollectionOrder++; + } + return dataCollection; + } + } + + private Framelist3VO createFrames(List sampleOneDimensionalFilesList){ + if (sampleOneDimensionalFilesList.size() > 0){ + return this.primaryDataProcessing3Service.addFrameList(sampleOneDimensionalFilesList); + } + return null; + } + + private void addSubtraction(Integer dataCollectionId, String rgStdev, String i0, String i0Stdev, String firstPointUsed, + String lastPointUsed, String quality, String isagregated, String rgGuinier, String rgGnom, String dmax, String total, + String volume, String sampleOneDimensionalFiles, String bufferOneDimensionalFiles, String sampleAverageFilePath, + String bestBufferFilePath, String subtractedFilePath, String experimentalDataPlotFilePath, String densityPlotFilePath, + String guinierPlotFilePath, String kratkyPlotFilePath, String gnomOutputFilePath, Subtraction3VO subtraction) { + + HashMap params = new HashMap(); + + long start = this.logInit("addSubtraction_CP_3.1", new Gson().toJson(params)); + + /** Getting the frames **/ + Type typeOfHash = new TypeToken>>() {}.getType(); + List> sampleOneDimensionalFilesListObject = new Gson().fromJson(sampleOneDimensionalFiles, typeOfHash); + List> bufferOneDimensionalFilesListObject = new Gson().fromJson(bufferOneDimensionalFiles, typeOfHash); + + List sampleOneDimensionalFilesList = new ArrayList(); + List bufferOneDimensionalFilesList = new ArrayList(); + + for (HashMap fileObject : sampleOneDimensionalFilesListObject) { + if (fileObject.get("filePath")!= null){ + sampleOneDimensionalFilesList.add(fileObject.get("filePath")); + } + } + + for (HashMap fileObject : bufferOneDimensionalFilesListObject) { + if (fileObject.get("filePath")!= null){ + bufferOneDimensionalFilesList.add(fileObject.get("filePath")); + } + } + + LOG.info(sampleOneDimensionalFilesList); + Framelist3VO sampleFiles = this.createFrames(sampleOneDimensionalFilesList); + this.logFinish("addSubtraction_CP_3.1", start); + + start = this.logInit("addSubtraction_CP_3.2", new Gson().toJson(params)); + Framelist3VO bufferFiles = this.createFrames(bufferOneDimensionalFilesList); + this.logFinish("addSubtraction_CP_3.2", start); + + + start = this.logInit("addSubtraction_CP_3.3", new Gson().toJson(params)); + subtraction.setDataCollectionId(dataCollectionId); + subtraction.setRg(rgGuinier); + subtraction.setRgStdev(rgStdev); + subtraction.setI0(i0); + subtraction.setI0stdev(i0Stdev); + subtraction.setFirstPointUsed(firstPointUsed); + subtraction.setLastPointUsed(lastPointUsed); + subtraction.setQuality(quality); + subtraction.setIsagregated(isagregated); + subtraction.setGnomFilePath(densityPlotFilePath); + subtraction.setRgGuinier(rgGuinier); + subtraction.setRgGnom(rgGnom); + subtraction.setDmax(dmax); + subtraction.setTotal(total); + subtraction.setVolume(volume); + subtraction.setKratkyFilePath(kratkyPlotFilePath); + subtraction.setScatteringFilePath(experimentalDataPlotFilePath); + subtraction.setGuinierFilePath(guinierPlotFilePath); + subtraction.setSubstractedFilePath(subtractedFilePath); + subtraction.setGnomFilePathOutput(gnomOutputFilePath); + subtraction.setCreationTime(getNow()); + + if (sampleFiles != null){ + subtraction.setSampleOneDimensionalFiles(sampleFiles); + } + if (bufferFiles != null){ + subtraction.setBufferOneDimensionalFiles(bufferFiles); + } + subtraction.setSampleAverageFilePath(sampleAverageFilePath); + subtraction.setBufferAverageFilePath(bestBufferFilePath); + + this.entityManager.merge(subtraction); + this.logFinish("addSubtraction_CP_3.3", start); + + } + + private void addSubtraction(int dataCollectionId, String rgStdev, String i0, String i0Stdev, String firstPointUsed, + String lastPointUsed, String quality, String isagregated, String rgGuinier, String rgGnom, String dmax, String total, + String volume, String sampleOneDimensionalFiles, String bufferOneDimensionalFiles, String sampleAverageFilePath, + String bestBufferFilePath, String subtractedFilePath, String experimentalDataPlotFilePath, String densityPlotFilePath, + String guinierPlotFilePath, String kratkyPlotFilePath, String gnomOutputFilePath) { + + Subtraction3VO subtraction = new Subtraction3VO(); + this.addSubtraction(dataCollectionId, rgStdev, i0, i0Stdev, firstPointUsed, lastPointUsed, quality, isagregated, rgGuinier, rgGnom, dmax, total, volume, sampleOneDimensionalFiles, bufferOneDimensionalFiles, sampleAverageFilePath, bestBufferFilePath, subtractedFilePath, experimentalDataPlotFilePath, densityPlotFilePath, guinierPlotFilePath, kratkyPlotFilePath, gnomOutputFilePath, subtraction); + + } + + + @Override + public void addSubtraction(String sampleMeasurementId, String rgStdev, String i0, String i0Stdev, String firstPointUsed, + String lastPointUsed, String quality, String isagregated, String rgGuinier, String rgGnom, String dmax, String total, + String volume, String sampleOneDimensionalFiles, String bufferOneDimensionalFiles, String sampleAverageFilePath, + String bestBufferFilePath, String subtractedFilePath, String experimentalDataPlotFilePath, String densityPlotFilePath, + String guinierPlotFilePath, String kratkyPlotFilePath, String gnomOutputFilePath) { + + + + HashMap params = new HashMap(); + long start = this.logInit("addSubtraction_CP_1", new Gson().toJson(params)); + if (sampleMeasurementId != null){ + // TODO: To be changed, performace problems!!! + Experiment3VO experiment = experiment3Service.findByMeasurementId(Integer.parseInt(sampleMeasurementId)); + this.logFinish("addSubtraction_CP_1", start); + + + if (experiment != null) { + start = this.logInit("addSubtraction_CP_2", new Gson().toJson(params)); + int dataCollectionId = experiment.getDataCollectionByMeasurementId(Integer.parseInt(sampleMeasurementId)).getDataCollectionId(); + this.logFinish("addSubtraction_CP_2", start); + + + start = this.logInit("addSubtraction_CP_3", new Gson().toJson(params)); + this.addSubtraction(dataCollectionId, rgStdev, i0, i0Stdev, firstPointUsed, lastPointUsed, quality, isagregated, rgGuinier, rgGnom, dmax, total, volume, sampleOneDimensionalFiles, bufferOneDimensionalFiles, sampleAverageFilePath, bestBufferFilePath, subtractedFilePath, experimentalDataPlotFilePath, densityPlotFilePath, guinierPlotFilePath, kratkyPlotFilePath, gnomOutputFilePath); + this.logFinish("addSubtraction_CP_3", start); + + } + } + } + + + /** + * Adds a new subtraction to several measurements To note: there are not any data collection yet + */ + public void addSubtraction( + String experimentId, + String runNumberList, + String rgStdev, + String i0, + String i0Stdev, + String firstPointUsed, + String lastPointUsed, + String quality, + String isagregated, + String rgGuinier, + String rgGnom, + String dmax, + String total, + String volume, + String sampleOneDimensionalFiles, + String bufferOneDimensionalFiles, + String sampleAverageFilePath, + String bestBufferFilePath, + String subtractedFilePath, + String experimentalDataPlotFilePath, + String densityPlotFilePath, + String guinierPlotFilePath, + String kratkyPlotFilePath, + String gnomOutputFilePath) throws Exception { + + Type typeOfList = new TypeToken>() {}.getType(); + List ids = new Gson().fromJson(runNumberList, typeOfList); + + /** Finding measurement by experimentId and run number **/ + List measurementIds = new ArrayList(); + for (String runNumber : ids) { + Measurement3VO measurement = measurement3Service.findMeasurementByCode(Integer.parseInt(experimentId), runNumber.toString()); + if (measurement != null) { + measurementIds.add(measurement.getMeasurementId()); + } else { + throw new Exception("No measurement found for runNumber: " + runNumber + " and experimentId: " + experimentId); + } + } + + System.out.println("Adding data collection and subtraction"); + + /** Adding data collection and subtraction **/ + if (measurementIds != null) { + if (measurementIds.size() > 0) { + Experiment3VO experiment = experiment3Service.findById(Integer.parseInt(experimentId), ExperimentScope.MEDIUM); + if (experiment != null) { + /** This method creates the data collection if it does not exist yet **/ + SaxsDataCollection3VO dataCollection = getSaxsDataCollectionByMeasurementIds(experiment, measurementIds); + + if (dataCollection.getSubstraction3VOs().size() == 0){ + this.addSubtraction(dataCollection.getDataCollectionId(), rgStdev, i0, i0Stdev, firstPointUsed, lastPointUsed, quality, isagregated, rgGuinier, rgGnom, dmax, total, volume, sampleOneDimensionalFiles, bufferOneDimensionalFiles, sampleAverageFilePath, bestBufferFilePath, subtractedFilePath, experimentalDataPlotFilePath, densityPlotFilePath, guinierPlotFilePath, kratkyPlotFilePath, gnomOutputFilePath); + } + else{ + /** + * Upgrading subtraction + */ + Subtraction3VO subtraction = (Subtraction3VO) Arrays.asList(dataCollection.getSubstraction3VOs().toArray()).get(0); + this.addSubtraction(dataCollection.getDataCollectionId(), rgStdev, i0, i0Stdev, firstPointUsed, lastPointUsed, quality, isagregated, rgGuinier, rgGnom, dmax, total, volume, sampleOneDimensionalFiles, bufferOneDimensionalFiles, sampleAverageFilePath, bestBufferFilePath, subtractedFilePath, experimentalDataPlotFilePath, densityPlotFilePath, guinierPlotFilePath, kratkyPlotFilePath, gnomOutputFilePath, subtraction); + } + } + } + } + } + + + + @Override + public void addAbinitioModelling(String experimentId, String runNumber, + ArrayList models3vo, Model3VO referenceModel, + Model3VO refinedModel) throws Exception { + + Measurement3VO measurement = measurement3Service.findMeasurementByCode(Integer.parseInt(experimentId), (runNumber)); + if (measurement != null){ + ArrayList measurementIds = new ArrayList(); + measurementIds.add(measurement.getMeasurementId()); + AbInitioModelling3Service.addAbinitioModeling(measurementIds, + models3vo, + referenceModel, + refinedModel); + + } + else{ + throw new Exception("No measurement found for " + experimentId + " and " + runNumber); + } + } + + @Override + public Model3VO getModel(String experimentId, String runNumber, String pdbfilepath) throws Exception { + Measurement3VO measurement = measurement3Service.findMeasurementByCode(Integer.parseInt(experimentId), (runNumber)); + if (measurement != null){ + return AbInitioModelling3Service.getModel(measurement.getMeasurementId(), pdbfilepath); + } + else{ + throw new Exception("Not measurement found for " + experimentId + " and " + runNumber); + } + } + + @Override + public Model3VO merge(Model3VO model) { + return this.entityManager.merge(model); + } + + @Override + public List getMacromoleculeByAcronym(String proposal, String acronym) { + List proposal3VOs = this.proposalService.findProposalByLoginName(proposal); + if (proposal3VOs != null){ + if (proposal3VOs.size() > 0){ + Proposal3VO proposal3VO = proposal3VOs.get(0); + List macromolecules = this.saxsProposal3Service.findMacromoleculesBy(acronym, proposal3VO.getProposalId()); + return macromolecules; + } + } + return new ArrayList(); + } + + public List getSubtractionByCode(String experimentId, String runNumber) { + Measurement3VO measurement = measurement3Service.findMeasurementByCode(Integer.valueOf(experimentId), runNumber); + System.out.println("measurement " + measurement); + if (measurement != null){ + return primaryDataProcessing3Service.getSubstractionsByMeasurementId(measurement.getMeasurementId()); + } + System.out.println("measurement not found "); + return null; + } + + public Macromolecule3VO getMacromoleculeByCode(String experimentId, String runNumber) { + Measurement3VO measurement = measurement3Service.findMeasurementByCode(Integer.valueOf(experimentId), runNumber); + if (measurement != null){ + Specimen3VO specimen = entityManager.find(Specimen3VO.class, measurement.getSpecimenId()); + if (specimen != null){ + return saxsProposal3Service.findMacromoleculesById(specimen.getMacromolecule3VO().getMacromoleculeId()); +// return experiment3Service.findMacromoleculeById(specimen.getMacromolecule3VO().getMacromoleculeId()); + } + } + return null; + } + + /** + * + * @param experimentId + * @param runNumber + * @param filePath {filePath:"/filepath.dat"} + * @return + */ + public Structure3VO getStructuresByCode(String experimentId, String runNumber, String filePath) { + Macromolecule3VO macromolecule = this.getMacromoleculeByCode(experimentId, runNumber); + for (Structure3VO structure : macromolecule.getStructure3VOs()) { + if (structure.getFilePath().trim().equals(filePath.trim())){ + return structure; + } + } + + return null; + } + + @Override + public void addMixture(String experimentId, String runNumber, String fitFilePath, String pdb) throws Exception { + List subtractions = this.getSubtractionByCode(experimentId, runNumber); + System.out.println("+++addMixture " + experimentId + " " + runNumber); + if (subtractions != null){ + if (subtractions.size() > 0){ + /** Getting last one are they should come by order **/ + Subtraction3VO subtraction = subtractions.get(subtractions.size() - 1); + + System.out.println("+++Subtraction Found"); + /** Getting Structure **/ + + Type ListType = new TypeToken>>() {}.getType(); + + /** Reading pdbs **/ + List> pdbFilePath = new Gson().fromJson(pdb, ListType); + + List mixtures = new ArrayList(); + if (pdbFilePath.size() > 0){ + for (HashMap pdbHash : pdbFilePath) { + MixtureToStructure mixture = new MixtureToStructure(); + + if (pdbHash.containsKey("filePath")){ + /** Finding the structue linked to such filePath */ + Structure3VO structure = this.getStructuresByCode(experimentId, runNumber, pdbHash.get("filePath")); + /** If structure does not exist we create a new one **/ + if (structure == null){ + structure = new Structure3VO(); + Macromolecule3VO macromolecule = this.getMacromoleculeByCode(experimentId, runNumber); + System.out.println("Macromolecule found"); + if (macromolecule != null){ + structure.setMacromoleculeId(macromolecule.getMacromoleculeId()); + structure.setFilePath(pdbHash.get("filePath")); + if (pdbHash.get("filePath") != null){ + String name = new File(pdbHash.get("filePath")).getName(); + structure.setName(name); + } + structure.setType("PDB"); + structure.setCreationDate(getNow()); + structure = this.entityManager.merge(structure); + System.out.println("Structure created"); + } + else{ + throw new Exception("Macromolecule not found for experimentId " + experimentId + " and runNumber " + runNumber ); + } + } + + /** Structure exists here **/ + mixture.setStructureId(structure.getStructureId()); + mixture.setCreationDate(getNow()); + + } + + if (pdbHash.containsKey("volumeFraction")){ + System.out.println("+++" +"Volume: " + pdbHash.get("volumeFraction")); + mixture.setVolumeFraction(pdbHash.get("volumeFraction")); + } + + mixtures.add(mixture); + } + + System.out.println( "+++Mixtures --- " + mixtures.size()); + System.out.println("+++" +fitFilePath + " --- " + fitFilePath); + + + /** Creating FitStructureToExperimentalData **/ + FitStructureToExperimentalData3VO fit = new FitStructureToExperimentalData3VO(); + fit.setSubtractionId(subtraction.getSubtractionId()); + fit.setCreationDate(getNow()); + fit.setFitFilePath(fitFilePath); + fit = entityManager.merge(fit); + System.out.println("+++Merging fit " + fit.getFitFilePath() + " " + fit.getFitStructureToExperimentalDataId()); + + /** Creating MixtureToStructures **/ + for (MixtureToStructure mixtureToStructure : mixtures) { + mixtureToStructure.setMixtureId(fit.getFitStructureToExperimentalDataId()); + mixtureToStructure = this.entityManager.merge(mixtureToStructure); + System.out.println("+++Merging mixturetoStructure " + mixtureToStructure.getVolumeFraction() + " " + mixtureToStructure.getStructureId() + " " + mixtureToStructure.getFitToStructureId()); + } + } + + /** Reading fitFilePath **/ +// Type HashType = new TypeToken>() {}.getType(); +// HashMap fitFilePathHash = new Gson().fromJson(fitFilePath, HashType); +// if (fitFilePathHash.containsKey("filePath")){ +// fitFilePath = fitFilePathHash.get("filePath"); +// } + + + } + else{ + throw new Exception("0 subtractions has been found for experimentId= " + experimentId + " and runNumber= " + runNumber); + } + + } + else{ + throw new Exception("No subtraction has been found for experimentId= " + experimentId + " and runNumber= " + runNumber); + } +// if (fitStructure == null){ +// fitStructure = new FitStructureToExperimentalData(); +// } +// +// fitStructure.setFitFilePath(fitFilePath); +//// fitStructure.setLogFilePath(logFile); +// fitStructure.setOutputFilePath(summaryFile); +//// fitStructure.setStructureId(Integer.parseInt(structureId)); +// fitStructure.setSubtractionId(Integer.parseInt(subtractionId)); +// fitStructure.setWorkflowId(Integer.parseInt(workflowId)); +// fitStructure.setComments("Finished at " + getNow()); +// fitStructure = advancedAnalysis3Service.merge(fitStructure); + + } + + @Override + public void addRigidBodyModeling(String experimentId, String runNumber, String fitFilePath, String rigidBodyModelFilePath, + String logFilePath, String curveConfigFilePath, String subunitConfigFilePath, String crosscorrConfigFilePath, + String contactConditionsFilePath, String masterSymmetry) throws Exception { + + List subtractions = this.getSubtractionByCode(experimentId, runNumber); + if (subtractions != null){ + if (subtractions.size() > 0){ + RigidBodyModeling3VO rigid = new RigidBodyModeling3VO(); + rigid.setSubtractionId(subtractions.get(subtractions.size() - 1).getSubtractionId()); + rigid.setFitFilePath(fitFilePath); + rigid.setRigidBodyModelFilePath(rigidBodyModelFilePath); + rigid.setLogFilePath(logFilePath); + rigid.setCurveConfigFilePath(curveConfigFilePath); + rigid.setSubUnitConfigFilePath(subunitConfigFilePath); + rigid.setCrossCorrConfigFilePath(crosscorrConfigFilePath); + rigid.setContactDescriptionFilePath(contactConditionsFilePath); + rigid.setSymmetry(masterSymmetry); + rigid.setCreationDate(getNow()); + this.entityManager.merge(rigid); + } + else{ + throw new Exception("No subtraction found for " + experimentId + " " + runNumber); + } + } + else{ + throw new Exception("No subtraction found for " + experimentId + " " + runNumber + "(null)"); + } + } + + @Override + public void addSuperposition(String experimentId, String runNumber, String abinitioModelPdbFilePath, String aprioriPdbFilePath, + String alignedPdbFilePath) throws Exception { + + List subtractions = this.getSubtractionByCode(experimentId, runNumber); + if (subtractions != null){ + Superposition3VO superposition = new Superposition3VO(); + superposition.setSubtractionId(subtractions.get(subtractions.size() - 1).getSubtractionId()); + superposition.setAbinitioModelPdbFilePath(abinitioModelPdbFilePath); + superposition.setAprioriPdbFilePath(aprioriPdbFilePath); + superposition.setAlignedPdbFilePath(alignedPdbFilePath); + superposition.setCreationDate(getNow()); + this.entityManager.merge(superposition); + } + else{ + throw new Exception("No subtraction found for " + experimentId + " " + runNumber + "(null)"); + } + + + } + + @Override + public HashMap getAprioriInformationByRunNumber(String proposal, String acronym) throws Exception { + HashMap info = new HashMap(); + List macromolecules = this.getMacromoleculeByAcronym(proposal, acronym); + if (macromolecules.size() == 1){ + Macromolecule3VO macromolecule = macromolecules.get(0); + if (macromolecule != null){ + info.put("ACRONYM", macromolecule.getAcronym()); + List> pdb = new ArrayList>(); + List> fasta = new ArrayList>(); + List> rbms = new ArrayList>(); + if (macromolecule.getStructure3VOs() != null){ + for (Structure3VO structure : macromolecule.getStructure3VOs()) { + if (structure.getType().equals("PDB")){ + HashMap pdbEntry = new HashMap(); + pdbEntry.put("FILEPATH", structure.getFilePath()); + pdbEntry.put("SYMMETRY", structure.getSymmetry()); + pdbEntry.put("MULTIPLICITY", structure.getMultiplicity()); + pdb.add(pdbEntry); + } + if (structure.getType().equals("SEQUENCE")){ + HashMap fastaEntry = new HashMap(); + fastaEntry.put("FILEPATH", structure.getFilePath()); + fasta.add(fastaEntry); + } + } + } + + HashMap rbm = new HashMap(); + rbm.put("FILEPATH", macromolecule.getContactsDescriptionFilePath()); + rbm.put("SYMMETRY", macromolecule.getSymmetry()); + rbms.add(rbm); + + info.put("PDB", pdb); + info.put("SEQUENCE", fasta); + info.put("RIGIDBODY", rbms); + return info; + } + else{ + throw new Exception("macromolecule not found"); + } + } + else{ + throw new Exception(macromolecules.size() + " macromolecules found"); + } + } + + + private void logFinish(String methodName, long id) { + LOG.debug("### [" + methodName.toUpperCase() + "] Execution time was " + (System.currentTimeMillis() - this.now) + " ms."); + LoggerFormatter.log(LOG, LoggerFormatter.Package.BIOSAXS_WS, methodName, id, System.currentTimeMillis(), + System.currentTimeMillis() - this.now); + + } + + protected long logInit(String methodName, String params) { + LOG.info("-----------------------"); + this.now = System.currentTimeMillis(); + LOG.info(methodName.toUpperCase()); + LoggerFormatter.log(LOG, LoggerFormatter.Package.BIOSAXS_WS, methodName, System.currentTimeMillis(), + System.currentTimeMillis(), params); + return this.now; + } + + @Override + public List getMacromoleculesByProposal(String code, String number) throws Exception { + ProposalWS3VO proposal = proposalService.findForWSByCodeAndNumber(code, number); + return this.saxsProposal3Service.findMacromoleculesByProposalId(proposal.getProposalId()); + } + + @Override + public List getBuffersByProposal(String code, String number) throws Exception { + ProposalWS3VO proposal = proposalService.findForWSByCodeAndNumber(code, number); + return this.saxsProposal3Service.findBuffersByProposalId(proposal.getProposalId()); + } +} diff --git a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/vos/dataAcquisition/Experiment3VO.java b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/vos/dataAcquisition/Experiment3VO.java index c68884135..4610826b9 100644 --- a/ispyb-ejb/src/main/java/ispyb/server/biosaxs/vos/dataAcquisition/Experiment3VO.java +++ b/ispyb-ejb/src/main/java/ispyb/server/biosaxs/vos/dataAcquisition/Experiment3VO.java @@ -1,505 +1,501 @@ -/******************************************************************************* - * This file is part of ISPyB. - * - * ISPyB is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ISPyB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with ISPyB. If not, see . - * - * Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Brenchereau, M. Bodin, A. De Maria Antolinos - ******************************************************************************************************************************/ - -package ispyb.server.biosaxs.vos.dataAcquisition; - -import static javax.persistence.GenerationType.IDENTITY; -import ispyb.server.biosaxs.services.core.plateType.PlateType3Service; -import ispyb.server.biosaxs.vos.assembly.Macromolecule3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.plate.Platetype3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplate3VO; -import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplateposition3VO; -import ispyb.server.biosaxs.vos.datacollection.MeasurementTodataCollection3VO; -import ispyb.server.biosaxs.vos.datacollection.SaxsDataCollection3VO; -import ispyb.server.common.util.ejb.Ejb3ServiceLocator; - -import java.util.ArrayList; -import java.util.Date; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import javax.naming.NamingException; -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.OneToMany; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; -import javax.persistence.Transient; - -/** - * Experiment3VO generated by hbm2java - */ -@Entity -@Table(name = "Experiment") -public class Experiment3VO implements java.io.Serializable { - - private static final long serialVersionUID = 1L; - - protected Integer experimentId; - - protected Integer sessionId; - - protected String name; - - protected Date creationDate; - - protected String sourceFilePath; - - protected String type; - - protected String comments; - - protected String dataAcquisitionFilePath; - - protected String status; - - protected int proposalId; - - protected Set samplePlate3VOs = new HashSet(0); - - protected List platetype3VOs = new ArrayList(); - - protected Set samples3VOs = new HashSet(0); - - protected Set dataCollections = new HashSet(0); - - /** Services for filling up plate types **/ - private static final Ejb3ServiceLocator ejb3ServiceLocator = Ejb3ServiceLocator.getInstance(); - - private PlateType3Service plateType3Service; - - public Experiment3VO() { - try { - this.plateType3Service = (PlateType3Service) ejb3ServiceLocator.getLocalService(PlateType3Service.class); - this.platetype3VOs = this.plateType3Service.findAll(); - } catch (NamingException e) { - // //e.printStackTrace(); - } - } - - @Id - @GeneratedValue(strategy = IDENTITY) - @Column(name = "experimentId", unique = true, nullable = false) - public Integer getExperimentId() { - return this.experimentId; - } - - public void setExperimentId(Integer experimentId) { - this.experimentId = experimentId; - } - - @Column(name = "name") - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - - @Column(name = "sessionId") - public Integer getSessionId() { - return this.sessionId; - } - - public void setSessionId(Integer sessionId) { - this.sessionId = sessionId; - } - - @Temporal(TemporalType.TIMESTAMP) - @Column(name = "creationDate", length = 0) - public Date getCreationDate() { - return this.creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - @Column(name = "comments", length = 512) - public String getComments() { - return this.comments; - } - - public void setComments(String comments) { - this.comments = comments; - } - - @Column(name = "proposalId") - public int getProposalId() { - return this.proposalId; - } - - public void setProposalId(int proposalId) { - this.proposalId = proposalId; - } - - /** - * TRANSIENT METHODS *. - * - * @param macromolecules - * List of all the macromolecules of the proposal which this experiment belongs to - * @return the macromolecules - */ - @Transient - public Set getMacromolecules() { - Set macromolecules = new HashSet(); - for (Specimen3VO sample : this.getSamples()) { - if (sample.getMacromolecule3VO() != null) { - macromolecules.add(sample.getMacromolecule3VO()); - } - } - return macromolecules; - } - - @Transient - public List getMeasurements() { - List specimen3VOs = new ArrayList(); - for (Specimen3VO sample : this.getSamples()) { - specimen3VOs.addAll(sample.getMeasurements()); - } - - return specimen3VOs; - } - - @Transient - public Specimen3VO getSampleWithThisBufferId(int bufferId) { - for (Specimen3VO sample : this.getSamples()) { - if (sample.getBufferId().equals(bufferId) && (sample.getMacromolecule3VO() == null)) { - return sample; - } - } - return null; - } - - @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) - @JoinColumn(name = "experimentId") - public Set getSamplePlate3VOs() { - return samplePlate3VOs; - } - - public void setSamplePlate3VOs(Set samplePlate3VOs) { - this.samplePlate3VOs = samplePlate3VOs; - } - - @Transient - public List getPlatetype3VOs() { - if (this.platetype3VOs == null) { - try { - this.plateType3Service = (PlateType3Service) ejb3ServiceLocator - .getLocalService(PlateType3Service.class); - this.platetype3VOs = this.plateType3Service.findAll(); - } catch (NamingException e) { - e.printStackTrace(); - } - } - return this.platetype3VOs; - } - - @Transient - public Measurement3VO getMeasurementById(Integer measurementId) { - List measurements = this.getMeasurements(); - for (Measurement3VO measurement : measurements) { - // System.out.println("Experiment3VO.getSpecimenById: " + measurement); - if (measurement.getMeasurementId() != null) { - if (measurement.getMeasurementId().equals(measurementId)) { - return measurement; - } - } - } - return null; - } - - @Transient - public Sampleplate3VO getSamplePlateById(Integer samplePlateId) { - Set samplePlates = this.getSamplePlate3VOs(); - for (Sampleplate3VO samplePlate : samplePlates) { - if (samplePlate.getSamplePlateId().equals(samplePlateId)) { - return samplePlate; - } - } - return null; - } - - @Transient - public Sampleplateposition3VO getPosition(Integer samplePlateId, int row, int well) { - Sampleplate3VO samplePlate = this.getSamplePlateById(samplePlateId); - if (samplePlate != null) { - for (Sampleplateposition3VO position : samplePlate.getSampleplateposition3VOs()) { - if ((position.getRowNumber() == row) && (well == position.getColumnNumber())) { - return position; - } - } - } - - /** If not found **/ - return null; - - } - - - @Transient - public String getCodeSample(Specimen3VO sample) { - int count = 1; - String name = new String(); - do { - if (sample.getMacromolecule3VO() != null) { - name = sample.getMacromolecule3VO().getAcronym() + "_" + "Buffer_Not-Know";// this.getBufferById(sample.getBufferId()).getAcronym(); - } else { - name = "Buffer_Not-Know"; // this.getBufferById(sample.getBufferId()).getAcronym(); - } - if (count > 0) { - name = name + "-" + count; - } - count++; - } while (this.isSpecimenCodeUsed(name, sample)); - return name; - - } - - @Transient - public boolean isSpecimenCodeUsed(String code, Specimen3VO specimen) { - Set samples = this.getSamples(); - for (Specimen3VO sample3vo : samples) { - if (sample3vo.getCode().equals(code)) { - if (specimen.getSpecimenId() == null) { - return true; - } else { - if (specimen.getSpecimenId().equals(sample3vo.getSpecimenId())) { - return false; - } else { - return true; - } - } - } - } - return false; - } - - @OneToMany(fetch = FetchType.EAGER) - @JoinColumn(name = "experimentId") - public Set getDataCollections() { - return dataCollections; - } - - public void setDataCollections(Set dataCollections) { - this.dataCollections = dataCollections; - } - - @Transient - public List getDataCollectionList() { - List list = new ArrayList(); - Iterator iterator = this.getDataCollections().iterator(); - while (iterator.hasNext()) { - list.add(iterator.next()); - } - return list; - } - - @OneToMany(fetch = FetchType.LAZY) - @JoinColumn(name = "experimentId") - public Set getSamples() { - return samples3VOs; - } - - public void setSamples(Set samples) { - this.samples3VOs = samples; - } - - @Transient - public Specimen3VO getSampleById(int sampleId) { - for (Specimen3VO sample : this.getSamples()) { - if (sample.getSpecimenId().equals(sampleId)) { - return sample; - } - } - return null; - - } - - public String getCodeSpecimen(Measurement3VO specimen) { - Specimen3VO sample = this.getSampleById(specimen.getSpecimenId()); - return this.getCodeSpecimen(sample, specimen); - } - - public String getCodeSpecimen(Specimen3VO sample3vo, Measurement3VO specimen) { - String code = sample3vo.getCode();// this.getCodeSample(sample3vo); - if (sample3vo.getMacromolecule3VO() == null) { - return code + "_" + specimen.getExposureTemperature(); - } else { - return code + "_" + specimen.getExposureTemperature() + "_" + sample3vo.getConcentration(); - } - } - - @Transient - public Specimen3VO getSampleByPosition(Sampleplateposition3VO position) { - Set specimens = this.getSamples(); - for (Specimen3VO sample : specimens) { - if (sample.getSampleplateposition3VO() != null) { - Sampleplateposition3VO specimenPosition = sample.getSampleplateposition3VO(); - if (specimenPosition.getSamplePlateId().equals(position.getSamplePlateId())) { - if (specimenPosition.getRowNumber() == position.getRowNumber() - && specimenPosition.getColumnNumber() == position.getColumnNumber()) { - return sample; - } - } - } - } - return null; - } - - @Transient - public SaxsDataCollection3VO getDataCollectionByMeasurementId(int measurementId) { - Measurement3VO measurement = this.getMeasurementById(measurementId); - Set dataCollections = this.getDataCollections(); - for (SaxsDataCollection3VO saxsDataCollection3VO : dataCollections) { - for (MeasurementTodataCollection3VO measurementToDataCollection : saxsDataCollection3VO - .getMeasurementtodatacollection3VOs()) { - if (measurement.getMeasurementId().equals(measurementToDataCollection.getMeasurementId())) { - return saxsDataCollection3VO; - } - } - } - return null; - } - - @Transient - public List getDataCollectionListByMeasurementId(int measurementId) { - List list = new ArrayList(); - Measurement3VO measurement = this.getMeasurementById(measurementId); - Set dataCollections = this.getDataCollections(); - for (SaxsDataCollection3VO saxsDataCollection3VO : dataCollections) { - for (MeasurementTodataCollection3VO measurementToDataCollection : saxsDataCollection3VO - .getMeasurementtodatacollection3VOs()) { - if (measurement.getMeasurementId().equals(measurementToDataCollection.getMeasurementId())) { - list.add(saxsDataCollection3VO); - } - } - } - return list; - } - - @Transient - public List getMeasurementsOfDataCollectionByMeasurementId(int measurementId) { - SaxsDataCollection3VO dc = getDataCollectionByMeasurementId(measurementId); - List list = new ArrayList(); - for (MeasurementTodataCollection3VO iterable_element : dc.getMeasurementtodatacollection3VOs()) { - list.add(iterable_element); - } - return list; - } - - @Transient - public SaxsDataCollection3VO getDataCollectionById(int dataCollectionId) { - Set dataCollections = this.getDataCollections(); - for (SaxsDataCollection3VO saxsDataCollection3VO : dataCollections) { - if (saxsDataCollection3VO.equals(dataCollectionId)) { - return saxsDataCollection3VO; - } - } - return null; - } - - @Transient - public Measurement3VO getMeasurementBefore(int measurementId) { - Integer measurementBeforeId = this.getMeasurementIdBefore(measurementId); - if (measurementBeforeId != null) - return this.getMeasurementById(measurementBeforeId); - return null; - } - - @Transient - public Integer getMeasurementIdBefore(int measurementId) { - SaxsDataCollection3VO dataCollection = this.getDataCollectionByMeasurementId(measurementId); - if (dataCollection != null) { - for (MeasurementTodataCollection3VO measurement : dataCollection.getMeasurementtodatacollection3VOs()) { - if (measurement.getDataCollectionOrder() == 1) { - return measurement.getMeasurementId(); - } - } - } - return null; - } - - @Transient - public Measurement3VO getMeasurementAfter(int measurementId) { - Integer measurementAfterId = this.getMeasurementIdAfter(measurementId); - if (measurementAfterId != null) - return this.getMeasurementById(measurementAfterId); - return null; - } - - @Transient - public Integer getMeasurementIdAfter(int measurementId) { - SaxsDataCollection3VO dataCollection = this.getDataCollectionByMeasurementId(measurementId); - if (dataCollection != null) { - for (MeasurementTodataCollection3VO measurement : dataCollection.getMeasurementtodatacollection3VOs()) { - if (measurement.getDataCollectionOrder() == 3) { - return measurement.getMeasurementId(); - } - } - } - return null; - } - - @Column(name = "sourceFilePath") - public String getSourceFilePath() { - return sourceFilePath; - } - - public void setSourceFilePath(String sourceFilePath) { - this.sourceFilePath = sourceFilePath; - } - - @Column(name = "experimentType") - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - @Column(name = "dataAcquisitionFilePath") - public String getDataAcquisitionFilePath() { - return dataAcquisitionFilePath; - } - - public void setDataAcquisitionFilePath(String dataAcquisitionFilePath) { - this.dataAcquisitionFilePath = dataAcquisitionFilePath; - } - - @Column(name = "status") - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - -} +/******************************************************************************* + * This file is part of ISPyB. + * + * ISPyB is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ISPyB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with ISPyB. If not, see . + * + * Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Brenchereau, M. Bodin, A. De Maria Antolinos + ******************************************************************************************************************************/ + +package ispyb.server.biosaxs.vos.dataAcquisition; + +import static javax.persistence.GenerationType.IDENTITY; +//import ispyb.server.biosaxs.services.core.plateType.PlateType3Service; +import ispyb.server.biosaxs.vos.assembly.Macromolecule3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.plate.Platetype3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplate3VO; +import ispyb.server.biosaxs.vos.dataAcquisition.plate.Sampleplateposition3VO; +import ispyb.server.biosaxs.vos.datacollection.MeasurementTodataCollection3VO; +import ispyb.server.biosaxs.vos.datacollection.SaxsDataCollection3VO; +import ispyb.server.common.util.ejb.Ejb3ServiceLocator; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import javax.naming.NamingException; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Transient; + +/** + * Experiment3VO generated by hbm2java + */ +@Entity +@Table(name = "Experiment") +public class Experiment3VO implements java.io.Serializable { + + private static final long serialVersionUID = 1L; + + protected Integer experimentId; + + protected Integer sessionId; + + protected String name; + + protected Date creationDate; + + protected String sourceFilePath; + + protected String type; + + protected String comments; + + protected String dataAcquisitionFilePath; + + protected String status; + + protected int proposalId; + + protected Set samplePlate3VOs = new HashSet(0); + + protected List platetype3VOs = new ArrayList(); + + protected Set samples3VOs = new HashSet(0); + + protected Set dataCollections = new HashSet(0); + + /** Services for filling up plate types **/ + private static final Ejb3ServiceLocator ejb3ServiceLocator = Ejb3ServiceLocator.getInstance(); + + //private PlateType3Service plateType3Service; + + public Experiment3VO() { + // TODO remove this : not correct design no service should be called from a VO to be replaced by the init method of ExperimentService +// try { +// this.plateType3Service = (PlateType3Service) ejb3ServiceLocator.getLocalService(PlateType3Service.class); +// this.platetype3VOs = this.plateType3Service.findAll(); +// } catch (NamingException e) { +// e.printStackTrace(); +// } + } + + @Id + @GeneratedValue(strategy = IDENTITY) + @Column(name = "experimentId", unique = true, nullable = false) + public Integer getExperimentId() { + return this.experimentId; + } + + public void setExperimentId(Integer experimentId) { + this.experimentId = experimentId; + } + + @Column(name = "name") + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + @Column(name = "sessionId") + public Integer getSessionId() { + return this.sessionId; + } + + public void setSessionId(Integer sessionId) { + this.sessionId = sessionId; + } + + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "creationDate", length = 0) + public Date getCreationDate() { + return this.creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + @Column(name = "comments", length = 512) + public String getComments() { + return this.comments; + } + + public void setComments(String comments) { + this.comments = comments; + } + + @Column(name = "proposalId") + public int getProposalId() { + return this.proposalId; + } + + public void setProposalId(int proposalId) { + this.proposalId = proposalId; + } + + /** + * TRANSIENT METHODS *. + * + * @param macromolecules + * List of all the macromolecules of the proposal which this experiment belongs to + * @return the macromolecules + */ + @Transient + public Set getMacromolecules() { + Set macromolecules = new HashSet(); + for (Specimen3VO sample : this.getSamples()) { + if (sample.getMacromolecule3VO() != null) { + macromolecules.add(sample.getMacromolecule3VO()); + } + } + return macromolecules; + } + + @Transient + public List getMeasurements() { + List specimen3VOs = new ArrayList(); + for (Specimen3VO sample : this.getSamples()) { + specimen3VOs.addAll(sample.getMeasurements()); + } + + return specimen3VOs; + } + + @Transient + public Specimen3VO getSampleWithThisBufferId(int bufferId) { + for (Specimen3VO sample : this.getSamples()) { + if (sample.getBufferId().equals(bufferId) && (sample.getMacromolecule3VO() == null)) { + return sample; + } + } + return null; + } + + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + @JoinColumn(name = "experimentId") + public Set getSamplePlate3VOs() { + return samplePlate3VOs; + } + + public void setSamplePlate3VOs(Set samplePlate3VOs) { + this.samplePlate3VOs = samplePlate3VOs; + } + + @Transient + public List getPlatetype3VOs() { + return this.platetype3VOs; + } + + public void setPlatetype3VOs(List platetype3vOs) { + platetype3VOs = platetype3vOs; + } + + @Transient + public Measurement3VO getMeasurementById(Integer measurementId) { + List measurements = this.getMeasurements(); + for (Measurement3VO measurement : measurements) { + // System.out.println("Experiment3VO.getSpecimenById: " + measurement); + if (measurement.getMeasurementId() != null) { + if (measurement.getMeasurementId().equals(measurementId)) { + return measurement; + } + } + } + return null; + } + + @Transient + public Sampleplate3VO getSamplePlateById(Integer samplePlateId) { + Set samplePlates = this.getSamplePlate3VOs(); + for (Sampleplate3VO samplePlate : samplePlates) { + if (samplePlate.getSamplePlateId().equals(samplePlateId)) { + return samplePlate; + } + } + return null; + } + + @Transient + public Sampleplateposition3VO getPosition(Integer samplePlateId, int row, int well) { + Sampleplate3VO samplePlate = this.getSamplePlateById(samplePlateId); + if (samplePlate != null) { + for (Sampleplateposition3VO position : samplePlate.getSampleplateposition3VOs()) { + if ((position.getRowNumber() == row) && (well == position.getColumnNumber())) { + return position; + } + } + } + + /** If not found **/ + return null; + + } + + + @Transient + public String getCodeSample(Specimen3VO sample) { + int count = 1; + String name = new String(); + do { + if (sample.getMacromolecule3VO() != null) { + name = sample.getMacromolecule3VO().getAcronym() + "_" + "Buffer_Not-Know";// this.getBufferById(sample.getBufferId()).getAcronym(); + } else { + name = "Buffer_Not-Know"; // this.getBufferById(sample.getBufferId()).getAcronym(); + } + if (count > 0) { + name = name + "-" + count; + } + count++; + } while (this.isSpecimenCodeUsed(name, sample)); + return name; + + } + + @Transient + public boolean isSpecimenCodeUsed(String code, Specimen3VO specimen) { + Set samples = this.getSamples(); + for (Specimen3VO sample3vo : samples) { + if (sample3vo.getCode().equals(code)) { + if (specimen.getSpecimenId() == null) { + return true; + } else { + if (specimen.getSpecimenId().equals(sample3vo.getSpecimenId())) { + return false; + } else { + return true; + } + } + } + } + return false; + } + + @OneToMany(fetch = FetchType.EAGER) + @JoinColumn(name = "experimentId") + public Set getDataCollections() { + return dataCollections; + } + + public void setDataCollections(Set dataCollections) { + this.dataCollections = dataCollections; + } + + @Transient + public List getDataCollectionList() { + List list = new ArrayList(); + Iterator iterator = this.getDataCollections().iterator(); + while (iterator.hasNext()) { + list.add(iterator.next()); + } + return list; + } + + @OneToMany(fetch = FetchType.LAZY) + @JoinColumn(name = "experimentId") + public Set getSamples() { + return samples3VOs; + } + + public void setSamples(Set samples) { + this.samples3VOs = samples; + } + + @Transient + public Specimen3VO getSampleById(int sampleId) { + for (Specimen3VO sample : this.getSamples()) { + if (sample.getSpecimenId().equals(sampleId)) { + return sample; + } + } + return null; + + } + + public String getCodeSpecimen(Measurement3VO specimen) { + Specimen3VO sample = this.getSampleById(specimen.getSpecimenId()); + return this.getCodeSpecimen(sample, specimen); + } + + public String getCodeSpecimen(Specimen3VO sample3vo, Measurement3VO specimen) { + String code = sample3vo.getCode();// this.getCodeSample(sample3vo); + if (sample3vo.getMacromolecule3VO() == null) { + return code + "_" + specimen.getExposureTemperature(); + } else { + return code + "_" + specimen.getExposureTemperature() + "_" + sample3vo.getConcentration(); + } + } + + @Transient + public Specimen3VO getSampleByPosition(Sampleplateposition3VO position) { + Set specimens = this.getSamples(); + for (Specimen3VO sample : specimens) { + if (sample.getSampleplateposition3VO() != null) { + Sampleplateposition3VO specimenPosition = sample.getSampleplateposition3VO(); + if (specimenPosition.getSamplePlateId().equals(position.getSamplePlateId())) { + if (specimenPosition.getRowNumber() == position.getRowNumber() + && specimenPosition.getColumnNumber() == position.getColumnNumber()) { + return sample; + } + } + } + } + return null; + } + + @Transient + public SaxsDataCollection3VO getDataCollectionByMeasurementId(int measurementId) { + Measurement3VO measurement = this.getMeasurementById(measurementId); + Set dataCollections = this.getDataCollections(); + for (SaxsDataCollection3VO saxsDataCollection3VO : dataCollections) { + for (MeasurementTodataCollection3VO measurementToDataCollection : saxsDataCollection3VO + .getMeasurementtodatacollection3VOs()) { + if (measurement.getMeasurementId().equals(measurementToDataCollection.getMeasurementId())) { + return saxsDataCollection3VO; + } + } + } + return null; + } + + @Transient + public List getDataCollectionListByMeasurementId(int measurementId) { + List list = new ArrayList(); + Measurement3VO measurement = this.getMeasurementById(measurementId); + Set dataCollections = this.getDataCollections(); + for (SaxsDataCollection3VO saxsDataCollection3VO : dataCollections) { + for (MeasurementTodataCollection3VO measurementToDataCollection : saxsDataCollection3VO + .getMeasurementtodatacollection3VOs()) { + if (measurement.getMeasurementId().equals(measurementToDataCollection.getMeasurementId())) { + list.add(saxsDataCollection3VO); + } + } + } + return list; + } + + @Transient + public List getMeasurementsOfDataCollectionByMeasurementId(int measurementId) { + SaxsDataCollection3VO dc = getDataCollectionByMeasurementId(measurementId); + List list = new ArrayList(); + for (MeasurementTodataCollection3VO iterable_element : dc.getMeasurementtodatacollection3VOs()) { + list.add(iterable_element); + } + return list; + } + + @Transient + public SaxsDataCollection3VO getDataCollectionById(int dataCollectionId) { + Set dataCollections = this.getDataCollections(); + for (SaxsDataCollection3VO saxsDataCollection3VO : dataCollections) { + if (saxsDataCollection3VO.equals(dataCollectionId)) { + return saxsDataCollection3VO; + } + } + return null; + } + + @Transient + public Measurement3VO getMeasurementBefore(int measurementId) { + Integer measurementBeforeId = this.getMeasurementIdBefore(measurementId); + if (measurementBeforeId != null) + return this.getMeasurementById(measurementBeforeId); + return null; + } + + @Transient + public Integer getMeasurementIdBefore(int measurementId) { + SaxsDataCollection3VO dataCollection = this.getDataCollectionByMeasurementId(measurementId); + if (dataCollection != null) { + for (MeasurementTodataCollection3VO measurement : dataCollection.getMeasurementtodatacollection3VOs()) { + if (measurement.getDataCollectionOrder() == 1) { + return measurement.getMeasurementId(); + } + } + } + return null; + } + + @Transient + public Measurement3VO getMeasurementAfter(int measurementId) { + Integer measurementAfterId = this.getMeasurementIdAfter(measurementId); + if (measurementAfterId != null) + return this.getMeasurementById(measurementAfterId); + return null; + } + + @Transient + public Integer getMeasurementIdAfter(int measurementId) { + SaxsDataCollection3VO dataCollection = this.getDataCollectionByMeasurementId(measurementId); + if (dataCollection != null) { + for (MeasurementTodataCollection3VO measurement : dataCollection.getMeasurementtodatacollection3VOs()) { + if (measurement.getDataCollectionOrder() == 3) { + return measurement.getMeasurementId(); + } + } + } + return null; + } + + @Column(name = "sourceFilePath") + public String getSourceFilePath() { + return sourceFilePath; + } + + public void setSourceFilePath(String sourceFilePath) { + this.sourceFilePath = sourceFilePath; + } + + @Column(name = "experimentType") + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + @Column(name = "dataAcquisitionFilePath") + public String getDataAcquisitionFilePath() { + return dataAcquisitionFilePath; + } + + public void setDataAcquisitionFilePath(String dataAcquisitionFilePath) { + this.dataAcquisitionFilePath = dataAcquisitionFilePath; + } + + @Column(name = "status") + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + +} diff --git a/ispyb-ejb/src/main/java/ispyb/server/common/services/sessions/Session3Service.java b/ispyb-ejb/src/main/java/ispyb/server/common/services/sessions/Session3Service.java index 9562ad36f..7129971ae 100644 --- a/ispyb-ejb/src/main/java/ispyb/server/common/services/sessions/Session3Service.java +++ b/ispyb-ejb/src/main/java/ispyb/server/common/services/sessions/Session3Service.java @@ -175,6 +175,8 @@ public List findByStartDateAndBeamLineNameAndNbShifts(final Integer */ public Session3VO findByAutoProcScalingId(final Integer autoProcScalingId) throws Exception; + public Session3VO findByAutoProcProgramAttachmentId(final Integer autoProcProgramAttachmentId) throws Exception; + public void protectSession(Integer sessionId) throws Exception; /** @@ -196,6 +198,8 @@ public List findByStartDateAndBeamLineNameAndNbShifts(final Integer public Integer getNbOfTests(final Integer sesId) throws Exception; public List findSessionByDateProposalAndBeamline(int proposalId, String beamlineName, Date date); + public Session3VO findByAutoProcProgramId(int autoProcProgramId); + } \ No newline at end of file diff --git a/ispyb-ejb/src/main/java/ispyb/server/common/services/sessions/Session3ServiceBean.java b/ispyb-ejb/src/main/java/ispyb/server/common/services/sessions/Session3ServiceBean.java index 8d77fabb7..0b8a22f96 100644 --- a/ispyb-ejb/src/main/java/ispyb/server/common/services/sessions/Session3ServiceBean.java +++ b/ispyb-ejb/src/main/java/ispyb/server/common/services/sessions/Session3ServiceBean.java @@ -111,6 +111,19 @@ private static final String FIND_ALL(boolean fetchDataCollectionGroup, boolean f + " where s.sessionId = g.sessionId and " + " g.dataCollectionGroupId = c.dataCollectionGroupId and " + " c.dataCollectionId = api.dataCollectionId and " + " api.autoProcIntegrationId = apshi.autoProcIntegrationId and " + " apshi.autoProcScalingId = aps.autoProcScalingId and " + " aps.autoProcScalingId = :autoProcScalingId "; + + private static final String FIND_BY_AUTOPROCPROGRAMATTACHMENT_ID = "select s.* from BLSession s, " + + " DataCollectionGroup g, DataCollection c, AutoProcIntegration api, AutoProcProgram autoprocProgram, AutoProcProgramAttachment autoProcProgramAttachment" + + " where s.sessionId = g.sessionId and g.dataCollectionGroupId = c.dataCollectionGroupId and autoprocProgram.autoProcProgramId = api.autoProcProgramId" + + " and c.dataCollectionId = api.dataCollectionId and autoprocProgram.autoProcProgramId = autoProcProgramAttachment.autoProcProgramId " + + " and autoProcProgramAttachment.autoProcProgramAttachmentId = :autoProcProgramAttachmentId "; + + + private static final String FIND_BY_AUTOPROCPROGRAM_ID = "select s.* from BLSession s, " + + " DataCollectionGroup g, DataCollection c, AutoProcIntegration api, AutoProcProgram autoprocProgram " + + " where s.sessionId = g.sessionId and g.dataCollectionGroupId = c.dataCollectionGroupId and autoprocProgram.autoProcProgramId = api.autoProcProgramId" + + " and c.dataCollectionId = api.dataCollectionId and autoprocProgram.autoProcProgramId = :autoProcProgramId "; + private static String getProposalCodeNumberQuery() { String query = "select * " + " FROM BLSession ses, Proposal pro " @@ -533,6 +546,32 @@ public Session3VO findByAutoProcScalingId(final Integer autoProcScalingId) throw } return null; } + + + @SuppressWarnings("unchecked") + public Session3VO findByAutoProcProgramAttachmentId(final Integer autoProcProgramAttachmentId) throws Exception { + String query = FIND_BY_AUTOPROCPROGRAMATTACHMENT_ID; + List col = this.entityManager.createNativeQuery(query, "sessionNativeQuery") + .setParameter("autoProcProgramAttachmentId", autoProcProgramAttachmentId).getResultList(); + if (col != null && col.size() > 0) { + return col.get(0); + } + return null; + } + + + @Override + public Session3VO findByAutoProcProgramId(int autoProcProgramId) { + String query = FIND_BY_AUTOPROCPROGRAM_ID; + @SuppressWarnings("unchecked") + List col = this.entityManager.createNativeQuery(query, "sessionNativeQuery") + .setParameter("autoProcProgramId", autoProcProgramId).getResultList(); + if (col != null && col.size() > 0) { + return col.get(0); + } + return null; + } + /** @@ -826,4 +865,6 @@ private void checkChangeRemoveAccess(Session3VO vo) throws AccessDeniedException + + } \ No newline at end of file diff --git a/ispyb-ejb/src/main/resources/queries/DataCollectionGroupRestWsServiceBean/getViewTableQuery.sql b/ispyb-ejb/src/main/resources/queries/DataCollectionGroupRestWsServiceBean/getViewTableQuery.sql index 6afd198e8..841a908b1 100644 --- a/ispyb-ejb/src/main/resources/queries/DataCollectionGroupRestWsServiceBean/getViewTableQuery.sql +++ b/ispyb-ejb/src/main/resources/queries/DataCollectionGroupRestWsServiceBean/getViewTableQuery.sql @@ -20,7 +20,7 @@ GROUP_CONCAT(`scalingStatisticsType` SEPARATOR ', ') AS `scalingStatisticsTypes` GROUP_CONCAT(`resolutionLimitHigh` SEPARATOR ', ') AS `resolutionsLimitHigh`, GROUP_CONCAT(`resolutionLimitLow` SEPARATOR ', ') AS `resolutionsLimitLow`, GROUP_CONCAT(`rMerge` SEPARATOR ', ') AS `rMerges`, -GROUP_CONCAT(`completeness` SEPARATOR ', ') AS `completenessList`, +GROUP_CONCAT(`completeness` SEPARATOR ', ') AS `completenessList`, GROUP_CONCAT(`AutoProc_spaceGroup` SEPARATOR ', ') AS `AutoProc_spaceGroups`, (SELECT count(*) diff --git a/ispyb-ejb/src/main/resources/queries/DataCollectionRestWsServiceBean/getViewTableQuery.sql b/ispyb-ejb/src/main/resources/queries/DataCollectionRestWsServiceBean/getViewTableQuery.sql index 89b48324c..c90e57e0c 100644 --- a/ispyb-ejb/src/main/resources/queries/DataCollectionRestWsServiceBean/getViewTableQuery.sql +++ b/ispyb-ejb/src/main/resources/queries/DataCollectionRestWsServiceBean/getViewTableQuery.sql @@ -16,7 +16,7 @@ GROUP_CONCAT(`scalingStatisticsType` SEPARATOR ', ') AS `scalingStatisticsTypes` GROUP_CONCAT(`resolutionLimitHigh` SEPARATOR ', ') AS `resolutionsLimitHigh`, GROUP_CONCAT(`resolutionLimitLow` SEPARATOR ', ') AS `resolutionsLimitLow`, GROUP_CONCAT(`rMerge` SEPARATOR ', ') AS `rMerges`, -GROUP_CONCAT(`completeness` SEPARATOR ', ') AS `completenessList`, +GROUP_CONCAT(`completeness` SEPARATOR ', ') AS `completenessList`, GROUP_CONCAT(`AutoProc_spaceGroup` SEPARATOR ', ') AS `AutoProc_spaceGroups`, (SELECT count(*) diff --git a/ispyb-ui/pom.xml b/ispyb-ui/pom.xml index 7d2baa67a..625170c04 100644 --- a/ispyb-ui/pom.xml +++ b/ispyb-ui/pom.xml @@ -22,7 +22,7 @@ ispyb ispyb-ejb3 - 5.2.0 + 5.3.4 provided @@ -179,12 +179,17 @@ org.hibernate hibernate-core - 4.0.0.Final + 5.0.10.Final + provided antlr antlr + + dom4j + dom4j + @@ -470,4 +475,4 @@ - + \ No newline at end of file diff --git a/ispyb-ui/src/main/java/ispyb/client/biosaxs/dataAdapter/BiosaxsActions.java b/ispyb-ui/src/main/java/ispyb/client/biosaxs/dataAdapter/BiosaxsActions.java index 5f865c402..3e1b36c2e 100644 --- a/ispyb-ui/src/main/java/ispyb/client/biosaxs/dataAdapter/BiosaxsActions.java +++ b/ispyb-ui/src/main/java/ispyb/client/biosaxs/dataAdapter/BiosaxsActions.java @@ -179,6 +179,7 @@ public Experiment3VO getExperimentById(Integer experimentId, ExperimentScope exp */ public Experiment3VO createExperiment(Integer proposalId, Integer sessionId, String name, String comments, Date createTime) { Experiment3VO experiment = new Experiment3VO(); + experiment = experiment3Service.initPlates(experiment); experiment.setCreationDate(createTime); experiment.setComments(comments); experiment.setName(name); diff --git a/ispyb-ui/src/main/webapp/help/release_notes.txt b/ispyb-ui/src/main/webapp/help/release_notes.txt index ca4a293d1..9e02aaccc 100644 --- a/ispyb-ui/src/main/webapp/help/release_notes.txt +++ b/ispyb-ui/src/main/webapp/help/release_notes.txt @@ -1,3 +1,29 @@ +New TAG : 5.3.3 + + +************** +* 31.10.2017 * +************** + +- work in progress: #135 +- fixes issue #176 + + +New TAG : 5.3.2 + + +************** +* 19.10.2017 * +************** + +- work in progress: #135 + +************** +* 22.09.2017 * +************** + +- fixes: #174 + ************** * 12.09.2017 * ************** diff --git a/ispyb-ui/src/main/webapp/tiles/bodies/biosaxs/welcome.jsp b/ispyb-ui/src/main/webapp/tiles/bodies/biosaxs/welcome.jsp index 37f289009..bd9e8d15c 100644 --- a/ispyb-ui/src/main/webapp/tiles/bodies/biosaxs/welcome.jsp +++ b/ispyb-ui/src/main/webapp/tiles/bodies/biosaxs/welcome.jsp @@ -91,8 +91,10 @@ <% String targetDataAcquisitions = request.getContextPath() + "/user/viewProjectList.do?reqCode=display"; %> <% String targetListShipment = request.getContextPath() + "/user/viewProjectList.do?reqCode=display&menu=list_shipment"; %> <% String targetPrepareExperiment = request.getContextPath() + "/user/viewProjectList.do?reqCode=display&menu=prepareexperiment"; %> +<% String toUpdateDB = request.getContextPath() + "/updateDB.do?reqCode=updateProposal"; %> -

In case of problems when creating shipments/samples, update ISPyB database (this may take a few minutes). + +

In case of problems when creating shipments/samples, update ISPyB database (this may take a few minutes).


diff --git a/ispyb-ui/src/main/webapp/tiles/bodies/common/help/mxcubeHelp.jsp b/ispyb-ui/src/main/webapp/tiles/bodies/common/help/mxcubeHelp.jsp index 7c010e569..2f9bc3f28 100644 --- a/ispyb-ui/src/main/webapp/tiles/bodies/common/help/mxcubeHelp.jsp +++ b/ispyb-ui/src/main/webapp/tiles/bodies/common/help/mxcubeHelp.jsp @@ -18,6 +18,7 @@ Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Bren --------------------------------------------------------------------------------------------------%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@page import="ispyb.common.util.Constants"%> +<%@ page isELIgnored="false" %> Help page for MxCube @@ -33,25 +34,25 @@ Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Bren <%-- 1) Select shipment --%> - 1- Select the dewars you want for processing.
+ 1- Select the dewars you want for processing.
<%-- 2) Fill Sample changer --%> - 2- Fill the sample changer: assign a location for your containers (only required if not using Damatrix codes)
+ 2- Fill the sample changer: assign a location for your containers (only required if not using Damatrix codes)
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
diff --git a/ispyb-ui/src/main/webapp/tiles/bodies/common/help/prepareHelp.jsp b/ispyb-ui/src/main/webapp/tiles/bodies/common/help/prepareHelp.jsp index e265a0fac..633193999 100644 --- a/ispyb-ui/src/main/webapp/tiles/bodies/common/help/prepareHelp.jsp +++ b/ispyb-ui/src/main/webapp/tiles/bodies/common/help/prepareHelp.jsp @@ -19,6 +19,8 @@ Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Bren <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@page import="ispyb.common.util.Constants"%> +<%@ page isELIgnored="false" %> + Help page for Prepare Experiment @@ -29,25 +31,25 @@ Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Bren <%-- 1) Select shipment --%> - 1- Select the dewars you want for processing.
+ 1- Select the dewars you want for processing.
<%-- 2) Fill Sample changer --%> - 2- Fill the sample changer: assign a location for your containers (only required if not using Damatrix codes)
+ 2- Fill the sample changer: assign a location for your containers (only required if not using Damatrix codes)
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
@@ -66,7 +68,7 @@ Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Bren - +

diff --git a/ispyb-ui/src/main/webapp/tiles/bodies/common/proposal/updateISPYBdb.jsp b/ispyb-ui/src/main/webapp/tiles/bodies/common/proposal/updateISPYBdb.jsp index badc48481..692b6a7af 100644 --- a/ispyb-ui/src/main/webapp/tiles/bodies/common/proposal/updateISPYBdb.jsp +++ b/ispyb-ui/src/main/webapp/tiles/bodies/common/proposal/updateISPYBdb.jsp @@ -66,7 +66,8 @@ Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Bren - + + diff --git a/ispyb-ui/src/main/webapp/tiles/bodies/mx/prepare/fillSampleChanger.jsp b/ispyb-ui/src/main/webapp/tiles/bodies/mx/prepare/fillSampleChanger.jsp index a799d3335..6f31ef7a4 100644 --- a/ispyb-ui/src/main/webapp/tiles/bodies/mx/prepare/fillSampleChanger.jsp +++ b/ispyb-ui/src/main/webapp/tiles/bodies/mx/prepare/fillSampleChanger.jsp @@ -25,6 +25,8 @@ Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Bren <%@page import="ispyb.common.util.Constants"%> +<%@ page isELIgnored="false" %> + <% String buttonLabel = "Next step: Link Samples in " + Constants.BCM_NAME; %> @@ -42,25 +44,25 @@ Contributors : S. Delageniere, R. Leal, L. Launer, K. Levik, S. Veyrier, P. Bren <%-- 1) Select shipment --%> - 1- Select the dewars you want for processing.
+ 1- Select the dewars you want for processing.
<%-- 2) Fill Sample changer --%> - 2- Fill the sample changer: assign a location for your containers (only required if not using Damatrix codes)
+ 2- Fill the sample changer: assign a location for your containers (only required if not using Damatrix codes)
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
- 2- Fill the sample changer: assign a location for your containers
+ 2- Fill the sample changer: assign a location for your containers
diff --git a/ispyb-ws/pom.xml b/ispyb-ws/pom.xml index 04cfb68ef..63798c30c 100644 --- a/ispyb-ws/pom.xml +++ b/ispyb-ws/pom.xml @@ -4,7 +4,7 @@ ispyb ispyb-parent - 5.2.0 + 5.3.4 ispyb-ws war @@ -12,7 +12,7 @@ ispyb ispyb-ejb3 - 5.2.0 + 5.3.4 provided @@ -38,7 +38,6 @@ javax.ejb javax.ejb-api 3.2 - provided @@ -152,4 +151,4 @@ - + \ No newline at end of file diff --git a/ispyb-ws/src/main/java/ispyb/ws/rest/mx/AutoprocintegrationRestWebService.java b/ispyb-ws/src/main/java/ispyb/ws/rest/mx/AutoprocintegrationRestWebService.java index cdfe38b5e..4de80758b 100644 --- a/ispyb-ws/src/main/java/ispyb/ws/rest/mx/AutoprocintegrationRestWebService.java +++ b/ispyb-ws/src/main/java/ispyb/ws/rest/mx/AutoprocintegrationRestWebService.java @@ -8,6 +8,8 @@ import ispyb.server.mx.vos.autoproc.AutoProcIntegration3VO; import ispyb.server.mx.vos.autoproc.AutoProcProgram3VO; import ispyb.server.mx.vos.autoproc.AutoProcProgramAttachment3VO; +import ispyb.server.mx.vos.collections.DataCollection3VO; +import ispyb.server.mx.vos.collections.Session3VO; import java.io.File; import java.util.ArrayList; @@ -32,6 +34,7 @@ public class AutoprocintegrationRestWebService extends MXRestWebService { private final static Logger logger = Logger.getLogger(AutoprocintegrationRestWebService.class); + private static final String NOT_ALLOWED = "You don't have access to this resource"; @RolesAllowed({ "User", "Manager", "Industrial", "Localcontact" }) @GET @@ -126,6 +129,13 @@ public Response getAttachments(@PathParam("token") String token, @PathParam("pro } + + + private boolean checkProposalByAutoProcProgramId(int proposalId, int autoProcProgramId) throws NamingException, Exception{ + return this.getSession3Service().findByAutoProcProgramId(autoProcProgramId).getProposalVOId().equals(proposalId); + } + + /** * AutoProcProgramAttachment has not AutoProcProgramId mapped in the EJB object * so it is necessary to keep separately the possible list of ids in order @@ -147,13 +157,19 @@ public Response downloadAttachments( String methodName = "downloadAttachments"; long start = this.logInit(methodName, logger, token, proposal); try { - List ids = this.parseToInteger(autoprocattachmentids); + List autoProcProgramIds = this.parseToInteger(autoprocattachmentids); List> list = new ArrayList>(); HashMap filePaths = new HashMap(); String filename = "download.zip"; - for (Integer id : ids) { - AutoProcProgram3VO autoProcProgram3VO = this.getAutoProcProgram3Service().findByPk(id, true); + for (Integer autoProcProgramId : autoProcProgramIds) { + /** Check that id correspond to the proposal **/ + if (!this.checkProposalByAutoProcProgramId(this.getProposalId(proposal), autoProcProgramId)){ + throw new Exception(NOT_ALLOWED); + } + + + AutoProcProgram3VO autoProcProgram3VO = this.getAutoProcProgram3Service().findByPk(autoProcProgramId, true); /** Prefix for the name of the file and the internal structure if many results are retrieved **/ String prefix = String.format("%s_%s", autoProcProgram3VO.getProcessingPrograms(), autoProcProgram3VO.getAutoProcProgramId()); @@ -164,7 +180,7 @@ public Response downloadAttachments( String filePath = auto.getFilePath() + "/" + auto.getFileName(); if (new File(filePath).exists()){ if (new File(filePath).isFile()){ - if (ids.size() > 1){ + if (autoProcProgramIds.size() > 1){ String zipNameFile = prefix + "/" + auto.getFileName(); filePaths.put(zipNameFile, filePath); } @@ -176,7 +192,7 @@ public Response downloadAttachments( } /** If it is a single result then filename is the name of the program and the ID **/ - if (ids.size() == 1){ + if (autoProcProgramIds.size() == 1){ filename = prefix + ".zip"; } @@ -333,6 +349,10 @@ public Response getXScaleWilson(@PathParam("token") String token, @PathParam("pr } } + private boolean checkProposalByAutoProcProgramAttachmentId(int proposalId, int autoProcProgramAttachmentId) throws NamingException, Exception{ + return this.getSession3Service().findByAutoProcProgramAttachmentId(autoProcProgramAttachmentId).getProposalVOId().equals(proposalId); + } + @RolesAllowed({ "User", "Manager", "Industrial", "Localcontact" }) @GET @Path("{token}/proposal/{proposal}/mx/autoprocintegration/autoprocattachmentid/{autoProcAttachmentId}/download") @@ -343,11 +363,17 @@ public Response downloadAutoProcAttachment(@PathParam("token") String token, @Pa String methodName = "downloadAutoProcAttachment"; long start = this.logInit(methodName, logger, token, proposal); try { - AutoProcProgramAttachment3VO attachment = this.getAutoProcProgramAttachment3Service().findByPk(autoProcAttachmentId); - this.logFinish(methodName, start, logger); - File file = new File(attachment.getFilePath() + "/" + attachment.getFileName()); - this.logFinish(methodName, start, logger); - return this.downloadFileAsAttachment(file.getAbsolutePath()); + /** Checking that attachment is linked to the proposal **/ + if (this.checkProposalByAutoProcProgramAttachmentId(this.getProposalId(proposal), autoProcAttachmentId)){ + AutoProcProgramAttachment3VO attachment = this.getAutoProcProgramAttachment3Service().findByPk(autoProcAttachmentId); + this.logFinish(methodName, start, logger); + File file = new File(attachment.getFilePath() + "/" + attachment.getFileName()); + this.logFinish(methodName, start, logger); + return this.downloadFileAsAttachment(file.getAbsolutePath()); + } + else{ + throw new Exception(NOT_ALLOWED); + } } catch (Exception e) { return this.logError(methodName, e, start, logger); } @@ -360,13 +386,19 @@ public Response downloadAutoProcAttachment(@PathParam("token") String token, @Pa public Response getAutoProcAttachment(@PathParam("token") String token, @PathParam("proposal") String proposal, @PathParam("autoProcAttachmentId") int autoProcAttachmentId) { - String methodName = "downloadAutoProcAttachment"; + String methodName = "getAutoProcAttachment"; long start = this.logInit(methodName, logger, token, proposal); try { - AutoProcProgramAttachment3VO attachment = this.getAutoProcProgramAttachment3Service().findByPk(autoProcAttachmentId); - File file = new File(attachment.getFilePath() + "/" + attachment.getFileName()); - this.logFinish(methodName, start, logger); - return this.downloadFile(file.getAbsolutePath()); + /** Checking that attachment is linked to the proposal **/ + if (this.checkProposalByAutoProcProgramAttachmentId(this.getProposalId(proposal), autoProcAttachmentId)){ + AutoProcProgramAttachment3VO attachment = this.getAutoProcProgramAttachment3Service().findByPk(autoProcAttachmentId); + File file = new File(attachment.getFilePath() + "/" + attachment.getFileName()); + this.logFinish(methodName, start, logger); + return this.downloadFile(file.getAbsolutePath()); + } + else{ + throw new Exception(NOT_ALLOWED); + } } catch (Exception e) { return this.logError(methodName, e, start, logger); } diff --git a/ispyb-ws/src/main/java/ispyb/ws/rest/mx/DataCollectionRestWebService.java b/ispyb-ws/src/main/java/ispyb/ws/rest/mx/DataCollectionRestWebService.java index 82f7cae2b..45f112b79 100644 --- a/ispyb-ws/src/main/java/ispyb/ws/rest/mx/DataCollectionRestWebService.java +++ b/ispyb-ws/src/main/java/ispyb/ws/rest/mx/DataCollectionRestWebService.java @@ -1,8 +1,5 @@ package ispyb.ws.rest.mx; -import ispyb.common.util.export.ExiPdfRtfExporter; -import ispyb.server.mx.vos.collections.DataCollection3VO; - import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; @@ -18,11 +15,16 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import org.apache.log4j.Logger; import org.jboss.resteasy.annotations.GZIP; +import ispyb.common.util.export.ExiPdfRtfExporter; +import ispyb.server.mx.vos.collections.DataCollection3VO; +import ispyb.server.mx.vos.collections.Session3VO; + @Path("/") public class DataCollectionRestWebService extends MXRestWebService { @@ -217,18 +219,22 @@ public Response getViewDataCollectionBySessionId(@PathParam("token") String toke @RolesAllowed({"User", "Manager", "Industrial", "Localcontact"}) @GET - @Path("{token}/proposal/{proposal}/mx/datacollection/session/{sessionId}/report/{reportType}/pdf") + @Path("{token}/proposal/{proposal}/mx/datacollection/session/{sessionId}/report/pdf") @Produces({ "application/pdf" }) public Response getDataCollectionsReportBySessionIdPDF(@PathParam("token") String token, @PathParam("proposal") String proposal, - @PathParam("sessionId") String sessionId, @PathParam("reportType") String reportType) throws NamingException { + @PathParam("sessionId") String sessionId, @QueryParam("nbRows") String nbRows) throws NamingException { String methodName = "getDataCollectionReportyBySessionIdPdf"; long start = this.logInit(methodName, logger, token, proposal, sessionId); try { - byte[] byteToExport = this.getPdfRtf(sessionId, proposal, reportType, false); + byte[] byteToExport = this.getPdfRtf(sessionId, proposal, nbRows, false); this.logFinish(methodName, start, logger); - return this.downloadFile(byteToExport, "DataCollectionsReport.pdf"); + Session3VO ses = this.getSession3Service().findByPk(new Integer(sessionId), false, false, false); + if (ses != null) + return this.downloadFile(byteToExport, "Report_" + proposal + "_"+ ses.getBeamlineName()+ "_" + ses.getStartDate() + ".pdf"); + else + return this.downloadFile(byteToExport, "No_session.pdf"); } catch (Exception e) { return this.logError(methodName, e, start, logger); @@ -237,24 +243,75 @@ public Response getDataCollectionsReportBySessionIdPDF(@PathParam("token") Strin @RolesAllowed({"User", "Manager", "Industrial", "Localcontact"}) @GET - @Path("{token}/proposal/{proposal}/mx/datacollection/session/{sessionId}/report/{reportType}/rtf") + @Path("{token}/proposal/{proposal}/mx/datacollection/session/{sessionId}/report/rtf") @Produces({ "application/rtf" }) public Response getDataCollectionsReportBySessionIdRTF(@PathParam("token") String token, @PathParam("proposal") String proposal, - @PathParam("sessionId") String sessionId, @PathParam("reportType") String reportType) throws NamingException { + @PathParam("sessionId") String sessionId, @QueryParam("nbRows") String nbRows) throws NamingException { String methodName = "getDataCollectionReportyBySessionIdRtf"; long start = this.logInit(methodName, logger, token, proposal, sessionId); try { - byte[] byteToExport = this.getPdfRtf(sessionId, proposal, reportType, true); + byte[] byteToExport = this.getPdfRtf(sessionId, proposal, nbRows, true); this.logFinish(methodName, start, logger); - return this.downloadFile(byteToExport, "DataCollectionsReport.rtf"); + Session3VO ses = this.getSession3Service().findByPk(new Integer(sessionId), false, false, false); + if (ses != null) + return this.downloadFile(byteToExport, "Report_" + proposal + "_"+ ses.getBeamlineName()+ "_" + ses.getStartDate() + ".rtf"); + else + return this.downloadFile(byteToExport, "No_session.pdf"); } catch (Exception e) { return this.logError(methodName, e, start, logger); } } + @RolesAllowed({"User", "Manager", "Industrial", "Localcontact"}) + @GET + @Path("{token}/proposal/{proposal}/mx/datacollection/session/{sessionId}/analysisreport/pdf") + @Produces({ "application/pdf" }) + public Response getDataCollectionsAnalysisReportBySessionIdPDF(@PathParam("token") String token, + @PathParam("proposal") String proposal, + @PathParam("sessionId") String sessionId, @QueryParam("nbRows") String nbRows) throws NamingException { + + String methodName = "getDataCollectionAnalysisReportyBySessionIdPdf"; + long start = this.logInit(methodName, logger, token, proposal, sessionId); + try { + byte[] byteToExport = this.getAnalysisPdfRtf(sessionId, proposal, nbRows, false); + this.logFinish(methodName, start, logger); + Session3VO ses = this.getSession3Service().findByPk(new Integer(sessionId), false, false, false); + if (ses !=null) + return this.downloadFile(byteToExport, "AnalysisReport_" + proposal + "_"+ ses.getBeamlineName()+ "_" + ses.getStartDate() + ".pdf"); + else + return this.downloadFile(byteToExport, "No_session.pdf"); + + } catch (Exception e) { + return this.logError(methodName, e, start, logger); + } + } + + @RolesAllowed({"User", "Manager", "Industrial", "Localcontact"}) + @GET + @Path("{token}/proposal/{proposal}/mx/datacollection/session/{sessionId}/analysisreport/rtf") + @Produces({ "application/rtf" }) + public Response getDataCollectionsAnalysisReportBySessionIdRTF(@PathParam("token") String token, + @PathParam("proposal") String proposal, + @PathParam("sessionId") String sessionId, @QueryParam("nbRows") String nbRows) throws NamingException { + + String methodName = "getDataCollectionReportyBySessionIdRtf"; + long start = this.logInit(methodName, logger, token, proposal, sessionId); + try { + byte[] byteToExport = this.getAnalysisPdfRtf(sessionId, proposal, nbRows, true); + this.logFinish(methodName, start, logger); + Session3VO ses = this.getSession3Service().findByPk(new Integer(sessionId), false, false, false); + if (ses !=null) + return this.downloadFile(byteToExport, "AnalysisReport_" + proposal + "_"+ ses.getBeamlineName()+ "_" + ses.getStartDate() + ".rtf"); + else + return this.downloadFile(byteToExport, "No_session.pdf"); + } catch (Exception e) { + return this.logError(methodName, e, start, logger); + } + } + @RolesAllowed({ "User", "Manager", "Industrial", "Localcontact" }) @GET @@ -355,22 +412,42 @@ public Response getViewDataCollectionByProteinAcronym(@PathParam("token") String } } - private byte [] getPdfRtf(String sessionId, String proposal, String reportType, boolean isRtf) throws NamingException, Exception { + private byte [] getPdfRtf(String sessionId, String proposal, String nbRows, boolean isRtf) throws NamingException, Exception { Integer id = new Integer(sessionId); List> dataCollections = this.getWebServiceDataCollectionGroup3Service().getViewDataCollectionBySessionId(this.getProposalId(proposal), id); - - // for testing purposes, to be removed later - Integer nbRowsMax = new Integer(reportType); - ExiPdfRtfExporter pdf = new ExiPdfRtfExporter(proposal, id , dataCollections, nbRowsMax); + Integer nbRowsMax = dataCollections.size(); + + if (nbRows != null && !nbRows.isEmpty()) { + nbRowsMax = new Integer(nbRows); + } + + ExiPdfRtfExporter pdf = new ExiPdfRtfExporter(this.getProposalId(proposal), proposal, id , dataCollections, nbRowsMax); byte [] byteToExport = pdf.exportDataCollectionReport(isRtf).toByteArray(); - if (reportType.equals("1")) { - byteToExport = pdf.exportDataCollectionReport(isRtf).toByteArray(); + + return byteToExport; + } + + private byte [] getAnalysisPdfRtf(String sessionId, String proposal, String nbRows, boolean isRtf) throws NamingException, Exception { + + Integer id = new Integer(sessionId); + + List> dataCollections = + this.getWebServiceDataCollectionGroup3Service().getViewDataCollectionBySessionId(this.getProposalId(proposal), id); + + Integer nbRowsMax = dataCollections.size(); + + if (nbRows != null && !nbRows.isEmpty()) { + nbRowsMax = new Integer(nbRows); } + + ExiPdfRtfExporter pdf = new ExiPdfRtfExporter(this.getProposalId(proposal), proposal, id , dataCollections, nbRowsMax); + + byte [] byteToExport = pdf.exportDataCollectionAnalysisReport(isRtf).toByteArray(); return byteToExport; } diff --git a/ispyb-ws/src/main/java/ispyb/ws/soap/em/ToolsForEMDataCollection.java b/ispyb-ws/src/main/java/ispyb/ws/soap/em/ToolsForEMDataCollection.java index 6e9e29103..5d2204930 100644 --- a/ispyb-ws/src/main/java/ispyb/ws/soap/em/ToolsForEMDataCollection.java +++ b/ispyb-ws/src/main/java/ispyb/ws/soap/em/ToolsForEMDataCollection.java @@ -186,9 +186,7 @@ public CTF getDataCollectionsByWorkingFolder( ) { try { - log.info("getDataCollectionsByWorkingFolder. technique=EM proposal={} workingFolder={} ", proposal, workingFolder); - DataCollection3Service dataCollection3Service = (EM3Service) ejb3ServiceLocator.getLocalService(DataCollection3Service.class); - return service.addCTF(proposal, movieFullPath, spectraImageSnapshotFullPath, spectraImageFullPath, defocusU, defocusV, angle, crossCorrelationCoefficient, resolutionLimit, estimatedBfactor, logFilePath); + throw new Exception("Not implemented yet"); } catch (Exception exp) { exp.printStackTrace(); log.error("getDataCollectionsByWorkingFolder. technique=EM proposal={} workingFolder={} ", proposal, workingFolder); diff --git a/ispyb-ws/src/main/java/ispyb/ws/soap/mx/ToolsForAutoprocessingWebService.java b/ispyb-ws/src/main/java/ispyb/ws/soap/mx/ToolsForAutoprocessingWebService.java index c37fd0ab0..3d5a9c407 100644 --- a/ispyb-ws/src/main/java/ispyb/ws/soap/mx/ToolsForAutoprocessingWebService.java +++ b/ispyb-ws/src/main/java/ispyb/ws/soap/mx/ToolsForAutoprocessingWebService.java @@ -102,7 +102,6 @@ import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; import javax.naming.NamingException; -import javax.ws.rs.core.Response; import org.apache.log4j.Logger; import org.jboss.ejb3.annotation.SecurityDomain; diff --git a/pom.xml b/pom.xml index 3ab16191a..290b587c2 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 ispyb ispyb-parent - 5.2.0 + 5.3.4 pom ispyb-ejb