Skip to content

Commit

Permalink
a little bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomokar committed Apr 28, 2016
1 parent c54d897 commit fe71534
Show file tree
Hide file tree
Showing 18 changed files with 400 additions and 204 deletions.
129 changes: 89 additions & 40 deletions src/main/java/ohtuhatut/controller/ReferenceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,115 +11,164 @@

/**
* Controller for handling references
*
* @author tuomokar
*/
@Controller
@RequestMapping("/references")
public class ReferenceController {

@Autowired
private ReferenceService referenceService;

@Autowired
private ReferenceListService referenceListService;

/**
* Adds all the references to the model and returns the view showing them.
*/
@RequestMapping(method = RequestMethod.GET)
public String getAllReferences(Model model) {
model.addAttribute("references", referenceService.getAllReferences());
return "references";
return "references/references";
}

/**
* Adds a single reference (found by the given id) to the model and returns
* the view for it.
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String getReference(Model model, @PathVariable Long id) {
model.addAttribute("reference", referenceService.getReference(id));
return "reference";
return "references/reference_show";
}

// ----------- edit

/**
* Handles the GET request for editing a reference. Adds the reference
* corresponding to the given id to the model, and returns the editing view
* for it. If the reference is null, returns the view informing the user on
* it.
*/
@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
public String editReference(Model model, @PathVariable Long id) {
Reference reference = referenceService.getReference(id);
if (reference != null) {
model.addAttribute("reference", reference);
return "reference_edit";
} else {
return "reference_does_not_exist";

if (reference == null) {
return "references/reference_does_not_exist";
}

model.addAttribute("reference", reference);
return "references/reference_edit";
}


/**
* Handles the POST request for editing a reference. The method takes the
* reference as an instance of the superclass from which it is then bound to
* the correct subclass type. The method checks if the given reference has
* any mandatory values left empty, and if so, returns the editing view
* along with the appropriate error message. Otherwise the edited reference
* gets saved to the database and user gets redirected to the page of the
* reference.
*/
@RequestMapping(value = "/{id}/edit", method = RequestMethod.POST)
public String updateReference(@ModelAttribute Reference reference,
@PathVariable Long id, RedirectAttributes attr, Model model) {

reference = referenceService.bindReference(reference);

if (!reference.getEmptyMandatoryFields().isEmpty()) {
model.addAttribute("emptyFields", referenceService.getErrorMessages(reference.getEmptyMandatoryFields()));
model.addAttribute("reference", referenceService.getReference(reference.getId()));
return "reference_edit";
return "references/reference_edit";
}

referenceService.saveReference(reference);

attr.addAttribute("id", reference.getId());
return "redirect:/references/{id}";
}

// <--- edit

// ----------- new

/**
* Returns the view for the user to choose what kind of a reference they
* want to create.
*/
@RequestMapping(value = "/choose", method = RequestMethod.GET)
public String chooseReferenceType(Model model) {
return "reference_choose";
public String chooseReferenceType() {
return "references/reference_choose";
}

/**
* Handles the GET request for creation of a new reference. If the type
* parameter in the request isn't given or if it is a value that no
* reference has as their type, the user is redirected to the choosing page
* with the appropriate message. Otherwise a new reference is created and
* that and the mandatory and optional fields for the type are added to the
* model, and the view for the user to create a new reference is returned.
*/
@RequestMapping(value = "/new", method = RequestMethod.GET)
public String newReference(Model model, @RequestParam("type") String type) {
public String newReference(Model model, @RequestParam(value = "type", required = false) String type,
RedirectAttributes redirectAttr) {

if (type == null || referenceService.typeIsNotKnown(type)) {
redirectAttr.addFlashAttribute("typeNotChosen", "Please choose a type first");
return "redirect:/references/choose";
}

Reference ref = new Reference();
ref.setType(type);
model.addAttribute("reference", ref);
model.addAttribute("mandatoryFields", ReferenceService.getMandatoryFields(type));
model.addAttribute("optionalFields", ReferenceService.getOptionalFields(type));
model.addAttribute("mandatoryFields", referenceService.getMandatoryFields(type));
model.addAttribute("optionalFields", referenceService.getOptionalFields(type));

return "reference_new";
return "references/reference_new";
}

/**
* Handles the POST request for creating a new reference. If the user left
* any mandatory fields empty, the method returns the view to create a new
* reference along with an error message informing the user what fields were
* left empty. Otherwise the reference is saved to the database and the user
* is redirected to the newly created reference's page.
*/
@RequestMapping(value = "/new", method = RequestMethod.POST)
public String newReferenceCreate(@ModelAttribute Reference reference, RedirectAttributes attr,
Model model) {
public String newReferenceCreate(@ModelAttribute Reference reference,
RedirectAttributes attr,
Model model) {

reference = referenceService.bindReference(reference);

if (!reference.getEmptyMandatoryFields().isEmpty()) {
model.addAttribute("emptyFields", referenceService.getErrorMessages(reference.getEmptyMandatoryFields()));
model.addAttribute("reference", reference);
model.addAttribute("mandatoryFields", ReferenceService.getMandatoryFields(reference.getType()));
model.addAttribute("optionalFields", ReferenceService.getOptionalFields(reference.getType()));
return "reference_new";
model.addAttribute("mandatoryFields", referenceService.getMandatoryFields(reference.getType()));
model.addAttribute("optionalFields", referenceService.getOptionalFields(reference.getType()));
return "references/reference_new";
}
referenceService.saveReference(reference);

attr.addAttribute("id", reference.getId());
return "redirect:/references/{id}";
}

// <--- new

/**
* Handles the deletion of a reference. If the reference searched by the id
* is null, i.e. it doesn't exist, the method returns the view informing the
* user on that. Otherwise the reference is removed from any reference lists
* that may have had the reference, and then the reference is deleted from
* the database, and the user is redirected to the page showing all the
* references.
*/
@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
public String deleteReference(@PathVariable Long id) {

Reference reference = referenceService.getReference(id);

if (reference == null) {
return "reference_does_not_exist";
return "references/reference_does_not_exist";
}

referenceListService.removeReference(reference);

referenceService.deleteReference(reference);

return "redirect:/references/";
}
}
109 changes: 76 additions & 33 deletions src/main/java/ohtuhatut/controller/ReferenceListController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,60 +24,83 @@
/**
* Controller class for ReferenceList page.
*/

@Controller
@RequestMapping("/referencelists")
public class ReferenceListController {

@Autowired
private ReferenceListService referenceListService;

@Autowired
private ReferenceService referenceService;

@Autowired
private ReferenceFormatService referenceFormatService;

/**
* Handles the GET request for the page showing all the reference lists.
* All the lists are added to the model and the view for them is returned.
*/
@RequestMapping(method = RequestMethod.GET)
public String getAllReferenceLists(Model model) {
model.addAttribute("referenceLists", referenceListService.getAllReferenceLists());
return "referencelists";
return "referencelists/referencelists";
}


/**
* Handles the GET request for the page of a specific reference list.
* The list is added to the model along with all the references in the
* database (to make it possible for a user to add them to the list).
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String getReferenceList(Model model, @PathVariable Long id){
public String getReferenceList(Model model, @PathVariable Long id) {
model.addAttribute("referenceList", referenceListService.getReferenceList(id));
model.addAttribute("references", referenceService.getAllReferences());

return "referencelist_show";
return "referencelists/referencelist_show";
}


/**
* Handles the GET request for creating a new reference list.
*/
@RequestMapping(value = "/new", method = RequestMethod.GET)
public String newReferenceList(Model model){
public String newReferenceList(Model model) {
model.addAttribute("referenceList", new ReferenceList());
return "referencelist_new";

return "referencelists/referencelist_new";
}


/**
* Handles the POST request for creating a new reference list. If the list's
* name is left empty, returns the page again with the error message
* informing the user on what went wrong. Otherwise the list is saved
* into the database and the user gets redirected to the page of the newly
* created list
*/
@RequestMapping(value = "/new", method = RequestMethod.POST)
public String newReferenceListCreate(@Valid @ModelAttribute ReferenceList referenceList,
BindingResult bindingResult,
RedirectAttributes attr) {

if (bindingResult.hasErrors()) {
return "referencelist_new";
return "referencelists/referencelist_new";
}
referenceListService.save(referenceList);

attr.addAttribute("id", referenceList.getId().toString());
return "redirect:/referencelists/{id}";
}


/**
* Handles the POST request for adding references to a reference list.
* The reference is added to the list only if the list doesn't have that
* reference yet. The user is then redirected back to the list's page.
*/
@RequestMapping(value = "/{referenceListId}/references", method = RequestMethod.POST)
public String addReferenceToList(@PathVariable(value="referenceListId") Long id,
@RequestParam(value="referenceId") Long referenceId,
public String addReferenceToList(@PathVariable(value = "referenceListId") Long id,
@RequestParam(value = "referenceId") Long referenceId,
RedirectAttributes redirectAttrs) {

ReferenceList list = referenceListService.getReferenceList(id);
Reference reference = referenceService.getReference(referenceId);

Expand All @@ -87,15 +110,21 @@ public String addReferenceToList(@PathVariable(value="referenceListId") Long id,
}

redirectAttrs.addAttribute("id", id);

return "redirect:/referencelists/{id}";

}


/**
* Handles the POST request for removing a reference from a list. Removes
* the reference only if it is actually on the list. The user is then
* redirected to the page of the list.
*/
@RequestMapping(value = "/{referenceListId}/references/remove", method = RequestMethod.POST)
public String removeReferenceFromList(@PathVariable(value="referenceListId") Long id,
@RequestParam(value="referenceId") Long referenceId,
public String removeReferenceFromList(@PathVariable(value = "referenceListId") Long id,
@RequestParam(value = "referenceId") Long referenceId,
RedirectAttributes redirectAttrs) {

ReferenceList list = referenceListService.getReferenceList(id);
Reference reference = referenceService.getReference(referenceId);

Expand All @@ -105,25 +134,39 @@ public String removeReferenceFromList(@PathVariable(value="referenceListId") Lon
}

redirectAttrs.addAttribute("id", id);

return "redirect:/referencelists/{id}";
}

/**
* Exports all the references in the given reference list to a Bibtex file
* in the correct format. If no name is given for the file, then the
* list's name is given to it.
*/
@RequestMapping(value = "/{referenceListId}/export", method = RequestMethod.GET)
public void exportReferenceList(@PathVariable(value="referenceListId") Long id,
@RequestParam(value="name", required=false, defaultValue="references") String name,
HttpServletResponse response) throws IOException {
public void exportReferenceList(@PathVariable(value = "referenceListId") Long id,
@RequestParam(value = "name", required = false) String name,
HttpServletResponse response) throws IOException {

ReferenceList list = referenceListService.getReferenceList(id);

if (name == null || name.isEmpty()) {
name = list.getName();
}

String formattedText = referenceFormatService.formatReferencesToBibTeX(list.getReferences());

returnResponseWithBibTeXFile(name + ".bib", formattedText, response);
}

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

/**
* Exports all the references in the database to a Bibtex file. The file
* gets the name "references"
*/
@RequestMapping(value = "/export_all", method = RequestMethod.GET)
public void exportAll(HttpServletResponse response) throws IOException {
String formattedText = referenceFormatService.formatReferencesToBibTeX(referenceService.getAllReferences());

returnResponseWithBibTeXFile("references.bib", formattedText, response);
}

Expand All @@ -135,4 +178,4 @@ private void returnResponseWithBibTeXFile(String filename, String content, HttpS
out.flush();
out.close();
}
}
}
Loading

0 comments on commit fe71534

Please sign in to comment.