Search in sources :

Example 1 with VersionManagerImpl

use of org.apache.jackrabbit.core.VersionManagerImpl in project pentaho-platform by pentaho.

the class JcrRepositoryFileUtilsTest method testCheckinNearestVersionableNodeIfNecessary.

@Test
public void testCheckinNearestVersionableNodeIfNecessary() throws Exception {
    String versionMessage = "Version message";
    String username = "admin";
    Date curDate = new Date();
    Node mockNode = mock(Node.class);
    Node parentNode = mock(Node.class);
    IPentahoSession pentahoSession = mock(IPentahoSession.class);
    when(pentahoSession.getName()).thenReturn(username);
    JcrRepositoryFileUtils.setRepositoryVersionManager(repositoryVersionManagerMockTrue);
    VersionManagerImpl versionManager = mock(VersionManagerImpl.class);
    when(workspaceMock.getVersionManager()).thenReturn(versionManager);
    when(mockNode.getParent()).thenReturn(parentNode);
    PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(sessionMock);
    when(mockNode.isNodeType(pentahoJcrConstants.getPHO_MIX_VERSIONABLE())).thenReturn(Boolean.TRUE);
    PentahoSessionHolder.setSession(pentahoSession);
    JcrRepositoryFileUtils.checkinNearestVersionableNodeIfNecessary(sessionMock, pentahoJcrConstants, mockNode, versionMessage, curDate, true);
    Calendar cal = Calendar.getInstance();
    if (curDate != null) {
        cal.setTime(curDate);
    } else {
        cal.setTime(new Date());
    }
    // validate checkin was called
    verify(versionManager).checkin(mockNode.getPath(), cal);
}
Also used : VersionManagerImpl(org.apache.jackrabbit.core.VersionManagerImpl) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Node(javax.jcr.Node) Calendar(java.util.Calendar) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with VersionManagerImpl

use of org.apache.jackrabbit.core.VersionManagerImpl in project pentaho-platform by pentaho.

the class JcrRepositoryFileUtils method checkinNearestVersionableNodeIfNecessary.

/**
 * Conditionally checks in node if node is versionable.
 * <p/>
 * TODO mlowery move commented out version labeling to its own method
 */
public static void checkinNearestVersionableNodeIfNecessary(final Session session, final PentahoJcrConstants pentahoJcrConstants, final Node node, final String versionMessage, Date versionDate, final boolean aclOnlyChange) throws RepositoryException {
    Assert.notNull(node);
    session.save();
    /*
     * session.save must be called inside the versionable node block and outside to ensure user changes are made when a
     * file is not versioned.
     */
    Node versionableNode = findNearestVersionableNode(session, pentahoJcrConstants, node);
    if (versionableNode != null) {
        versionableNode.setProperty(pentahoJcrConstants.getPHO_VERSIONAUTHOR(), getUsername());
        if (StringUtils.hasText(versionMessage)) {
            versionableNode.setProperty(pentahoJcrConstants.getPHO_VERSIONMESSAGE(), versionMessage);
        } else {
            // TODO mlowery why do I need to check for hasProperty here? in JR 1.6, I didn't need to
            if (versionableNode.hasProperty(pentahoJcrConstants.getPHO_VERSIONMESSAGE())) {
                versionableNode.setProperty(pentahoJcrConstants.getPHO_VERSIONMESSAGE(), (String) null);
            }
        }
        if (aclOnlyChange) {
            versionableNode.setProperty(pentahoJcrConstants.getPHO_ACLONLYCHANGE(), true);
        } else {
            // TODO mlowery why do I need to check for hasProperty here? in JR 1.6, I didn't need to
            if (versionableNode.hasProperty(pentahoJcrConstants.getPHO_ACLONLYCHANGE())) {
                versionableNode.getProperty(pentahoJcrConstants.getPHO_ACLONLYCHANGE()).remove();
            }
        }
        // required before checkin since we set some properties above
        session.save();
        Calendar cal = Calendar.getInstance();
        if (versionDate != null) {
            cal.setTime(versionDate);
        } else {
            cal.setTime(new Date());
        }
        ((VersionManagerImpl) session.getWorkspace().getVersionManager()).checkin(versionableNode.getPath(), cal);
        // prevent the number of versions from increasing. We still need a versioned node
        if (!getRepositoryVersionManager().isVersioningEnabled(versionableNode.getPath())) {
            List<VersionSummary> versionSummaries = (List<VersionSummary>) getVersionSummaries(session, pentahoJcrConstants, versionableNode.getIdentifier(), Boolean.TRUE);
            if ((versionSummaries != null) && (versionSummaries.size() > 1)) {
                VersionSummary versionSummary = (VersionSummary) versionSummaries.toArray()[versionSummaries.size() - 2];
                if (versionSummary != null) {
                    String versionId = (String) versionSummary.getId();
                    session.getWorkspace().getVersionManager().getVersionHistory(versionableNode.getPath()).removeVersion(versionId);
                    session.save();
                }
            }
        }
    }
}
Also used : VersionManagerImpl(org.apache.jackrabbit.core.VersionManagerImpl) Node(javax.jcr.Node) Calendar(java.util.Calendar) VersionSummary(org.pentaho.platform.api.repository2.unified.VersionSummary) List(java.util.List) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 3 with VersionManagerImpl

