Skip to content

Commit 153b2c4

Browse files
committed
2 parents 2918928 + c294346 commit 153b2c4

File tree

6 files changed

+170
-27
lines changed

6 files changed

+170
-27
lines changed

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,76 @@
1616
*/
1717
package com.ericsson.ei.frontend;
1818

19+
import java.io.BufferedInputStream;
20+
import java.io.BufferedReader;
1921
import java.io.File;
2022
import java.io.FileInputStream;
2123
import java.io.FileNotFoundException;
2224
import java.io.IOException;
2325
import java.io.InputStream;
26+
import java.io.InputStreamReader;
27+
import java.io.StringWriter;
28+
import java.net.URL;
29+
import java.nio.charset.Charset;
30+
import java.nio.charset.StandardCharsets;
31+
import java.nio.file.Files;
32+
import java.nio.file.Path;
33+
import java.nio.file.Paths;
2434

2535
import javax.servlet.ServletContext;
2636
import javax.servlet.http.HttpServletRequest;
2737
import javax.servlet.http.HttpServletResponse;
2838

39+
import org.omg.IOP.Encoding;
40+
import org.springframework.beans.factory.annotation.Autowired;
2941
import org.springframework.beans.factory.annotation.Value;
3042
import org.springframework.boot.context.properties.ConfigurationProperties;
43+
import org.springframework.core.io.Resource;
44+
import org.springframework.core.io.ResourceLoader;
3145
import org.springframework.stereotype.Controller;
3246
import org.springframework.util.FileCopyUtils;
3347
import org.springframework.web.bind.annotation.RequestMapping;
3448
import org.springframework.web.bind.annotation.RequestMethod;
3549
import org.springframework.web.bind.annotation.ResponseBody;
50+
51+
import com.google.common.io.CharStreams;
52+
import com.sun.glass.ui.Application;
53+
import org.apache.tomcat.util.http.fileupload.IOUtils;
54+
3655
import org.springframework.web.bind.annotation.RequestBody;
3756

3857

