/** Test getting list of all directories for a given user. */ public void testGetMediaFileDirectories() throws Exception { User testUser = null; Weblog testWeblog = null; testUser = TestUtils.setupUser("mediaFileTestUser2"); testWeblog = TestUtils.setupWeblog("mediaFileTestWeblog2", testUser); MediaFileManager mfMgr = WebloggerFactory.getWeblogger().getMediaFileManager(); // no need to create root directory, that is done automatically now MediaFileDirectory rootDirectory = mfMgr.getMediaFileRootDirectory(testWeblog); // MediaFileDirectory rootDirectory = new MediaFileDirectory(null, "root", "root d", // testWeblog); // mfMgr.createMediaFileDirectory(rootDirectory); MediaFileDirectory directory2 = new MediaFileDirectory(rootDirectory, "dir2", "directory 2", testWeblog); mfMgr.createMediaFileDirectory(directory2); rootDirectory.getChildDirectories().add(directory2); MediaFileDirectory directory3 = new MediaFileDirectory(rootDirectory, "dir3", "directory 3", testWeblog); mfMgr.createMediaFileDirectory(directory3); rootDirectory.getChildDirectories().add(directory3); TestUtils.endSession(true); testWeblog = TestUtils.getManagedWebsite(testWeblog); List<MediaFileDirectory> directories = mfMgr.getMediaFileDirectories(testWeblog); assertNotNull(directories); assertEquals(3, directories.size()); assertTrue(containsPath(directories, "/")); assertTrue(containsPath(directories, "/dir2")); assertTrue(containsPath(directories, "/dir3")); TestUtils.endSession(true); TestUtils.teardownWeblog(testWeblog.getId()); TestUtils.teardownUser(testUser.getUserName()); }
/** * Fetches and displays list of media file for the given directory. The directory could be chosen * by ID or path. * * @return String The result of the action. */ @SkipValidation public String execute() { MediaFileManager manager = WebloggerFactory.getWeblogger().getMediaFileManager(); try { MediaFileDirectory directory; if (StringUtils.isNotEmpty(this.directoryId)) { directory = manager.getMediaFileDirectory(this.directoryId); } else if (StringUtils.isNotEmpty(this.directoryPath)) { directory = manager.getMediaFileDirectoryByPath(getActionWeblog(), this.directoryPath); } else { directory = manager.getMediaFileRootDirectory(getActionWeblog()); } this.directoryId = directory.getId(); this.directoryPath = directory.getPath(); this.childDirectories = new ArrayList<MediaFileDirectory>(); this.childDirectories.addAll(directory.getChildDirectories()); this.childFiles = new ArrayList<MediaFile>(); this.childFiles.addAll(directory.getMediaFiles()); if ("type".equals(sortBy)) { Collections.sort(this.childFiles, new MediaFileComparator(MediaFileComparatorType.TYPE)); } else if ("date_uploaded".equals(sortBy)) { Collections.sort( this.childFiles, new MediaFileComparator(MediaFileComparatorType.DATE_UPLOADED)); } else { // default to sort by name sortBy = "name"; Collections.sort( this.childDirectories, new MediaFileDirectoryComparator(DirectoryComparatorType.NAME)); Collections.sort(this.childFiles, new MediaFileComparator(MediaFileComparatorType.NAME)); } this.currentDirectory = directory; return SUCCESS; } catch (FileIOException ex) { log.error("Error viewing media file directory ", ex); addError("MediaFile.error.view"); } catch (Exception e) { log.error("Error viewing media file directory ", e); addError("MediaFile.error.view"); } return SUCCESS; }
/** Test moving files across directories. */ public void testMoveDirectoryContents() throws Exception { User testUser = null; Weblog testWeblog = null; testUser = TestUtils.setupUser("mediaFileTestUser11"); testWeblog = TestUtils.setupWeblog("mediaFileTestUser11", testUser); MediaFileManager mfMgr = WebloggerFactory.getWeblogger().getMediaFileManager(); // no need to create root directory, that is done automatically now MediaFileDirectory rootDirectory = mfMgr.getMediaFileRootDirectory(testWeblog); try { // MediaFileDirectory rootDirectory = new MediaFileDirectory(null, "root", "root d", // testWeblog); mfMgr.createMediaFileDirectory(rootDirectory); MediaFileDirectory directory1 = new MediaFileDirectory(rootDirectory, "dir1", "directory 1", testWeblog); mfMgr.createMediaFileDirectory(directory1); rootDirectory.getChildDirectories().add(directory1); String dir1Id = directory1.getId(); MediaFileDirectory directory2 = new MediaFileDirectory(rootDirectory, "dir2", "directory 2", testWeblog); mfMgr.createMediaFileDirectory(directory2); rootDirectory.getChildDirectories().add(directory2); MediaFileDirectory directory3 = new MediaFileDirectory(rootDirectory, "dir3", "directory 3", testWeblog); mfMgr.createMediaFileDirectory(directory3); rootDirectory.getChildDirectories().add(directory3); MediaFile mediaFile = new MediaFile(); mediaFile.setDirectory(rootDirectory); rootDirectory.getMediaFiles().add(mediaFile); mediaFile.setWeblog(testWeblog); mediaFile.setName("test7_1.jpg"); mediaFile.setDescription("This is a test image 7.1"); mediaFile.setCopyrightText("test 7.1 copyright text"); mediaFile.setSharedForGallery(false); mediaFile.setLength(4000); mediaFile.setInputStream(getClass().getResourceAsStream(TEST_IMAGE)); mediaFile.setContentType("image/jpeg"); mfMgr.createMediaFile(testWeblog, mediaFile, new RollerMessages()); MediaFile mediaFile2 = new MediaFile(); mediaFile2.setDirectory(rootDirectory); rootDirectory.getMediaFiles().add(mediaFile2); mediaFile2.setWeblog(testWeblog); mediaFile2.setName("test7_2.jpg"); mediaFile2.setDescription("This is a test image 7.2"); mediaFile2.setCopyrightText("test 7.2 copyright text"); mediaFile2.setSharedForGallery(true); mediaFile2.setLength(4000); mediaFile2.setInputStream(getClass().getResourceAsStream(TEST_IMAGE)); mediaFile2.setContentType("image/jpeg"); mfMgr.createMediaFile(testWeblog, mediaFile2, new RollerMessages()); TestUtils.endSession(true); testWeblog = TestUtils.getManagedWebsite(testWeblog); rootDirectory = mfMgr.getMediaFileDirectory(rootDirectory.getId()); Set<MediaFile> mediaFiles = rootDirectory.getMediaFiles(); assertEquals(2, mediaFiles.size()); assertTrue(containsFileWithName(mediaFiles, "test7_1.jpg")); assertTrue(containsFileWithName(mediaFiles, "test7_2.jpg")); MediaFileDirectory targetDirectory = mfMgr.getMediaFileDirectory(dir1Id); mfMgr.moveMediaFiles(mediaFiles, targetDirectory); TestUtils.endSession(true); rootDirectory = mfMgr.getMediaFileDirectory(rootDirectory.getId()); targetDirectory = mfMgr.getMediaFileDirectory(dir1Id); mediaFiles = targetDirectory.getMediaFiles(); assertEquals(2, mediaFiles.size()); assertTrue(containsFileWithName(mediaFiles, "test7_1.jpg")); assertTrue(containsFileWithName(mediaFiles, "test7_2.jpg")); mediaFiles = rootDirectory.getMediaFiles(); assertEquals(0, mediaFiles.size()); } finally { TestUtils.endSession(true); TestUtils.teardownWeblog(testWeblog.getId()); TestUtils.teardownUser(testUser.getUserName()); } }