use of org.apache.jackrabbit.core.VersionManagerImpl in project pentaho-platform by pentaho.

the class JcrRepositoryFileDaoTest method setUp.

@Before
public void setUp() throws RepositoryException {
    Node node = mock(Node.class);
    Node nodeParent = mock(Node.class);
    when(node.getIdentifier()).thenReturn("");
    when(nodeParent.getIdentifier()).thenReturn("");
    when(node.getParent()).thenReturn(nodeParent);
    when(node.isNodeType("null:pentahoFile")).thenReturn(true);
    when(node.isNodeType("null:pentahoVersionable")).thenReturn(true);
    VersionManagerImpl versionManager = mock(VersionManagerImpl.class);
    Workspace workspace = mock(Workspace.class);
    when(workspace.getVersionManager()).thenReturn(versionManager);
    Session session = mock(Session.class);
    when(session.getWorkspace()).thenReturn(workspace);
    when(session.getNodeByIdentifier(anyString())).thenReturn(node);
    when(session.getItem(anyString())).thenReturn(node);
    pentahoSession = mock(IPentahoSession.class);
    PentahoSessionHolder.setSession(pentahoSession);
    IRepositoryVersionManager repositoryVersionManager = mock(IRepositoryVersionManager.class);
    when(repositoryVersionManager.isVersioningEnabled(anyString())).thenReturn(true);
    PentahoSystem.registerObject(repositoryVersionManager);
    JcrTemplate jcrTemplate = new JcrTemplate() {

        @Override
        public Object execute(JcrCallback callback) throws DataAccessException {
            try {
                return callback.doInJcr(session);
            } catch (Exception e) {
                // wrapping exception to comply overriding rules
                throw new RuntimeException(e);
            }
        }
    };
    List<ITransformer<IRepositoryFileData>> transformerList = Collections.emptyList();
    IPathConversionHelper pathConversionHelper = new DefaultPathConversionHelper();
    IRepositoryFileAclDao aclDao = mock(IRepositoryFileAclDao.class);
    accessVoterManager = mock(IRepositoryAccessVoterManager.class);
    JcrRepositoryFileDao jcrDao = new JcrRepositoryFileDao(jcrTemplate, transformerList, null, null, pathConversionHelper, aclDao, null, accessVoterManager);
    dao = spy(jcrDao);
}
Also used : JcrTemplate(org.springframework.extensions.jcr.JcrTemplate) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Node(javax.jcr.Node) JcrCallback(org.springframework.extensions.jcr.JcrCallback) IRepositoryFileAclDao(org.pentaho.platform.repository2.unified.IRepositoryFileAclDao) IRepositoryVersionManager(org.pentaho.platform.api.repository2.unified.IRepositoryVersionManager) DataAccessException(org.springframework.dao.DataAccessException) AccessDeniedException(javax.jcr.AccessDeniedException) RepositoryException(javax.jcr.RepositoryException) VersionManagerImpl(org.apache.jackrabbit.core.VersionManagerImpl) IRepositoryAccessVoterManager(org.pentaho.platform.api.repository2.unified.IRepositoryAccessVoterManager) Workspace(javax.jcr.Workspace) Session(javax.jcr.Session) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Before(org.junit.Before)

Example 4 with VersionManagerImpl

