rzd.vivc.documentexamination.controller.DocumentController.java Source code

Java tutorial

Introduction

Here is the source code for rzd.vivc.documentexamination.controller.DocumentController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rzd.vivc.documentexamination.controller;

import java.io.IOException;
import javax.servlet.http.Part;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import org.springframework.web.bind.annotation.RequestPart;
import rzd.vivc.documentexamination.model.dto.documents.Document;
import rzd.vivc.documentexamination.repository.DocumentRepository;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import rzd.vivc.documentexamination.controller.exception.FileLoadingException;
import rzd.vivc.documentexamination.form.DocumentForm;
import rzd.vivc.documentexamination.repository.DepartmentRepository;
import rzd.vivc.documentexamination.repository.DocumentTypeRepository;
import rzd.vivc.documentexamination.service.DocumentAdditionalSavingServicesRepository;
import rzd.vivc.documentexamination.service.FileSavingService;

/**
 * ? document.jsp
 *
 * @author VVolgina
 */
@Controller
@RequestMapping("/director/document")
public class DocumentController {

    //? ?? ? ? ? 
    private final DocumentRepository documentRepository;
    // ?  
    @Autowired
    private DocumentTypeRepository documentTypeRepository;
    // ? ?? 
    @Autowired
    private DepartmentRepository departmentRepository;

    //? ??  
    @Autowired
    private FileSavingService fileSavingService;

    @Autowired
    private DocumentAdditionalSavingServicesRepository documentAdditionalSavingServicesRepository;

    /**
     * ?
     *
     * @param documentRepository 
     */
    @Autowired
    public DocumentController(DocumentRepository documentRepository) {
        this.documentRepository = documentRepository;
    }

    /**
    *  GET ? c path parameter  /documents   ???
    *  ?  id   document -  ??  ??
    * ?    ? view ?? document,   ? ?
    * ??
    *
    * @param documentID id 
    * @param model  ?  ? view
    * @return ?? 
    */
    @RequestMapping(value = "/{documentID}", method = RequestMethod.GET)
    //value="documentID"  ?, ?      value  requestMapping ?
    public String document(@PathVariable(value = "documentID") long documentID, Model model) {
        //       ?   ? ??
        if (!model.containsAttribute("document")) {
            Document findOne = documentRepository.findOne(documentID);
            model.addAttribute(findOne);
            model.addAttribute("link", fileSavingService.getFileLink(findOne.getFile()));
        } else {
            model.addAttribute("link",
                    fileSavingService.getFileLink(((Document) model.asMap().get("document")).getFile()));
        }
        return "document";
    }

    /**
     *   ? ? ?  ??
     * 
     *
     * @param documentID id  ? ?
     * @param model 
     * @return view ? ? ?
     */
    @RequestMapping(value = "/edit/{documentID}", method = GET)
    public String edit(@PathVariable(value = "documentID") long documentID, Model model) {
        addTypes(model);
        // ? ? ? ? ?  ???  
        //  document, ?       
        Document document = documentRepository.findOne(documentID);
        DocumentForm documentForm = new DocumentForm(document);
        fileSavingService.addFileLink(documentForm);
        model.addAttribute(documentForm);
        return "editDocument";
    }

    /**
     *   ? ?
     *
     * @param model 
     * @return view ? ? ?
     */
    @RequestMapping(value = "/edit", method = GET)
    public String edit(Model model) {
        addTypes(model);
        // ? ? ? ? ?  ???  
        //  document, ?       
        model.addAttribute(new DocumentForm());
        return "editDocument";
    }

    /**
     *   ?  ? @Valid ,   
     * ? ?  ?? DocumentForm
     *
     * @param file  
     * @param document ,  
     * @param errors ??  
     * @param model  ? ,   ?   ?
     *  placeholder ?   ? - ? , 
     *   ?,  ???  ?  ?
     * @return "redirect: /documents/{documentID}" ? ? ?,  
     * , ? ? 
     */
    @RequestMapping(value = "/edit", method = POST)
    public String processEdit(@RequestPart Part file, @Valid DocumentForm document, Errors errors,
            RedirectAttributes model) {
        return processEditRealisation(file, 0, document, errors, model);
    }

    /**
     *   ?  ? @Valid ,   
     * ? ?  ?? DocumentForm ? ?
     * ?? 
     *
     * @param documentID id 
     * @param file  
     * @param document ,  
     * @param errors ??  
     * @param model  ? ,   ?   ?
     *  placeholder ?   ? - ? , 
     *   ?,  ???  ?  ?
     * @return "redirect: /documents/{documentID}" ? ? ?,  
     * , ? ? 
     */
    @RequestMapping(value = "/edit/{documentID}", method = POST)
    public String processEdit(@PathVariable(value = "documentID") long documentID, @RequestPart Part file,
            @Valid DocumentForm document, Errors errors, RedirectAttributes model) {
        return processEditRealisation(file, documentID, document, errors, model);
    }

    private String processEditRealisation(Part file, long documentID, DocumentForm document, Errors errors,
            RedirectAttributes model) {
        if (errors.hasErrors()) {
            addTypes(model);
            return "editDocument";
        }
        document.setFile(file);

        // ,   
        try {
            fileSavingService.saveUploadedFile(document);
        } catch (IOException e) {
            //?  ? ? 
            throw new FileLoadingException();
        }
        Document dtoDocument = document.toDocument();
        dtoDocument.setId(documentID);
        Document saved = documentAdditionalSavingServicesRepository.saveWythExaminations(dtoDocument);
        model.addAttribute("documentID", saved.getId());
        //    document. ?  ???   
        model.addFlashAttribute(saved);
        return "redirect:/director/document/{documentID}";
    }

    /**
     * ?   ??  
     */
    private void addTypes(Model model) {
        model.addAttribute("types", documentTypeRepository.findAll(new Sort(Sort.Direction.ASC, "name")));
        model.addAttribute("departments", departmentRepository.findAll(new Sort(Sort.Direction.ASC, "name")));
    }
}