DefaultDeleteHelper implements IDeleteHelper { public void permanentlyDeleteFile( final Session session, final PentahoJcrConstants pentahoJcrConstants, final Serializable fileId ) throws RepositoryException { Assert.notNull( fileId ); Node fileNode = session.getNodeByIdentifier( fileId.toString() ); Assert.notNull( fileNode ); Set<RepositoryFile> referrers = new HashSet<RepositoryFile>(); PropertyIterator refIter = fileNode.getReferences(); if ( refIter.hasNext() ) { while ( refIter.hasNext() ) { RepositoryFile referrer = getReferrerFile( session, pentahoJcrConstants, refIter.nextProperty() ); if ( referrer != null ) { referrers.add( referrer ); } } if ( !referrers.isEmpty() ) { RepositoryFile referee = JcrRepositoryFileUtils .nodeToFile( session, pentahoJcrConstants, pathConversionHelper, lockHelper, fileNode ); throw new RepositoryFileDaoReferentialIntegrityException( referee, referrers ); } } if ( fileNode.isLocked() ) { Lock lock = session.getWorkspace().getLockManager().getLock( fileNode.getPath() ); lockHelper.removeLockToken( session, pentahoJcrConstants, lock ); } IPentahoSession pentahoSession = PentahoSessionHolder.getSession(); String tenantId = (String) pentahoSession.getAttribute( IPentahoSession.TENANT_ID_KEY ); String trashFolder = ServerRepositoryPaths.getUserHomeFolderPath( new Tenant( tenantId, true ), PentahoSessionHolder.getSession() .getName() ) + RepositoryFile.SEPARATOR + FOLDER_NAME_TRASH; Node parent = fileNode.getParent(); purgeHistory( fileNode, session, pentahoJcrConstants ); if ( fileNode.getPath().startsWith( trashFolder ) ) { fileNode.remove(); parent.remove(); } else { fileNode.remove(); } } DefaultDeleteHelper( final ILockHelper lockHelper, final IPathConversionHelper pathConversionHelper ); void deleteFile( final Session session, final PentahoJcrConstants pentahoJcrConstants,
                          final Serializable fileId ); List<RepositoryFile> getDeletedFiles( final Session session, final PentahoJcrConstants pentahoJcrConstants,
                                               final String origParentFolderPath, final String filter ); List<RepositoryFile> getDeletedFiles( final Session session, final PentahoJcrConstants pentahoJcrConstants ); List<RepositoryFile> getAllDeletedFiles( final Session session,
      final PentahoJcrConstants pentahoJcrConstants ); void permanentlyDeleteFile( final Session session, final PentahoJcrConstants pentahoJcrConstants,
                                     final Serializable fileId ); void undeleteFile( final Session session, final PentahoJcrConstants pentahoJcrConstants,
                            final Serializable fileId ); String getOriginalParentFolderPath( final Session session, final PentahoJcrConstants pentahoJcrConstants,
                                             final Serializable fileId ); static final String JCR_ROOT_VERSION; }
