Search in sources :

Example 6 with IPlatformImporter

use of org.pentaho.platform.plugin.services.importer.IPlatformImporter in project data-access by pentaho.

the class MetadataService method importMetadataDatasource.

public void importMetadataDatasource(String domainId, InputStream metadataFile, boolean overwrite, List<InputStream> localeFileStreams, List<String> localeFileNames, RepositoryFileAclDto acl) throws PentahoAccessControlException, PlatformImportException, Exception {
    if (StringUtils.isEmpty(domainId)) {
        throw new PlatformImportException(Messages.getString("MetadataDatasourceService.ERROR_005_DOMAIN_NAME_EMPTY"));
    }
    accessValidation();
    FileResource fr = createNewFileResource();
    Object reservedCharsObject = fr.doGetReservedChars().getEntity();
    String reservedChars = objectToString(reservedCharsObject);
    if (reservedChars != null && domainId.matches(".*[" + reservedChars.replaceAll("/", "") + "]+.*")) {
        String msg = prohibitedSymbolMessage(domainId, fr);
        throw new PlatformImportException(msg, PlatformImportException.PUBLISH_PROHIBITED_SYMBOLS_ERROR);
    }
    metadataFile = validateFileSize(metadataFile, domainId);
    // domain ID comes with ".xmi" suffix when creating or editing domain
    // (see ModelerService.serializeModels( Domain, String, boolean ) ),
    // but when the user enters domain ID manually when importing metadata file,
    // it will unlikely contain that suffix, so let's add it forcibly.
    domainId = forceXmiSuffix(domainId);
    RepositoryFileImportBundle.Builder bundleBuilder = createNewRepositoryFileImportBundleBuilder(metadataFile, overwrite, domainId, acl);
    if (localeFileStreams != null) {
        for (int i = 0; i < localeFileStreams.size(); i++) {
            IPlatformImportBundle localizationBundle = createNewRepositoryFileImportBundle(localeFileStreams.get(i), localeFileNames.get(i), domainId);
            bundleBuilder.addChildBundle(localizationBundle);
        }
    }
    IPlatformImportBundle bundle = bundleBuilder.build();
    IPlatformImporter importer = getImporter();
    importer.importFile(bundle);
    IPentahoSession pentahoSession = getSession();
    publish(pentahoSession);
}
Also used : IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) FileResource(org.pentaho.platform.web.http.api.resources.FileResource) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter)

Example 7 with IPlatformImporter

use of org.pentaho.platform.plugin.services.importer.IPlatformImporter in project data-access by pentaho.

the class DatasourceResourceIT method testImportZipFile.

private void testImportZipFile(String filePath, final String expectedSchemaName, boolean annotated) throws Exception {
    FormDataContentDisposition schemaFileInfo = mock(FormDataContentDisposition.class);
    File f = new File(filePath);
    when(schemaFileInfo.getFileName()).thenReturn(f.getName());
    Mockery mockery = new Mockery();
    final IPlatformImporter mockImporter = mockery.mock(IPlatformImporter.class);
    mp.defineInstance(IPlatformImporter.class, mockImporter);
    if (annotated) {
        mockery.checking(new Expectations() {

            {
                exactly(2).of(mockImporter).importFile(with(match(new TypeSafeMatcher<IPlatformImportBundle>() {

                    public boolean matchesSafely(IPlatformImportBundle bundle) {
                        return true;
                    }

                    public void describeTo(Description description) {
                        description.appendText("bundle with zipped mondrian schema");
                    }
                })));
            }
        });
    } else {
        mockery.checking(new Expectations() {

            {
                oneOf(mockImporter).importFile(with(match(new TypeSafeMatcher<IPlatformImportBundle>() {

                    public boolean matchesSafely(IPlatformImportBundle bundle) {
                        return bundle.getProperty("domain-id").equals(expectedSchemaName);
                    }

                    public void describeTo(Description description) {
                        description.appendText("bundle with zipped mondrian schema");
                    }
                })));
            }
        });
    }
    AnalysisService service = new AnalysisService();
    FileInputStream in = new FileInputStream(filePath);
    try {
        service.putMondrianSchema(in, schemaFileInfo, null, null, null, false, false, "Datasource=SampleData;overwrite=false", null);
        mockery.assertIsSatisfied();
    } finally {
        IOUtils.closeQuietly(in);
    }
}
Also used : Expectations(org.jmock.Expectations) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter) AnalysisService(org.pentaho.platform.dataaccess.datasource.api.AnalysisService) ZipFile(java.util.zip.ZipFile) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) Mockery(org.jmock.Mockery) FileInputStream(java.io.FileInputStream)