use of org.apache.jackrabbit.core.VersionManagerImpl in project pentaho-platform by pentaho.

the class JcrRepositoryFileUtilsTest method testCheckinNearestVersionableNodeIfNecessaryWithoutVersioning.

@Test
public void testCheckinNearestVersionableNodeIfNecessaryWithoutVersioning() throws Exception {
    String versionMessage = "Version message";
    String username = "admin";
    String mockNodeId = "12345";
    String versionName = "versionName";
    String versionableIdentifier = "versionableIdentifier";
    Date curDate = new Date();
    Calendar cal = Calendar.getInstance();
    if (curDate != null) {
        cal.setTime(curDate);
    } else {
        cal.setTime(new Date());
    }
    // disable versioning
    JcrRepositoryFileUtils.setRepositoryVersionManager(repositoryVersionManagerMockFalse);
    Node mockNode = mock(Node.class);
    Node parentNode = mock(Node.class);
    Node versionNode = mock(Node.class);
    Version mockVersion = mock(Version.class);
    Version mockVersion2 = mock(Version.class);
    VersionManagerImpl versionManager = mock(VersionManagerImpl.class);
    VersionHistory mockVersionHistory = mock(VersionHistory.class);
    IPentahoSession pentahoSession = mock(IPentahoSession.class);
    when(pentahoSession.getName()).thenReturn(username);
    when(sessionMock.getNodeByIdentifier(anyString())).thenReturn(mockNode);
    String[] mockVersionLabels = { "label1" };
    when(mockVersionHistory.getVersionLabels(mockVersion)).thenReturn(mockVersionLabels);
    when(mockVersionHistory.getVersionableIdentifier()).thenReturn(versionableIdentifier);
    Version[] mockVersionsList = { mockVersion, mockVersion2 };
    Version[] mockVersionsList2 = { mockVersion };
    when(mockVersion.getSuccessors()).thenReturn(mockVersionsList, mockVersionsList2, null);
    when(mockVersion.getNode((String) anyObject())).thenReturn(versionNode);
    when(mockVersion.getName()).thenReturn(versionName);
    when(mockVersion.getCreated()).thenReturn(cal);
    when(versionManager.getVersionHistory(mockNode.getPath())).thenReturn(mockVersionHistory);
    when(workspaceMock.getVersionManager()).thenReturn(versionManager);
    when(mockVersionHistory.getRootVersion()).thenReturn(mockVersion);
    when(mockNode.getParent()).thenReturn(parentNode);
    when(mockNode.getIdentifier()).thenReturn(mockNodeId);
    PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(sessionMock);
    when(mockNode.isNodeType(pentahoJcrConstants.getPHO_MIX_VERSIONABLE())).thenReturn(Boolean.TRUE);
    PentahoSessionHolder.setSession(pentahoSession);
    JcrRepositoryFileUtils.checkinNearestVersionableNodeIfNecessary(sessionMock, pentahoJcrConstants, mockNode, versionMessage, curDate, true);
    // verify checkin was called
    verify(versionManager).checkin(mockNode.getPath(), cal);
    // verify version is deleted
    verify(mockVersionHistory).removeVersion((String) anyObject());
}
Also used : VersionManagerImpl(org.apache.jackrabbit.core.VersionManagerImpl) Version(javax.jcr.version.Version) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Calendar(java.util.Calendar) Node(javax.jcr.Node) Matchers.anyString(org.mockito.Matchers.anyString) VersionHistory(javax.jcr.version.VersionHistory) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Node (javax.jcr.Node)4 VersionManagerImpl (org.apache.jackrabbit.core.VersionManagerImpl)4 Calendar (java.util.Calendar)3 Date (java.util.Date)3 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)3 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AccessDeniedException (javax.jcr.AccessDeniedException)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 Workspace (javax.jcr.Workspace)1 Version (javax.jcr.version.Version)1 VersionHistory (javax.jcr.version.VersionHistory)1 Before (org.junit.Before)1 IRepositoryAccessVoterManager (org.pentaho.platform.api.repository2.unified.IRepositoryAccessVoterManager)1 IRepositoryVersionManager (org.pentaho.platform.api.repository2.unified.IRepositoryVersionManager)1 VersionSummary (org.pentaho.platform.api.repository2.unified.VersionSummary)1