Search in sources :

Example 11 with IRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.

the class RepositoryUtils method createFile.

/**
 * Creates a new file that is known to not exist
 *
 * @param filePath
 *          the full path to the file
 * @param data
 *          the data to be stored in the file
 * @param createParentDirs
 *          indicates of the directory structure should be created if it doesn't exist
 * @param versionMessage
 *          the version message
 * @return the newly create {@code IRepositoryFile} or {@code null} if it couldn't be created
 */
protected RepositoryFile createFile(final String filePath, final IRepositoryFileData data, final boolean createParentDirs, final boolean versioned, final String versionMessage) {
    final String path = RepositoryFilenameUtils.getFullPath(filePath);
    final String filename = RepositoryFilenameUtils.getName(filePath);
    final RepositoryFile parentDir = getFolder(path, createParentDirs, createParentDirs, versionMessage);
    if (null == parentDir) {
        return null;
    }
    return repository.createFile(parentDir.getId(), new RepositoryFile.Builder(filename).versioned(versioned).build(), data, versionMessage);
}
Also used : RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 12 with IRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.

the class RepositoryFileHelper method getFileData.

public static IRepositoryFileData getFileData(RepositoryFile repositoryFile) {
    IRepositoryContentConverterHandler converterHandler;
    Map<String, Converter> converters;
    IPlatformMimeResolver mimeResolver;
    IRepositoryFileData repositoryFileData = null;
    if (!repositoryFile.isFolder()) {
        // Get the extension
        final String ext = RepositoryFilenameUtils.getExtension(repositoryFile.getName());
        if ((ext == null) || (ext.isEmpty())) {
            return null;
        }
        // Find the converter
        // If we have not been given a handler, try PentahoSystem
        converterHandler = PentahoSystem.get(IRepositoryContentConverterHandler.class);
        // fail if we have no converter handler
        if (converterHandler == null) {
            return null;
        }
        converters = converterHandler.getConverters();
        final Converter converter = converters.get(ext);
        if (converter == null) {
            return null;
        }
        // Check the mime type
        mimeResolver = PentahoSystem.get(IPlatformMimeResolver.class);
        // fail if we have no mime resolver
        if (mimeResolver == null) {
            return null;
        }
        final String mimeType = mimeResolver.resolveMimeTypeForFileName(repositoryFile.getName()).getName();
        if ((mimeType == null) || (mimeType.isEmpty())) {
            return null;
        }
        // Get the input stream
        InputStream inputStream = converter.convert(repositoryFile.getId());
        if (inputStream == null) {
            return null;
        }
        // Get the file data
        repositoryFileData = converter.convert(inputStream, "UTF-8", mimeType);
        if (repositoryFileData == null) {
            return null;
        }
    }
    return repositoryFileData;
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) IRepositoryContentConverterHandler(org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler) IPlatformMimeResolver(org.pentaho.platform.api.mimetype.IPlatformMimeResolver) InputStream(java.io.InputStream) Converter(org.pentaho.platform.api.repository2.unified.Converter)

Example 13 with IRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.

the class DefaultUnifiedRepositoryContentIT method testTransactionRollback.

/**
 * Create a versioned file then update it with invalid data and the checkout that we did before setting the data
 * should be rolled back.
 */
