Skip to content

Commit 815ed8a

Browse files
author
eztyhva
committed
Redone with using knockout
1 parent ef18f82 commit 815ed8a

File tree

5 files changed

+236
-114
lines changed

5 files changed

+236
-114
lines changed

src/main/java/com/ericsson/ei/frontend/WebController.java

Lines changed: 80 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package com.ericsson.ei.frontend;
1818

19-
2019
import com.ericsson.ei.frontend.model.BackEndInformation;
2120
import com.ericsson.ei.frontend.model.Index;
2221
import com.ericsson.ei.frontend.model.ListWrapper;
@@ -28,21 +27,22 @@
2827
import org.slf4j.LoggerFactory;
2928
import org.springframework.beans.factory.annotation.Autowired;
3029
import org.springframework.beans.factory.annotation.Value;
30+
import org.springframework.http.HttpStatus;
31+
import org.springframework.http.ResponseEntity;
3132
import org.springframework.stereotype.Controller;
3233
import org.springframework.ui.Model;
33-
import org.springframework.validation.BindingResult;
34-
import org.springframework.web.bind.annotation.ModelAttribute;
3534
import org.springframework.web.bind.annotation.RequestMapping;
3635
import org.springframework.web.bind.annotation.RequestMethod;
3736

3837
import javax.annotation.PostConstruct;
39-
import javax.validation.Valid;
38+
import javax.servlet.http.HttpServletRequest;
4039
import java.io.FileOutputStream;
4140
import java.io.IOException;
4241
import java.nio.file.Files;
4342
import java.nio.file.Paths;
4443
import java.util.ArrayList;
4544
import java.util.List;
45+
import java.util.stream.Collectors;
4646

