// Path: src/main/java/org/jugbd/mnet/domain/enums/PictureInformationType.java
// public enum PictureInformationType {
// 
//     DAY_ONE("Day One"),
// 
//     PREOPERATIVE("Pre-operative"),
// 
//     PRE_OPERATION("Pre-operation"),
// 
//     POSTOPERATIVE("Post operative"),
// 
//     ON_DISCHARGE("On Discharge");
// 
//     private String label;
// 
//     PictureInformationType(String label) {
//         this.label = label;
//     }
// 
//     public String getLabel() {
//         return label;
//     }
// 
// }
// 
// Path: src/main/java/org/jugbd/mnet/service/PictureInformationService.java
// @Component
// public interface PictureInformationService {
//     PictureInformation save(PictureInformation pictureInformation);
// 
//     PictureInformation findOne(Long id);
// 
//     void upload(Long registerId, MultipartFile file, PictureInformationType pictureInformationType, String fileName, String comment);
// 
//     ResponseEntity<byte[]> getUploadedFileAsResponseEntity(Long fileId);
// 
//     PictureInformation findPictureInformationByRegistrationId(Long registerId);
// }
// 
// Path: src/main/java/org/jugbd/mnet/utils/FileUtils.java
// public class FileUtils {
//     public static final int FILE_NAME_MAX_SIZE = 60;
// 
//     private static final Map<String, String> extensionContentTypeMap;
// 
//     static {
//         extensionContentTypeMap = new HashMap<>();
//         extensionContentTypeMap.put("jpg", "image/jpeg");
//         extensionContentTypeMap.put("jpeg", "image/jpeg");
//         extensionContentTypeMap.put("png", "image/png");
//         extensionContentTypeMap.put("pdf", "application/pdf");
//         extensionContentTypeMap.put("doc", "application/msword");
//         extensionContentTypeMap.put("docx", "application/msword");
//     }
// 
//     public static boolean isValidFile(MultipartFile file, String[] fileTypeList) {
//         String fileName = file.getOriginalFilename();
//         String extension = getExtensionInLowerCase(fileName);
//         for (String validFileType : fileTypeList) {
//             if (extension.equalsIgnoreCase(validFileType)) {
//                 return true;
//             }
//         }
//         return false;
//     }
// 
//     public static String getContentType(String extension) {
//         return extensionContentTypeMap.get(extension.toLowerCase());
//     }
// 
//     public static String getExtensionInLowerCase(String fileName) {
//         return FilenameUtils.getExtension(fileName).toLowerCase();
//     }
// 
//     public static String getExtensionFromContentType(String contentType) {
//         String key = "";
//         for (Map.Entry<String, String> entry : extensionContentTypeMap.entrySet()) {
//             if ((entry.getValue().equalsIgnoreCase(contentType))) {
//                 key = entry.getKey();
//             }
//         }
//         return key;
//     }
// 
//     /**
//      * Get file name trimmed to FILE_NAME_MAX_SIZE and also extension to lower case (bcoz, people tend to have file in uppercase extension like JPG, jpg)
//      *
//      * @param fileName User's file name
//      * @return
//      */
//     public static String getFilteredFileName(String fileName) {
//         String extension = getExtensionInLowerCase(fileName);
//         String baseName = FilenameUtils.getBaseName(fileName);
//         baseName = baseName.replaceAll(" ", "-");
//         return StringUtils.getTrimmedString(baseName, FILE_NAME_MAX_SIZE - extension.length()) + "." + extension;
//     }
// }


