📄 fileuploadmanager.java
字号:
FileManager.deleteFile( FileManager.getLocalName( obj.getFileName(), obj.getFolder() ) ); // Delete the related database record. hnd.remove(); hnd.commit(); } catch( EQLException ex ) { ERROR( ex ); throw new GenericSystemException( ex ); } } /** * Synchronizes the directory tree with related database tables. * @throws FileManagerException */ public synchronized void syncDirectories() throws FileManagerException { long time = System.currentTimeMillis(); INFO( "Syncing the directories tree with database..." ); JEOManagerLocal jeoManager = getJEOManager(); int added = 0; int removed = 0; try { // 1. Get the list of the folder objects in the database. List hnds = UploadFolderObjectHandler.getAllFolders( jeoManager, ls ); int dbSize = ( hnds == null ) ? 0 : hnds.size(); DEBUG( "Found " + dbSize + " database directory objects." ); List dbDirList = new ArrayList( dbSize ); for( int i = 0; i < dbSize; i++ ) { UploadFolderObjectHandler hnd = ( UploadFolderObjectHandler ) hnds.get( i ); UploadFolderObject obj = ( UploadFolderObject ) hnd.getJEObject(); String dirName = obj.getFolder(); if( StringHelper.isEmpty( dirName ) ) { throw new NullPointerException( "Name is empty for the directory object with ID = " + obj.getFolder_id().longValue() ); } dbDirList.add( FileManager.getFullFile( dirName ) ); } // 2. Get all existing folders. List fsDirList = FileHelper.getDirectoriesList( FileManager.getFSDirectory() ); int fsSize = ( fsDirList == null ) ? 0 : fsDirList.size(); DEBUG( "Found " + fsSize + " existing directories." ); // 3. Add missing records to the database. for( int i = 0; i < fsSize; i++ ) { File dir = ( File ) fsDirList.get( i ); if( !dbDirList.contains( dir ) ) { JEObjectHandler hnd = jeoManager.create( ls, UploadFolderObjectHandler.class ); UploadFolderObject obj = ( UploadFolderObject ) hnd.getJEObject(); obj.setFolder( FileManager.getLocalName( dir ) ); hnd.commit(); added++; if( getLogger().isDebugEnabled() ) { String path = dir.getAbsolutePath(); DEBUG( "Record added for the '" + path + "' directory." ); } } } // 4. Remove extra records from the database. for( int i = 0; i < dbSize; i++ ) { File dir = ( File ) dbDirList.get( i ); if( !fsDirList.contains( dir ) ) { UploadFolderObjectHandler hnd = ( UploadFolderObjectHandler ) hnds.get( i ); hnd.remove(); hnd.commit(); removed++; if( getLogger().isDebugEnabled() ) { String path = dir.getAbsolutePath(); DEBUG( "Record removed for the '" + path + "' directory." ); } } } } catch( EQLException ex ) { ERROR( ex ); throw new GenericSystemException( ex ); } if( getLogger().isInfoEnabled() ) { time = System.currentTimeMillis() - time; INFO( "The directories tree is synced with database. Time(ms) = " + time + ". Records added - " + added + ", removed - " + removed + "." ); } } /** * Synchronizes the files tree with related database tables. * @throws FileManagerException */ public synchronized void syncFiles() throws FileManagerException { long time = System.currentTimeMillis(); INFO( "Syncing the files tree with database..." ); JEOManagerLocal jeoManager = getJEOManager(); File fs = FileManager.getFSDirectory(); int added = 0; int removed = 0; try { // 1. Get the list of the file objects in the database. List hnds = ImageObjectHandler.selectAll( jeoManager, ls ); int dbSize = ( hnds == null ) ? 0 : hnds.size(); DEBUG( "Found " + dbSize + " database file objects." ); List dbFileList = new ArrayList( dbSize ); for( int i = 0; i < dbSize; i++ ) { ImageObjectHandler fileHandler = ( ImageObjectHandler ) hnds.get( i ); ImageObject fileObject = ( ImageObject ) fileHandler.getJEObject(); String dirName = fileObject.getFolder(); String fileName = fileObject.getFileName(); if( fileName == null ) { throw new NullPointerException( "File name is empty." ); } dbFileList.add( FileManager.getFullFile( dirName, fileName ) ); } // 2. Get all existing files. List fsFileList = FileHelper.getFilesList( fs ); int fsSize = ( fsFileList == null ) ? 0 : fsFileList.size(); DEBUG( "Found " + fsSize + " existing files." ); // 3. Add missing records to the database. for( int i = 0; i < fsSize; i++ ) { File file = ( File ) fsFileList.get( i ); if( !dbFileList.contains( file ) ) { addFileRecord( file, null, null ); added++; if( getLogger().isDebugEnabled() ) { String path = file.getAbsolutePath(); DEBUG( "Record added for the '" + path + "' file." ); } } } // 4. Remove extra records from the database. for( int i = 0; i < dbSize; i++ ) { File file = ( File ) dbFileList.get( i ); if( !fsFileList.contains( file ) ) { ImageObjectHandler hnd = ( ImageObjectHandler ) hnds.get( i ); hnd.remove(); hnd.commit(); removed++; if( getLogger().isDebugEnabled() ) { String path = file.getAbsolutePath(); DEBUG( "Record removed for the '" + path + "' file." ); } } } } catch( EQLException ex ) { ERROR( ex ); throw new GenericSystemException( ex ); } if( getLogger().isInfoEnabled() ) { time = System.currentTimeMillis() - time; INFO( "The files tree is synced with database. Time(ms) = " + time + ". Records added - " + added + ", removed - " + removed + "." ); } } /** * Gets the LOCAL name for the given file. * * @param file given File object * @return LOCAL name * @throws FileManagerException * @see FileManager#getLocalName */ public static String getLocalName( File file ) throws FileManagerException { // Get file paths. String filePath = FileManager.getLocalName( file ); // File path _MUST_ be following: <folder>/<file name> // If no folder - add '.'! if( filePath.indexOf( FileManager.NAME_SEPARATOR ) < 0 ) { filePath = "." + FileManager.NAME_SEPARATOR + filePath; } return filePath; } // ========================================================= Protected methods // Adds record about file to the database protected void addFileRecord( File file, String entityName, Long recordKey ) throws EQLException { JEOManagerLocal jeoManager = getJEOManager(); Date now = DateHelper.getNowDate(); String fileName = file.getName(); Long fileSize = new Long( file.length() ); String localFolderName = FileManager.getLocalName( file.getParentFile() ); // Add the master record. JEObjectHandler imageHnd = jeoManager.create( ls, ImageObjectHandler.class ); ImageObject imageObj = ( ImageObject ) imageHnd.getJEObject(); imageObj.setFilename( fileName ); imageObj.setFolder( localFolderName ); imageObj.setFile_size( fileSize ); imageObj.setCreator_id( new Long( ls.getUser().getUserID() ) ); imageObj.setModifier_id( new Long( ls.getUser().getUserID() ) ); imageObj.setCreated( now ); imageObj.setModified( now ); imageHnd.commit(); Long fileID = imageObj.getImage_id(); // Add the record about file usage. if( entityName != null ) { // Get the database object name. EntityViewConfigManagerLocal entityManager = ( EntityViewConfigManagerLocal )new CacheObjectManager().getLocalObject( JNDINames.EntityViewConfigManager, EntityViewConfigManagerLocalHome.class ); String tableName = entityManager.getEntityViewConfig( entityName ).getDbobject(); // Update image record. JEObjectHandler usageHnd = jeoManager.create( ls, ImageUsageObjectHandler.class ); ImageUsageObject usageObj = ( ImageUsageObject ) usageHnd.getJEObject(); usageObj.setImage_id( fileID ); usageObj.setRecord_key( recordKey ); usageObj.setTable_name( tableName ); usageHnd.commit(); } } // Update the file record in the database. protected void updateFileRecord( ImageObjectHandler hnd, File file ) throws EQLException { ImageObject obj = ( ImageObject ) ( hnd.getJEObject() ); obj.setModifier_id( new Long( ls.getUser().getUserID() ) ); obj.setModified( DateHelper.getNowDate() ); obj.setFile_size( new Long( file.length() ) ); hnd.commit(); } // Gets JEO Manager EJB local interface. protected JEOManagerLocal getJEOManager() { return( JEOManagerLocal ) com. getLocalObject( JNDINames.JEOManager, JEOManagerLocalHome.class ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -