Statistics
| Revision:

root / hci / trunk / eneraptor-web-app / grails-app / controllers / com / eneraptor / hci / ConfigController.groovy @ 6

History | View | Annotate | Download (4.21 KB)

1
package com.eneraptor.hci
2

    
3
import groovy.sql.*
4
import java.sql.*
5

    
6
class ConfigController {
7
        
8
        def dataSource
9

    
10
    def main = { }
11
        
12
        def hci = {
13
                
14
                def db_sql = new Sql(dataSource)
15
                
16
                def single = db_sql.firstRow("show data_directory")
17
                def loca = single.data_directory
18
                def partition = new File(loca)
19
                def totalSpace = partition.getTotalSpace()
20
                def freeSpace  = partition.getFreeSpace()
21
                
22
                totalSpace = (int)((totalSpace/1024)/1024)
23
                freeSpace = (int)((freeSpace/1024)/1024)
24
                
25
                def percent = 100 - ((int) ((freeSpace/totalSpace)*100))
26
                
27
                def allDevices = DeviceInfo.list()
28
                [conf: InternalConfig, devices: allDevices, totalSpace: totalSpace, freeSpace: freeSpace, percent: percent]                
29
        }
30
        
31
        def addDevice = {
32
        }
33
        
34
        def addDeviceSave = {
35
                def newDevice = new DeviceInfo(params)
36
                if(newDevice.validate() && (params.inputDevice || params.outputDevice)) {
37
                        newDevice.save()
38
                        flash['confirms'] = "<p>New device added successfully.</p>"
39
                        redirect(action:'hci')
40
                } else {
41
                        flash['errors'] = "<p>New device was not saved. Check input data.</p>"
42
                        if(!params.inputDevice && !params.outputDevice)
43
                                flash['errors'] += "<p>Device must be capable of making masurements, actions or both. You have selected none.</p>"
44
                        redirect(action:'addDevice', params: params)
45
                }
46
        }
47
        
48
        def deleteHciEntry = {
49
                
50
                def deviceToBeDeleted = DeviceInfo.findById(params.id)
51
                
52
                if(deviceToBeDeleted) {
53
                        deviceToBeDeleted.delete()
54
                        flash['confirms'] = "<p>Device deleted succesfully.</p>"
55
                } else {
56
                        flash['errors'] = "<p>There was no device deleted.</p><p>There was no device with id ${params.id}.</p>"
57
                }
58
                redirect(action: 'hci')
59
                
60
        }
61
        
62
        def changeHciEntry = {
63
                
64
                def deviceToBeChanged = DeviceInfo.findById(params.id)
65
                if(!deviceToBeChanged) {
66
                        flash['errors'] = "<p>There is no device with id ${params.id}.</p>"
67
                        redirect(action: 'hci')
68
                        return false
69
                }
70
                deviceToBeChanged.getProperties()
71
    }
72
        
73
        def changeHciEntrySave = {
74
                
75
                def deviceToBeUpdated = DeviceInfo.findById(params.id)
76
                deviceToBeUpdated.properties = params
77
                if(deviceToBeUpdated.save()) {
78
                        flash['confirms'] = "<p>Device updated succesfully.</p>"
79
                        redirect(action: 'hci')
80
                } else {
81
                        flash['errors'] = "<p>Device was not changed.</p>"
82
                        redirect(action:'changeHciEntry', params: params)
83
                }
84
                
85
        }
86
        
87
        def dbInfo = {
88
                
89
                List databaseInfo = new ArrayList()
90
                def db_sql = new Sql(dataSource)
91
                db_sql.eachRow "SHOW ALL", {
92
                        databaseInfo << it.toRowResult()
93
                }
94
                
95
                [databaseInfo: databaseInfo]
96
                
97
        }
98
        
99
        def logic = {
100
                
101
                [conf: InternalConfig]
102
                
103
        }
104
        
105
        def connection = {
106
                
107
                [conf: InternalConfig]
108
                
109
        }
110
        
111
        def saveConnection = {
112
                
113
                if(params['logic-ip'] == 'localhost') params['logic-ip'] = '127.0.0.1'
114
                
115
                if(params['logic-ip'] && params['logic-port']) {
116
                        InternalConfig.findByConfigId('logic-ip').configVal = params['logic-ip']
117
                        InternalConfig.findByConfigId('logic-port').configVal = params['logic-port']
118
                        flash['confirms'] = "<p>Changes saved.</p>"
119
                        redirect(action: 'connection')
120
                } else {
121
                        flash['errors'] = "<p>Logic IP address and port fields cannot be blank.</p>"
122
                        redirect(action: 'connection')
123
                }
124
                
125
        }
126
        
127
        def dbExec = {
128
                
129
                if(params['sqlQuery']) {
130
                        
131
                        flash['confirms'] = null
132
                        flash['errors'] = null
133
                        
134
                        def db_sql = new Sql(dataSource)
135
                        List rows
136
                        List keys = new ArrayList()
137
                        boolean wasError = false
138
                        try {
139
                                rows = db_sql.rows(params['sqlQuery'])
140
                                rows[0].each {
141
                                        keys << it.key
142
                                }
143
                                flash['confirms'] = "<p>SQL query executed succesfully. Chrck returned rows below.</p>"
144
                        } catch (SQLException e) {
145
                                rows = new ArrayList()
146
                                if(e.getSQLState().equals("02000")) {
147
                                        keys << 'SQL query result'
148
                                        rows << ['SQL query result' : 'No rows were returned.']
149
                                        flash['confirms'] = "<p>SQL query executed succesfully.</p>"
150
                                } else {
151
                                        rows << e
152
                                        rows << e.getSQLState()
153
                                        wasError = true
154
                                        flash['errors'] = "<p>There was an error while executing the SQL query. Check the report below.</p><p>The query was not executed</p>"
155
                                }
156
                        }
157
                        return [sqlResult: rows, keys: keys, wasError: wasError]
158
                }
159
                
160
        }
161
        
162
        def getHelp = {
163
                [what : params.what]
164
        }
165
        
166
}