import org.jugbd.mnet.dao.AttachmentDao;
import org.jugbd.mnet.domain.Attachment;
import org.jugbd.mnet.domain.PictureInformation;
import org.jugbd.mnet.domain.enums.PictureInformationType;
import org.jugbd.mnet.service.PictureInformationService;
import org.jugbd.mnet.service.RegisterService;
import org.jugbd.mnet.utils.Contestant;
import org.jugbd.mnet.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.io.IOException;
    ) throws IOException {
        log.debug("uploadPhoto() pictureInformationType ={}", pictureInformationType);

        String errorMsg = validate(file, Contestant.VALID_FILE_TYPE_LIST, Contestant.FILE_MAX_SIZE_BYTES, "Photo");

        if (!errorMsg.isEmpty()) {

            redirectAttributes.addFlashAttribute("error", errorMsg);
            return "picture/show";
        }

        pictureInformationService.upload(registerId, file, pictureInformationType, fileName, comment);
        redirectAttributes.addFlashAttribute("message", "File successfully uploaded");

        return "redirect:/picture/" + registerId;
    }

    @RequestMapping(value = "picture/delete/{registerId}/{attachmentId}", method = RequestMethod.POST)
    public String remove(@PathVariable Long registerId, @PathVariable Long attachmentId) {

        Attachment attachment = attachmentDao.findOne(attachmentId);
        attachment.setDeleted(true);

        attachmentDao.save(attachment);

        return "redirect:/picture/" + registerId;
    }

    private String validate(MultipartFile file, String[] validFileTypes, int maxFileSize, String field) {
        if (!FileUtils.isValidFile(file, validFileTypes)) {
// Path: src/main/java/org/jugbd/mnet/domain/enums/PictureInformationType.java
// public enum PictureInformationType {
// 
//     DAY_ONE("Day One"),
// 
//     PREOPERATIVE("Pre-operative"),
// 
//     PRE_OPERATION("Pre-operation"),
// 
//     POSTOPERATIVE("Post operative"),
// 
//     ON_DISCHARGE("On Discharge");
// 
//     private String label;
// 
//     PictureInformationType(String label) {
//         this.label = label;
//     }
// 
//     public String getLabel() {
//         return label;
//     }
// 
// }
// 
// Path: src/main/java/org/jugbd/mnet/service/PictureInformationService.java
// @Component
// public interface PictureInformationService {
//     PictureInformation save(PictureInformation pictureInformation);
// 
//     PictureInformation findOne(Long id);
// 
//     void upload(Long registerId, MultipartFile file, PictureInformationType pictureInformationType, String fileName, String comment);
// 
//     ResponseEntity<byte[]> getUploadedFileAsResponseEntity(Long fileId);
// 
//     PictureInformation findPictureInformationByRegistrationId(Long registerId);
// }
// 
// Path: src/main/java/org/jugbd/mnet/utils/FileUtils.java
// public class FileUtils {
//     public static final int FILE_NAME_MAX_SIZE = 60;
// 
//     private static final Map<String, String> extensionContentTypeMap;
// 
//     static {
//         extensionContentTypeMap = new HashMap<>();
//         extensionContentTypeMap.put("jpg", "image/jpeg");
//         extensionContentTypeMap.put("jpeg", "image/jpeg");
//         extensionContentTypeMap.put("png", "image/png");
//         extensionContentTypeMap.put("pdf", "application/pdf");
//         extensionContentTypeMap.put("doc", "application/msword");
//         extensionContentTypeMap.put("docx", "application/msword");
//     }
// 
//     public static boolean isValidFile(MultipartFile file, String[] fileTypeList) {
//         String fileName = file.getOriginalFilename();
//         String extension = getExtensionInLowerCase(fileName);
//         for (String validFileType : fileTypeList) {
//             if (extension.equalsIgnoreCase(validFileType)) {
//                 return true;
//             }
//         }
//         return false;
//     }
// 
//     public static String getContentType(String extension) {
//         return extensionContentTypeMap.get(extension.toLowerCase());
//     }
// 
//     public static String getExtensionInLowerCase(String fileName) {
//         return FilenameUtils.getExtension(fileName).toLowerCase();
//     }
// 
//     public static String getExtensionFromContentType(String contentType) {
//         String key = "";
//         for (Map.Entry<String, String> entry : extensionContentTypeMap.entrySet()) {
//             if ((entry.getValue().equalsIgnoreCase(contentType))) {
//                 key = entry.getKey();
//             }
//         }
//         return key;
//     }
// 
//     /**
//      * Get file name trimmed to FILE_NAME_MAX_SIZE and also extension to lower case (bcoz, people tend to have file in uppercase extension like JPG, jpg)
//      *
//      * @param fileName User's file name
//      * @return
//      */
//     public static String getFilteredFileName(String fileName) {
//         String extension = getExtensionInLowerCase(fileName);
//         String baseName = FilenameUtils.getBaseName(fileName);
//         baseName = baseName.replaceAll(" ", "-");
//         return StringUtils.getTrimmedString(baseName, FILE_NAME_MAX_SIZE - extension.length()) + "." + extension;
//     }
// }


// Path: src/main/java/org/jugbd/mnet/web/controller/PictureInformationController.java
import org.jugbd.mnet.dao.AttachmentDao;
import org.jugbd.mnet.domain.Attachment;
import org.jugbd.mnet.domain.PictureInformation;
import org.jugbd.mnet.domain.enums.PictureInformationType;
import org.jugbd.mnet.service.PictureInformationService;
import org.jugbd.mnet.service.RegisterService;
import org.jugbd.mnet.utils.Contestant;
import org.jugbd.mnet.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.io.IOException;

    ) throws IOException {
        log.debug("uploadPhoto() pictureInformationType ={}", pictureInformationType);

        String errorMsg = validate(file, Contestant.VALID_FILE_TYPE_LIST, Contestant.FILE_MAX_SIZE_BYTES, "Photo");

        if (!errorMsg.isEmpty()) {

            redirectAttributes.addFlashAttribute("error", errorMsg);
            return "picture/show";
        }

        pictureInformationService.upload(registerId, file, pictureInformationType, fileName, comment);
        redirectAttributes.addFlashAttribute("message", "File successfully uploaded");

        return "redirect:/picture/" + registerId;
    }

    @RequestMapping(value = "picture/delete/{registerId}/{attachmentId}", method = RequestMethod.POST)
    public String remove(@PathVariable Long registerId, @PathVariable Long attachmentId) {

        Attachment attachment = attachmentDao.findOne(attachmentId);
        attachment.setDeleted(true);

        attachmentDao.save(attachment);

        return "redirect:/picture/" + registerId;
    }

    private String validate(MultipartFile file, String[] validFileTypes, int maxFileSize, String field) {
        if (!FileUtils.isValidFile(file, validFileTypes)) {
            return String.format("Please make sure your %s is in valid format", field);