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

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

Implemented searching in 'all fields'

  • Property svn:keywords set to Rev Author Date
File size: 5.6 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: 1800 $
12 * $Author: robert@isdat.nl $
13 * $Date: 2011-05-02 15:03:36 +0000 (ma, 02 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         * Searches for studies based on the given criteria. All criteria have to be satisfied and
36         * criteria for the different entities are satisfied as follows:
37         *
38         *              Study.title = 'abc'             
39         *                              All returned studies will have title 'abc'
40         *             
41         *              Subject.species = 'human'
42         *                              All returned studies will have one or more subjects with species = 'human' 
43         *
44         *              Sample.name = 'sample 1'
45         *                              All returned studies will have one or more samples with name = 'sample 1'
46         *
47         *              Event.startTime = '0s'
48         *                              All returned studies will have one or more events with start time = '0s' 
49         *
50         *              Assay.module = 'metagenomics'
51         *                              All returned studies will have one or more assays with module = 'metagenomics' 
52         *
53         * When searching the system doesn't look at the connections between different entities. This means,
54         * the system doesn't look for human subjects having a sample with name 'sample 1'. The sample 1 might
55         * as well belong to a mouse subject and still the study satisfies the criteria.
56         *
57         * When searching for more than one criterion per entity, these are taken combined. Searching for
58         *
59         *              Subject.species = 'human'
60         *              Subject.name = 'Jan'
61         *
62         *  will result in all studies having a human subject named 'Jan'. Studies with only a mouse subject
63         *  named 'Jan' or a human subject named 'Kees' won't satisfy the criteria.
64         *     
65         */
66        @Override
67        protected void executeAnd() {
68                def studies = Study.list().findAll { it.canRead( this.user ) };
69
70                executeAnd( studies );
71        }
72
73        /**
74         * Searches for studies based on the given criteria. Only one criteria have to be satisfied and
75         * criteria for the different entities are satisfied as follows:
76         *
77         *              Study.title = 'abc'
78         *                              The returned study will have title 'abc'
79         *
80         *              Subject.species = 'human'
81         *                              The returned study will have one or more subjects with species = 'human'
82         *
83         *              Sample.name = 'sample 1'
84         *                              The returned study will have one or more samples with name = 'sample 1'
85         *
86         *              Event.startTime = '0s'
87         *                              The returned study will have one or more events with start time = '0s'
88         *
89         *              Assay.module = 'metagenomics'
90         *                              The returned study will have one or more assays with module = 'metagenomics'
91         *
92         * When searching the system doesn't look at the connections between different entities. This means,
93         * the system doesn't look for human subjects having a sample with name 'sample 1'. The sample 1 might
94         * as well belong to a mouse subject and still the study satisfies the criteria.
95         *
96         * When searching for more than one criterion per entity, these are taken separately. Searching for
97         *
98         *              Subject.species = 'human'
99         *              Subject.name = 'Jan'
100         *
101         *  will result in all studies having a human subject or a subject named 'Jan'. Studies with only a
102         *  mouse subject named 'Jan' or a human subject named 'Kees' will satisfy the criteria.
103         *
104         */
105        @Override
106        protected void executeOr() {
107                def allStudies = Study.list().findAll { it.canRead( this.user ) };
108                executeOr( allStudies );
109        }
110
111        /**
112        * Returns a closure for the given entitytype that determines the value for a criterion
113        * on the given object. The closure receives two parameters: the object and a criterion.
114        *
115        * This method should be implemented by all searches
116        *
117        * For example:
118        *               For a study search, the object given is a study. How to determine the value for that study of
119        *               the criterion field of type sample? This is done by returning the field values for all
120        *               samples in the study
121        *                       { study, criterion -> return study.samples?.collect { criterion.getFieldValue( it ); } }
122        * @return
123        */
124        protected Closure valueCallback( String entity ) {
125                switch( entity ) {
126                        case "Study":
127                                return { study, criterion -> return criterion.getFieldValue( study ) }
128                        case "Subject":
129                                return { study, criterion -> return study.subjects?.collect { criterion.getFieldValue( it ); } }
130                        case "Sample":
131                                return { study, criterion -> return study.samples?.collect { criterion.getFieldValue( it ); } }
132                        case "Event":
133                                return { study, criterion -> return study.events?.collect { criterion.getFieldValue( it ); } }
134                        case "SamplingEvent":
135                                return { study, criterion -> return study.samplingEvents?.collect { criterion.getFieldValue( it ); } }
136                        case "Assay":
137                                return { study, criterion -> return study.assays?.collect { criterion.getFieldValue( it ); } }
138                        default:
139                                return super.valueCallback( entity );
140                }
141        }
142
143        /**
144         * Returns the saved field data that could be shown on screen. This means, the data
145         * is filtered to show only data of the query results. Also, the study title and sample
146         * name are filtered out, in order to be able to show all data on the screen without
147         * checking further
148         *
149         * @return      Map with the entity id as a key, and a field-value map as value
150         */
151        public Map getShowableResultFields() {
152                Map showableFields = super.getShowableResultFields()
153                showableFields.each { sampleElement ->
154                        sampleElement.value = sampleElement.value.findAll { fieldElement ->
155                                fieldElement.key != "Study title" && fieldElement.key != "Subject species"
156                        }
157                }
158                return showableFields
159        }
160}
Note: See TracBrowser for help on using the repository browser.