jeudi 19 mars 2015

How to edit two model classes in a single form in Spring MVC

I need to edit a form which handles two entity classes, file.java and client.java having the primary key of client, clientId as Client_clientId in file.java. I'm probably doing something wrong in the urls and am in a fix.


This is file.java



@Entity
public class file {
@Id
private int fileNumber;
private String fileName;
private String description;
private String purpose;
@Transient
private String assignedAdvocate;
private String comment;
private int Client_clientID;
@DateTimeFormat(pattern = "dd-MM-yyyy")
private Date timestamp;


This is client.java



@Entity
public class client {

@Id
@GeneratedValue
private int clientId;
@NotNull
private String clientName;
private String clientAddress;
private String contactPerson;
private int phone;
@Email
private String email;
private String bank;
private String branch;
private String accountNumber;


This is the controller class where I am handling the edits.



ManageFiles.java



@Controller
@RequestMapping("/advocatoree/manageFiles")
public class ManageFiles {

@PersistenceContext
EntityManager em;

@RequestMapping(value = "/searchFile")
public ModelAndView inputFileNumber(ModelAndView model){

model.setViewName("searchFile");
return model;
}

@Transactional
@RequestMapping(value = "/fileStatus", method = RequestMethod.GET)
public ModelAndView viewFileStatus(@RequestParam("file_number")int id, ModelAndView model){

model.setViewName("fileStatus");

file file = em.find(com.techflakes.advocatoree.model.file.class, id);
model.addObject("file", file);

client client = (com.techflakes.advocatoree.model.client)em.createQuery("SELECT c FROM client c WHERE c.clientId =:id")
.setParameter("id", file.getClient_clientID()).getSingleResult();
model.addObject("client", client);

List<deposit> deposits = (List<deposit>)em.createQuery("SELECT d FROM deposit d WHERE d.File_fileNumber =:id")
.setParameter("id", id).getResultList();


List<expense> expenses = (List)em.createQuery("SELECT e FROM expense e WHERE e.File_fileNumber =:id")
.setParameter("id", id).getResultList();

model.addObject("depositList", deposits);
model.addObject("expenseList", expenses);

List<bill> fileBills = em.createQuery("SELECT b FROM bill b WHERE b.file_fileNumber =:id")
.setParameter("id", id).getResultList();

model.addObject("fileBills", fileBills);

List<fileStatus> fileStatusList = (List)em.createQuery("SELECT f FROM fileStatus f WHERE f.file_fileNumber =:id").setParameter("id", id).getResultList();
for(fileStatus fs: fileStatusList){
String s =(String)em.createQuery("SELECT e.name FROM employee e WHERE e.employeeID =:id")
.setParameter("id", fs.getEmployee_employeeID()).getSingleResult();
fs.setEmployeeName(s);
}
model.addObject("fileStatusList", fileStatusList);

List<employee> employeeList = em.createQuery("SELECT e FROM employee e WHERE e.employeeID !=0").getResultList();

model.addObject("employeeList", employeeList);

List<Integer> employeefiles = em.createQuery("SELECT e.employee_employeeID FROM employeefile e WHERE e.file_fileNumber =:id")
.setParameter("id", id).getResultList();

ArrayList<String> employeeNames = new ArrayList<String>();
for(Integer i: employeefiles){
employee e = em.find(employee.class, i);
employeeNames.add(e.getName());
}

model.addObject("employeeNames", employeeNames.toString());

model.addObject("employeeFiles", employeefiles);
return model;
}


@Transactional
@RequestMapping(value = "/updateFileClient/{id}", method = RequestMethod.POST)
public ModelAndView modifyFileClientInfo(ModelAndView model, @PathVariable("id")int id){
model.setViewName("modifyFileClientInfo");
file file = em.find(com.techflakes.advocatoree.model.file.class, id);
// client client = em.find(com.techflakes.advocatoree.model.client.class, id);
client client = (com.techflakes.advocatoree.model.client)em.createQuery("SELECT c FROM client c WHERE c.clientId =:id")
.setParameter("id", file.getClient_clientID()).getSingleResult();
int zero=0;
List<employee> employeeList = em.createQuery("SELECT e FROM employee e WHERE e.employeeID >:zero")
.setParameter("zero", zero).getResultList();
model.addObject("employeeList", employeeList);
model.addObject("files", file);
model.addObject("clients", client);
return model;
}

@Transactional
@RequestMapping(value = "/updatedFileInfo/{id}", method = RequestMethod.POST)
public String updateFileInfo(@ModelAttribute("")file f, client c,
@PathVariable("id")int id){

file file = em.find(com.techflakes.advocatoree.model.file.class, id);
client client = em.find(com.techflakes.advocatoree.model.client.class, id);
// client client = (com.techflakes.advocatoree.model.client)em.createQuery("SELECT c FROM client c WHERE c.clientId =:id")
// .setParameter("id", file.getClient_clientID()).getSingleResult();
file.setFileName(f.getFileName());
file.setDescription(f.getDescription());
file.setPurpose(f.getPurpose());
file.setAssignedAdvocate(f.getAssignedAdvocate());
file.setComment(f.getComment());
file.setTimestamp(f.getTimestamp());
file.setClient_ClientID(c.getClientId());
client.setClientName(c.getClientName());
client.setClientAddress(c.getClientAddress());
client.setContactPerson(c.getContactPerson());
client.setPhone(c.getPhone());
client.setEmail(c.getEmail());
client.setBank(c.getBank());
client.setBranch(c.getBranch());
client.setAccountNumber(c.getAccountNumber());
em.persist(client);
em.flush();
em.refresh(client);
em.persist(file);
return "redirect:/advocatoree/manageFiles/searchFile";
}

}


This is the edit button in filestatus.jsp



<form action="/advocatoree/manageFiles/updateFileClient/${file.fileNumber}" method="post">
<button type="submit" class="btn btn-purple">Edit Information</button>
</form>


and this is the url action in modifyFileClientInfo.jsp



<form method="post" action="/advocatoree/manageFiles/updatedFileInfo/${clients.clientId}">


Once I edit the data and submit them, they're supposed to be persisted in the database and should also show in my page with the edited info. What I see is when I click the edit button, it takes the fileNumber and after I edit and submit the data, the edited info is persisted in that file number which is equal to the clientId of the previous file. If 15 is the clientId of file Number 65, the data is persisted in file number 15.


Please. Help!


Aucun commentaire:

Enregistrer un commentaire