source: trunk/src/groovy/dbnp/query/StudySearch.groovy @ 1820

Last change on this file since 1820 was 1820, checked in by robert@…, 9 years ago

Adjusted querying process in order to improve speed with large amounts of data. The querying is now performed directly in HQL, instead of fetching all possible objects from the database and filtering them.

  • Property svn:keywords set to Rev Author Date
File size: 3.8 KB
Line 
1/**
2 * StudySearch Domain Class
3 *
4 * This class provides querying capabilities for searching for studies
5 *
6 * @author  Robert Horlings (robert@isdat.nl)
7 * @since       20110118
8 * @package     dbnp.query
9 *
10 * Revision information:
11 * $Rev: 1820 $
12 * $Author: robert@isdat.nl $
13 * $Date: 2011-05-06 15:41:49 +0000 (vr, 06 mei 2011) $
14 */
15package dbnp.query
16
17import groovy.lang.Closure;
18
19import java.util.List;
20import java.util.Map;
21
22import dbnp.studycapturing.*
23import org.dbnp.gdt.*
24import org.apache.commons.logging.LogFactory;
25
26class StudySearch extends Search {
27        private static final log = LogFactory.getLog(this);
28
29        public StudySearch() {
30                super();
31                this.entity = "Study";
32        }
33
34        /**
35        * Returns a closure for the given entitytype that determines the value for a criterion
36        * on the given object. The closure receives two parameters: the object and a criterion.
37        *
38        * This method should be implemented by all searches
39        *
40        * For example:
41        *               For a study search, the object given is a study. How to determine the value for that study of
42        *               the criterion field of type sample? This is done by returning the field values for all
43        *               samples in the study
44        *                       { study, criterion -> return study.samples?.collect { criterion.getFieldValue( it ); } }
45        * @return
46        */
47        protected Closure valueCallback( String entity ) {
48                switch( entity ) {
49                        case "Study":
50                                return { study, criterion -> return criterion.getFieldValue( study ) }
51                        case "Subject":
52                                return { study, criterion -> return study.subjects?.collect { criterion.getFieldValue( it ); } }
53                        case "Sample":
54                                return { study, criterion -> return study.samples?.collect { criterion.getFieldValue( it ); } }
55                        case "Event":
56                                return { study, criterion -> return study.events?.collect { criterion.getFieldValue( it ); } }
57                        case "SamplingEvent":
58                                return { study, criterion -> return study.samplingEvents?.collect { criterion.getFieldValue( it ); } }
59                        case "Assay":
60                                return { study, criterion -> return study.assays?.collect { criterion.getFieldValue( it ); } }
61                        default:
62                                return null;
63                }
64        }
65       
66        /**
67         * Returns the HQL name for the element or collections to be searched in, for the given entity name
68         * For example: when searching for Subject.age > 50 with Study results, the system must search in all study.subjects for age > 50.
69     * But when searching for Sample results, the system must search in sample.parentSubject for age > 50
70         *
71     * @param entity    Name of the entity of the criterion
72         * @return                      HQL name for this element or collection of elements
73         */
74        protected String elementName( String entity ) {
75                switch( entity ) {
76                        case "Study":                   return "study"
77                        case "Subject":                 return "study.subjects"
78                        case "Sample":                  return "study.samples"
79                        case "Event":                   return "study.events"
80                        case "SamplingEvent":   return "study.samplingEvents"
81                        case "Assay":                   return "study.assays"
82                        default:                                return null;
83                }
84        } 
85       
86        /**
87         * Returns true iff the given entity is accessible by the user currently logged in
88         *
89         * @param entity                Study to determine accessibility for.
90         * @return                      True iff the user is allowed to access this study
91         */
92        protected boolean isAccessible( def entity ) {
93                return entity?.canRead( this.user );
94        }
95       
96        /**
97         * Returns the saved field data that could be shown on screen. This means, the data
98         * is filtered to show only data of the query results. Also, the study title and sample
99         * name are filtered out, in order to be able to show all data on the screen without
100         * checking further
101         *
102         * @return      Map with the entity id as a key, and a field-value map as value
103         */
104        public Map getShowableResultFields() {
105                Map showableFields = super.getShowableResultFields()
106                showableFields.each { sampleElement ->
107                        sampleElement.value = sampleElement.value.findAll { fieldElement ->
108                                fieldElement.key != "Study title" && fieldElement.key != "Subject species"
109                        }
110                }
111                return showableFields
112        }
113}
Note: See TracBrowser for help on using the repository browser.