@Test public void testPermanentlyDeleteFile() throws Exception { String fileID = "testFileID"; final Node nodeDeletedFile = mock( Node.class ); final Node nodeTrash = mock( Node.class ); when( nodeTrash.hasNode( anyString() ) ).thenReturn( true ); when( nodeTrash.getNode( anyString() ) ).thenReturn( nodeDeletedFile ); final Node nodeUserFolder = mock( Node.class ); when( nodeUserFolder.hasNode( anyString() ) ).thenReturn( true ); when( nodeUserFolder.getNode( anyString() ) ).thenReturn( nodeTrash ); final Node nodeDeletedParent = mock( Node.class ); when( nodeDeletedParent.getPath() ).thenReturn( "parentPath" ); final PropertyIterator referencesPropertyIterator = mock( PropertyIterator.class ); when( referencesPropertyIterator.hasNext() ).thenReturn( false ); final Node nodeToRemove = mock( Node.class ); when( nodeToRemove.getPath() ).thenReturn( "nodePath" ); when( nodeToRemove.getParent() ).thenReturn( nodeDeletedParent ); when( nodeToRemove.getReferences() ).thenReturn( referencesPropertyIterator ); when( nodeToRemove.isLocked() ).thenReturn( false ); when( session.getNodeByIdentifier( fileID ) ).thenReturn( nodeToRemove ); when( session.getItem( anyString() ) ).thenReturn( nodeUserFolder ); Workspace workspace = mock( Workspace.class ); VersionManager versionManager = mock( VersionManager.class ); when( workspace.getVersionManager() ).thenReturn( versionManager ); when( session.getWorkspace() ).thenReturn( workspace ); try { defaultDeleteHelper.permanentlyDeleteFile( session, pentahoJcrConstants, fileID ); verify( nodeToRemove ).remove(); } catch ( Exception e ) { e.printStackTrace(); fail(); } when( nodeToRemove.isLocked() ).thenReturn( true ); final Lock lock = mock( Lock.class ); final LockManager lockManager = mock( LockManager.class ); when( lockManager.getLock( eq( nodeToRemove.getPath() ) ) ).thenReturn( lock ); when( workspace.getLockManager() ).thenReturn( lockManager ); when( session.getWorkspace() ).thenReturn( workspace ); try { defaultDeleteHelper.permanentlyDeleteFile( session, pentahoJcrConstants, fileID ); verify( lockHelper ).removeLockToken( eq( session ), eq( pentahoJcrConstants ), eq( lock ) ); verify( nodeToRemove, times( 2 ) ).remove(); } catch ( Exception e ) { e.printStackTrace(); fail(); } final String trashPath = ServerRepositoryPaths.getUserHomeFolderPath( new Tenant( null, true ), PentahoSessionHolder.getSession() .getName() ) + RepositoryFile.SEPARATOR + ".trash"; when( nodeToRemove.getPath() ).thenReturn( trashPath ); try { defaultDeleteHelper.permanentlyDeleteFile( session, pentahoJcrConstants, fileID ); verify( nodeToRemove, times( 3 ) ).remove(); verify( nodeDeletedParent ).remove(); } catch ( Exception e ) { e.printStackTrace(); fail(); } final Node referenceNode = mock( Node.class ); when( referenceNode.isNodeType( eq( pentahoJcrConstants.getPHO_NT_PENTAHOHIERARCHYNODE() ) ) ).thenReturn( true ); when( referenceNode.isNodeType( eq( pentahoJcrConstants.getPHO_NT_PENTAHOFOLDER() ) ) ).thenReturn( false ); final Property property = mock( Property.class ); when( property.getParent() ).thenReturn( referenceNode ); when( referencesPropertyIterator.hasNext() ).thenReturn( true, true, false ); when( referencesPropertyIterator.nextProperty() ).thenReturn( property ); try { defaultDeleteHelper.permanentlyDeleteFile( session, pentahoJcrConstants, fileID ); } catch ( RepositoryFileDaoReferentialIntegrityException e1 ) { } catch ( Exception e ) { e.printStackTrace(); fail(); } }
@Test public void testVersionHistoryDeleted() throws Exception { String fileID = "testFileID"; final Node nodeToRemove = mock( Node.class ); when( nodeToRemove.getPath() ).thenReturn( "nodePath" ); when( nodeToRemove.isNodeType( pentahoJcrConstants.getNT_FROZENNODE() ) ).thenReturn( true ); when( nodeToRemove.isNodeType( pentahoJcrConstants.getPHO_MIX_VERSIONABLE() ) ).thenReturn( true ); NodeType nt = mock( NodeType.class ); when( nt.getName() ).thenReturn( NameConstants.MIX_VERSIONABLE.getLocalName() ); when( nodeToRemove.getPrimaryNodeType() ).thenReturn( nt ); when( nodeToRemove.getMixinNodeTypes() ).thenReturn( new NodeType[] {} ); Property prop1 = mock( Property.class ); String pho_nt_pentahofile = pentahoJcrConstants.getPHO_NT_PENTAHOFILE(); when( prop1.toString() ).thenReturn( pho_nt_pentahofile ); when( prop1.getString() ).thenReturn( pho_nt_pentahofile ); when( nodeToRemove.getProperty( pentahoJcrConstants.getJCR_FROZENPRIMARYTYPE() ) ).thenReturn( prop1 ); final PropertyIterator referencesPropertyIterator = mock( PropertyIterator.class ); when( referencesPropertyIterator.hasNext() ).thenReturn( false ); when( nodeToRemove.getReferences() ).thenReturn( referencesPropertyIterator ); when( session.getNodeByIdentifier( fileID ) ).thenReturn( nodeToRemove ); when( session.getNamespacePrefix( anyString() ) ).thenReturn( "prefix:" ); Workspace workspace = mock( Workspace.class ); when( session.getWorkspace() ).thenReturn( workspace ); VersionManager versionManager = mock( VersionManager.class ); when( workspace.getVersionManager() ).thenReturn( versionManager ); VersionHistory history = mock( VersionHistory.class ); when( versionManager.getVersionHistory( "nodePath" ) ).thenReturn( history ); VersionIterator versions = mock( VersionIterator.class ); when( history.getAllVersions() ).thenReturn( versions ); when( versions.hasNext() ).thenReturn( true, false ); Version version = mock( Version.class ); when( versions.next() ).thenReturn( version ); String value = "Omega Man"; when( version.getName() ).thenReturn( value ); try { defaultDeleteHelper.permanentlyDeleteFile( session, pentahoJcrConstants, fileID ); verify( versionManager ).getVersionHistory( nodeToRemove.getPath() ); verify( nodeToRemove ).remove(); verify( history ).removeVersion( value ); } catch ( Exception e ) { e.printStackTrace(); fail(); } }
@Test public void testVersionHistoryDeletedWithParent() throws Exception { String fileID = "testFileID"; final Node nodeToRemove = mock( Node.class ); when( nodeToRemove.getPath() ).thenReturn( "nodePath" ); when( nodeToRemove.isNodeType( pentahoJcrConstants.getNT_FROZENNODE() ) ).thenReturn( true ); NodeType nt = mock( NodeType.class ); when( nt.getName() ).thenReturn( NameConstants.MIX_VERSIONABLE.getLocalName() ); when( nodeToRemove.getPrimaryNodeType() ).thenReturn( nt ); when( nodeToRemove.getMixinNodeTypes() ).thenReturn( new NodeType[] {} ); when( nodeToRemove.isNodeType( pentahoJcrConstants.getNT_FROZENNODE() ) ).thenReturn( true ); when( nodeToRemove.isNodeType( pentahoJcrConstants.getPHO_MIX_VERSIONABLE() ) ).thenReturn( true ); Property prop1 = mock( Property.class ); String pho_nt_pentahofile = pentahoJcrConstants.getPHO_NT_PENTAHOFILE(); when( prop1.toString() ).thenReturn( pho_nt_pentahofile ); when( prop1.getString() ).thenReturn( pho_nt_pentahofile ); when( nodeToRemove.getProperty( pentahoJcrConstants.getJCR_FROZENPRIMARYTYPE() ) ).thenReturn( prop1 ); final PropertyIterator referencesPropertyIterator = mock( PropertyIterator.class ); when( referencesPropertyIterator.hasNext() ).thenReturn( false ); when( nodeToRemove.getReferences() ).thenReturn( referencesPropertyIterator ); when( session.getNodeByIdentifier( fileID ) ).thenReturn( nodeToRemove ); when( session.getNamespacePrefix( anyString() ) ).thenReturn( "prefix:" ); Workspace workspace = mock( Workspace.class ); when( session.getWorkspace() ).thenReturn( workspace ); VersionManager versionManager = mock( VersionManager.class ); when( workspace.getVersionManager() ).thenReturn( versionManager ); VersionHistory history = mock( VersionHistory.class ); when( versionManager.getVersionHistory( "nodePath" ) ).thenReturn( history ); VersionIterator versions = mock( VersionIterator.class ); when( history.getAllVersions() ).thenReturn( versions ); when( versions.hasNext() ).thenReturn( true, false ); Version version = mock( Version.class ); when( versions.next() ).thenReturn( version ); String value = "Omega Man"; when( version.getName() ).thenReturn( value ); String parentID = "parent"; final Node parent = mock( Node.class ); when( session.getNodeByIdentifier( parentID ) ).thenReturn( parent ); when( parent.getPath() ).thenReturn( "parentNodePath" ); when( parent.isNodeType( pentahoJcrConstants.getNT_FROZENNODE() ) ).thenReturn( true ); Property prop = mock( Property.class ); String pho_nt_pentahofolder = pentahoJcrConstants.getPHO_NT_PENTAHOFOLDER(); when( prop.toString() ).thenReturn( pho_nt_pentahofolder ); when( prop.getString() ).thenReturn( pho_nt_pentahofolder ); when( parent.getProperty( pentahoJcrConstants.getJCR_FROZENPRIMARYTYPE() ) ).thenReturn( prop ); when( parent.getReferences() ).thenReturn( referencesPropertyIterator ); NodeIterator nodeIterator = mock( NodeIterator.class ); when( nodeIterator.hasNext() ).thenReturn( true, false ); when( nodeIterator.next() ).thenReturn( nodeToRemove ); when( parent.getNodes() ).thenReturn( nodeIterator ); try { defaultDeleteHelper.permanentlyDeleteFile( session, pentahoJcrConstants, parentID ); verify( versionManager ).getVersionHistory( nodeToRemove.getPath() ); verify( parent ).remove(); verify( history ).removeVersion( value ); } catch ( Exception e ) { e.printStackTrace(); fail(); } }