Skip to content

Commit 1d22cb9

Browse files
author
eznedan
committed
ADD: select in main page
FIX: radio buttons
1 parent dc774a0 commit 1d22cb9

File tree

7 files changed

+144
-55
lines changed

7 files changed

+144
-55
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public interface BackEndInformationController {
3333
@RequestMapping(value = "/switch-backend", method = RequestMethod.POST)
3434
ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request);
3535

36+
@RequestMapping(value = "/switch-backendByMainPage", method = RequestMethod.POST)
37+
ResponseEntity<String> switchBackEndInstanceByMainPage(Model model, HttpServletRequest request);
38+
3639
@RequestMapping(value = "/switch-backend", method = RequestMethod.DELETE)
3740
ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequest request);
3841

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@
1818

1919
import com.ericsson.ei.frontend.model.BackEndInformation;
2020
import com.ericsson.ei.frontend.utils.BackEndInstancesUtils;
21+
import com.google.gson.Gson;
22+
import com.google.gson.JsonArray;
2123
import com.google.gson.JsonObject;
2224
import com.google.gson.JsonParser;
25+
import com.google.gson.reflect.TypeToken;
2326
import org.springframework.beans.factory.annotation.Autowired;
2427
import org.springframework.http.HttpStatus;
2528
import org.springframework.http.ResponseEntity;
2629
import org.springframework.stereotype.Component;
2730
import org.springframework.ui.Model;
2831

2932
import javax.servlet.http.HttpServletRequest;
33+
import java.util.ArrayList;
34+
import java.util.List;
3035
import java.util.stream.Collectors;
3136

3237
@Component
@@ -75,7 +80,7 @@ public ResponseEntity<String> addInstanceInformation(Model model, HttpServletReq
7580
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
7681
JsonObject instance = new JsonParser().parse(body).getAsJsonObject();
7782
if (!utils.checkIfInstanceAlreadyExist(instance)) {
78-
instance.addProperty("checked", false);
83+
instance.addProperty("default", false);
7984
utils.getInstances().add(instance);
8085
utils.writeIntoFile();
8186
return new ResponseEntity<>(HttpStatus.OK);
@@ -86,4 +91,28 @@ public ResponseEntity<String> addInstanceInformation(Model model, HttpServletReq
8691
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
8792
}
8893
}
94+
95+
public ResponseEntity<String> switchBackEndInstanceByMainPage(Model model, HttpServletRequest request) {
96+
try {
97+
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
98+
List<BackEndInformation> info = new ArrayList<>();
99+
for (BackEndInformation backEndInformation : utils.getInformation()) {
100+
backEndInformation.setActive(false);
101+
if (backEndInformation.getName().equals(body)) {
102+
utils.setBackEndProperties(backEndInformation);
103+
backEndInformation.setActive(true);
104+
}
105+
info.add(backEndInformation);
106+
}
107+
utils.setInformation(info);
108+
JsonArray result = (JsonArray) new Gson().toJsonTree(utils.getInformation(), new TypeToken<List<BackEndInformation>>() {
109+
}.getType());
110+
utils.setInstances(result);
111+
utils.writeIntoFile();
112+
utils.parseBackEndInstancesFile();
113+
return new ResponseEntity<>(HttpStatus.OK);
114+
} catch (Exception e) {
115+
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
116+
}
117+
}
89118
}

src/main/java/com/ericsson/ei/frontend/utils/BackEndInstancesUtils.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818

