Statistics
| Revision:

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

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