3958
@Controller
4059
public class SubscriptionFilesController {
4160

61+
@Autowired
62+
private ResourceLoader resourceLoader;
63+
64+
@Autowired
65+
ServletContext servletContext;
66+
4267
@Value("${ei.subscriptionFilePath}") private String subscriptionFilePath;
4368
private static final String APPLICATION_JSON = "application/json";
4469

4570

4671
@RequestMapping(value = "/download/subscriptiontemplate", method = RequestMethod.GET, produces = APPLICATION_JSON)
47-
public @ResponseBody void getSubscriptionJsonTemplate(HttpServletResponse response) throws IOException {
48-
File file = getFile(subscriptionFilePath);
49-
InputStream in = new FileInputStream(file);
72+
public void getSubscriptionJsonTemplate(HttpServletResponse response) throws IOException {
5073

51-
response.setContentType(APPLICATION_JSON);
52-
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
53-
response.setHeader("Content-Length", String.valueOf(file.length()));
54-
FileCopyUtils.copy(in, response.getOutputStream());
74+
InputStream is = null;
75+
try {
76+
is = getClass().getResourceAsStream("/subscriptionsTemplate.json");
77+
} catch (NullPointerException e) {
78+
System.out.println("ERROR: " + e.getMessage());
79+
}
80+
81+
try {
82+
IOUtils.copy(is, response.getOutputStream());
83+
response.getOutputStream().flush();
84+
} catch (IOException e) {
85+
System.out.println("Error :- " + e.getMessage());
86+
}
87+
5588
}
56-
57-
58-
// @RequestMapping(value = "/upload/subscriptions", method = RequestMethod.GET, produces = APPLICATION_JSON)
59-
// public @ResponseBody void validateAndCreateSubscriptions(@RequestBody File subscriptionJsonFile) throws IOException {
60-
//
61-
//
62-
// }
6389

6490

6591
private File getFile(String filePath) throws FileNotFoundException {

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ei.backendServiceHost=localhost
1111
ei.backendServicePort=8090
1212

1313
###### Subscription Template file ########
14-
ei.subscriptionFilePath=./src/main/resources/subscriptions/subscriptionsTemplate.json
14+
ei.subscriptionFilePath=subscriptions/subscriptionsTemplate.json
1515

1616

1717

src/main/resources/static/assets/jquery-ui/jquery-ui.min.css

Lines changed: 33 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jQuery(document).ready(function() {
5555
}
5656
// /Stop ## Global AJAX Sender function ##################################
5757

58-
58+
var checkEiBackend = false;
5959
// Check EI Backend Server Status ########################################
6060
function checkEiBackendServer() {
6161
var EIConnBtn = document.getElementById("btnEIConnection");
@@ -74,14 +74,31 @@ jQuery(document).ready(function() {
7474
// console.log("EI BACKEND ONLINE");
7575
var green="#00ff00";
7676
EIConnBtn.style.background = green;
77+
checkEiBackend=true;
7778
},
7879
complete: function (XMLHttpRequest, textStatus) {
7980
}
8081
});
8182

8283
}
83-
84-
84+
function getInstanceInfo() {
85+
var text = document.getElementById('info_text');
86+
$.ajax({
87+
url: backendServiceUrl + "/information",
88+
contentType : 'application/json;charset=UTF-8',
89+
type: 'GET',
90+
error : function (XMLHttpRequest, textStatus, errorThrown) {
91+
document.getElementById('info_text').innerHTML = errorThrown;
92+
},
93+
success : function (data, textStatus, xhr) {
94+
95+
},
96+
complete: function (XMLHttpRequest, textStatus) {
97+
var s = JSON.parse(XMLHttpRequest.responseText);
98+
text.innerHTML = JSON.stringify(s,undefined, 2);
99+
}
100+
});
101+
}
85102
// Check if EI Backend Server is online every X seconds
86103
window.setInterval(function(){
87104
checkEiBackendServer();
@@ -95,6 +112,23 @@ jQuery(document).ready(function() {
95112

96113
checkEiBackendServer();
97114
});
115+
116+
$('.container').on( 'click', 'button.btnEIInstanceInfo', function () {
117+
if(checkEiBackend){
118+
getInstanceInfo();
119+
var modal = document.getElementById('modal_info');
120+
var span = document.getElementsByClassName("close_instance")[0];
121+
modal.style.display = "block";
122+
span.onclick = function() {
123+
modal.style.display = "none";
124+
}
125+
window.onclick = function(event) {
126+
if (event.target == modal) {
127+
modal.style.display = "none";
128+
}}}else{
129+
alert("EI BACKEND OFFLINE");
130+
}
131+
});
98132
// END OF EI Backend Server check #########################################
99133

100134

@@ -380,12 +414,49 @@ jQuery(document).ready(function() {
380414
event.stopPropagation();
381415
event.preventDefault();
382416

383-
function validateSubscriptionFormat(subscriptionFile) {
417+
function tryToCreateSubscription(subscriptionJson) {
384418
// Send Subscription JSON file to Spring MVC
385-
419+
// AJAX Callback handling
420+
var callback = {
421+
beforeSend : function () {
422+
},
423+
success : function (data, textStatus) {
424+
425+
var returnData = [data];
426+
if (returnData.length > 0) {
427+
$.jGrowl("Successful created subscription " + subscriptionJson.subscriptionName, {
428+
sticky : false,
429+
theme : 'Error'
430+
});
431+
reload_table();
432+
}
433+
434+
},
435+
error : function (XMLHttpRequest, textStatus, errorThrown) {
436+
437+
$.jGrowl("Failed to create Subscription: " + subscriptionJson.subscriptionName + " Error: " + XMLHttpRequest.responseText, {
438+
sticky : false,
439+
theme : 'Error'
440+
});
441+
442+
},
443+
complete : function () {
444+
}
445+
};
446+
447+
// Fetch Date and format
448+
var now = new Date();
449+
var nowStr = now.format("isoDate") + ' ' + now.format("isoTime");
450+
451+
// Update property created with datetime (formatted)
452+
subscriptionJson.created = String(nowStr);
453+
454+
// Perform AJAX
455+
var ajaxHttpSender = new AjaxHttpSender();
456+
ajaxHttpSender.sendAjax(backendServiceUrl + "/subscriptions", "POST", ko.toJSON(subscriptionJson), callback);
386457
}
387458

388-
function validateJsonandSubscriptionFormat(subscriptionFile){
459+
function validateJsonAndCreateSubscriptions(subscriptionFile){
389460

390461
var reader = new FileReader();
391462

@@ -405,7 +476,10 @@ jQuery(document).ready(function() {
405476
theme : 'Notify'
406477
});
407478

408-
validateSubscriptionFormat(subscriptionFile);
479+
var subscriptionJsonList = JSON.parse(fileContent);
480+
for (i=0; i < subscriptionJsonList.length; i++) {
481+
tryToCreateSubscription(subscriptionJsonList[i]);
482+
}
409483
};
410484

411485
reader.readAsText(subscriptionFile);
@@ -430,7 +504,7 @@ jQuery(document).ready(function() {
430504
// All other Web Browser
431505
pom.onchange = function uploadFinished() {
432506
var subscriptionFile = pom.files[0];
433-
validateJsonandSubscriptionFormat(subscriptionFile);
507+
validateJsonAndCreateSubscriptions(subscriptionFile);
434508
};
435509

436510
if (document.createEvent) {
@@ -521,8 +595,12 @@ jQuery(document).ready(function() {
521595
var indexToRemove = context.$index();
522596

523597
// Removing Requirement(Condition), based on index position, from Requirement form in Add_Subscription window.
524-
vm.subscription()[0].requirements.splice(indexToRemove,1);
525-
598+
if (indexToRemove > 0 ){
599+
vm.subscription()[0].requirements.splice(indexToRemove,1);
600+
}
601+
else {
602+
$.alert("You need to have atleast one Condition.");
603+
}
526604
});
527605
// /Stop ## Delete Condition ################################################
528606

src/main/resources/templates/index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141

4242
<div class="container">
4343
<h1 style="font-size:20pt">Eiffel Intelligence Subscription Handling (<span th:text="${backendServiceUrl}"></span>)
44-
<button data-toggle="tooltip" title="EI connection status check is refreshed continuesly. Click button to force check status." type="button" id="btnEIConnection" class="btn btnEIConnectionStatus"><i class="glyphicon"></i>EI Backend Status</button>
44+
<button data-toggle="tooltip" title="EI connection status check is refreshed continuesly. Click button to force check status." type="button" id="btnEIConnection" class="btn btnEIConnectionStatus"><i class="glyphicon"></i>EI Backend Status</button>
45+
<button data-toggle="tooltip" title="Eiffel intelligence instance information" type="button" id="btnInfo" class="btn btnEIInstanceInfo"><i class="glyphicon"></i> EI Backend Instance Info</button>
4546
</h1>
4647

4748
<h3>Subscription Data</h3>
@@ -190,6 +191,12 @@ <h3 class="modal-title text-center">Subscription Form</h3>
190191
</div><!-- /.modal-content -->
191192
</div><!-- /.modal-dialog -->
192193
</div><!-- /.modal -->
194+
<div id = "modal_info" class="modal_instance">
195+
<div class="modal_info-content">
196+
<span class="close_instance">&times;</span>
197+
<pre id="info_text"></pre>
198+
</div>
199+
</div>
193200
<!-- End Bootstrap modal -->
194201

195202

0 commit comments

Comments
 (0)