Search in sources :

Example 1 with RepositoryFileDaoReferentialIntegrityException

use of org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException in project pentaho-platform by pentaho.

the class DefaultDeleteHelperTest method testPermanentlyDeleteFile.

@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();
    }
    // test locked file
    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();
    }
    // if remove from trash folder, folder containing the file need to be removed
    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();
    }
    // test if there are references to this file
    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) {
    // it's ok
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : LockManager(javax.jcr.lock.LockManager) Tenant(org.pentaho.platform.core.mt.Tenant) RepositoryFileDaoReferentialIntegrityException(org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Matchers.anyString(org.mockito.Matchers.anyString) VersionManager(javax.jcr.version.VersionManager) Property(javax.jcr.Property) RepositoryFileDaoReferentialIntegrityException(org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException) RepositoryFileDaoFileExistsException(org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoFileExistsException) RepositoryException(javax.jcr.RepositoryException) Workspace(javax.jcr.Workspace) Lock(javax.jcr.lock.Lock) Test(org.junit.Test)

Example 2 with RepositoryFileDaoReferentialIntegrityException

use of org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException in project pentaho-platform by pentaho.

the class DefaultDeleteHelper method permanentlyDeleteFile.

/**
 * {@inheritDoc}
 */
public void permanentlyDeleteFile(final Session session, final PentahoJcrConstants pentahoJcrConstants, final Serializable fileId) throws RepositoryException {
    Assert.notNull(fileId);
    Node fileNode = session.getNodeByIdentifier(fileId.toString());
    // guard against using a file retrieved from a more lenient session inside a more strict session
    Assert.notNull(fileNode);
    // see if anything is referencing this node; if yes, then we cannot delete it as a
    // ReferentialIntegrityException
    // will result
    Set<RepositoryFile> referrers = new HashSet<RepositoryFile>();
    PropertyIterator refIter = fileNode.getReferences();
    if (refIter.hasNext()) {
        while (refIter.hasNext()) {
            // for each referrer property, march up the tree until we find the file node to which the property belongs
            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);
        }
    }
    // it first
    if (fileNode.isLocked()) {
        Lock lock = session.getWorkspace().getLockManager().getLock(fileNode.getPath());
        // don't need lock token anymore
        lockHelper.removeLockToken(session, pentahoJcrConstants, lock);
    }
    // if this file was non-permanently deleted, delete its containing folder too
    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)) {
        // Remove the file and then the wrapper foler
        fileNode.remove();
        parent.remove();
    } else {
        fileNode.remove();
    }
}
Also used : Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) RepositoryFileDaoReferentialIntegrityException(org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) HashSet(java.util.HashSet) Lock(javax.jcr.lock.Lock)

Aggregations

Node (javax.jcr.Node)2 PropertyIterator (javax.jcr.PropertyIterator)2 Lock (javax.jcr.lock.Lock)2 Tenant (org.pentaho.platform.core.mt.Tenant)2 RepositoryFileDaoReferentialIntegrityException (org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException)2 HashSet (java.util.HashSet)1 Property (javax.jcr.Property)1 RepositoryException (javax.jcr.RepositoryException)1 Workspace (javax.jcr.Workspace)1 LockManager (javax.jcr.lock.LockManager)1 VersionManager (javax.jcr.version.VersionManager)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)1 ITenant (org.pentaho.platform.api.mt.ITenant)1 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)1 RepositoryFileDaoFileExistsException (org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoFileExistsException)1