@Test
public void testTransactionRollback() throws Exception {
    loginAsSysTenantAdmin();
    ITenant tenantAcme = tenantManager.createTenant(systemTenant, TENANT_ID_ACME, tenantAdminRoleName, tenantAuthenticatedRoleName, ANONYMOUS_ROLE_NAME);
    userRoleDao.createUser(tenantAcme, USERNAME_ADMIN, PASSWORD, "", new String[] { tenantAdminRoleName });
    login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
    userRoleDao.createUser(tenantAcme, USERNAME_SUZY, PASSWORD, "", null);
    login(USERNAME_SUZY, tenantAcme, new String[] { tenantAuthenticatedRoleName });
    final String expectedName = "helloworld.sample";
    final String sampleString = "Ciao World!";
    final boolean sampleBoolean = true;
    final int sampleInteger = 99;
    final String parentFolderPath = ClientRepositoryPaths.getUserHomeFolderPath(USERNAME_SUZY);
    final String expectedAbsolutePath = ServerRepositoryPaths.getTenantRootFolderPath() + parentFolderPath + RepositoryFile.SEPARATOR + expectedName;
    RepositoryFile newFile = createSampleFile(parentFolderPath, expectedName, sampleString, sampleBoolean, sampleInteger, true);
    assertNotNull(SimpleJcrTestUtils.getItem(testJcrTemplate, expectedAbsolutePath));
    try {
        repo.updateFile(newFile, new IRepositoryFileData() {

            @Override
            public long getDataSize() {
                return 0;
            }
        }, null);
        fail("expected UnifiedRepositoryException");
    } catch (UnifiedRepositoryException e) {
    // ignore
    }
    assertFalse(SimpleJcrTestUtils.isCheckedOut(testJcrTemplate, expectedAbsolutePath));
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) ITenant(org.pentaho.platform.api.mt.ITenant) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 14 with IRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.

the class DefaultUnifiedRepositoryContentIT method testCreateFileUnrecognizedContentType.

@Test(expected = UnifiedRepositoryException.class)
public void testCreateFileUnrecognizedContentType() throws Exception {
    loginAsSysTenantAdmin();
    ITenant tenantAcme = tenantManager.createTenant(systemTenant, TENANT_ID_ACME, tenantAdminRoleName, tenantAuthenticatedRoleName, ANONYMOUS_ROLE_NAME);
    userRoleDao.createUser(tenantAcme, USERNAME_ADMIN, PASSWORD, "", new String[] { tenantAdminRoleName });
    login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
    userRoleDao.createUser(tenantAcme, USERNAME_SUZY, PASSWORD, "", null);
    login(USERNAME_SUZY, tenantAcme, new String[] { tenantAuthenticatedRoleName });
    RepositoryFile parentFolder = repo.getFile(ClientRepositoryPaths.getUserHomeFolderPath(USERNAME_SUZY));
    IRepositoryFileData content = new IRepositoryFileData() {

        @Override
        public long getDataSize() {
            // TODO Auto-generated method stub
            return 0;
        }
    };
    repo.createFile(parentFolder.getId(), new RepositoryFile.Builder("helloworld.xaction").build(), content, null);
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) ITenant(org.pentaho.platform.api.mt.ITenant) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Test(org.junit.Test)

Example 15 with IRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.

the class JcrRepositoryFileDaoIT method testUpdateFile1.

@Test
public // Running within defined date
void testUpdateFile1() throws Exception {
    RepositoryFile newFile = createFile("JcrRepositoryFileDaoTest1.test");
    IRepositoryFileData dataMock = new SampleRepositoryFileData("", true, 0);
    Date startDate = new Date();
    newFile = new RepositoryFile.Builder(newFile).createdDate(startDate).build();
    repo.updateFile(newFile, dataMock, "edition #2");
    List<VersionSummary> summaries = repo.getVersionSummaries(newFile.getId());
    Date lastVersionDate = summaries.get(summaries.size() - 1).getDate();
    assertEquals("incorrect version date", lastVersionDate, startDate);
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) SampleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.sample.SampleRepositoryFileData) VersionSummary(org.pentaho.platform.api.repository2.unified.VersionSummary) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Date(java.util.Date) Test(org.junit.Test)

Aggregations

RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)23 IRepositoryFileData (org.pentaho.platform.api.repository2.unified.IRepositoryFileData)16 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)9 Test (org.junit.Test)6 NodeRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)5 FileNotFoundException (java.io.FileNotFoundException)4 Serializable (java.io.Serializable)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Converter (org.pentaho.platform.api.repository2.unified.Converter)4 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 Node (javax.jcr.Node)3 Session (javax.jcr.Session)3 Matchers.anyString (org.mockito.Matchers.anyString)3 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)3