4747
@Controller
4848
public class WebController {
@@ -87,25 +87,28 @@ public class WebController {
8787

8888
private List<BackEndInformation> information = new ArrayList<>();
8989

90+
private JSONArray instances = new JSONArray();
91+
9092
@PostConstruct
9193
public void init() {
92-
index.setIndex(0);
93-
information.add(backEndInformation);
94-
if (eiInstancesPath != null) {
95-
try {
96-
JSONArray inputBackEndInstances = new JSONArray(new String(Files.readAllBytes(Paths.get(eiInstancesPath))));
97-
for (Object o : inputBackEndInstances) {
98-
JSONObject instance = (JSONObject) o;
99-
BackEndInformation backEndInformations = new ObjectMapper().readValue(instance.toString(), BackEndInformation.class);
100-
if (!checkIfInstanceAlreadyExist(backEndInformations)) {
101-
information.add(backEndInformations);
102-
}
103-
}
104-
} catch (IOException e) {
105-
LOG.error("Failure when try to parse json file" + e.getMessage());
106-
}
107-
}
108-
writeIntoFile();
94+
instances.put(getCurrentInstance());
95+
// index.setIndex(0);
96+
// information.add(backEndInformation);
97+
// if (eiInstancesPath != null) {
98+
// try {
99+
// JSONArray inputBackEndInstances = new JSONArray(new String(Files.readAllBytes(Paths.get(eiInstancesPath))));
100+
// for (Object o : inputBackEndInstances) {
101+
// JSONObject instance = (JSONObject) o;
102+
// BackEndInformation backEndInformations = new ObjectMapper().readValue(instance.toString(), BackEndInformation.class);
103+
// if (!checkIfInstanceAlreadyExist(backEndInformations)) {
104+
// information.add(backEndInformations);
105+
// }
106+
// }
107+
// } catch (IOException e) {
108+
// LOG.error("Failure when try to parse json file" + e.getMessage());
109+
// }
110+
// }
111+
// writeIntoFile();
109112
}
110113

111114
@RequestMapping("/")
@@ -166,70 +169,84 @@ public String jmesPathRulesSetUp(Model model) {
166169
return "jmesPathRulesSetUp";
167170
}
168171

169-
@RequestMapping(value = "/switch-backend", method = RequestMethod.GET)
172+
@RequestMapping("/switch-backend.html")
170173
public String switchBackEnd(Model model) {
171-
wrapper.setBackEndInformation(information);
172-
model.addAttribute("listWrapper", wrapper);
173-
model.addAttribute("index", index);
174174
return "switch-backend";
175175
}
176176

177-
@RequestMapping(params = "switch", value = "/switch-backend", method = RequestMethod.POST)
178-
public String switchBackEndInstance(@ModelAttribute("index") Index index, Model model) {
179-
this.index.setIndex(index.getIndex());
180-
setBackEndProperties(index);
181-
information.set(0, getBackEndProperties());
182-
return "redirect:/";
183-
}
184-
185-
@RequestMapping(params = "delete", value = "/switch-backend", method = RequestMethod.POST)
186-
public String deleteBackEndInstance(@ModelAttribute("index") Index index, Model model) {
187-
information.remove(index.getIndex());
188-
writeIntoFile();
189-
wrapper.setBackEndInformation(information);
190-
model.addAttribute("listWrapper", wrapper);
191-
model.addAttribute("index", index);
192-
return "switch-backend.html";
193-
}
194-
195-
@RequestMapping(value = "/add-instances", method = RequestMethod.GET)
177+
@RequestMapping("/add-instances.html")
196178
public String addInstance(Model model) {
197-
model.addAttribute("backendinformation", new BackEndInformation());
198179
return "add-instances";
199180
}
200181

182+
@RequestMapping(value = "/switch-backend", method = RequestMethod.POST)
183+
public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request) {
184+
try {
185+
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
186+
instances = new JSONArray(body);
187+
LOG.info(instances.toString());
188+
// for(int i = 0; i < instances.length(); i++) {
189+
// if(instances.getJSONObject(i).get("checked").equals(true) {
190+
// instances.getJSONObject(i);
191+
// break;
192+
// }
193+
// }
194+
return new ResponseEntity<>(HttpStatus.OK);
195+
} catch (Exception e) {
196+
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
197+
}
198+
}
199+
201200
@RequestMapping(value = "/add-instances", method = RequestMethod.POST)
202-
public String addInstanceInformation(@Valid @ModelAttribute(value = "backendinformation") BackEndInformation backEndInfo, BindingResult bindingResult) {
203-
if (bindingResult.hasErrors()) {
204-
return "/add-instances";
205-
} else {
206-
information.add(backEndInfo);
207-
if (!checkIfInstanceAlreadyExist(backEndInfo)) {
208-
writeIntoFile();
201+
public ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request) {
202+
try {
203+
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
204+
JSONObject instance = new JSONObject(body);
205+
if(!checkIfInstanceAlreadyExist(instance)) {
206+
instance.put("checked", false);
207+
instances.put(instance);
208+
LOG.info(instances.toString());
209+
return new ResponseEntity<>(HttpStatus.OK);
210+
} else {
211+
return new ResponseEntity<>("Instance already exist", HttpStatus.BAD_REQUEST);
209212
}
213+
} catch (Exception e) {
214+
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
210215
}
211-
return "/switch-backend";
212216
}
213217

214-
private BackEndInformation getBackEndProperties() {
215-
BackEndInformation backEndInformationFromProperties = new BackEndInformation();
216-
backEndInformationFromProperties.setHost(host);
217-
backEndInformationFromProperties.setPort(port);
218-
backEndInformationFromProperties.setPath(path);
219-
backEndInformationFromProperties.setHttps(https);
220-
return backEndInformationFromProperties;
218+
@RequestMapping(value = "/get-instances", method = RequestMethod.GET)
219+
public ResponseEntity<String> getInstances(Model model) {
220+
return new ResponseEntity<>(instances.toString(), HttpStatus.OK);
221221
}
222222

223-
private void setBackEndProperties(Index index) {
223+
private JSONObject getCurrentInstance() {
224+
JSONObject instance = new JSONObject();
225+
instance.put("name", "core");
226+
instance.put("host", host);
227+
instance.put("port", port);
228+
instance.put("path", path);
229+
instance.put("https", https);
230+
instance.put("checked", true);
231+
return instance;
232+
}
233+
234+
private void setBackEndProperties(JSONObject instance) {
224235
backEndInformation.setName(information.get(index.getIndex()).getName());
225236
backEndInformation.setHost(information.get(index.getIndex()).getHost());
226237
backEndInformation.setPort(information.get(index.getIndex()).getPort());
227238
backEndInformation.setPath(information.get(index.getIndex()).getPath());
228239
backEndInformation.setHttps(information.get(index.getIndex()).isHttps());
229240
}
230241

231-
private boolean checkIfInstanceAlreadyExist(BackEndInformation backEndInformation) {
232-
return backEndInformation.getHost().equals(host) && backEndInformation.getPort() == port;
242+
private boolean checkIfInstanceAlreadyExist(JSONObject instance) {
243+
for(int i = 0; i < instances.length(); i++) {
244+
if(instances.getJSONObject(i).get("host").equals(instance.get("host")) &&
245+
instances.getJSONObject(i).get("port").equals(instance.get("port"))) {
246+
return true;
247+
}
248+
}
249+
return false;
233250
}
234251

235252
private void writeIntoFile() {
Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
jQuery(document).ready(function() {
2-
function loadSwitchPage(){
32

4-
$("#mainFrame").load("switch-backend.html");
5-
}
3+
function instanceModel() {
4+
var self = this;
5+
self.instance = {
6+
name: ko.observable(""),
7+
host: ko.observable(""),
8+
port: ko.observable(""),
9+
path: ko.observable(""),
10+
https: ko.observable(false)
11+
};
612

7-
document.getElementById("addBtn").onclick = function() {
13+
self.add = function(instance) {
14+
var data = ko.toJSON(instance);
15+
var dataJSON = JSON.parse(data);
16+
if(dataJSON.host == "" || dataJSON.port == "") {
17+
$.jGrowl("Host and port fields cannot be empty", {sticky: false, theme: 'Error'});
18+
} else {
19+
$.ajax({
20+
url: "/add-instances",
21+
type: "POST",
22+
data: data,
23+
contentType: 'application/json; charset=utf-8',
24+
cache: false,
25+
error: function (XMLHttpRequest, textStatus, errorThrown) {
26+
$.jGrowl(XMLHttpRequest.responseText, {sticky: false, theme: 'Error'});
27+
},
28+
success: function (responseData, textStatus) {
29+
$.jGrowl("Added new backend instance", {sticky: false, theme: 'Notify'});
30+
$("#mainFrame").load("switch-backend.html");
31+
}
32+
});
33+
}
34+
}
35+
}
36+
37+
var observableObject = $("#instanceModel")[0];
38+
ko.cleanNode(observableObject);
39+
ko.applyBindings(new instanceModel(), observableObject);
840

9-
loadSwitchPage();
10-
}
1141
});
Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,82 @@
11
jQuery(document).ready(function() {
2-
function loadSwitchPage(){
32

4-
$("#mainFrame").load("switch-backend.html");
5-
}
3+
function singleInstanceModel(name, host, port, path, https, checked) {
4+
this.name = ko.observable(name),
5+
this.host = ko.observable(host),
6+
this.port = ko.observable(port),
7+
this.path = ko.observable(path),
8+
this.https = ko.observable(https),
9+
this.checked = ko.observable(checked)
10+
}
611

7-
document.getElementById("deleteBtn").onclick = function() {
12+
function multipleInstancesModel(data) {
13+
var self = this;
14+
self.instances = ko.observableArray();
15+
16+
var json = JSON.parse(data);
17+
for(var i = 0; i < json.length; i++) {
18+
var obj = json[i];
19+
var instance = new singleInstanceModel(obj.name, obj.host, obj.port, obj.path, obj.https, obj.checked);
20+
self.instances.push(instance);
21+
}
22+
23+
self.removeInstance = function() {
24+
self.instances.remove(this);
25+
}
26+
27+
self.submit = function(instances) {
28+
$.ajax({
29+
url: "/switch-backend",
30+
type: "POST",
31+
data: ko.toJSON(instances),
32+
contentType: 'application/json; charset=utf-8',
33+
cache: false,
34+
error: function (XMLHttpRequest, textStatus, errorThrown) {
35+
$.jGrowl(XMLHttpRequest.responseText, {sticky: false, theme: 'Error'});
36+
},
37+
success: function (responseData, textStatus) {
38+
$.jGrowl("Backend instance was switched", {sticky: false, theme: 'Notify'});
39+
$("#mainFrame").load("subscriptionpage.html");
40+
}
41+
});
42+
}
43+
}
44+
45+
$.ajax({
46+
url: "/get-instances",
47+
type: "GET",
48+
contentType: 'application/json; charset=utf-8',
49+
cache: false,
50+
error: function (XMLHttpRequest, textStatus, errorThrown) {
51+
$.jGrowl(XMLHttpRequest.responseText, {sticky: false, theme: 'Error'});
52+
},
53+
success: function (responseData, textStatus) {
54+
var observableObject = $("#instancesModel")[0];
55+
ko.cleanNode(observableObject);
56+
ko.applyBindings(new multipleInstancesModel(responseData), observableObject);
57+
58+
}
59+
})
60+
61+
var selectedBox = null;
62+
$(".activeInstance").click(function() {
63+
selectedBox = this.id;
64+
65+
$(".activeInstance").each(function() {
66+
if ( this.id == selectedBox )
67+
{
68+
this.checked = true;
69+
}
70+
else
71+
{
72+
this.checked = false;
73+
};
74+
});
75+
});
76+
77+
// $("#activeInstance").change(function() {
78+
// $("#activeInstance").prop('checked', false);
79+
// $(this).prop('checked', true);
80+
// });
881

9-
loadSwitchPage();
10-
}
11-
var sendbtn = document.getElementById('switcher').disabled = true;
12-
$('input[type="checkbox"]').on('change', function() {
13-
var sendbtn = document.getElementById('switcher').disabled = false;
14-
$('input[name="' + this.name + '"]').not(this).prop('checked', false);
15-
});
1682
});

0 commit comments

Comments
 (0)