Code:
@Controller
@SessionAttributes("publisher")
public class PublisherController {
public static final Logger LOGGER = Logger.getLogger(PublisherController.class);
private static final View JSON_VIEW = new MappingJacksonJsonView();
private static final String PUBLISHER_CREATE_URL_KEY = "/secure/publisher/create.htm";
private static final String PUBLISHER_UPDATE_URL_KEY = "/secure/publisher/update.htm";
private static final String PUBLISHER_LIST_URL_KEY = "/secure/publisher/list.htm";
private static final String PUBLISHER_CREATE_VIEW_KEY = "secure/publisher/create";
private static final String PUBLISHER_LIST_VIEW_KEY = "secure/publisher/list";
private static final String PUBLISHER_LIST_REDIRECT_VIEW_KEY = "list.htm";
private static final String PUBLISHER_KEY = "publisher";
private static final String PUBLISHERS_KEY = "publishers";
private static final String VERTICALS_KEY = "verticals";
private static final String ERRORS_KEY = "errors";
private static final String STATUS_KEY = "status";
private static final String SUCCESS_KEY = "success";
private static final String FAILURE_KEY = "failure";
private static final String UPDATE_KEY = "update";
private static final String DELETE_KEY = "delete";
private static final String ID_KEY = "id";
@Autowired
private PublisherService publisherService;
@Autowired
private VerticalService verticalService;
@Autowired
private PublisherValidator publisherValidator;
@Autowired
private VerticalEditor verticalEditor;
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(ID_KEY);
dataBinder.registerCustomEditor(Vertical.class, verticalEditor);
}
//Called only once at session start up because once it is added to session attribute map, it is only updated not created at subsequent calls of CRUD methods.
@ModelAttribute(PUBLISHER_KEY)
public Publisher getPublisher() {
return new Publisher();
}
@RequestMapping(value = PUBLISHER_CREATE_URL_KEY, method = RequestMethod.GET)
public ModelAndView setupCreateForm() {
LOGGER.info("Inside PublisherController.setupCreateForm");
ModelMap modelMap = new ModelMap();
//Always return a new publisher to clear old modelAttribute from previous method executions.
modelMap.addAttribute(PUBLISHER_KEY, new Publisher());
modelMap.addAttribute(VERTICALS_KEY, verticalService.getAllVerticals());
return new ModelAndView(PUBLISHER_CREATE_VIEW_KEY, modelMap);
}
@RequestMapping(value = PUBLISHER_CREATE_URL_KEY, method = RequestMethod.POST)
public ModelAndView createPublisher(Publisher publisher, BindingResult result, SessionStatus status) {
LOGGER.info("Inside PublisherController.create");
publisherValidator.validate(publisher, result);
if (result.hasErrors()) {
return setupCreateForm();
} else {
return createPublisher(publisher, status);
}
}
private ModelAndView createPublisher(Publisher publisher, SessionStatus status) {
publisher.setCreationTime(new Date());
publisherService.savePublisher(publisher);
status.setComplete();
return new ModelAndView(new RedirectView(PUBLISHER_LIST_REDIRECT_VIEW_KEY));
}
@RequestMapping(value = PUBLISHER_UPDATE_URL_KEY, method = RequestMethod.GET)
public ModelAndView setupUpdateForm(@RequestParam(value = ID_KEY, required = true) Integer publisherId) {
LOGGER.info("Inside PublisherController.setupUpdateForm");
ModelMap modelMap = new ModelMap();
Publisher publisher = publisherService.getPublisher(publisherId);
//Always pass the copy of publisher to avoid changes in actual hibernate bean. Sometimes if you don't do it, hibernate will complain about duplicate keys
//because you would have actually modified the bean to make it equal to another already existing bean.
Publisher publisherCopy = new Publisher();
publisherCopy = publisherCopy.fill(publisher);
modelMap.addAttribute(PUBLISHER_KEY, publisherCopy);
modelMap.addAttribute(VERTICALS_KEY, verticalService.getAllVerticals());
return new ModelAndView(JSON_VIEW, modelMap);
}
@RequestMapping(value = PUBLISHER_UPDATE_URL_KEY, params = UPDATE_KEY, method = RequestMethod.POST)
public ModelAndView updatePublisher(Publisher publisherCopy, BindingResult result, SessionStatus status) {
LOGGER.info("Inside PublisherController.update");
Publisher publisher = publisherService.getPublisher(publisherCopy.getId());
//If user has changed no key field in publisher. Only non key fields need to be updated.
if (publisherCopy.getName().equalsIgnoreCase(publisher.getName()) && publisherCopy.getVertical().equals(publisher.getVertical())) {
return updatePublisher(publisher, publisherCopy, status);
}
//If code is here, means some key field has changed, hence need to validate for constraints and duplicity of publisher.
publisherValidator.validate(publisherCopy, result);
// Some validation has failed, hence take it back to update screen again.
if (result.hasErrors()) {
ModelAndView modelAndView = setupUpdateForm(publisherCopy.getId());
ModelMap modelMap = modelAndView.getModelMap();
modelMap.addAttribute(STATUS_KEY, FAILURE_KEY);
modelMap.addAttribute(ERRORS_KEY, result.getAllErrors());
return modelAndView;
} else {
//Every validation passes. Just update the publisher.
return updatePublisher(publisher, publisherCopy, status);
}
}
private ModelAndView updatePublisher(Publisher publisher, Publisher publisherCopy, SessionStatus status) {
publisher = publisher.fill(publisherCopy);
publisher.setUpdateTime(new Date());
publisherService.updatePublisher(publisher);
status.setComplete();
ModelMap modelMap = new ModelMap();
modelMap.addAttribute(STATUS_KEY, SUCCESS_KEY);
return new ModelAndView(JSON_VIEW, modelMap);
}
@RequestMapping(value = PUBLISHER_UPDATE_URL_KEY, params = DELETE_KEY, method = RequestMethod.POST)
public ModelAndView deletePublisher(Publisher publisherCopy, SessionStatus status) {
LOGGER.info("Inside PublisherController.delete");
//Take original publisher to delete it from the hibernate managed entities. If you delete publisherCopy instead, hibernate will throw exception
//because it still contains another bean with same identifier.
Publisher publisher = publisherService.getPublisher(publisherCopy.getId());
publisherService.deletePublisher(publisher);
status.setComplete();
ModelMap modelMap = new ModelMap();
modelMap.addAttribute(STATUS_KEY, SUCCESS_KEY);
return new ModelAndView(JSON_VIEW, modelMap);
}
@RequestMapping(value = PUBLISHER_LIST_URL_KEY, method = RequestMethod.GET)
public ModelAndView getAllPublishers() {
LOGGER.info("Inside PublisherController.getAllPublishers");
return new ModelAndView(PUBLISHER_LIST_VIEW_KEY, PUBLISHERS_KEY, publisherService.getAllPublishers());
}
}