- Timestamp:
- May 23, 2011, 4:36:00 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/SimpleWizardController.groovy
r1838 r1864 37 37 */ 38 38 def index = { 39 // if( params.id )40 // redirect( action: "simpleWizard", id: params.id );41 // else42 // redirect( action: "simpleWizard" );43 39 redirect action: 'simpleWizard', params: params 44 40 } … … 50 46 if (!flow.study) retrievalError() 51 47 52 flow.inferDesign = params.inferDesign53 48 // Search for studies 54 49 flow.studies = Study.giveWritableStudies( authenticationService.getLoggedInUser(), 100 ) … … 824 819 def errors = []; 825 820 826 if (flow.inferDesign) { 827 828 println 'Entered infer design...' 829 830 // find the indices of the classes of interest in the records 831 def sampleIdx = table[0].findIndexOf{it.class.name == 'dbnp.studycapturing.Sample'} 832 def samplingEventIdx = table[0].findIndexOf{it.class.name == 'dbnp.studycapturing.SamplingEvent'} 833 def subjectIdx = table[0].findIndexOf{it.class.name == 'dbnp.studycapturing.Subject'} 834 835 // Check for duplicate samples 836 def samples = table.collect{it[sampleIdx]} 837 838 def uniques = [] as Set 839 def duplicates = [] as Set 840 841 // this approach separates the unique from the duplicate entries 842 samples*.name.each { 843 uniques.add(it) || duplicates.add(it) 844 } 845 846 duplicates.each{ duplicateName -> 847 samples.findAll{it.name == duplicateName}.each{ sample -> 848 numInvalidEntities++ 849 failedcells = addNonValidatingCells(failedcells, sample, flow) 850 errors += "(Sample) duplicate name: $duplicateName" 851 } 852 } 853 854 // A closure that returns a sub list of entities from a list that have 855 // unique values of a property indicated by propertyName 856 def uniqueEntitiesByProperty = { entities, propertyName -> 857 858 entities*."$propertyName".unique().collect { uniquePropertyValue -> 859 860 entities.find{ it."$propertyName" == uniquePropertyValue } 861 862 } 863 } 864 865 def addToCollectionIfNonexistent = { parent, collectionName, entity, propertyName -> 866 867 if (!parent[collectionName].find{it[propertyName] == entity[propertyName]}) 868 parent."addTo${collectionName.capitalize()}" entity 869 870 } 871 872 // collect unique subjects and sampling events from table 873 def uniqueSubjects = 874 uniqueEntitiesByProperty(table.collect{it[subjectIdx]}, 'name') 875 uniqueSubjects.each{ 876 addToCollectionIfNonexistent study, 'subjects', it, 'name' 877 it.species = Term.findByName('Homo sapiens') 878 } 879 880 def uniqueSamplingEvents = 881 uniqueEntitiesByProperty(table.collect{it[samplingEventIdx]}, 'startTime') 882 uniqueSamplingEvents.each{ 883 it.setFieldValue( 'sampleTemplate', flow.sampleForm.template.Sample.name ) 884 } 885 886 // create an event group for each unique sampling event (not much of a group, is it ...) 887 def eventGroups = uniqueSamplingEvents.collect{ 888 889 def eventGroupName = "Sampling_${it.sampleTemplate.name}_${new RelTime(it.startTime).toString()}" 890 891 def eventGroup = study.eventGroups.find{it.name == eventGroupName} ?: //EventGroup.findByParentAndName(study, eventGroupName) ?: 892 new EventGroup(name: eventGroupName) 893 894 eventGroup.addToSamplingEvents it 895 896 if (!study.eventGroups.find{it == eventGroup}) 897 study.addToEventGroups eventGroup 898 899 if (!it.parent) study.addToSamplingEvents it 900 901 println eventGroup.name 902 903 eventGroup 904 905 } 906 907 table.each{ record -> 908 909 Sample sample = record[sampleIdx] 910 911 // gather all sample related entities 912 def correspondingSamplingEvent = uniqueSamplingEvents.find {it.startTime == record[samplingEventIdx].startTime} 913 def correspondingSubject = uniqueSubjects.find{it.name == record[subjectIdx].name} 914 def correspondingEventGroup = eventGroups.find{correspondingSamplingEvent in it.samplingEvents} 915 916 addToCollectionIfNonexistent correspondingSamplingEvent, 'samples', sample, 'name' 917 918 sample.parentSubject = correspondingSubject 919 920 correspondingEventGroup.addToSamplingEvents correspondingSamplingEvent 921 922 if (!correspondingEventGroup.subjects.find{it.name == correspondingSubject.name}) 923 correspondingEventGroup.addToSubjects correspondingSubject 924 925 addToCollectionIfNonexistent study, 'samples', sample, 'name' 926 927 } 928 929 } else { 930 // Add all samples 931 table.each { record -> 932 record.each { entity -> 933 if( entity ) { 934 // Determine entity class and add a parent. Add the entity to the study 935 def preferredIdentifier = importerService.givePreferredIdentifier( entity.class ); 936 def equalClosure = { it.getIdentifier() == entity.getIdentifier() } 937 def entityName = entity.class.name[ entity.class.name.lastIndexOf( "." ) + 1 .. -1 ] 938 939 entity.parent = study 940 941 switch( entity.class ) { 942 case Sample: 943 if( !study.samples?.find( equalClosure ) ) { 944 study.addToSamples( entity ); 821 // Add all samples 822 table.each { record -> 823 record.each { entity -> 824 if( entity ) { 825 // Determine entity class and add a parent. Add the entity to the study 826 def preferredIdentifier = importerService.givePreferredIdentifier( entity.class ); 827 def equalClosure = { it.getIdentifier() == entity.getIdentifier() } 828 def entityName = entity.class.name[ entity.class.name.lastIndexOf( "." ) + 1 .. -1 ] 829 830 entity.parent = study 831 832 switch( entity.class ) { 833 case Sample: 834 if( !study.samples?.find( equalClosure ) ) { 835 study.addToSamples( entity ); 836 } 837 838 // If an eventgroup is created, add it to the study 839 // The eventgroup must have a unique name, but the user shouldn't be bothered with it 840 // Add 'group ' + samplename and it that is not unique, add a number to it 841 if( entity.parentEventGroup ) { 842 study.addToEventGroups( entity.parentEventGroup ) 843 844 entity.parentEventGroup.name = "Group " + entity.name 845 while( !entity.parentEventGroup.validate() ) { 846 //entity.parentEventGroup.getErrors().each { println it } 847 entity.parentEventGroup.name += "" + Math.floor( Math.random() * 100 ) 945 848 } 946 947 // If an eventgroup is created, add it to the study 948 // The eventgroup must have a unique name, but the user shouldn't be bothered with it 949 // Add 'group ' + samplename and it that is not unique, add a number to it 950 if( entity.parentEventGroup ) { 951 study.addToEventGroups( entity.parentEventGroup ) 952 953 entity.parentEventGroup.name = "Group " + entity.name 954 while( !entity.parentEventGroup.validate() ) { 955 //entity.parentEventGroup.getErrors().each { println it } 956 entity.parentEventGroup.name += "" + Math.floor( Math.random() * 100 ) 849 } 850 851 break; 852 case Subject: 853 if( !study.samples?.find( equalClosure ) ) { 854 855 if( preferredIdentifier ) { 856 // Subjects without a name should just be called 'subject' 857 if( !entity.getFieldValue( preferredIdentifier.name ) ) 858 entity.setFieldValue( preferredIdentifier.name, "Subject" ); 859 860 // Subjects should have unique names; if the user has entered the same name multiple times, 861 // the subject will be renamed 862 def baseName = entity.getFieldValue( preferredIdentifier.name ) 863 def counter = 2; 864 865 while( study.subjects?.find { it.getFieldValue( preferredIdentifier.name ) == entity.getFieldValue( preferredIdentifier.name ) } ) { 866 entity.setFieldValue( preferredIdentifier.name, baseName + " (" + counter++ + ")" ) 957 867 } 958 868 } 959 869 960 break; 961 case Subject: 962 if( !study.samples?.find( equalClosure ) ) { 963 964 if( preferredIdentifier ) { 965 // Subjects without a name should just be called 'subject' 966 if( !entity.getFieldValue( preferredIdentifier.name ) ) 967 entity.setFieldValue( preferredIdentifier.name, "Subject" ); 968 969 // Subjects should have unique names; if the user has entered the same name multiple times, 970 // the subject will be renamed 971 def baseName = entity.getFieldValue( preferredIdentifier.name ) 972 def counter = 2; 973 974 while( study.subjects?.find { it.getFieldValue( preferredIdentifier.name ) == entity.getFieldValue( preferredIdentifier.name ) } ) { 975 entity.setFieldValue( preferredIdentifier.name, baseName + " (" + counter++ + ")" ) 976 } 977 } 978 979 study.addToSubjects( entity ); 980 981 } 982 983 break; 984 case Event: 985 if( !study.events?.find( equalClosure ) ) { 986 study.addToEvents( entity ); 987 } 988 break; 989 case SamplingEvent: 990 // Sampling events have a 'sampleTemplate' value, which should be filled by the 991 // template that is chosen for samples. 992 if( !entity.getFieldValue( 'sampleTemplate' ) ) { 993 entity.setFieldValue( 'sampleTemplate', flow.sampleForm.template.Sample.name ) 994 } 995 996 if( !study.samplingEvents?.find( equalClosure ) ) { 997 study.addToSamplingEvents( entity ); 998 } 999 break; 1000 } 1001 1002 if (!entity.validate()) { 1003 numInvalidEntities++; 1004 1005 // Add this field to the list of failed cells, in order to give the user feedback 1006 failedcells = addNonValidatingCells( failedcells, entity, flow ) 1007 1008 // Also create a full list of errors 1009 def currentErrors = getHumanReadableErrors( entity ) 1010 if( currentErrors ) { 1011 currentErrors.each { 1012 errors += "(" + entityName + ") " + it.value; 1013 } 870 study.addToSubjects( entity ); 871 872 } 873 874 break; 875 case Event: 876 if( !study.events?.find( equalClosure ) ) { 877 study.addToEvents( entity ); 878 } 879 break; 880 case SamplingEvent: 881 // Sampling events have a 'sampleTemplate' value, which should be filled by the 882 // template that is chosen for samples. 883 if( !entity.getFieldValue( 'sampleTemplate' ) ) { 884 entity.setFieldValue( 'sampleTemplate', flow.sampleForm.template.Sample.name ) 885 } 886 887 if( !study.samplingEvents?.find( equalClosure ) ) { 888 study.addToSamplingEvents( entity ); 889 } 890 break; 891 } 892 893 if (!entity.validate()) { 894 numInvalidEntities++; 895 896 // Add this field to the list of failed cells, in order to give the user feedback 897 failedcells = addNonValidatingCells( failedcells, entity, flow ) 898 899 // Also create a full list of errors 900 def currentErrors = getHumanReadableErrors( entity ) 901 if( currentErrors ) { 902 currentErrors.each { 903 errors += "(" + entityName + ") " + it.value; 1014 904 } 1015 905 }
Note: See TracChangeset
for help on using the changeset viewer.