Revision 6 hci/trunk/eneraptor-web-app/grails-app/controllers/com/eneraptor/hci/ConfigController.groovy
ConfigController.groovy | ||
---|---|---|
1 | 1 |
package com.eneraptor.hci |
2 | 2 |
|
3 |
class ConfigController { |
|
3 |
import groovy.sql.* |
|
4 |
import java.sql.* |
|
5 |
|
|
6 |
class ConfigController { |
|
7 |
|
|
8 |
def dataSource |
|
4 | 9 |
|
5 | 10 |
def main = { } |
6 | 11 |
|
7 | 12 |
def hci = { |
8 |
|
|
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 |
|
|
9 | 27 |
def allDevices = DeviceInfo.list() |
10 |
[conf: InternalConfig, devices: allDevices] |
|
28 |
[conf: InternalConfig, devices: allDevices, totalSpace: totalSpace, freeSpace: freeSpace, percent: percent]
|
|
11 | 29 |
} |
12 | 30 |
|
13 | 31 |
def addDevice = { |
... | ... | |
15 | 33 |
|
16 | 34 |
def addDeviceSave = { |
17 | 35 |
def newDevice = new DeviceInfo(params) |
18 |
if(newDevice.validate()) { |
|
36 |
if(newDevice.validate() && (params.inputDevice || params.outputDevice)) {
|
|
19 | 37 |
newDevice.save() |
20 | 38 |
flash['confirms'] = "<p>New device added successfully.</p>" |
21 | 39 |
redirect(action:'hci') |
22 | 40 |
} else { |
23 | 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>" |
|
24 | 44 |
redirect(action:'addDevice', params: params) |
25 | 45 |
} |
26 | 46 |
} |
27 | 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 |
|
|
28 | 99 |
def logic = { |
29 | 100 |
|
30 | 101 |
[conf: InternalConfig] |
... | ... | |
53 | 124 |
|
54 | 125 |
} |
55 | 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 |
|
|
56 | 162 |
def getHelp = { |
57 | 163 |
[what : params.what] |
58 | 164 |
} |
Also available in: Unified diff