List of usage examples for org.apache.commons.io FileUtils moveDirectoryToDirectory
public static void moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) throws IOException
From source file:org.interreg.docexplore.datalink.fs2.DeleteFS2BooksAction.java
public void undoAction() throws Exception { File root = ((DataLinkFS2) link.getLink()).getFile(); for (File bookDir : cacheDir.listFiles()) if (bookDir.isDirectory() && bookDir.getName().startsWith("book")) FileUtils.moveDirectoryToDirectory(bookDir, root, true); for (Book book : books) link.books.put(book.getId(), book); }
From source file:org.interreg.docexplore.datalink.fs2.DeleteFS2PagesAction.java
public void doAction() throws Exception { File root = ((DataLinkFS2) link.getLink()).getFile(); newPageNumbers.clear();//from w ww . ja v a 2 s .c om for (Page page : pages) { File pageDir = PageFS2.getPageDir(root, book.getId(), page.getPageNumber()); FileUtils.moveDirectoryToDirectory(pageDir, cacheDir, true); } Collection<File> files = orderedPages(BookFS2.getBookDir(root, book.getId())); int curNum = 1; NavigableMap<Integer, Page> newPagesByNumber = new TreeMap<Integer, Page>(); for (File file : files) { int pageNum = Integer.parseInt(file.getName().substring("page".length())); if (curNum != pageNum) { file.renameTo(new File(file.getParentFile(), "page" + curNum)); Page page = book.pagesByNumber.get(pageNum); if (page != null) page.pageNum = curNum; book.pagesByNumber.remove(pageNum); newPagesByNumber.put(curNum, page); } else newPagesByNumber.put(pageNum, book.pagesByNumber.get(pageNum)); newPageNumbers.put(pageNum, curNum); curNum++; } book.pagesByNumber = newPagesByNumber; }
From source file:org.interreg.docexplore.datalink.fs2.DeleteFS2PagesAction.java
public void undoAction() throws Exception { File root = ((DataLinkFS2) link.getLink()).getFile(); for (Map.Entry<Integer, Integer> entry : newPageNumbers.descendingMap().entrySet()) { File file = PageFS2.getPageDir(root, book.getId(), entry.getValue()); file.renameTo(new File(file.getParentFile(), "page" + entry.getKey())); Page page = book.pagesByNumber.get(entry.getValue()); if (page != null) page.pageNum = entry.getKey(); book.pagesByNumber.remove(entry.getValue()); book.pagesByNumber.put(entry.getKey(), page); }/*www.j av a2 s . co m*/ Collection<File> files = orderedPages(cacheDir); for (File file : files) FileUtils.moveDirectoryToDirectory(file, BookFS2.getBookDir(root, book.getId()), true); for (Page page : pages) book.pagesByNumber.put(page.pageNum, page); }
From source file:org.interreg.docexplore.datalink.fs2.DeleteFS2RegionsAction.java
public void doAction() throws Exception { dirs = new HashMap<Region, File>(); for (Region region : regions) { Page page = region.getPage();/*ww w. j a va 2 s. c o m*/ File dir = RegionFS2.getRegionDir(link.root, page.getBook().getId(), page.getPageNumber(), region.getId()); FileUtils.moveDirectoryToDirectory(dir, cacheDir, true); page.regions.remove(region.getId()); dirs.put(region, dir); } }
From source file:org.interreg.docexplore.datalink.fs2.DeleteFS2RegionsAction.java
public void undoAction() throws Exception { for (Region region : dirs.keySet()) { File dir = dirs.get(region); FileUtils.moveDirectoryToDirectory(new File(cacheDir, dir.getName()), dir.getParentFile(), true); region.getPage().regions.put(region.getId(), region); }/*from w ww .j a v a2 s .com*/ }
From source file:org.jahia.utils.maven.plugin.osgi.ConvertToOSGiMojo.java
@Override public void doExecute() throws MojoExecutionException, MojoFailureException { ScmManager scmManager = null;/*from ww w .j a v a 2 s . c o m*/ File pomXmlFile = new File(baseDir, "pom.xml"); String scmURL = null; Reader reader = null; ScmRepository scmRepository = null; try { reader = new FileReader(pomXmlFile); Model model = new MavenXpp3Reader().read(reader); final Scm scm = model.getScm(); scmURL = scm != null ? scm.getConnection() : null; } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(reader); } if (scmURL != null && !"dummy".equalsIgnoreCase(ScmUrlUtils.getProvider(scmURL))) { try { scmManager = new BasicScmManager(); scmManager.setScmProvider("svn", new SvnExeScmProvider()); scmManager.setScmProvider("git", new GitExeScmProvider()); scmRepository = scmManager.makeScmRepository(scmURL); } catch (ScmRepositoryException e) { e.printStackTrace(); } catch (NoSuchScmProviderException e) { e.printStackTrace(); } } File webapp = new File(baseDir, "src/main/webapp"); File resources = new File(baseDir, "src/main/resources"); try { if (resources.exists()) { File oldWorkflowDir = new File(resources, "org/jahia/services/workflow"); File newWorkflowDirParent = new File(resources, "org/jahia/modules/custom"); if (oldWorkflowDir.exists()) { ScmFileSet filesToRemove = new ScmFileSet(oldWorkflowDir, null, null); getLog().info("Moving " + oldWorkflowDir + " to " + newWorkflowDirParent + "..."); FileUtils.moveDirectoryToDirectory(oldWorkflowDir, newWorkflowDirParent, true); if (scmRepository != null) { scmManager.remove(scmRepository, filesToRemove, "remove workflow dir"); scmManager.add(scmRepository, new ScmFileSet(newWorkflowDirParent, null, null)); } } } if (webapp.exists()) { List<File> filesToRemove = listFilesAndDirectories(webapp); getLog().info("Removing " + new File(webapp, "WEB-INF/web.xml") + " no longer needed..."); File webXml = new File(webapp, "WEB-INF/web.xml"); FileUtils.deleteQuietly(webXml); getLog().info("Moving contents of directory " + webapp + " into directory " + resources + "..."); moveWithMerge(webapp, resources); if (scmRepository != null) { scmManager.add(scmRepository, new ScmFileSet(new File(""), resources)); List<File> filesToAdd = listFilesAndDirectories(resources); scmManager.add(scmRepository, new ScmFileSet(resources, filesToAdd), "add resources files"); scmManager.remove(scmRepository, new ScmFileSet(webapp, filesToRemove), "remove webapps files"); } } getLog().info("Performing Maven project modifications..."); parsePom(); if (scmRepository != null) scmManager.add(scmRepository, new ScmFileSet(new File(""), pomXmlFile)); if (performMigration) { getLog().info("Performing needed migration modifications"); } else { getLog().info("Checking for migration issues..."); } List<String> messages = checkForMigrationIssues(baseDir, performMigration); if (messages.size() > 0) { getLog().info( "====================================================================================================================="); getLog().info("Transformation messages:"); getLog().info( "---------------------------------------------------------------------------------------------------------------------"); for (String message : messages) { getLog().info(message); } getLog().info( "---------------------------------------------------------------------------------------------------------------------"); if (!performMigration) { getLog().info( "None of the source code files were modified. If you would like to have the goal convert the files, please"); getLog().info("relaunch the convert-to-osgi goal using the following parameter : "); getLog().info(" mvn jahia:convert-to-osgi -Djahia.osgi.conversion.performMigration=true"); getLog().info( "---------------------------------------------------------------------------------------------------------------------"); } else { getLog().info( "Source files were modified. Please review all code changes to make sure everything is ok as detection may have "); getLog().info( "matched false statements (100% automatic conversion is not guaranteed by this tool."); getLog().info( "---------------------------------------------------------------------------------------------------------------------"); } } } catch (DocumentException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (ScmException e) { e.printStackTrace(); } }
From source file:org.jahia.utils.maven.plugin.osgi.ConvertToOSGiMojo.java
private void moveWithMerge(File src, File dst) throws IOException { // @todo this doesn't handle SVN directories properly File[] files = src.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return !name.startsWith("."); }/*from ww w .j a va2 s . c om*/ }); for (File file : files) { if (file.isDirectory()) { File subDir = new File(dst, file.getName()); if (subDir.exists()) { moveWithMerge(file, subDir); } else { FileUtils.moveDirectoryToDirectory(file, dst, true); } } else { FileUtils.moveFileToDirectory(file, dst, true); } } }
From source file:org.messic.server.api.APIAlbum.java
public long createOrUpdateAlbum(User user, Album album) throws IOException, ExistingMessicException { Long sidResult = 0l;/* w w w. j a v a 2 s . com*/ MDOGenre mdoGenre = null; MDOAlbum mdoAlbum = null; MDOAuthor mdoAuthor = null; MDOUser mdouser = daoUser.getUserByLogin(user.getLogin()); char replacementChar = daoSettings.getSettings().getIllegalCharacterReplacement(); MDOMessicSettings settings = daoSettings.getSettings(); // 1st getting genre ############################################################################### if (album.getGenre() != null && album.getGenre().getSid() != null) { mdoGenre = daoGenre.get(album.getGenre().getSid()); } if (mdoGenre == null) { if (album.getGenre() != null && album.getGenre().getName() != null && album.getGenre().getName().trim().length() > 0) { mdoGenre = daoGenre.getByName(user.getLogin(), album.getGenre().getName()); } } if (mdoGenre == null && album.getGenre() != null && album.getGenre().getName() != null && album.getGenre().getName().trim().length() > 0) { mdoGenre = new MDOGenre(album.getGenre().getName(), mdouser); } // 2nd getting the album if exist // ############################################################################### if (album.getSid() > 0) { mdoAlbum = daoAlbum.get(album.getSid()); } // 3rd getting the author ############################################################################### if (album.getAuthor().getSid() > 0) { // trying by sid mdoAuthor = daoAuthor.get(user.getLogin(), album.getAuthor().getSid()); } if (mdoAuthor == null) { // trying by name mdoAuthor = daoAuthor.getByName(album.getAuthor().getName(), user.getLogin()); } if (mdoAuthor != null) { // an existing album from this autor?? if (mdoAlbum == null) { mdoAlbum = daoAlbum.getByName(mdoAuthor.getName(), album.getName(), user.getLogin()); } } // let's create a new author if (mdoAuthor == null) { mdoAuthor = new MDOAuthor(); mdoAuthor.setName(album.getAuthor().getName().trim()); mdoAuthor.setOwner(mdouser); mdoAuthor.setLocation( Util.replaceIllegalFilenameCharacters(album.getAuthor().getName(), replacementChar)); } // 4th new album if none ############################################################################### if (mdoAlbum == null) { mdoAlbum = new MDOAlbum(); } boolean flagExistingAuthor = (mdoAuthor.getSid() != null && mdoAuthor.getSid() > 0); boolean flagAuthorNameChanged = false; boolean flagExistingAlbum = (mdoAlbum.getSid() != null && mdoAlbum.getSid() > 0); // checking if the user is trying to create a new album with the same name and author of another existing one. // the reason to forbide this is because of the filesystem. Both albums will have the same filesystem path, and // will merge their content!! panic!! List<MDOAlbum> albumsSameName = daoAlbum.findAlbum(album.getName(), user.getLogin()); if (albumsSameName.size() > 0) { for (MDOAlbum mdoAlbumSameName : albumsSameName) { if (mdoAlbumSameName.getSid() != mdoAlbum.getSid()) { String authorName = mdoAlbumSameName.getAuthor().getName(); if (authorName.toUpperCase().equals(album.getAuthor().getName().toUpperCase())) { throw new ExistingMessicException(); } } } } // old album path if the album was an existing one String oldAlbumPath = null; if (flagExistingAlbum) { oldAlbumPath = mdoAlbum.calculateAbsolutePath(settings); } // 5th updating / creating the album // ############################################################################### // if its an existing author and the name of the author has changed...wow!! we must do more things if (flagExistingAuthor && !album.getAuthor().getName().trim().toUpperCase() .equals(mdoAuthor.getName().trim().toUpperCase())) { // the name of the author has changed!!! flagAuthorNameChanged = true; mdoAuthor.setName(album.getAuthor().getName()); mdoAuthor.setLocation( Util.replaceIllegalFilenameCharacters(album.getAuthor().getName(), replacementChar)); } mdoAlbum.setName(album.getName()); mdoAlbum.setLocation(Util.replaceIllegalFilenameCharacters(album.getName(), replacementChar)); mdoAlbum.setAuthor(mdoAuthor); mdoAlbum.setComments(album.getComments()); mdoAlbum.setGenre(mdoGenre); mdoAlbum.setOwner(mdouser); mdoAlbum.setYear(album.getYear()); // 6th saving author, genre and album // ############################################################################### daoAuthor.save(mdoAuthor); if (mdoGenre != null) { daoGenre.save(mdoGenre); } daoAlbum.save(mdoAlbum); sidResult = mdoAlbum.getSid(); if (sidResult <= 0) { mdoAlbum = daoAlbum.merge(mdoAlbum); sidResult = mdoAlbum.getSid(); } // 7th moving album resources to definitive location // ############################################################################### String currentAlbumPath = mdoAlbum.calculateAbsolutePath(settings); File currentAlbumPathFile = new File(currentAlbumPath); String userTmpPath = mdouser.calculateTmpPath(settings, album.getCode()); // creating album path currentAlbumPathFile.mkdirs(); // 7.1 - Songs resources if (album.getSongs() != null && album.getSongs().size() > 0) { List<Song> songs = album.getSongs(); for (Song song : songs) { MDOSong mdoSong = new MDOSong(); File fnew = null; if (song.getSid() <= 0) { MDOSongStatistics ss = new MDOSongStatistics(); ss.setTimesplayed(0); ss.setTimesstopped(0); daoSongStatistics.save(ss); // new song mdoSong.setStatistics(ss); mdoSong.setTrack(song.getTrack()); mdoSong.setName(song.getName()); String secureExtension = song.calculateSecureExtension(replacementChar); String theoricalFileName = mdoSong.calculateSongTheoricalFileName(secureExtension, replacementChar); mdoSong.setLocation(theoricalFileName); mdoSong.setOwner(mdouser); mdoSong.setAlbum(mdoAlbum); daoSong.save(mdoSong); // moving resource to the new location File tmpRes = new File( userTmpPath + File.separatorChar + song.calculateSecureFileName(replacementChar)); fnew = new File(mdoSong.calculateAbsolutePath(settings)); if (fnew.exists()) { fnew.delete(); } FileUtils.moveFile(tmpRes, fnew); } else { // existing song... mdoSong = daoSong.get(user.getLogin(), song.getSid()); if (mdoSong != null) { mdoSong.setTrack(song.getTrack()); mdoSong.setName(song.getName()); String oldLocation = mdoSong.calculateAbsolutePath(settings); String theoricalFileName = mdoSong.calculateSongTheoricalFileName(mdoSong.getExtension(), replacementChar); mdoSong.setLocation(theoricalFileName); daoSong.save(mdoSong); File fold = new File(oldLocation); fnew = new File(mdoSong.calculateAbsolutePath(settings)); if (!fold.getAbsolutePath().equals(fnew.getAbsolutePath())) { FileUtils.moveFile(fold, fnew); } } } AudioTaggerTAGWizardPlugin atp = new AudioTaggerTAGWizardPlugin(); org.messic.server.api.tagwizard.service.Album salbum = new org.messic.server.api.tagwizard.service.Album(); salbum.author = mdoAlbum.getAuthor().getName(); salbum.name = mdoAlbum.getName(); if (mdoAlbum.getComments() != null) salbum.comments = mdoAlbum.getComments(); if (mdoAlbum.getGenre() != null) salbum.genre = mdoAlbum.getGenre().getName(); salbum.year = mdoAlbum.getYear(); org.messic.server.api.tagwizard.service.Song ssong = new org.messic.server.api.tagwizard.service.Song(); ssong.track = mdoSong.getTrack(); ssong.name = mdoSong.getName(); try { atp.saveTags(salbum, ssong, fnew); } catch (CannotReadException e) { log.error(e); // throw new IOException( e.getMessage(), e.getCause() ); } catch (TagException e) { log.error(e); // throw new IOException( e.getMessage(), e.getCause() ); } catch (ReadOnlyFileException e) { log.error(e); // throw new IOException( e.getMessage(), e.getCause() ); } catch (InvalidAudioFrameException e) { log.error(e); // throw new IOException( e.getMessage(), e.getCause() ); } catch (CannotWriteException e) { log.error(e); // throw new IOException( e.getMessage(), e.getCause() ); } } } // 7.2 - Artwork resources if (album.getArtworks() != null && album.getArtworks().size() > 0) { List<org.messic.server.api.datamodel.File> files = album.getArtworks(); for (org.messic.server.api.datamodel.File file : files) { if (file.getSid() <= 0) { MDOArtwork mdopr = new MDOArtwork(); mdopr.setLocation(file.calculateSecureFileName(replacementChar)); mdopr.setOwner(mdouser); mdopr.setAlbum(mdoAlbum); org.messic.server.api.datamodel.File fcover = album.getCover(); if (fcover != null && file.getFileName().equals(album.getCover().getFileName())) { mdopr.setCover(true); } daoPhysicalResource.save(mdopr); mdoAlbum.getArtworks().add(mdopr); // moving resource to the new location File tmpRes = new File( userTmpPath + File.separatorChar + file.calculateSecureFileName(replacementChar)); File newFile = new File( currentAlbumPath + File.separatorChar + file.calculateSecureFileName(replacementChar)); if (newFile.exists()) { newFile.delete(); } FileUtils.moveFileToDirectory(tmpRes, currentAlbumPathFile, false); } else { // existing artwork... MDOAlbumResource resource = daoAlbumResource.get(user.getLogin(), file.getSid()); if (resource != null) { String oldLocation = resource.calculateAbsolutePath(settings); resource.setLocation(file.calculateSecureFileName(replacementChar)); daoAlbumResource.save(resource); File fold = new File(oldLocation); File fnew = new File(resource.calculateAbsolutePath(settings)); if (!fold.getAbsolutePath().equals(fnew.getAbsolutePath())) { FileUtils.moveFile(fold, fnew); } } } } daoAlbum.save(mdoAlbum); } // 7.3 - Other resources if (album.getOthers() != null && album.getOthers().size() > 0) { List<org.messic.server.api.datamodel.File> files = album.getOthers(); for (org.messic.server.api.datamodel.File file : files) { if (file.getSid() <= 0) { MDOOtherResource mdopr = new MDOOtherResource(); mdopr.setLocation(file.calculateSecureFileName(replacementChar)); mdopr.setOwner(mdouser); mdopr.setAlbum(mdoAlbum); daoPhysicalResource.save(mdopr); mdoAlbum.getOthers().add(mdopr); // moving resource to the new location File tmpRes = new File( userTmpPath + File.separatorChar + file.calculateSecureFileName(replacementChar)); File newFile = new File(mdopr.calculateAbsolutePath(settings)); if (newFile.exists()) { newFile.delete(); } FileUtils.moveFileToDirectory(tmpRes, currentAlbumPathFile, false); } else { // existing artwork... MDOAlbumResource resource = daoAlbumResource.get(user.getLogin(), file.getSid()); if (resource != null) { String oldLocation = resource.calculateAbsolutePath(settings); resource.setLocation(file.calculateSecureFileName(replacementChar)); daoAlbumResource.save(resource); File fold = new File(oldLocation); File fnew = new File(resource.calculateAbsolutePath(settings)); if (!fold.getAbsolutePath().equals(fnew.getAbsolutePath())) { FileUtils.moveFile(fold, fnew); } } } } daoAlbum.save(mdoAlbum); } // let's see if the album was an existing one... then it should moved to the new location if (oldAlbumPath != null && !oldAlbumPath.equals(currentAlbumPath)) { List<MDOAlbumResource> resources = mdoAlbum.getAllResources(); for (int i = 0; i < resources.size(); i++) { MDOAlbumResource resource = resources.get(i); String resourceNewPath = resource.calculateAbsolutePath(settings); String resourceCurrentPath = oldAlbumPath + File.separatorChar + resource.getLocation(); File fnewPath = new File(resourceNewPath); File foldPath = new File(resourceCurrentPath); if (foldPath.exists()) { FileUtils.moveFile(foldPath, fnewPath); } } File fAlbumOldPath = new File(oldAlbumPath); FileUtils.deleteDirectory(fAlbumOldPath); // if the author have changed the name, we have only moved those resources from this album, but the author // could have other albums, we need to move also. if (flagAuthorNameChanged) { File newAuthorLocation = new File(mdoAuthor.calculateAbsolutePath(settings)); // we must move all the authors albums to the new one folder File oldAuthorFolder = fAlbumOldPath.getParentFile(); File[] oldfiles = oldAuthorFolder.listFiles(); for (File file2 : oldfiles) { if (file2.isDirectory()) { FileUtils.moveDirectoryToDirectory(file2, newAuthorLocation, true); } } // finally we remove the old location FileUtils.deleteDirectory(oldAuthorFolder); } } return sidResult; }
From source file:org.onebusaway.util.FileUtility.java
/** * Copy the sourceDirectory to the given destinationDirectory *///from w ww .j av a2 s .c o m public void moveDir(String sourceDirectoryPath, String destinationDirectoryPath) throws IOException { File sourceDir = new File(sourceDirectoryPath); File destinationDir = new File(destinationDirectoryPath); FileUtils.moveDirectoryToDirectory(sourceDir, destinationDir, true); }
From source file:org.openmicroscopy.shoola.util.file.TestIOUtil.java
/** Creates a directory with subfolder and zips it.*/ public void testZipDirectoryWithSubfolder() { try {//from ww w . ja v a 2 s . c om File dir = Files.createTempDir(); File f = File.createTempFile("test", ".tmp", dir); File subfolder = Files.createTempDir(); File f1 = File.createTempFile("test1", ".tmp", subfolder); FileUtils.moveDirectoryToDirectory(subfolder, dir, false); File zip = IOUtil.zipDirectory(dir); File destDir = Files.createTempDir(); boolean b = unzip(zip, destDir); assertEquals(true, b); File[] files = destDir.listFiles(); assertEquals(2, files.length); for (int i = 0; i < files.length; i++) { File ff = files[i]; if (ff.isFile()) { assertEquals(f.getName(), ff.getName()); } else { assertEquals(subfolder.getName(), ff.getName()); File[] list = ff.listFiles(); assertEquals(1, list.length); assertEquals(f1.getName(), list[0].getName()); } } //clean clean(dir); clean(zip); clean(destDir); } catch (Exception e) { fail(e.getMessage()); } }