1 | import dbnp.studycapturing.* |
---|
2 | import dbnp.authentication.* |
---|
3 | import org.codehaus.groovy.grails.commons.GrailsApplication |
---|
4 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
5 | import grails.converters.JSON |
---|
6 | import 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: 1585 $ |
---|
19 | * $Author: s.h.sikkema@gmail.com $ |
---|
20 | * $Date: 2011-03-03 19:24:14 +0000 (do, 03 mrt 2011) $ |
---|
21 | */ |
---|
22 | class HomeController { |
---|
23 | def authenticationService |
---|
24 | def dataSource |
---|
25 | def gdtService |
---|
26 | |
---|
27 | def index = { |
---|
28 | // create sql instance for advanced queries |
---|
29 | def config = ConfigurationHolder.config |
---|
30 | def sql = new groovy.sql.Sql(dataSource) |
---|
31 | def db = config.dataSource.driverClassName |
---|
32 | |
---|
33 | // get study statistics |
---|
34 | def user = authenticationService.getLoggedInUser() |
---|
35 | def studyCount = Study.count() |
---|
36 | def readableStudyCount = Study.countReadableStudies(user) |
---|
37 | def readableAndWritableStudyCount = Study.countReadableAndWritableStudies(user) |
---|
38 | def readOnlyStudyCount = (readableStudyCount - readableAndWritableStudyCount) |
---|
39 | def noAccessStudyCount = studyCount - readableAndWritableStudyCount |
---|
40 | def publishedPublicStudyCount = Study.countPublicStudies(true) |
---|
41 | def unPublishedPublicStudyCount = Study.countPublicStudies(false) |
---|
42 | def publicStudyCount = publishedPublicStudyCount + unPublishedPublicStudyCount |
---|
43 | def publishedPrivateStudyCount = Study.countPrivateStudies(true) |
---|
44 | def unPublishedPrivateStudyCount = Study.countPrivateStudies(false) |
---|
45 | def privateStudyCount = publishedPrivateStudyCount + unPublishedPrivateStudyCount |
---|
46 | |
---|
47 | // daily statistics |
---|
48 | def startDate, endDate, date, userTotal, studyTotal, templateTotal |
---|
49 | def dailyStatistics = [:] |
---|
50 | if (db == "org.postgresql.Driver") { |
---|
51 | //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))} |
---|
52 | 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") |
---|
53 | 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") |
---|
54 | 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") |
---|
55 | |
---|
56 | // combine daily statistics |
---|
57 | dailyStatistics = [:] |
---|
58 | long oneDay = (1 * 24 * 60 * 60 * 1000) |
---|
59 | startDate = (usersPerDay[0].day <= studiesPerDay[0].day) ? usersPerDay[0].day : studiesPerDay[0].day |
---|
60 | startDate = (templatesPerDay[0].day != null && templatesPerDay[0].day < startDate) ? templatesPerDay[0].day : startDate |
---|
61 | endDate = (usersPerDay[usersPerDay.size()-1].day >= studiesPerDay[studiesPerDay.size()-1].day) ? usersPerDay[usersPerDay.size()-1].day : studiesPerDay[studiesPerDay.size()-1].day |
---|
62 | endDate = (templatesPerDay[0].day != null && templatesPerDay[templatesPerDay.size()-1].day > endDate) ? templatesPerDay[templatesPerDay.size()-1].day : endDate |
---|
63 | date = startDate.clone() |
---|
64 | |
---|
65 | userTotal = 0 |
---|
66 | studyTotal = 0 |
---|
67 | templateTotal = 0 |
---|
68 | while (date <= endDate) { |
---|
69 | def userDay = usersPerDay.find{ it.day == date } |
---|
70 | def studyDay = studiesPerDay.find{ it.day == date } |
---|
71 | def templateDay = templatesPerDay.find{ it.day == date } |
---|
72 | def users = (userDay) ? userDay.count : 0 |
---|
73 | def studies = (studyDay) ? studyDay.count : 0 |
---|
74 | def templates = (templateDay) ? templateDay.count : 0 |
---|
75 | |
---|
76 | userTotal += users |
---|
77 | studyTotal += studies |
---|
78 | templateTotal += templates |
---|
79 | |
---|
80 | dailyStatistics[ date.clone() ] = [ |
---|
81 | users : users, |
---|
82 | userTotal : userTotal, |
---|
83 | studies : studies, |
---|
84 | studyTotal : studyTotal, |
---|
85 | templates : templates, |
---|
86 | templateTotal : templateTotal |
---|
87 | ] |
---|
88 | date.setTime(date.getTime() + oneDay) |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | [ |
---|
93 | // daily statistics |
---|
94 | startDate : startDate, |
---|
95 | dailyStatistics : dailyStatistics, |
---|
96 | |
---|
97 | // study |
---|
98 | studyCount : studyCount, |
---|
99 | publishedPublicStudyCount : publishedPublicStudyCount, |
---|
100 | unPublishedPublicStudyCount : unPublishedPublicStudyCount, |
---|
101 | publicStudyCount : publicStudyCount, |
---|
102 | publishedPrivateStudyCount : publishedPrivateStudyCount, |
---|
103 | unPublishedPrivateStudyCount: unPublishedPrivateStudyCount, |
---|
104 | privateStudyCount : privateStudyCount, |
---|
105 | readOnlyStudyCount : readOnlyStudyCount, |
---|
106 | readWriteStudyCount : readableAndWritableStudyCount, |
---|
107 | noAccessStudyCount : noAccessStudyCount, |
---|
108 | readableTemplates : org.dbnp.gdt.Template.count(), |
---|
109 | |
---|
110 | // miscelaneous |
---|
111 | facebookLikeUrl : '/' |
---|
112 | ] |
---|
113 | } |
---|
114 | |
---|
115 | def ajaxQuickSearch = { |
---|
116 | def query = params.name_startsWith |
---|
117 | def result = [ total: 0, data: [] ] |
---|
118 | def user = authenticationService.getLoggedInUser() |
---|
119 | |
---|
120 | // search studies |
---|
121 | Study.textSearchReadableStudies(user,query).each { study -> |
---|
122 | result.data << [ |
---|
123 | link : createLink(controller:'study', action:'show', id:study.id), |
---|
124 | name : "${study.title}", |
---|
125 | category : 'Study' |
---|
126 | ] |
---|
127 | } |
---|
128 | |
---|
129 | // search templates |
---|
130 | Template.createCriteria().list { |
---|
131 | or { |
---|
132 | ilike("name", "%${query}%") |
---|
133 | ilike("description", "%${query}%") |
---|
134 | } |
---|
135 | }.each { template -> |
---|
136 | def entityName = template.entity.toString().split(/\./) |
---|
137 | def encodedEntity = gdtService.encryptEntity(template.entity.toString()).decodeURL() |
---|
138 | |
---|
139 | result.data << [ |
---|
140 | link : createLink(controller:'templateEditor', action:'template', params:[entity:encodedEntity, standalone:true, template:template.id]), |
---|
141 | name : "${template.name}", |
---|
142 | category : "${entityName[entityName.size()-1]} Template" |
---|
143 | ] |
---|
144 | } |
---|
145 | |
---|
146 | // set total |
---|
147 | result.total = result.data.size() |
---|
148 | |
---|
149 | // render result |
---|
150 | if (params.callback) { |
---|
151 | render "${params.callback}(${result as JSON})" |
---|
152 | } else { |
---|
153 | render result as JSON |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|