Advertisement
Coriic

Untitled

Jan 18th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.59 KB | None | 0 0
  1. public class FolderController {
  2.     private static final int SUCCESSFULOPERATION = 200;
  3.     private static final int CREATED = 201;
  4.     private static final int DELETED = 204;
  5.     private static final int UNSUCCESSFULAUTHORIZATION= 401;
  6.     private static final int INVALIDPATHPARAMETER = 405;
  7.     private static final int FOLDERALREADYEXISTS = 402;
  8.     private static final int FOLDERDOESNOTEXIST = 403;
  9.  
  10.     private final FolderMetadataDao folderMetadataRepostiory;
  11.     private final FileMetadataDao fileMetadataRepository;
  12.     private final FileContentsDao fileContentsRepository;
  13.     private final SessionDataDao sessionDataRepository;
  14.     private final Gson gson = new Gson();
  15.     private final Configuration configuration;
  16.  
  17.     public FolderController(FolderMetadataDao folderMetadataRepostiory,
  18.                             FileMetadataDao fileMetadataRepository,
  19.                             FileContentsDao fileContentsRepository,
  20.                             SessionDataDao sessionDataRepository,
  21.                             Configuration configuration) {
  22.         this.folderMetadataRepostiory = folderMetadataRepostiory;
  23.         this.fileMetadataRepository = fileMetadataRepository;
  24.         this.fileContentsRepository = fileContentsRepository;
  25.         this.sessionDataRepository = sessionDataRepository;
  26.         this.configuration = configuration;
  27.     }
  28.  
  29.     public FolderMetadata handleCreateNewFolder(Request request, Response response){
  30.         try{
  31.             PathManager pathManager = new PathManager(request.params(":path"));
  32.             String session = request.params(":session");
  33.             if(sessionDataRepository.fetchBySessionId(request.params(":session")).get(0).getUserId()
  34.                     .equals(fileMetadataRepository.fetchByFolderPath(pathManager.getPath()).get(0).getOwnerId())) {
  35.                 if(folderMetadataRepostiory.fetchByPathDisplay(pathManager.getPath()).isEmpty()){
  36.                     Integer parentId = folderMetadataRepostiory.fetchByPathDisplay(pathManager.getParentPath()).get(0).getFolderId();
  37.                     Integer userId = sessionDataRepository.fetchBySessionId(session).get(0).getUserId();
  38.                     FolderMetadata folderMetadata = new FolderMetadata(null,
  39.                             pathManager.getFileName(),
  40.                             pathManager.getPathToLowerCase(),
  41.                             pathManager.getPath(),
  42.                             parentId,
  43.                             LocalDateTime.now().format(DateTimeFormatter.ofPattern("%Y-%m-%dT%H:%M:%SZ")),
  44.                             userId);
  45.                     FolderMetadata inserted = folderMetadataRepostiory.insertFolder(folderMetadata);
  46.                     response.status(CREATED);
  47.                     return inserted;
  48.                 }
  49.                 else{
  50.                     response.status(FOLDERALREADYEXISTS);
  51.                     throw new FileAlreadyExists("Folder with path: " + pathManager.getPath() + " already exists");
  52.                 }
  53.             }
  54.             else{
  55.                 response.status(UNSUCCESSFULAUTHORIZATION);
  56.                 throw new UnsuccessfulAuthorization("Unsuccessful authorization");
  57.             }
  58.         }
  59.         catch(WrongPathFormat ex){
  60.             response.status(INVALIDPATHPARAMETER);
  61.             throw ex;
  62.         }
  63.     }
  64.  
  65.     public void handleDeleteFolder(Request request, Response response){
  66.         try{
  67.             PathManager path = new PathManager(request.params(":path"));
  68.             if(sessionDataRepository.fetchBySessionId(request.params(":session")).get(0).getUserId()
  69.                     .equals(fileMetadataRepository.fetchByFolderPath(path.getPath()).get(0).getOwnerId())) {
  70.                 if(!(folderMetadataRepostiory.fetchByPathDisplay(path.getPath()).isEmpty())){
  71.                     folderMetadataRepostiory.deleteFoldersByPath(path.getPath());
  72.                     fileContentsRepository.deleteById(fileMetadataRepository.getFileIdFromPath(path.getPath()));
  73.                     fileMetadataRepository.deleteFromSpecifiedPath(path.getPath());
  74.                     response.status(DELETED);
  75.                 }
  76.                 else{
  77.                     response.status(FOLDERDOESNOTEXIST);
  78.                     throw new FileDoesNotExist("Folder with path: " + path.getPath() + " already exists");
  79.                 }
  80.             }
  81.             else{
  82.                 response.status(UNSUCCESSFULAUTHORIZATION);
  83.                 throw new UnsuccessfulAuthorization("Unsuccessful authorization");
  84.             }
  85.         }
  86.         catch(WrongPathFormat ex){
  87.             response.status(INVALIDPATHPARAMETER);
  88.             throw ex;
  89.         }
  90.     }
  91.  
  92.     public FolderMetadata handleGetMetadata(Request request, Response response){
  93.         try{
  94.             PathManager pathManager = new PathManager(request.params(":path"));
  95.             if(sessionDataRepository.fetchBySessionId(request.params(":session")).get(0).getUserId()
  96.                     .equals(fileMetadataRepository.fetchByFolderPath(pathManager.getPath()).get(0).getOwnerId())) {
  97.                 if(!(folderMetadataRepostiory.fetchByPathDisplay(pathManager.getPath()).isEmpty())){
  98.                     response.status(SUCCESSFULOPERATION);
  99.                     return folderMetadataRepostiory.fetchByPathDisplay(pathManager.getPath()).get(0);
  100.                 }
  101.                 else{
  102.                     response.status(FOLDERDOESNOTEXIST);
  103.                     throw new FileDoesNotExist("Folder with path: " + pathManager.getPath() + " does not exist");
  104.                 }
  105.             }
  106.             else{
  107.                 response.status(UNSUCCESSFULAUTHORIZATION);
  108.                 throw new UnsuccessfulAuthorization("Unsuccessful authorization");
  109.             }
  110.         }
  111.         catch(WrongPathFormat ex){
  112.             response.status(INVALIDPATHPARAMETER);
  113.             throw ex;
  114.         }
  115.     }
  116.  
  117.     public FolderMetadata handleMoveFolder(Request request, Response response){
  118.         try{
  119.             PathManager path = new PathManager(request.params(":path"));
  120.             PathManager newPath = new PathManager(request.params(":new_path"));
  121.             if(sessionDataRepository.fetchBySessionId(request.params(":session")).get(0).getUserId()
  122.                     .equals(fileMetadataRepository.fetchByFolderPath(path.getPath()).get(0).getOwnerId())) {
  123.                 if(!(folderMetadataRepostiory.fetchByPathDisplay(path.getPath()).isEmpty())){
  124.                     fileMetadataRepository.moveFiles(path.getPath(), newPath.getPath());
  125.                     folderMetadataRepostiory.moveFolder(path.getPath(), newPath.getPath(), newPath.getFileName(), folderMetadataRepostiory.fetchByPathDisplay(newPath.getParentPath()).get(0).getFolderId());
  126.                     FolderMetadata tmp  = folderMetadataRepostiory.fetchByPathDisplay(newPath.getPath()).get(0);
  127.                     return tmp;
  128.                 }
  129.                 else{
  130.                     response.status(FOLDERDOESNOTEXIST);
  131.                     throw new FileDoesNotExist("Folder with path: " + path.getPath() + " does not exist");
  132.                 }
  133.             }
  134.             else{
  135.                 response.status(UNSUCCESSFULAUTHORIZATION);
  136.                 throw new UnsuccessfulAuthorization("Unsuccessful authorization");
  137.             }
  138.         }
  139.         catch(WrongPathFormat ex){
  140.             response.status(INVALIDPATHPARAMETER);
  141.             throw ex;
  142.         }
  143.     }
  144.  
  145.     public Folder handleListFolderContent(Request request, Response response){
  146.         try{
  147.             PathManager path = new PathManager(request.params(":path"));
  148.             if(sessionDataRepository.fetchBySessionId(request.params(":session")).get(0).getUserId()
  149.                     .equals(fileMetadataRepository.fetchByFolderPath(path.getPath()).get(0).getOwnerId())) {
  150.                 if(!(folderMetadataRepostiory.fetchByPathDisplay(path.getPath()).isEmpty())){
  151.                     boolean recursive = Boolean.valueOf(request.params(":recursive"));
  152.                     return new Folder(folderMetadataRepostiory.fetchByPathDisplay(path.getPath()).get(0), recursive, configuration);
  153.                 }
  154.                 else{
  155.                     response.status(FOLDERDOESNOTEXIST);
  156.                     throw new FileDoesNotExist("Folder with path: " + path.getPath() + " does not exist");
  157.                 }
  158.             }
  159.             else{
  160.                 response.status(UNSUCCESSFULAUTHORIZATION);
  161.                 throw new UnsuccessfulAuthorization("Unsuccessful authorization");
  162.             }
  163.         }
  164.         catch(WrongPathFormat ex){
  165.             response.status(INVALIDPATHPARAMETER);
  166.             throw ex;
  167.         }
  168.     }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement