1414import pl .dfs .distributedfilesystem .folders .FoldersRepository ;
1515import pl .dfs .distributedfilesystem .models .DataNodeOnTheList ;
1616import pl .dfs .distributedfilesystem .models .ObjectOnTheList ;
17+ import pl .dfs .distributedfilesystem .models .ToChange ;
18+ import pl .dfs .distributedfilesystem .models .ToChangeRepository ;
1719import pl .dfs .distributedfilesystem .nodes .DataNodesRepository ;
1820import pl .dfs .distributedfilesystem .files .FilesRepository ;
1921import pl .dfs .distributedfilesystem .files .SingleFile ;
@@ -37,6 +39,9 @@ public class FilesAccessController {
3739 @ Autowired
3840 FoldersRepository foldersRepository ;
3941
42+ @ Autowired
43+ ToChangeRepository toChangeRepository ;
44+
4045
4146 @ RequestMapping ("/" )
4247 public String mainPage (Model model , HttpSession session ) {
@@ -183,7 +188,6 @@ else if(filename.endsWith(".zip"))
183188 public String fileUpload (@ RequestParam ("file" ) MultipartFile file ,HttpSession session ,HttpServletRequest request ) {
184189 checkSession (session );
185190 if (dataNodesRepository .getNumber ()!=0 ) {
186-
187191 if (!file .isEmpty ()) {
188192 if (!filesRepository .checkIfExist (session .getAttribute ("path" ) + file .getOriginalFilename () + "/" )) {
189193 try {
@@ -392,27 +396,40 @@ public String changeFolder(@RequestParam String file,@RequestParam String folder
392396
393397 @ RequestMapping ("changeFolderName" )
394398 public String changeFolderName (Model model , HttpSession session ,@ RequestParam String oldFolderName ,@ RequestParam String newFolderName ) {
395- //
396- // ArrayList<String> subfoldersWithoutSelectedOne = foldersRepository.subfoldersOfFolder(session.getAttribute("path").toString());
397- // subfoldersWithoutSelectedOne.remove(oldFolderName);
398- // if(subfoldersWithoutSelectedOne.contains(newFolderName)) {
399- // session.setAttribute("error","folderNameAlreadyExists");
400- // return "redirect:/";
401- // }
402- // else {
403- // String path = session.getAttribute("path").toString();
404- //
405- // foldersRepository.renameFolder(path,oldFolderName,newFolderName);
406- //
407- // for(int i = 0; i < filesRepository.getAllFiles().size();i++) {
408- // if(filesRepository.getAllFiles().get(i).getPath().startsWith(path + oldFolderName + "/")) {
409- // filesRepository.getAllFiles().get(i).setPath(filesRepository.getAllFiles().get(i).getPath().replaceFirst(path + oldFolderName + "/",path + newFolderName + "/"));
410- // }
411- // }
412- //
413- // filesRepository.writeFilesInformationFile();
399+
400+ ArrayList <String > subfoldersWithoutSelectedOne = foldersRepository .subfoldersOfFolder (session .getAttribute ("path" ).toString ());
401+ subfoldersWithoutSelectedOne .remove (oldFolderName );
402+ if (subfoldersWithoutSelectedOne .contains (newFolderName )) {
403+ session .setAttribute ("error" ,"folderNameAlreadyExists" );
404+ return "redirect:/" ;
405+ }
406+ else {
407+ String path = session .getAttribute ("path" ).toString ();
408+
409+ foldersRepository .renameFolder (path ,oldFolderName ,newFolderName );
410+
411+ for (int i = 0 ; i < filesRepository .getAllFiles ().size ();i ++) {
412+ if (filesRepository .getAllFiles ().get (i ).getPath ().startsWith (path + oldFolderName + "/" )) {
413+ filesRepository .getAllFiles ().get (i ).setPath (filesRepository .getAllFiles ().get (i ).getPath ().replaceFirst (path + oldFolderName + "/" ,path + newFolderName + "/" ));
414+ }
415+ }
416+
417+ for (int i = 0 ; i < dataNodesRepository .getNumber ();i ++) {
418+ String address = "" ;
419+ try {
420+ address = dataNodesRepository .get (i ).getAddress ();
421+ dataNodesRepository .get (address ).writeString ("renameFolder " );
422+ dataNodesRepository .get (address ).writeString ("\" " + session .getAttribute ("path" ) + oldFolderName + "/" + "\" " );
423+ dataNodesRepository .get (address ).writeString ("\" " + session .getAttribute ("path" ) + newFolderName + "/" + "\" " );
424+ dataNodesRepository .get (address ).writeFlush ();
425+ String response = dataNodesRepository .get (address ).readResponse ();
426+ } catch (Exception e ) {
427+ toChangeRepository .add (new ToChange (address ,"renameFolder " ,"\" " + session .getAttribute ("path" ) + oldFolderName + "/" + "\" " ,"\" " + session .getAttribute ("path" ) + newFolderName + "/" + "\" " ));
428+ }
429+ }
430+ filesRepository .writeFilesInformationFile ();
414431 return "redirect:/" ;
415- // }
432+ }
416433 }
417434
418435 @ RequestMapping ("changeFileName" )
@@ -434,7 +451,7 @@ public String changeFileName(Model model, HttpSession session,@RequestParam Stri
434451 for (int j = divided .size ()-1 ;j >=0 ;j --) {
435452 String address = divided .get (j );
436453 try {
437- dataNodesRepository .get (address ).writeString ("rename " );
454+ dataNodesRepository .get (address ).writeString ("renameFile " );
438455 dataNodesRepository .get (address ).writeString ("\" " +session .getAttribute ("path" ) + oldFileName + "/" + "\" " );
439456 dataNodesRepository .get (address ).writeString ("\" " +session .getAttribute ("path" ) + newFileName + "/" + "\" " );
440457 dataNodesRepository .get (address ).writeFlush ();
@@ -443,8 +460,8 @@ public String changeFileName(Model model, HttpSession session,@RequestParam Stri
443460 divided .remove (i );
444461 }
445462 } catch (Exception e ){
446- // filesRepository.toDelete.add(new Pair<>(filename,address));
447- // filesRepository.rewriteFileToDelete( );
463+ //TODO Spójność zmiany nazwy między węzłami
464+ toChangeRepository . add ( new ToChange ( address , "renameFile " , " \" " + session . getAttribute ( "path" ) + oldFileName + "/" + " \" " , " \" " + session . getAttribute ( "path" ) + newFileName + "/" + " \" " ) );
448465 }
449466 }
450467
0 commit comments