Example 8 with IPlatformImporter

use of org.pentaho.platform.plugin.services.importer.IPlatformImporter in project data-access by pentaho.

the class DatasourceResourceIT method testImportFile.

private void testImportFile(String filePath, final String expectedSchemaName) throws Exception {
    FormDataContentDisposition schemaFileInfo = mock(FormDataContentDisposition.class);
    when(schemaFileInfo.getFileName()).thenReturn("stubFileName");
    Mockery mockery = new Mockery();
    final IPlatformImporter mockImporter = mockery.mock(IPlatformImporter.class);
    mp.defineInstance(IPlatformImporter.class, mockImporter);
    mockery.checking(new Expectations() {

        {
            oneOf(mockImporter).importFile(with(match(new TypeSafeMatcher<IPlatformImportBundle>() {

                public boolean matchesSafely(IPlatformImportBundle bundle) {
                    return bundle.getProperty("domain-id").equals(expectedSchemaName) && bundle.getMimeType().equals("application/vnd.pentaho.mondrian+xml");
                }

                public void describeTo(Description description) {
                    description.appendText("bundle with mondrian schema");
                }
            })));
        }
    });
    AnalysisService service = new AnalysisService();
    FileInputStream in = new FileInputStream(filePath);
    try {
        service.putMondrianSchema(in, schemaFileInfo, null, null, null, false, false, "Datasource=SampleData;overwrite=false", null);
        mockery.assertIsSatisfied();
    } finally {
        IOUtils.closeQuietly(in);
    }
}
Also used : Expectations(org.jmock.Expectations) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter) AnalysisService(org.pentaho.platform.dataaccess.datasource.api.AnalysisService) Mockery(org.jmock.Mockery) FileInputStream(java.io.FileInputStream)

Example 9 with IPlatformImporter

use of org.pentaho.platform.plugin.services.importer.IPlatformImporter in project pentaho-platform by pentaho.

the class FileService method systemRestore.

public void systemRestore(final InputStream fileUpload, String overwriteFile, String applyAclSettings, String overwriteAclSettings) throws PlatformImportException, SecurityException {
    if (doCanAdminister()) {
        boolean overwriteFileFlag = !"false".equals(overwriteFile);
        boolean applyAclSettingsFlag = !"false".equals(applyAclSettings);
        boolean overwriteAclSettingsFlag = "true".equals(overwriteAclSettings);
        IRepositoryImportLogger importLogger = null;
        Level level = Level.ERROR;
        ByteArrayOutputStream importLoggerStream = new ByteArrayOutputStream();
        String importDirectory = "/";
        RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder();
        bundleBuilder.input(fileUpload);
        bundleBuilder.charSet("UTF-8");
        bundleBuilder.hidden(RepositoryFile.HIDDEN_BY_DEFAULT);
        bundleBuilder.schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT);
        bundleBuilder.path(importDirectory);
        bundleBuilder.overwriteFile(overwriteFileFlag);
        bundleBuilder.name("SystemBackup.zip");
        bundleBuilder.applyAclSettings(applyAclSettingsFlag);
        bundleBuilder.overwriteAclSettings(overwriteAclSettingsFlag);
        bundleBuilder.retainOwnership(true);
        bundleBuilder.preserveDsw(true);
        ImportSession.getSession().setAclProperties(applyAclSettingsFlag, true, overwriteAclSettingsFlag);
        IPlatformImporter importer = PentahoSystem.get(IPlatformImporter.class);
        importLogger = importer.getRepositoryImportLogger();
        importLogger.startJob(importLoggerStream, importDirectory, level);
        try {
            importer.importFile(bundleBuilder.build());
        } finally {
            importLogger.endJob();
        }
    } else {
        throw new SecurityException();
    }
}
Also used : IRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.IRepositoryImportLogger) Level(org.apache.log4j.Level) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) GeneralSecurityException(java.security.GeneralSecurityException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter)

