I inherited this project, and am new to spring-boot. I can’t seem to figure out why the value of my checkboxes are being returned as true in manCreateGroup, but in manGroupsRedir they are false, which is what I want.
These are the methods I am refering to:
@GetMapping("/gotoman")
public String manGroupsRedir(Model model, RedirectAttributes redir, Authentication auth) {
Group group = new Group();
model.addAttribute("group", group);
MyUserDetails userD = (MyUserDetails) auth.getPrincipal();
Long idNum = userD.getId();
User currentUser = new User();
currentUser = this.userRepository.findById(idNum).orElse(null);
model.addAttribute("currentUser", currentUser);
Company currentCompany=currentUser.getCompany();
List<User> userList = userRepository.findByCompany(currentCompany);
model.addAttribute("userlist", userList);
//will contain users to be added to the group. may use group.reviewees instead
List<User> usersToAdd = new ArrayList<>();
model.addAttribute("usersToAdd", usersToAdd);
List<EvalTemplates> evaltemplates = this.evaluationRepository.findByCompany(currentCompany);
model.addAttribute("evaltemplates", evaltemplates);
Map<Integer, List<String>> syncmap = new HashMap<Integer, List<String>>();
model.addAttribute("syncmap", syncmap);
//initializes all activities as Synchronous for a newly created group.
//this is changed by the user upon creation.
// boolean synctrue[] = new boolean[]{true, true, true, true};
// boolean strueL1 = false;
// boolean strueL2 = false;
// boolean strueCR = false;
// boolean strueFTF = false;
// List<Boolean> synclist = new ArrayList<Boolean>();
// for (int i = 0; i < 4; i++) {
// synclist.add("sync")
// }
// model.addAttribute("strueL1", strueL1);
// model.addAttribute("strueL2", strueL2);
// model.addAttribute("strueCR", strueCR);
// model.addAttribute("strueFTF", strueFTF);
String[] levellist = new String[] {"Level 1", "Level 2", "Consistency Review", "Face to Face"};
Evaluator eval1 = new Evaluator(currentUser, group,
evalRoleRepository.findByNameAndCompany(levellist[0], currentUser.getCompany()), currentUser.getCompany());
Evaluator eval2 = new Evaluator(currentUser, group,
evalRoleRepository.findByNameAndCompany(levellist[1], currentUser.getCompany()), currentUser.getCompany());
Evaluator eval3 = new Evaluator(currentUser, group,
evalRoleRepository.findByNameAndCompany(levellist[2], currentUser.getCompany()), currentUser.getCompany());
Evaluator eval4 = new Evaluator(currentUser, group,
evalRoleRepository.findByNameAndCompany(levellist[3], currentUser.getCompany()), currentUser.getCompany());
model.addAttribute("eval1", eval1);
model.addAttribute("eval2", eval2);
model.addAttribute("eval3", eval3);
model.addAttribute("eval4", eval4);
return "manageGroups";
}
///////////////////////////////////////////////////////////////////////////////
@ResponseBody
@PostMapping("/testing")
public Object manCreateGroup(Model model, @ModelAttribute("group") Group group, @ModelAttribute("eval1") Evaluator eval1, @ModelAttribute("eval2") Evaluator eval2, @ModelAttribute("eval3") Evaluator eval3, @ModelAttribute("eval4") Evaluator eval4, RedirectAttributes redir, Authentication auth) {
User currentUser;
Company currentCompany;
MyUserDetails userD = (MyUserDetails) auth.getPrincipal();
Long idNum = userD.getId();
currentUser = this.userRepository.findById(idNum).orElse(null);
currentCompany = currentUser.getCompany();
//ensure user has correct permissions
if(!currentUser.getRole().getName().equalsIgnoreCase("Evaluator_admin")) {
RedirectView redirectView = new RedirectView("/adminGroups", true);
redir.addFlashAttribute("error", "invalid permissions on user "+currentUser.getName());
log.error("invalid permissions on user "+currentUser.getName()+ " does not have permission to create a eval group or assign evaluator role.");
return redirectView;
}
//test for duplicates of group name and year
//groupRepository.findByGroupName(group.getGroupName());
boolean duplicated = false;
if (!(groupRepository.findGroupsByGroupNameAndYear(group.getGroupName(), group.getYear()).isEmpty())) {
duplicated=true;
}
if(duplicated){
RedirectView redirectView = new RedirectView("/adminGroups", true);
redir.addFlashAttribute("error", "duplicate name and year entered: " + group.getGroupName()
+ ", " + group.getYear());
log.error("duplicate name and year entered: " + group.getGroupName()
+ ", " + group.getYear() + ", this group already exists..");
return redirectView;
}
//group = new Group(currentCompany);
//set group company to curr user company
currentCompany = currentUser.getCompany();
group.setCompany(currentCompany);
//defaults
long lastGroupNumber = groupRepository.count();
int groupNumber = (int) lastGroupNumber + 1;
int level = 0;
int defaultTemplate = 0;
EvalTemplates evalTemplate = evaluationRepository.findByCompany(currentCompany).get(defaultTemplate);
//deleted redundant cast
List<EvalRole> roles = evalRoleRepository.findByCompany(currentCompany);
//Add default evaluator roles if list is empty
if(roles.isEmpty()) {
EvalRole level1 = new EvalRole("Level 1", level, currentCompany);
EvalRole level2 = new EvalRole("Level 2", level++, currentCompany);
EvalRole consistencyReview = new EvalRole("Consistency Review", level++, currentCompany);
EvalRole faceToFace = new EvalRole("Face to face", level++, currentCompany);
roles.add(level1);
roles.add(level2);
roles.add(consistencyReview);
roles.add(faceToFace);
}
List<Evaluator> evaluatorlist = new ArrayList<Evaluator>();
evaluatorlist.add(eval1);
evaluatorlist.add(eval2);
evaluatorlist.add(eval3);
evaluatorlist.add(eval4);
for (Evaluator eval : evaluatorlist) {
log.info("sync check: " + eval.isSync());
evaluatorRepository.save(eval);
}
//NEW sync list setting:
// boolean sync1 = eval1.isSync();
// boolean sync2 = eval2.isSync();
// boolean sync3 = eval3.isSync();
// boolean sync4 = eval4.isSync();
//
List<Boolean> synclist = new ArrayList<Boolean>();
// model.addAttribute()
//
// eval1.setSync(sync1);
// eval2.setSync(sync2);
// eval3.setSync(sync3);
// eval4.setSync(sync4);
//OLD sync list setting:
//boolean synctrue[] = new boolean[] {strueL1, strueL2, strueCR, strueFTF};
// List<String> synclist = new ArrayList<String>();
//
// for (int j = 0; j < synctrue.length; j++) {
// if (synctrue[j]) {
// synclist.add("Sync");
// }
// else if (!synctrue[j]){
// synclist.add("Async");
// }
// else {
// RedirectView redirectView = new RedirectView("/adminGroups", true);
// redir.addFlashAttribute("error", " in new group Sync layout");
// log.error("error in new group Sync layout");
// return redirectView;
// }
// }
// String[] levellist = new String[] {"Level 1", "Level 2", "Consistency Review", "Face to Face"};
// for (int k = 0; k < levellist.length; k++) {
// Evaluator eval = new Evaluator(currentUser, group,
// evalRoleRepository.findByNameAndCompany(levellist[k], currentUser.getCompany()), currentUser.getCompany());
//unnecessary?
// int evallevel = eval.getLevel().getLevel();
//
// if (synclist.get(k).equals("Sync")) {
// eval.setSync(true);
// }
// else {
// eval.setSync(false);
// }
//
//
// evaluatorlist.add(eval);
// evaluatorRepository.save(eval);
// }
log.info("self eval:" + group.getSelfeval());
group.setNumber(groupNumber);
group.setEvalTemplates(evalTemplate);
//group.setSelfeval(false);
// group.setGroupName(name);
try {
groupRepository.save(group);
evalRoleRepository.saveAll(roles);
} catch(Exception e) {
RedirectView redirectView = new RedirectView("/adminGroups", true);
redir.addFlashAttribute("error", "There was an error saving the group. Please try again.");
log.error("Error saving group: "+e.getMessage());
return redirectView;
}
redir.addFlashAttribute("Group "+group.getGroupNumber()+" was successfully added.", true);
RedirectView redirectView = new RedirectView("/adminGroups", true);
// RedirectView redirectView = new RedirectView("/adminGroups", true);
if(group.getCompany() != null) {
log.info("Added Group " + group.getGroupNumber() + " (ID:" + group.getId() + ") For Company " + group.getCompany().getCompanyName() + " (ID:" + group.getCompany().getId() + ")");
} else {
log.info("Added Group " + group.getGroupNumber() + " (ID:" + group.getId() + ") [No Company]");
}
// ArrayList<Long> idfront = new ArrayList<>();
// idfront.add(group.getId());
// return idfront;
return redirectView;
}
This is my form in the html doc:
<form th:action="@{/testing}" th:object="${group}" enctype="multipart/form-data"
method="post">
<!-- <form class="form-inline" th:action="@{/testing}" th:object="${group}" enctype="multipart/form-data"-->
<!-- method="post">-->
<div class="row">
<div class="form-group col">
<label for="groupName">Group Name</label>
<input type="text" th:field="*{groupName}" class="form-control" id="groupName"
placeholder="Group Name">
</div>
<!-- <div class="form-group col">-->
<!-- <label for="groupYear">Year</label>-->
<!-- <input type="number" min="1980" max="2030" th:field="*{year}" class="form-control"-->
<!-- id="groupYear"-->
<!-- placeholder="YYYY">-->
<!-- </div>-->
<div class="form-group col">
<label for="groupYear">Choose a Year:</label>
<select class= "form-select" name="groupYear" type="number" id="groupYear" th:field="*{year}">
<option value=2023>2023</option>
<option value=2024>2024</option>
<option value=2025>2025</option>
<option value=2026>2026</option>
<option value=2027>2027</option>
<option value=2028>2028</option>
<option value=2029>2029</option>
<option value=2030>2030</option>
</select>
</div>
<!-- unnecessary and should not be changed by user-->
<!-- <label for="number">Group Number</label>-->
<!-- <input type="text" class="form-control" id="number" th:placeholder="${group.groupNumber}" readonly>-->
<!-- should not be changed by admin, as admin appears to be tied to one company according to the database-->
<!-- <div class="form-group col">-->
<!-- <label for="groupType">Company</label>-->
<!-- <input type="text" class="form-control" id="groupType"-->
<!-- th:placeholder="${currentUser.companyName}" readonly>-->
<!-- </div>-->
<div class="form-group col">
<div class="form-check ">
<label class="form-check-label" for="reqselfeval">
Self Eval Required?
</label>
<br>
<input class="form-check-input" th:field="*{selfeval}" type="checkbox"
value="" id="reqselfeval">
</div>
</div>
<!-- <label for="evalForm">Company</label>-->
<!-- <input type="" class="form-control" id="evalForm" :placeholder="">-->
<!-- <label>EvalForm:</label>-->
<!-- <select th:field="${group.evalTemplates}">-->
<!-- <option th:each="template : ${evaltemplates}" th:value="${template}" th:text="${template.name}" />-->
<!-- </select>-->
</div>
<!-- <input type="submit" name="submit" value="Submit">-->
<!-- </form>-->
<div class="mt-5">
<div class="reviewerperms d-flex">
<div>
<table class="table table-hover">
<caption style="caption-side: top">
<h5 style="color: #198754">Reviewer Permissions:</h5>
</caption>
<thead class="border-top-0">
<tr class="table ">
<th scope="col">
Level
</th>
<th scope="col" class="border-top-0">
Sync
</th>
<th scope="col" class="border-top-0">
Preview
</th>
</tr>
</thead>
<tbody>
<tr>
<td>Level 1</td>
<td>
<input class="form-check-input" th:field="${eval1.sync}" type="checkbox">
</td>
<td>
<input class="form-check-input" th:field="${eval1.preview}" type="checkbox"
value="">
</td>
</tr>
<tr>
<td>Level 2</td>
<td>
<input class="form-check-input" th:field="${eval2.sync}" type="checkbox">
</td>
<td>
<input class="form-check-input" th:field="${eval2.preview}" type="checkbox"
value="">
</td>
</tr>
<tr>
<td>Consistency Review</td>
<td>
<input class="form-check-input" th:field="${eval3.sync}" type="checkbox">
</td>
<td>
<input class="form-check-input" th:field="${eval3.preview}" type="checkbox"
value="">
</td>
</tr>
<tr>
<td>Face to face</td>
<td>
<input class="form-check-input" th:field="${eval4.sync}" type="checkbox"
>
</td>
<td>
<input class="form-check-input" th:field="${eval4.preview}" type="checkbox"
value="">
</td>
</tr>
</tbody>
</table>
</div>
</div>
<input type="submit" class="btn btn-outline-success" name="submit" value="Create Group">
</div>
</form>
I know the code is rather lengthy, with many lines commented out, but I figured I’d just include it anyways.
I have tried assigning IDs to all of the checkboxes that are labeled evalx.sync, but I didn’t expect that to fix it since Spring Boot assigns IDs itself.
I also removed the value tags inside each checkbox for evalx.sync