Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FolderController {
- private static final int SUCCESSFULOPERATION = 200;
- private static final int CREATED = 201;
- private static final int DELETED = 204;
- private static final int UNSUCCESSFULAUTHORIZATION= 401;
- private static final int INVALIDPATHPARAMETER = 405;
- private static final int FOLDERALREADYEXISTS = 402;
- private static final int FOLDERDOESNOTEXIST = 403;
- private final FolderMetadataDao folderMetadataRepostiory;
- private final FileMetadataDao fileMetadataRepository;
- private final FileContentsDao fileContentsRepository;
- private final SessionDataDao sessionDataRepository;
- private final Gson gson = new Gson();
- private final Configuration configuration;
- public FolderController(FolderMetadataDao folderMetadataRepostiory,
- FileMetadataDao fileMetadataRepository,
- FileContentsDao fileContentsRepository,
- SessionDataDao sessionDataRepository,
- Configuration configuration) {
- this.folderMetadataRepostiory = folderMetadataRepostiory;
- this.fileMetadataRepository = fileMetadataRepository;
- this.fileContentsRepository = fileContentsRepository;
- this.sessionDataRepository = sessionDataRepository;
- this.configuration = configuration;
- }
- public FolderMetadata handleCreateNewFolder(Request request, Response response){
- try{
- PathManager pathManager = new PathManager(request.params(":path"));
- String session = request.params(":session");
- if(sessionDataRepository.fetchBySessionId(request.params(":session")).get(0).getUserId()
- .equals(fileMetadataRepository.fetchByFolderPath(pathManager.getPath()).get(0).getOwnerId())) {
- if(folderMetadataRepostiory.fetchByPathDisplay(pathManager.getPath()).isEmpty()){
- Integer parentId = folderMetadataRepostiory.fetchByPathDisplay(pathManager.getParentPath()).get(0).getFolderId();
- Integer userId = sessionDataRepository.fetchBySessionId(session).get(0).getUserId();
- FolderMetadata folderMetadata = new FolderMetadata(null,
- pathManager.getFileName(),
- pathManager.getPathToLowerCase(),
- pathManager.getPath(),
- parentId,
- LocalDateTime.now().format(DateTimeFormatter.ofPattern("%Y-%m-%dT%H:%M:%SZ")),
- userId);
- FolderMetadata inserted = folderMetadataRepostiory.insertFolder(folderMetadata);
- response.status(CREATED);
- return inserted;
- }
- else{
- response.status(FOLDERALREADYEXISTS);
- throw new FileAlreadyExists("Folder with path: " + pathManager.getPath() + " already exists");
- }
- }
- else{
- response.status(UNSUCCESSFULAUTHORIZATION);
- throw new UnsuccessfulAuthorization("Unsuccessful authorization");
- }
- }
- catch(WrongPathFormat ex){
- response.status(INVALIDPATHPARAMETER);
- throw ex;
- }
- }
- public void handleDeleteFolder(Request request, Response response){
- try{
- PathManager path = new PathManager(request.params(":path"));
- if(sessionDataRepository.fetchBySessionId(request.params(":session")).get(0).getUserId()
- .equals(fileMetadataRepository.fetchByFolderPath(path.getPath()).get(0).getOwnerId())) {
- if(!(folderMetadataRepostiory.fetchByPathDisplay(path.getPath()).isEmpty())){
- folderMetadataRepostiory.deleteFoldersByPath(path.getPath());
- fileContentsRepository.deleteById(fileMetadataRepository.getFileIdFromPath(path.getPath()));
- fileMetadataRepository.deleteFromSpecifiedPath(path.getPath());
- response.status(DELETED);
- }
- else{
- response.status(FOLDERDOESNOTEXIST);
- throw new FileDoesNotExist("Folder with path: " + path.getPath() + " already exists");
- }
- }
- else{
- response.status(UNSUCCESSFULAUTHORIZATION);
- throw new UnsuccessfulAuthorization("Unsuccessful authorization");
- }
- }
- catch(WrongPathFormat ex){
- response.status(INVALIDPATHPARAMETER);
- throw ex;
- }
- }
- public FolderMetadata handleGetMetadata(Request request, Response response){
- try{
- PathManager pathManager = new PathManager(request.params(":path"));
- if(sessionDataRepository.fetchBySessionId(request.params(":session")).get(0).getUserId()
- .equals(fileMetadataRepository.fetchByFolderPath(pathManager.getPath()).get(0).getOwnerId())) {
- if(!(folderMetadataRepostiory.fetchByPathDisplay(pathManager.getPath()).isEmpty())){
- response.status(SUCCESSFULOPERATION);
- return folderMetadataRepostiory.fetchByPathDisplay(pathManager.getPath()).get(0);
- }
- else{
- response.status(FOLDERDOESNOTEXIST);
- throw new FileDoesNotExist("Folder with path: " + pathManager.getPath() + " does not exist");
- }
- }
- else{
- response.status(UNSUCCESSFULAUTHORIZATION);
- throw new UnsuccessfulAuthorization("Unsuccessful authorization");
- }
- }
- catch(WrongPathFormat ex){
- response.status(INVALIDPATHPARAMETER);
- throw ex;
- }
- }
- public FolderMetadata handleMoveFolder(Request request, Response response){
- try{
- PathManager path = new PathManager(request.params(":path"));
- PathManager newPath = new PathManager(request.params(":new_path"));
- if(sessionDataRepository.fetchBySessionId(request.params(":session")).get(0).getUserId()
- .equals(fileMetadataRepository.fetchByFolderPath(path.getPath()).get(0).getOwnerId())) {
- if(!(folderMetadataRepostiory.fetchByPathDisplay(path.getPath()).isEmpty())){
- fileMetadataRepository.moveFiles(path.getPath(), newPath.getPath());
- folderMetadataRepostiory.moveFolder(path.getPath(), newPath.getPath(), newPath.getFileName(), folderMetadataRepostiory.fetchByPathDisplay(newPath.getParentPath()).get(0).getFolderId());
- FolderMetadata tmp = folderMetadataRepostiory.fetchByPathDisplay(newPath.getPath()).get(0);
- return tmp;
- }
- else{
- response.status(FOLDERDOESNOTEXIST);
- throw new FileDoesNotExist("Folder with path: " + path.getPath() + " does not exist");
- }
- }
- else{
- response.status(UNSUCCESSFULAUTHORIZATION);
- throw new UnsuccessfulAuthorization("Unsuccessful authorization");
- }
- }
- catch(WrongPathFormat ex){
- response.status(INVALIDPATHPARAMETER);
- throw ex;
- }
- }
- public Folder handleListFolderContent(Request request, Response response){
- try{
- PathManager path = new PathManager(request.params(":path"));
- if(sessionDataRepository.fetchBySessionId(request.params(":session")).get(0).getUserId()
- .equals(fileMetadataRepository.fetchByFolderPath(path.getPath()).get(0).getOwnerId())) {
- if(!(folderMetadataRepostiory.fetchByPathDisplay(path.getPath()).isEmpty())){
- boolean recursive = Boolean.valueOf(request.params(":recursive"));
- return new Folder(folderMetadataRepostiory.fetchByPathDisplay(path.getPath()).get(0), recursive, configuration);
- }
- else{
- response.status(FOLDERDOESNOTEXIST);
- throw new FileDoesNotExist("Folder with path: " + path.getPath() + " does not exist");
- }
- }
- else{
- response.status(UNSUCCESSFULAUTHORIZATION);
- throw new UnsuccessfulAuthorization("Unsuccessful authorization");
- }
- }
- catch(WrongPathFormat ex){
- response.status(INVALIDPATHPARAMETER);
- throw ex;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement