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

Last change on this file since 1487 was 1487, checked in by robert@…, 10 years ago
  • Fixed StudySearch? unit tests (#303)
  • Changed css error message to class="errormessage" in order to avoid breaking the study wizard (see r1485)
  • 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: 1487 $
12 * $Author: robert@isdat.nl $
13 * $Date: 2011-02-03 09:51:16 +0000 (do, 03 feb 2011) $
14 */
15package dbnp.query
16
17import java.util.List;
18import java.util.Map;
19
20import dbnp.studycapturing.*
21import org.dbnp.gdt.*
22import org.apache.commons.logging.LogFactory;
23
24class StudySearch extends Search {
25        private static final log = LogFactory.getLog(this);
26       
27        public StudySearch() {
28                super();
29                this.entity = "Study";
30        }
31
32        /**
33         * Searches for studies based on the given criteria. All criteria have to be satisfied and
34         * criteria for the different entities are satisfied as follows:
35         *
36         *              Study.title = 'abc'             
37         *                              All returned studies will have title 'abc'
38         *             
39         *              Subject.species = 'human'
40         *                              All returned studies will have one or more subjects with species = 'human' 
41         *
42         *              Sample.name = 'sample 1'
43         *                              All returned studies will have one or more samples with name = 'sample 1'
44         *
45         *              Event.startTime = '0s'
46         *                              All returned studies will have one or more events with start time = '0s' 
47         *
48         *              Assay.module = 'metagenomics'
49         *                              All returned studies will have one or more assays with module = 'metagenomics' 
50         *
51         * When searching the system doesn't look at the connections between different entities. This means,
52         * the system doesn't look for human subjects having a sample with name 'sample 1'. The sample 1 might
53         * as well belong to a mouse subject and still the study satisfies the criteria.
54         *
55         * When searching for more than one criterion per entity, these are taken combined. Searching for
56         *
57         *              Subject.species = 'human'
58         *              Subject.name = 'Jan'
59         *
60         *  will result in all studies having a human subject named 'Jan'. Studies with only a mouse subject
61         *  named 'Jan' or a human subject named 'Kees' won't satisfy the criteria.
62         *     
63         */
64        @Override
65        void execute() {
66                super.execute();
67
68                def studies = Study.list().findAll { it.canRead( this.user ) };
69
70                // If no criteria are found, return all studies
71                if( !criteria || criteria.size() == 0 ) {
72                        results = studies;
73                        return;
74                }
75
76                // Perform filters
77                studies = filterOnStudyCriteria( studies );
78                studies = filterOnSubjectCriteria( studies );
79                studies = filterOnSampleCriteria( studies );
80                studies = filterOnEventCriteria( studies );
81                studies = filterOnSamplingEventCriteria( studies );
82                studies = filterOnAssayCriteria( studies );
83               
84                studies = filterOnModuleCriteria( studies );
85               
86                // Save matches
87                results = studies;
88        }
89
90        /**
91         * Filters the given list of studies on the study criteria
92         * @param studies       Original list of studies
93         * @return                      List with all studies that match the Study criteria
94         */
95        protected List filterOnStudyCriteria( List studies ) {
96                return filterOnTemplateEntityCriteria(studies, "Study", { study, criterion -> return criterion.getFieldValue( study ) })
97        }
98
99        /**
100         * Filters the given list of studies on the subject criteria
101         * @param studies       Original list of studies
102         * @return                      List with all studies that match the Subject-criteria
103         */
104        protected List filterOnSubjectCriteria( List studies ) {
105                return filterOnTemplateEntityCriteria(studies, "Subject", { study, criterion ->
106                        return study.subjects?.collect { criterion.getFieldValue( it ); }
107                })
108        }
109
110        /**
111         * Filters the given list of studies on the sample criteria
112         * @param studies       Original list of studies
113         * @return                      List with all studies that match the sample-criteria
114         */
115        protected List filterOnSampleCriteria( List studies ) {
116                return filterOnTemplateEntityCriteria(studies, "Sample", { study, criterion ->
117                        return study.samples?.collect { criterion.getFieldValue( it ); }
118                })
119        }
120
121        /**
122         * Filters the given list of studies on the event criteria
123         * @param studies       Original list of studies
124         * @return                      List with all studies that match the event-criteria
125         */
126        protected List filterOnEventCriteria( List studies ) {
127                return filterOnTemplateEntityCriteria(studies, "Event", { study, criterion ->
128                        return study.events?.collect { criterion.getFieldValue( it ); }
129                })
130        }
131
132        /**
133         * Filters the given list of studies on the sampling event criteria
134         * @param studies       Original list of studies
135         * @return                      List with all studies that match the event-criteria
136         */
137        protected List filterOnSamplingEventCriteria( List studies ) {
138                return filterOnTemplateEntityCriteria(studies, "SamplingEvent", { study, criterion ->
139                        return study.samplingEvents?.collect { criterion.getFieldValue( it ); }
140                })
141        }
142
143        /**
144         * Filters the given list of studies on the assay criteria
145         * @param studies       Original list of studies
146         * @return                      List with all studies that match the assay-criteria
147         */
148        protected List filterOnAssayCriteria( List studies ) {
149                return filterOnTemplateEntityCriteria(studies, "Assay", { study, criterion ->
150                        return study.assays?.collect { criterion.getFieldValue( it ); }
151                })
152        }
153
154        /**
155         * Returns the saved field data that could be shown on screen. This means, the data
156         * is filtered to show only data of the query results. Also, the study title and sample
157         * name are filtered out, in order to be able to show all data on the screen without
158         * checking further
159         *
160         * @return      Map with the entity id as a key, and a field-value map as value
161         */
162        public Map getShowableResultFields() {
163                Map showableFields = super.getShowableResultFields()
164                showableFields.each { sampleElement ->
165                        sampleElement.value = sampleElement.value.findAll { fieldElement ->
166                                fieldElement.key != "Study title" && fieldElement.key != "Subject species"
167                        }
168                }
169                return showableFields
170        }
171}
Note: See TracBrowser for help on using the repository browser.