Example 10 with IPlatformImporter

use of org.pentaho.platform.plugin.services.importer.IPlatformImporter in project data-access by pentaho.

the class DatasourceResourceIT method testPublishDsw.

@Test
public void testPublishDsw() throws Exception {
    DataSourceWizardResource service = new DataSourceWizardResource();
    Mockery mockery = new Mockery();
    final IPlatformImporter mockImporter = mockery.mock(IPlatformImporter.class);
    mp.defineInstance(IPlatformImporter.class, mockImporter);
    mockery.checking(new Expectations() {

        {
            oneOf(mockImporter).importFile(with(match(new TypeSafeMatcher<IPlatformImportBundle>() {

                public boolean matchesSafely(IPlatformImportBundle bundle) {
                    return bundle.isPreserveDsw() && bundle.getProperty("domain-id").equals("AModel.xmi") && bundle.getMimeType().equals("text/xmi+xml");
                }

                public void describeTo(Description description) {
                    description.appendText("bundle with xmi");
                }
            })));
            oneOf(mockImporter).importFile(with(match(new TypeSafeMatcher<IPlatformImportBundle>() {

                public boolean matchesSafely(IPlatformImportBundle bundle) {
                    return bundle.getProperty("domain-id").equals("AModel") && bundle.getMimeType().equals("application/vnd.pentaho.mondrian+xml");
                }

                public void describeTo(Description description) {
                    description.appendText("bundle with mondrian schema");
                }
            })));
        }
    });
    FileInputStream in = new FileInputStream(new File(new File("target/test-classes"), "SampleDataOlap.xmi"));
    try {
        Response resp = service.publishDsw("AModel.xmi", in, true, false, null);
        Assert.assertEquals(Response.Status.Family.SUCCESSFUL, Response.Status.fromStatusCode(resp.getStatus()).getFamily());
        mockery.assertIsSatisfied();
    } finally {
        IOUtils.closeQuietly(in);
    }
}
Also used : Expectations(org.jmock.Expectations) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) Response(javax.ws.rs.core.Response) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) DataSourceWizardResource(org.pentaho.platform.dataaccess.datasource.api.resources.DataSourceWizardResource) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter) Mockery(org.jmock.Mockery) ZipFile(java.util.zip.ZipFile) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

IPlatformImporter (org.pentaho.platform.plugin.services.importer.IPlatformImporter)16 IPlatformImportBundle (org.pentaho.platform.api.repository2.unified.IPlatformImportBundle)13 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)9 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)8 Test (org.junit.Test)8 Matchers.anyString (org.mockito.Matchers.anyString)7 RepositoryFileImportBundle (org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle)7 Response (javax.ws.rs.core.Response)6 FileResource (org.pentaho.platform.web.http.api.resources.FileResource)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FormDataContentDisposition (com.sun.jersey.core.header.FormDataContentDisposition)4 ArrayList (java.util.ArrayList)4 StringInputStream (org.apache.tools.ant.filters.StringInputStream)4 Domain (org.pentaho.metadata.model.Domain)4 XmiParser (org.pentaho.metadata.util.XmiParser)4 File (java.io.File)3 Description (org.hamcrest.Description)3 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)3 Expectations (org.jmock.Expectations)3