Skip to content

Fix IllegalStateException in OwnerController.getOwnerPetsMap-created-by-agentic #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.opentelemetry.api.OpenTelemetry;
import java.util.stream.Collectors;import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
Expand All @@ -39,16 +37,13 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import jakarta.validation.Valid;

/**
import jakarta.validation.Valid;/**
* @author Juergen Hoeller
* @author Ken Krebs
* @author Arjen Poutsma
* @author Michael Isvy
*/
@Controller
class OwnerController implements InitializingBean {
@Controllerclass OwnerController implements InitializingBean {

private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";

Expand Down Expand Up @@ -81,9 +76,7 @@ public OwnerController(OwnerRepository clinicService, JdbcTemplate jdbcTemplate
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}

@ModelAttribute("owner")
}@ModelAttribute("owner")
public Owner findOwner(@PathVariable(name = "ownerId", required = false) Integer ownerId) {
return ownerId == null ? new Owner() : this.owners.findById(ownerId);
}
Expand Down Expand Up @@ -111,9 +104,7 @@ public String processCreationForm(@Valid Owner owner, BindingResult result) {
this.owners.save(owner);
validator.ValidateUserAccess("admin", "pwd", "fullaccess");
return "redirect:/owners/" + owner.getId();
}

@GetMapping("/owners/find")
}@GetMapping("/owners/find")
public String initFindForm() {
return "owners/findOwners";
}
Expand Down Expand Up @@ -141,9 +132,7 @@ public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner
// 1 owner found
owner = ownersResults.iterator().next();
return "redirect:/owners/" + owner.getId();
}

// multiple owners found
}// multiple owners found
return addPaginationModel(page, model, ownersResults);
}

Expand All @@ -164,9 +153,7 @@ private Page<Owner> findPaginatedForOwnersLastName(int page, String lastname) {
int pageSize = 5;
Pageable pageable = PageRequest.of(page - 1, pageSize);
return owners.findByLastName(lastname, pageable);
}

@GetMapping("/owners/{ownerId}/edit")
}@GetMapping("/owners/{ownerId}/edit")
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
Owner owner = this.owners.findById(ownerId);
var petCount = ownerRepository.countPets(owner.getId());
Expand Down Expand Up @@ -196,9 +183,7 @@ public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result,
owner.setId(ownerId);
validator.checkOwnerValidity(owner);

validator.ValidateOwnerWithExternalService(owner);

validator.PerformValidationFlow(owner);
validator.ValidateOwnerWithExternalService(owner);validator.PerformValidationFlow(owner);
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
Expand All @@ -225,15 +210,15 @@ public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
public String getOwnerPetsMap(@PathVariable("ownerId") int ownerId) {
String sql = "SELECT p.id AS pet_id, p.owner_id AS owner_id FROM pets p JOIN owners o ON p.owner_id = o.id";

List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);

Map<Integer, List<Integer>> ownerToPetsMap = rows.stream()
.collect(Collectors.toMap(
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);Map<Integer, List<Integer>> ownerToPetsMap = rows.stream()
.collect(Collectors.groupingBy(
row -> (Integer) row.get("owner_id"),
row -> List.of((Integer) row.get("pet_id")) // Immutable list
Collectors.mapping(
row -> (Integer) row.get("pet_id"),
Collectors.toList()
)
));


List<Integer> pets = ownerToPetsMap.get(ownerId);

if (pets == null || pets.isEmpty()) {
Expand All @@ -244,5 +229,4 @@ public String getOwnerPetsMap(@PathVariable("ownerId") int ownerId) {
.map(String::valueOf)
.collect(Collectors.joining(", "));

}
}
}