Skip to content

Commit cb48dcf

Browse files
fabapp2ashakirin
authored andcommitted
WIP Made button load and send ajax request
1 parent 897fd46 commit cb48dcf

File tree

5 files changed

+92
-20
lines changed

5 files changed

+92
-20
lines changed

applications/rest-service/src/main/java/org/springframework/sbm/ReportController.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
import org.springframework.sbm.engine.context.ProjectContext;
88
import org.springframework.sbm.engine.context.ProjectContextHolder;
99
import org.springframework.stereotype.Controller;
10-
import org.springframework.web.bind.annotation.GetMapping;
11-
import org.springframework.web.bind.annotation.PostMapping;
12-
import org.springframework.web.bind.annotation.RequestParam;
13-
import org.springframework.web.bind.annotation.ResponseBody;
10+
import org.springframework.web.bind.annotation.*;
1411

1512
import java.util.List;
1613

1714
@Controller
15+
@CrossOrigin
1816
class ReportController{
1917

2018
@Autowired
@@ -48,4 +46,21 @@ public String applyRecipes(@RequestParam("recipeNames[]") String[] recipeNames)
4846
}
4947
return reportHolder.getReport();
5048
}
49+
50+
@PostMapping(path = "/spring-boot-upgrade")
51+
@ResponseBody
52+
public void applyRecipes2(@RequestBody String recipeNames) {
53+
/*
54+
ProjectContext context = contextHolder.getProjectContext();
55+
List.of(recipeNames).forEach(recipeName -> applyCommand.execute(context, recipeName));
56+
applyCommand.execute(context, "boot-2.7-3.0-upgrade-report2");
57+
58+
*/
59+
try {
60+
Thread.sleep(5000);
61+
} catch (InterruptedException e) {
62+
throw new RuntimeException(e);
63+
}
64+
}
65+
5166
}

applications/rest-service/src/main/java/org/springframework/sbm/SpringBootUpgradeReportApp.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import org.springframework.boot.SpringApplication;
1919
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
22+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
2023

2124
/**
2225
* @author Fabian Krüger
@@ -31,4 +34,14 @@ public static void main(String[] args) {
3134
exception.printStackTrace();
3235
}
3336
}
37+
38+
@Bean
39+
public WebMvcConfigurer corsConfigurer() {
40+
return new WebMvcConfigurer() {
41+
@Override
42+
public void addCorsMappings(CorsRegistry registry) {
43+
registry.addMapping("/**").allowedMethods("*");
44+
}
45+
};
46+
}
3447
}

applications/rest-service/src/main/java/org/springframework/sbm/SpringBootUpgradeReportStringRenderer.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,18 @@ public void processReport(String renderedReport) {
3535
String closingHeadTag = "</head>";
3636

3737
String additionalHeader =
38-
"<link rel=\"stylesheet\" href=\"css/button.css\">\n" +
39-
"<script src=\"js/setup2.js\"></script>\n" +
40-
"<script src=\"js/site2.js\"></script>\n" +
41-
"<script src=\"js/java.min.js\"></script>\n";
42-
htmlReport = htmlReport.replace(closingHeadTag, additionalHeader + closingHeadTag);
38+
// "<link rel=\"stylesheet\" href=\"css/button.css\">\n" +
39+
// "<script src=\"js/setup2.js\"></script>\n" +
40+
// "<script src=\"js/site2.js\"></script>\n" +
41+
42+
// "<script src=\"js/java.min.js\"></script>\n";
43+
// htmlReport = htmlReport.replace(closingHeadTag, additionalHeader + closingHeadTag);
4344

4445
htmlReport = htmlReport.replace("</body>", """
4546
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
4647
<script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script>
4748
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
48-
<script src="/js/recipe.js"></script>
49+
<script src="js/recipe.js"></script>
4950
</body>
5051
""");
5152
reportHolder.setReport(htmlReport);

applications/rest-service/src/main/resources/static/js/recipe.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class State {
1212

1313
startedRunningRecipe() {
1414
this.runningARecipe = true;
15+
// disable all other buttons
16+
console.log($("button"))
1517
this.notifyListeners();
1618
}
1719

@@ -42,7 +44,7 @@ function runRecipe(element) {
4244

4345
$.ajax({
4446
type: "POST",
45-
url: "http://localhost:8080/apply-recipe",
47+
url: "http://localhost:8080/spring-boot-upgrade",
4648
data: JSON.stringify({
4749
recipe: $(element).attr('recipe')
4850
}),
@@ -55,21 +57,48 @@ function runRecipe(element) {
5557
});
5658
}
5759

58-
function runAllRecipe() {
60+
/**
61+
*
62+
*/
63+
function applyRecipes(btn) {
5964

65+
console.log("applying recipes: " + $(btn).attr('recipe'));
6066
if (state.isRunningARecipe()) {
6167
return;
6268
}
63-
state.startedRunningRecipe();
69+
70+
console.log(this)
6471

6572
$.ajax({
6673
type: "POST",
67-
url: "http://localhost:8080/apply-all-recipe",
74+
url: "http://localhost:8080/spring-boot-upgrade",
75+
dataType: "json",
76+
contentType: 'application/json',
77+
data: JSON.stringify({
78+
recipes: $(btn).attr('recipe')
79+
}),
80+
beforeSend: function() {
81+
console.log("before")
82+
state.startedRunningRecipe();
83+
},
84+
success: function() {
85+
// remove this section from the report
86+
jQuery(this).closest('h3').next().remove();
87+
88+
},
89+
error: function() {
90+
// mark red flashlights / play alarm sound
91+
}
6892
}).always(function () {
93+
console.log("finished recipes")
6994
state.completedRunningRecipe();
7095
});
7196
}
7297

