source: trunk/grails-app/controllers/HomeController.groovy

Last change on this file was 1859, checked in by work@…, 13 years ago
  • Property svn:keywords set to Rev Author Date
File size: 7.2 KB
Line 
1import dbnp.studycapturing.*
2import dbnp.authentication.*
3import org.codehaus.groovy.grails.commons.GrailsApplication
4import org.codehaus.groovy.grails.commons.ConfigurationHolder
5import grails.converters.JSON
6import org.dbnp.gdt.Template
7
8/**
9 * Home Controler
10 *
11 * My Description
12 *
13 * @author  Kees van Bochove
14 * @since   20091102
15 * @package studycapturing
16 *
17 * Revision information:
18 * $Rev: 1859 $
19 * $Author: work@osx.eu $
20 * $Date: 2011-05-23 10:19:55 +0000 (ma, 23 mei 2011) $
21 */
22class HomeController {
23        def springSecurityService
24        def authenticationService
25        def dataSource
26        def gdtService
27
28        def index = {
29                // got a nostats parameter?
30                if (params.get('nostats')) {
31                        session.nostats = (params.get('nostats') == "true") ? true : false
32                }
33
34                // create sql instance for advanced queries
35                def config = ConfigurationHolder.config
36                def sql = new groovy.sql.Sql(dataSource)
37                def db  = config.dataSource.driverClassName
38
39                // get study statistics
40                def user = authenticationService.getLoggedInUser()
41                def studyCount                                          = Study.count()
42                def readableStudyCount                          = Study.countReadableStudies(user)
43                def readableAndWritableStudyCount       = Study.countReadableAndWritableStudies(user)
44                def readOnlyStudyCount                          = (readableStudyCount - readableAndWritableStudyCount)
45                def noAccessStudyCount                          = studyCount - readableAndWritableStudyCount
46                def publishedPublicStudyCount           = Study.countPublicStudies(true)
47                def unPublishedPublicStudyCount         = Study.countPublicStudies(false)
48                def publicStudyCount                            = publishedPublicStudyCount + unPublishedPublicStudyCount
49                def publishedPrivateStudyCount          = Study.countPrivateStudies(true)
50                def unPublishedPrivateStudyCount        = Study.countPrivateStudies(false)
51                def privateStudyCount                           = publishedPrivateStudyCount + unPublishedPrivateStudyCount
52
53                // daily statistics
54                def startDate, endDate, date, userTotal, studyTotal, templateTotal
55                def dailyStatistics = [:]
56                if (db == "org.postgresql.Driver" && studyCount > 0) {
57                        //sql.eachRow("SELECT b.*,(select cast( now() - '120 day'::interval * random() as date) FROM Template c WHERE c.id=b.id) as newDate FROM Template b WHERE b.id IN (SELECT id FROM Template a)") {sql.execute(sprintf("UPDATE Template SET date_created='%s' WHERE id=%s", it.newDate, it.id))}
58                        def studiesPerDay       = sql.rows("SELECT DISTINCT date_trunc('day', a.date_created) as day, (SELECT count(b.*) FROM study b WHERE date_trunc('day', b.date_created) = date_trunc('day', a.date_created)) as count FROM study a ORDER BY day ASC")
59                        def usersPerDay         = sql.rows("SELECT DISTINCT date_trunc('day', a.date_created) as day, (SELECT count(b.*) FROM sec_user b WHERE date_trunc('day', b.date_created) = date_trunc('day', a.date_created)) as count FROM sec_user a ORDER BY day ASC")
60                        def templatesPerDay     = sql.rows("SELECT DISTINCT date_trunc('day', a.date_created) as day, (SELECT count(b.*) FROM template b WHERE date_trunc('day', b.date_created) = date_trunc('day', a.date_created)) as count FROM template a ORDER BY day ASC")
61
62                        // combine daily statistics
63                        dailyStatistics = [:]
64                        long oneDay             = (1 * 24 * 60 * 60 * 1000)
65                        startDate               = (usersPerDay[0].day <= studiesPerDay[0].day) ? usersPerDay[0].day : studiesPerDay[0].day
66                        startDate               = (templatesPerDay[0].day != null && templatesPerDay[0].day < startDate) ? templatesPerDay[0].day : startDate
67                        endDate                 = (usersPerDay[usersPerDay.size()-1].day >= studiesPerDay[studiesPerDay.size()-1].day) ? usersPerDay[usersPerDay.size()-1].day : studiesPerDay[studiesPerDay.size()-1].day
68                        endDate                 = (templatesPerDay[0].day != null && templatesPerDay[templatesPerDay.size()-1].day > endDate) ? templatesPerDay[templatesPerDay.size()-1].day : endDate
69                        date                    = startDate.clone()
70
71                        userTotal               = 0
72                        studyTotal              = 0
73                        templateTotal   = 0
74                        while (date <= endDate) {
75                                def userDay             = usersPerDay.find{ it.day == date }
76                                def studyDay    = studiesPerDay.find{ it.day == date }
77                                def templateDay = templatesPerDay.find{ it.day == date }
78                                def users               = (userDay) ? userDay.count : 0
79                                def studies             = (studyDay) ? studyDay.count : 0
80                                def templates   = (templateDay) ? templateDay.count : 0
81
82                                userTotal += users
83                                studyTotal += studies
84                                templateTotal += templates
85
86                                dailyStatistics[ date.clone() ] = [
87                                        users                   : users,
88                                        userTotal               : userTotal,
89                                        studies                 : studies,
90                                        studyTotal              : studyTotal,
91                                        templates               : templates,
92                                        templateTotal   : templateTotal
93                                ]
94                                date.setTime(date.getTime() + oneDay)
95                        }
96                }
97       
98                [
99                        // config
100                        showstats                                       : (session?.nostats) ? !session.nostats : true,
101
102                        // daily statistics
103                        startDate                                       : startDate,
104                        dailyStatistics                         : dailyStatistics,
105
106                        // study
107                        studyCount                                      : studyCount,
108                        publishedPublicStudyCount       : publishedPublicStudyCount,
109                        unPublishedPublicStudyCount     : unPublishedPublicStudyCount,
110                        publicStudyCount                        : publicStudyCount,
111                        publishedPrivateStudyCount      : publishedPrivateStudyCount,
112                        unPublishedPrivateStudyCount: unPublishedPrivateStudyCount,
113                        privateStudyCount                       : privateStudyCount,
114                        readOnlyStudyCount                      : readOnlyStudyCount,
115                        readWriteStudyCount                     : readableAndWritableStudyCount,
116                        noAccessStudyCount                      : noAccessStudyCount,
117                        readableTemplates                       : org.dbnp.gdt.Template.count(),
118
119                        // miscelaneous
120                        facebookLikeUrl                         : '/',
121                        db                                                      : db
122                ]
123        }
124
125        /**
126         * Quicksearch Closure
127         */
128        def ajaxQuickSearch = {
129                def query       = params.name_startsWith
130                def result      = [ total: 0, data: [] ]
131                def user        = authenticationService.getLoggedInUser()
132
133                // search studies
134                Study.textSearchReadableStudies(user,query).each { study ->
135                        result.data << [
136                                link            : createLink(controller:'study', action:'show', id:study.id),
137                            name                : "${study.title}",
138                                category        : 'Study'
139                        ]
140                }
141
142                // search templates
143                Template.createCriteria().listDistinct {
144                        or {
145                                ilike("name", "%${query}%")
146                                ilike("description", "%${query}%")
147                        }
148                }.each { template ->
149                        def entityName = template.entity.toString().split(/\./)
150                        def encodedEntity = gdtService.encryptEntity(template.entity.toString()).decodeURL()
151
152                        result.data << [
153                                link            : createLink(controller:'templateEditor', action:'template', params:[entity:encodedEntity, standalone:true, template:template.id]),
154                            name                : "${template.name}",
155                                category        : "${entityName[entityName.size()-1]} Template"
156                        ]
157                }
158
159                // set total
160                result.total = result.data.size()
161
162                // got results?
163                if (!result.total) {
164                        result.data << [
165                            link        : '',
166                                name    : "no results",
167                                category: ""
168                        ]
169                }
170
171                // set output header to json
172                response.contentType = 'application/json'
173
174                // render result
175                if (params.callback) {
176                        render "${params.callback}(${result as JSON})"
177                } else {
178                        render result as JSON
179                }
180        }
181
182        /**
183         * Log the user in as admin and jump to the setup wizard
184         */
185        def setup = {
186                def config      = ConfigurationHolder.config
187                def db          = config.dataSource.driverClassName
188                def user        = authenticationService.getLoggedInUser()
189
190                // are we using the in-memory database in a non-development environment?
191                if (db == "org.hsqldb.jdbcDriver" && grails.util.GrailsUtil.environment == GrailsApplication.ENV_DEVELOPMENT) {
192                        // log in as administrator
193                        springSecurityService.reauthenticate(
194                                config.authentication.users.admin.username,
195                                config.authentication.users.admin.password
196                        )
197
198                        // and jump to the setup controller
199                        redirect(controller:"setup")
200                }
201
202                redirect(controller:"home")
203        }
204}
Note: See TracBrowser for help on using the repository browser.