Index: ActionsController.groovy
===================================================================
--- ActionsController.groovy	(revision 57)
+++ ActionsController.groovy	(revision 58)
@@ -1,11 +1,148 @@
 package com.eneraptor.hci
 
+import grails.converters.*
+
 class ActionsController {
-
+
+	def hciLogicCommService
+	
     def main = { }
 	
-	def view = { }
+	def view = {
+		
+		[includeToOnLoadDo: true]
+		
+	}
 	
+	def getActions = {
+		
+		String reqStr = "<?xml version=\"1.0\"?><hci-request type=\"getScenarios\"></hci-request>"
+		Map reply = hciLogicCommService.getReplyToRequest(reqStr)
+		
+		if(reply['status'] == 'error' || reply['content'] == null) {
+			return [replyError: reply['content']]
+		} else {
+			def filteredResponse = reply['content'].minus("<?xml version=\"1.0\"?>")
+			//System.out.println("[eneraptor-hci] Recieved reply (filtered): " + filteredResponse)
+			def rootNode = XML.parse(filteredResponse)
+			if(rootNode.@type == "getScenarios") {
+				List scenarios = new ArrayList()
+				rootNode.scenariotree.scenario.each {
+					scenarios << [it.@id.text(),it.@name.text(),it.inDevice.@id.text(),it.outDevice.@id.text()]
+				}
+				[scenarios:scenarios]
+			} else {
+				return [replyError: "The response is invalid. Check logic's IP and port numbers."]
+			}
+		}
+				
+	}
+	
 	def add = { }
+	
+	def viewAction = {
+		
+		String reqStr = "<?xml version=\"1.0\"?><hci-request type=\"getScenarios\"></hci-request>"
+		Map reply = hciLogicCommService.getReplyToRequest(reqStr)
+		
+		if(reply['status'] == 'error' || reply['content'] == null) {
+			flash['errors'] = "<p>Error connecting with logic: " + reply['content'] + "</p>"
+			redirect(action:'view')
+			return false
+		} else {
+			def filteredResponse = reply['content'].minus("<?xml version=\"1.0\"?>")
+			def rootNode = XML.parse(filteredResponse)
+			if(rootNode.@type == "getScenarios") {
+				def scenarioNode = rootNode.scenariotree[0].scenario.find {
+					it.@id.text() == params['actionId']
+				}
+				if(scenarioNode == null) {
+					flash['errors'] = "<p>Could not find a scanario with the id " + params['actionId'] + ".</p>"
+					redirect(action:'view')
+					return false
+				} else {
+					Map scenario = new HashMap()
+					scenario['id'] = scenarioNode.@id.text()
+					scenario['name'] = scenarioNode.@name.text()
+					scenario['inDevice'] = scenarioNode.inDevice.@id.text()
+					scenario['outDevice'] = scenarioNode.outDevice.@id.text()
+					scenario['reaction'] = scenarioNode.reaction.@func.text()
+					scenario['check_idle'] = scenarioNode.check_idle.@val.text()
+					scenario['check_active'] = scenarioNode.check_active.@val.text()
+					scenario['alpha'] = scenarioNode.alpha.@val.text()
+					scenario['params'] = scenarioNode.params.@val.text()
+					scenario['minVal'] = scenarioNode.inDevAalues.@minval.text()
+					scenario['maxVal'] = scenarioNode.inDevAalues.@maxval.text()
+					scenario['optVal'] = scenarioNode.inDevAalues.@optval.text()
+					scenario['tolerance'] = scenarioNode.inDevAalues.@tolerance.text()
+					scenario['action'] = scenarioNode.action.@act.text()
+					scenario['param'] = scenarioNode.param.@par.text()
+					
+					return [scenario:scenario]
+				}
+			} else {
+				flash['errors'] = "<p>Error connecting with logic: The response is invalid. Check logic's IP and port numbers.</p>"
+				redirect(action:'view')
+				return false
+			}
+		}
+		
+	}
+	
+	def deleteAction = {
+		
+		// First part: get XML from logic and modify
+		
+		String reqStr = "<?xml version=\"1.0\"?><hci-request type=\"getScenarios\"></hci-request>"
+		Map reply = hciLogicCommService.getReplyToRequest(reqStr)
+		
+		if(reply['status'] == 'error' || reply['content'] == null) {
+			flash['errors'] = "<p>Error connecting with logic: " + reply['content'] + "</p>"
+			redirect(action:'view')
+			return false
+		} else {
+			def filteredResponse = reply['content'].minus("<?xml version=\"1.0\"?>")
+			def rootNode = new groovy.util.XmlParser().parseText(filteredResponse)
+			if(rootNode.@type == "getScenarios") {
+				def scenarioTree = rootNode.scenariotree[0]
+				def nodeToRemove = scenarioTree.scenario.find {
+					it.@id == params['actionId']
+				}
+				scenarioTree.remove(nodeToRemove)
+				
+				def writer = new StringWriter()
+				new XmlNodePrinter(new PrintWriter(writer)).print(scenarioTree)
+				def result = writer.toString()
+				
+				result = "<?xml version=\"1.0\"?><hci-request type=\"setScenarios\">" + result + "</hci-request>"
+				
+				// Second part: give modified XML to logic
+				
+				Map reply2 = hciLogicCommService.getReplyToRequest(result)
+				
+				if(reply2['status'] == 'error' || reply2['content'] == null) {
+					flash['errors'] = "<p>Error connecting with logic: " + reply2['content'] + "</p>"
+					redirect(action:'view')
+					return false
+				} else {
+					def rootNode2 = XML.parse(reply2['content'])
+					if(rootNode2.@type.text() == "setScenarios") {
+						flash['confirms'] = "<p>Scenario deleted successfully.</p>"
+						redirect(action:'view')
+						return true
+					} else {
+						flash['errors'] = "<p>Error connecting with logic: The response is invalid. Check logic's IP and port numbers.</p>"
+						redirect(action:'view')
+						return false
+					}
+				}
+				
+			} else {
+				flash['errors'] = "<p>Error connecting with logic: The response is invalid. Check logic's IP and port numbers.</p>"
+				redirect(action:'view')
+				return false
+			}
+		}
+	}
 	
 }