98+
$( document ).ajaxStart(function() {
99+
$( ".log" ).text( "Triggered ajaxStart handler." );
100+
});
101+
73102

74103
state.registerListeners(function () {
75104

@@ -92,17 +121,18 @@ function changeAllRecipesButtonState() {
92121

93122
const runAllRecipeInLoadingState = '<button type="button" class="btn btn-primary" id="recipeButton" ' +
94123
'name="apply-all-recipes-button" action="apply-all-recipes" ' +
95-
'onclick="runAllRecipe()" disabled>' +
124+
'onclick="applyRecipes()" disabled>' +
96125
'Run All Recipes' +
97126
' <span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>\n' +
98127
' <span class="visually-hidden">Loading...</span>' +
99128
'</button>';
100129

101130
const runAllRecipeInNormalState = '<button type="button" class="btn btn-primary" id="recipeButton" ' +
102131
'name="apply-all-recipes-button" action="apply-all-recipes" ' +
103-
'onclick="runAllRecipe()">' +
132+
'onclick="applyRecipes()">' +
104133
'Run All Recipes' +
105134
'</button>';
135+
106136
$(document).ready(function () {
107137
$(".run-all-recipe").html(runAllRecipeInNormalState);
108138

@@ -114,7 +144,7 @@ const runRecipeNormalState = function () {
114144
const div = $(this);
115145
div.html('<button type="button" class="btn btn-primary" id="recipeButton" ' +
116146
'name="apply-recipe-button" recipe="' + div.attr('recipe') + '" ' +
117-
'onclick="runRecipe(this)">' +
147+
'onclick="applyRecipes(this)">' +
118148
'Run Recipe' +
119149
'</button>');
120150
});

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/report/SpringBootUpgradeReportSection.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,25 @@ private void renderRemediationPossibility(StringBuilder sb, RemediationPossibili
254254

255255
if(p.getRecipe() != null) {
256256
sb.append(ls).append(ls);
257-
String buttonCode = """
258-
++++
257+
/*
258+
<!--
259259
<form name="apply-<RECIPE>-form" action="http://localhost:8080/spring-boot-upgrade" method="post">
260-
<input type="hidden" name="recipeNames[0]" value="<RECIPE>" />
260+
<input type="hidden" name="recipeNames[0]" value="<RECIPE>" />
261261
<button name="<RECIPE>" type="submit" class="recipeButton">Run Recipe</button>
262+
262263
</form>
264+
-->
265+
266+
<button type="button" class="btn btn-primary recipeButton" onclick="applyRecipes('<RECIPE>')">
267+
Run All Recipes
268+
<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
269+
<span class="visually-hidden">Loading...</span>
270+
</button>
271+
*/
272+
String buttonCode = """
273+
++++
274+
<div class="run-a-recipe" recipe="<RECIPE>">
275+
</div>
263276
++++
264277
""";
265278
buttonCode = buttonCode.replace("<RECIPE>", p.getRecipe());

0 commit comments

Comments
 (0)