MarksheetCtl.java

package com.sunilos.proj0.ctl;

import java.util.List;

import java.util.Locale;

import javax.validation.Valid;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.MessageSource;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import com.sunilos.proj0.dto.MarksheetDTO;

import com.sunilos.proj0.dto.StudentDTO;

import com.sunilos.proj0.form.MarksheetForm;

import com.sunilos.proj0.service.MarksheetServiceInt;

import com.sunilos.proj0.service.StudentServiceInt;

/**

* Contains navigation logics for Marksheet, Marksheet List, Merit List, and

* GetMarksheet Usecases.

*

* @author SunilOS

* @version 1.0

* @Copyright (c) SunilOS

*/

@Controller

@RequestMapping(value = "/ctl/Marksheet")

public class MarksheetCtl extends BaseCtl {

/**

* Logger object

*/

private static Logger log = Logger.getLogger(CollegeCtl.class);

@Autowired

MarksheetServiceInt service;

@Autowired

StudentServiceInt studentService;

/**

* i18n Message source

*/

@Autowired

private MessageSource messageSource;

/**

* Preload Student List

*/

@Override

public void preload(Model model) {

model.addAttribute("studentList", studentService.search(null));

}

/**

* Displays Marksheet view.

*

* @param id

* @param form

* @param model

* @return

*/

@RequestMapping(method = RequestMethod.GET)

public String display(@RequestParam(required = false) Long id,

@ModelAttribute("form") MarksheetForm form, Model model) {

if (id != null && id > 0) {

form.populate(service.findByPK(id));

}

return "Marksheet";

}

/**

* Submits Marksheet data.

*

* @param locale

* @param operation

* @param form

* @param bindingResult

* @param model

* @return

*/

@RequestMapping(method = RequestMethod.POST)

public String submit(Locale locale, @RequestParam String operation,

@ModelAttribute("form") @Valid MarksheetForm form,

BindingResult bindingResult, Model model) {

if (bindingResult.hasErrors()) {

return "Marksheet";

}

try {

if (OP_SAVE.equalsIgnoreCase(operation)) {

MarksheetDTO dto = (MarksheetDTO) form.getDto();

StudentDTO sdto = studentService.findByPK(form.getStudentId());

dto.setName(sdto.getValue());

if (dto.getId() > 0) {

service.update(dto);

} else {

Long id = service.add(dto);

form.setId(id);

}

String msg = messageSource.getMessage("message.success", null,

locale);

model.addAttribute("success", msg);

} else if (OP_DELETE.equalsIgnoreCase(operation)) {

service.delete(form.getId());

String msg = messageSource.getMessage("message.success", null,

locale);

model.addAttribute("success", msg);

return "redirect:Marksheet/search";

}

} catch (Exception e) {

e.printStackTrace();

model.addAttribute("error", "Critical Issue " + e.getMessage());

}

return "Marksheet";

}

/**

* Displays Marksheet List View.

*

* @param form

* @param model

* @return

*/

@RequestMapping(value = "/search", method = RequestMethod.GET)

public String searchList(@ModelAttribute("form") MarksheetForm form,

Model model) {

model.addAttribute("list",

service.search(null, form.getPageNo(), form.getPageSize()));

return "MarksheetList";

}

/**

* Submits Marskheet List data.

*

* @param locale

* @param form

* @param operation

* @param model

* @return

*/

@RequestMapping(value = "/search", method = RequestMethod.POST)

public String searchList(Locale locale,

@ModelAttribute("form") MarksheetForm form,

@RequestParam(required = false) String operation, Model model) {

log.debug("in searchList method");

// Calculate next page number

int pageNo = form.getPageNo();

if (OP_NEXT.equals(operation)) {

pageNo++;

} else if (OP_PREVIOUS.equals(operation)) {

pageNo--;

}

pageNo = (pageNo < 1) ? 1 : pageNo;

form.setPageNo(pageNo);

if (OP_DELETE.equals(operation) && form.getIds() != null) {

for (long id : form.getIds()) {

service.delete(id);

}

String msg = messageSource.getMessage("message.success", null,

locale);

model.addAttribute("success", msg);

}

// Get search attributes

MarksheetDTO dto = (MarksheetDTO) form.getDto();

model.addAttribute("list",

service.search(dto, form.getPageNo(), form.getPageSize()));

return "MarksheetList";

}

/**

* Displays Meritlist view.

*

* @param form

* @param model

* @return

*/

@RequestMapping(value = "/meritlist", method = RequestMethod.GET)

public String getMeritList(@ModelAttribute("form") MarksheetForm form,

Model model) {

List meritList = service.getMeritList(0, 0);

model.addAttribute("list", meritList);

return "GetMeritList";

}

/**

* Gets Marksheet.

*

* @param form

* @param model

* @return

*/

@RequestMapping(value = "/get", method = RequestMethod.GET)

public String getMarksheet(@ModelAttribute("form") MarksheetForm form,

Model model) {

MarksheetDTO dto = service.findByRollNo(form.getRollNo());

if (dto != null) {

form.populate(dto);

} else {

model.addAttribute("error", "Invalid Roll Number");

}

return "GetMarksheet";

}

}