1919
import com.ericsson.ei.frontend.model.BackEndInformation;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
21-
import com.google.gson.JsonArray;
22-
import com.google.gson.JsonElement;
23-
import com.google.gson.JsonObject;
24-
import com.google.gson.JsonParser;
21+
import com.google.gson.*;
22+
import com.google.gson.reflect.TypeToken;
2523
import lombok.Getter;
2624
import lombok.Setter;
2725
import org.slf4j.Logger;
@@ -75,19 +73,22 @@ public class BackEndInstancesUtils {
7573

7674
@PostConstruct
7775
public void init() {
76+
if (eiInstancesPath.equals("")) {
77+
setEiInstancesPath(PATH_TO_WRITE);
78+
}
7879
parseBackEndInstancesFile();
7980
if (!checkIfInstanceAlreadyExist(getCurrentInstance())) {
8081
instances.add(getCurrentInstance());
8182
}
82-
if (eiInstancesPath.equals("")) {
83-
setEiInstancesPath(PATH_TO_WRITE);
84-
}
8583
writeIntoFile();
84+
information.clear();
85+
information = new Gson().fromJson(instances,new TypeToken<List<BackEndInformation>>() {
86+
}.getType());
8687
}
8788

8889
private JsonObject getCurrentInstance() {
8990
JsonObject instance = new JsonObject();
90-
instance.addProperty(NAME, "core");
91+
instance.addProperty(NAME, "default");
9192
instance.addProperty(HOST, host);
9293
instance.addProperty(PORT, port);
9394
instance.addProperty(PATH, path);
@@ -107,7 +108,7 @@ public void setBackEndProperties(BackEndInformation properties) {
107108
public boolean checkIfInstanceAlreadyExist(JsonObject instance) {
108109
for (JsonElement element : instances) {
109110
if (element.getAsJsonObject().get(HOST).equals(instance.get(HOST)) &&
110-
element.getAsJsonObject().get(PORT).equals(instance.get(PORT)) &&
111+
element.getAsJsonObject().get(PORT).getAsInt() == instance.get(PORT).getAsInt() &&
111112
element.getAsJsonObject().get(PATH).equals(instance.get(PATH))) {
112113
return true;
113114
}
@@ -116,30 +117,26 @@ public boolean checkIfInstanceAlreadyExist(JsonObject instance) {
116117
}
117118

118119
public void writeIntoFile() {
119-
if (eiInstancesPath != null) {
120-
try {
121-
FileWriter fileWriter = new FileWriter(eiInstancesPath);
122-
fileWriter.append(instances.toString());
123-
fileWriter.flush();
124-
} catch (IOException e) {
125-
LOG.error("Couldn't add instance to file " + e.getMessage());
126-
}
120+
try {
121+
FileWriter fileWriter = new FileWriter(eiInstancesPath);
122+
fileWriter.append(instances.toString());
123+
fileWriter.flush();
124+
} catch (IOException e) {
125+
LOG.error("Couldn't add instance to file " + e.getMessage());
127126
}
128127
}
129128

130129
public void parseBackEndInstancesFile() {
131-
if (eiInstancesPath != null) {
132-
try {
133-
information.clear();
134-
instances = new JsonArray();
135-
JsonArray inputBackEndInstances = new JsonParser().parse(new String(Files.readAllBytes(Paths.get(eiInstancesPath)))).getAsJsonArray();
136-
for (JsonElement element : inputBackEndInstances) {
137-
information.add(new ObjectMapper().readValue(element.toString(), BackEndInformation.class));
138-
instances.add(element);
139-
}
140-
} catch (IOException e) {
141-
LOG.error("Failure when try to parse json file" + e.getMessage());
130+
try {
131+
information.clear();
132+
instances = new JsonArray();
133+
JsonArray inputBackEndInstances = new JsonParser().parse(new String(Files.readAllBytes(Paths.get(eiInstancesPath)))).getAsJsonArray();
134+
for (JsonElement element : inputBackEndInstances) {
135+
information.add(new ObjectMapper().readValue(element.toString(), BackEndInformation.class));
136+
instances.add(element);
142137
}
138+
} catch (IOException e) {
139+
LOG.error("Failure when try to parse json file" + e.getMessage());
143140
}
144141
}
145142
}

src/main/resources/static/js/main.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,56 @@ jQuery(document).ready(function() {
8383

8484
initOneTime();
8585

86+
function singleInstanceModel(name, host, port, path, https, active) {
87+
this.name = ko.observable(name),
88+
this.host = ko.observable(host),
89+
this.port = ko.observable(port),
90+
this.path = ko.observable(path),
91+
this.https = ko.observable(https),
92+
this.active = ko.observable(active),
93+
this.information = name.toUpperCase() + " - " + host + " " + port + " " + path;
94+
}
95+
96+
function viewModel(data) {
97+
var self = this;
98+
var currentName;
99+
self.instances = ko.observableArray();
100+
var json = JSON.parse(data);
101+
for(var i = 0; i < json.length; i++) {
102+
var obj = json[i];
103+
var instance = new singleInstanceModel(obj.name, obj.host, obj.port, obj.path, obj.https, obj.active);
104+
self.instances.push(instance);
105+
if(obj.active == true){
106+
currentName = obj.name;
107+
}
108+
}
109+
self.selectedActive = ko.observable(currentName);
110+
self.onChange = function(){
111+
$.ajax({
112+
url: frontendServiceUrl + "/switch-backendByMainPage",
113+
type: "POST",
114+
data: self.selectedActive(),
115+
contentType: 'application/json; charset=utf-8',
116+
cache: false,
117+
error: function (XMLHttpRequest, textStatus, errorThrown) {
118+
$.jGrowl(XMLHttpRequest.responseText, {sticky: false, theme: 'Error'});
119+
},
120+
success: function (responseData, textStatus) {
121+
$.jGrowl("Backend instance was switched", {sticky: false, theme: 'Notify'});
122+
}
123+
});
124+
}
125+
}
126+
$.ajax({
127+
url: frontendServiceUrl + "/get-instances",
128+
type: "GET",
129+
contentType: 'application/json; charset=utf-8',
130+
cache: false,
131+
error: function (XMLHttpRequest, textStatus, errorThrown) {
132+
$.jGrowl(XMLHttpRequest.responseText, {sticky: false, theme: 'Error'});
133+
},
134+
success: function (responseData, textStatus) {
135+
ko.applyBindings(new viewModel(responseData));
136+
}
137+
});
86138
});

src/main/resources/static/js/switch-instances.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
jQuery(document).ready(function() {
2+
var frontendServiceUrl = $('#frontendServiceUrl').text();
3+
var sendbtn = document.getElementById('switcher').disabled = true;
24
function singleInstanceModel(name, host, port, path, https, active) {
35
this.name = ko.observable(name),
46
this.host = ko.observable(host),
@@ -9,17 +11,23 @@ function singleInstanceModel(name, host, port, path, https, active) {
911
}
1012
function multipleInstancesModel(data) {
1113
var self = this;
14+
var selected;
1215
self.instances = ko.observableArray();
1316
var json = JSON.parse(data);
1417
for(var i = 0; i < json.length; i++) {
1518
var obj = json[i];
1619
var instance = new singleInstanceModel(obj.name, obj.host, obj.port, obj.path, obj.https, obj.active);
1720
self.instances.push(instance);
1821
}
22+
self.checked = function(){
23+
var sendbtn = document.getElementById('switcher').disabled = false;
24+
selected = JSON.parse(ko.toJSON(this));
25+
return true;
26+
}
1927
self.removeInstance = function() {
2028
self.instances.remove(this);
2129
$.ajax({
22-
url: "/switch-backend",
30+
url: frontendServiceUrl + "/switch-backend",
2331
type: "DELETE",
2432
data: ko.toJSON(self.instances),
2533
contentType: 'application/json; charset=utf-8',
@@ -34,36 +42,35 @@ function multipleInstancesModel(data) {
3442
});
3543
}
3644
self.submit = function(instances) {
37-
var count = 0;
3845
var json = JSON.parse(ko.toJSON(instances));
46+
self.instances.removeAll();
3947
for(var i = 0; i < json.length; i++){
4048
var obj = json[i];
41-
if(obj.active == true){
42-
count++;
49+
if(obj.active == true &&
50+
!(obj.name == selected.name && obj.host == selected.host && obj.port == selected.port && obj.path == selected.path)){
51+
obj.active = false;
4352
}
53+
var instance = new singleInstanceModel(obj.name, obj.host, obj.port, obj.path, obj.https, obj.active);
54+
self.instances.push(instance);
4455
}
45-
if(count == 1){
46-
$.ajax({
47-
url: "/switch-backend",
48-
type: "POST",
49-
data: ko.toJSON(instances),
50-
contentType: 'application/json; charset=utf-8',
51-
cache: false,
52-
error: function (XMLHttpRequest, textStatus, errorThrown) {
53-
$.jGrowl(XMLHttpRequest.responseText, {sticky: false, theme: 'Error'});
54-
},
55-
success: function (responseData, textStatus) {
56-
$.jGrowl("Backend instance was switched", {sticky: false, theme: 'Notify'});
57-
$("#mainFrame").load("subscriptionpage.html");
58-
}
59-
});
60-
} else {
61-
$.jGrowl("Please choose one backend instance", {sticky: false, theme: 'Error'});
62-
}
56+
$.ajax({
57+
url: frontendServiceUrl + "/switch-backend",
58+
type: "POST",
59+
data: ko.toJSON(self.instances),
60+
contentType: 'application/json; charset=utf-8',
61+
cache: false,
62+
error: function (XMLHttpRequest, textStatus, errorThrown) {
63+
$.jGrowl(XMLHttpRequest.responseText, {sticky: false, theme: 'Error'});
64+
},
65+
success: function (responseData, textStatus) {
66+
$.jGrowl("Backend instance was switched", {sticky: false, theme: 'Notify'});
67+
$(location).attr('href', frontendServiceUrl)
68+
}
69+
});
6370
}
6471
}
6572
$.ajax({
66-
url: "/get-instances",
73+
url: frontendServiceUrl + "/get-instances",
6774
type: "GET",
6875
contentType: 'application/json; charset=utf-8',
6976
cache: false,

src/main/resources/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
3737
<span class="navbar-toggler-icon"></span>
3838
</button>
39+
<select data-bind="options: instances, optionsText: 'information', optionsValue: 'name', value: selectedActive, event: {change: onChange}" ></select>
3940
<div class="collapse navbar-collapse" id="navbarResponsive">
4041
<ul class="navbar-nav navbar-sidenav" id="exampleAccordion">
4142
<li id="subscriptionBtn" class="nav-item" data-toggle="tooltip" data-placement="right" title="Eiffel Subscription">

src/main/resources/templates/switch-backend.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
</thead>
2727
<tbody data-bind="foreach: instances">
2828
<tr>
29-
<td align="center"><input align="center" id="activeInstance" type="checkbox"
30-
data-bind="checked: active"/></td>
29+
<td align="center"><input align="center" name="activate" type="radio"
30+
data-bind="checked: active, checkedValue: true, click: $parent.checked"/></td>
3131
<td data-bind="text: name" align="center"></td>
3232
<td data-bind="text: host" align="center"></td>
3333
<td data-bind="text: port" align="center"></td>
@@ -37,7 +37,7 @@
3737
</tr>
3838
</tbody>
3939
</table>
40-
<button type="button" class="btn btn-primary btn-block"
40+
<button id="switcher" type="button" class="btn btn-primary btn-block"
4141
data-bind="click: function(){$root.submit($root.instances);}">Submit
4242
</button>
4343
</div>

0 commit comments

Comments
 (0)