Search in sources :

Example 16 with IPlatformImportBundle

use of org.pentaho.platform.api.repository2.unified.IPlatformImportBundle in project data-access by pentaho.

the class AnalysisService method putMondrianSchema.

public void putMondrianSchema(InputStream dataInputStream, FormDataContentDisposition schemaFileInfo, // Optional
String catalogName, // Optional
String origCatalogName, // Optional
String datasourceName, boolean overwrite, boolean xmlaEnabledFlag, String parameters, RepositoryFileAclDto acl) throws PentahoAccessControlException, PlatformImportException, Exception {
    accessValidation();
    String fileName = schemaFileInfo.getFileName();
    // sanity check to prevent common mistake - import of .xmi files.
    // See BISERVER-12815
    fileNameValidation(fileName);
    ZipInputStream zis = null;
    ByteArrayOutputStream mondrian = null;
    ByteArrayOutputStream annotations = null;
    if (fileName.endsWith(ZIP_EXTENSION)) {
        zis = new ZipInputStream(dataInputStream);
        ZipEntry ze = null;
        int len = 0;
        while ((ze = zis.getNextEntry()) != null) {
            if (ze.getName().endsWith(MONDRIAN_FILE_EXTENSION)) {
                IOUtils.copy(zis, mondrian = new ByteArrayOutputStream());
            } else if (ze.getName().equals(ANNOTATIONS_FILE)) {
                IOUtils.copy(zis, annotations = new ByteArrayOutputStream());
            }
            zis.closeEntry();
        }
        if (mondrian != null) {
            dataInputStream = new ByteArrayInputStream(mondrian.toByteArray());
        }
    }
    try {
        processMondrianImport(dataInputStream, catalogName, origCatalogName, overwrite, xmlaEnabledFlag, parameters, fileName, acl);
        if (annotations != null) {
            String catName = (catalogName != null) ? catalogName : fileName.substring(0, fileName.indexOf('.'));
            ByteArrayInputStream annots = new ByteArrayInputStream(annotations.toByteArray());
            IPlatformImportBundle mondrianBundle = new RepositoryFileImportBundle.Builder().input(annots).path(ANNOTATION_FOLDER + catName).name(ANNOTATIONS_FILE).charSet("UTF-8").overwriteFile(true).mime("text/xml").withParam("domain-id", catName).build();
            // do import
            importer.importFile(mondrianBundle);
            logger.debug("imported mondrian annotations");
            annots.close();
        }
    } finally {
        if (zis != null) {
            zis.close();
        }
        if (mondrian != null) {
            mondrian.close();
        }
        if (annotations != null) {
            annotations.close();
        }
    }
}
Also used : IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 17 with IPlatformImportBundle

use of org.pentaho.platform.api.repository2.unified.IPlatformImportBundle in project data-access by pentaho.

the class AnalysisService method processMondrianImport.

/**
 * This is the main method that handles the actual Import Handler to persist to PUR
 *
 * @param dataInputStream
 * @param catalogName
 * @param overwrite
 * @param xmlaEnabledFlag
 * @param parameters
 * @param fileName
 * @param acl acl information for the data source. This parameter is optional.
 * @throws PlatformImportException
 */
protected void processMondrianImport(InputStream dataInputStream, String catalogName, String origCatalogName, boolean overwrite, boolean xmlaEnabledFlag, String parameters, String fileName, RepositoryFileAclDto acl) throws PlatformImportException {
    boolean overWriteInRepository = determineOverwriteFlag(parameters, overwrite);
    IPlatformImportBundle bundle = createPlatformBundle(parameters, dataInputStream, catalogName, overWriteInRepository, fileName, xmlaEnabledFlag, acl);
    if (isChangeCatalogName(origCatalogName, bundle)) {
        IMondrianCatalogService catalogService = PentahoSystem.get(IMondrianCatalogService.class, PentahoSessionHolder.getSession());
        catalogService.removeCatalog(origCatalogName, PentahoSessionHolder.getSession());
    }
    if (isOverwriteAnnotations(parameters, overWriteInRepository)) {
        IMondrianCatalogService catalogService = PentahoSystem.get(IMondrianCatalogService.class, PentahoSessionHolder.getSession());
        List<MondrianCatalog> catalogs = catalogService.listCatalogs(PentahoSessionHolder.getSession(), false);
        for (MondrianCatalog catalog : catalogs) {
            if (catalog.getName().equals(bundle.getName())) {
                catalogService.removeCatalog(bundle.getName(), PentahoSessionHolder.getSession());
                break;
            }
        }
    }
    importer.importFile(bundle);
}
Also used : IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService)

Example 18 with IPlatformImportBundle

use of org.pentaho.platform.api.repository2.unified.IPlatformImportBundle in project data-access by pentaho.

the class DataSourceWizardService method publishDsw.

public String publishDsw(String domainId, InputStream metadataFile, List<InputStream> localizeFiles, List<String> localizeFileNames, boolean overwrite, boolean checkConnection, RepositoryFileAclDto acl) throws PentahoAccessControlException, IllegalArgumentException, DswPublishValidationException, Exception {
    if (!hasManageAccessCheck()) {
        throw new PentahoAccessControlException();
    }
    if (!endsWith(domainId, METADATA_EXT)) {
        // if doesn't end in case-sensitive '.xmi' there will be trouble later on
        final String errorMsg = "domainId must end in " + METADATA_EXT;
        throw new IllegalArgumentException(errorMsg);
    }
    if (localizeFiles == null ? (localizeFileNames != null) : (localizeFiles.size() != localizeFileNames.size())) {
        throw new IllegalArgumentException("localizeFiles and localizeFileNames must have equal size");
    }
    if (metadataFile == null) {
        throw new IllegalArgumentException("metadataFile is null");
    }
    if (!overwrite) {
        final List<String> overwritten = getOverwrittenDomains(domainId);
        if (!overwritten.isEmpty()) {
            final String domainIds = StringUtils.join(overwritten, ",");
            throw new DswPublishValidationException(DswPublishValidationException.Type.OVERWRITE_CONFLICT, domainIds);
        }
    }
    XmiParser xmiParser = createXmiParser();
    Domain domain = null;
    try {
        domain = xmiParser.parseXmi(metadataFile);
    } catch (Exception e) {
        throw new DswPublishValidationException(DswPublishValidationException.Type.INVALID_XMI, e.getMessage());
    }
    domain.setId(domainId);
    if (checkConnection) {
        final String connectionId = getMondrianDatasourceWrapper(domain);
        // Left second check with non-escaped name for backward compatibility
        if (datasourceMgmtSvc.getDatasourceByName(sanitizer.escape(connectionId)) == null && datasourceMgmtSvc.getDatasourceByName(connectionId) == null) {
            final String msg = "connection not found: '" + connectionId + "'";
            throw new DswPublishValidationException(Type.MISSING_CONNECTION, msg);
        }
    }
    // build bundles
    IPlatformImportBundle mondrianBundle = createMondrianDswBundle(domain, acl);
    InputStream metadataIn = toInputStreamWrapper(domain, xmiParser);
    IPlatformImportBundle metadataBundle = createMetadataDswBundle(domain, metadataIn, overwrite, acl);
    // add localization bundles
    if (localizeFiles != null) {
        for (int i = 0; i < localizeFiles.size(); i++) {
            IPlatformImportBundle localizationBundle = MetadataService.createNewRepositoryFileImportBundle(localizeFiles.get(i), localizeFileNames.get(i), domainId);
            metadataBundle.getChildBundles().add(localizationBundle);
            logger.info("created language file");
        }
    }
    // do import
    IPlatformImporter importer = getIPlatformImporter();
    importer.importFile(metadataBundle);
    logger.debug("imported metadata xmi");
    importer.importFile(mondrianBundle);
    logger.debug("imported mondrian schema");
    // trigger refreshes
    IPentahoSession session = getSession();
    PentahoSystem.publish(session, METADATA_PUBLISHER);
    PentahoSystem.publish(session, MONDRIAN_PUBLISHER);
    logger.info("publishDsw: Published DSW with domainId='" + domainId + "'.");
    return domainId;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) FileNotFoundException(java.io.FileNotFoundException) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) MondrianCatalogServiceException(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) IOException(java.io.IOException) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) XmiParser(org.pentaho.metadata.util.XmiParser) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter) Domain(org.pentaho.metadata.model.Domain)

Example 19 with IPlatformImportBundle

use of org.pentaho.platform.api.repository2.unified.IPlatformImportBundle 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 20 with IPlatformImportBundle

use of org.pentaho.platform.api.repository2.unified.IPlatformImportBundle 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)

Aggregations

IPlatformImportBundle (org.pentaho.platform.api.repository2.unified.IPlatformImportBundle)34 Test (org.junit.Test)21 InputStream (java.io.InputStream)15 FileInputStream (java.io.FileInputStream)14 IPlatformImporter (org.pentaho.platform.plugin.services.importer.IPlatformImporter)13 File (java.io.File)12 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)10 RepositoryFileImportBundle (org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle)8 ArrayList (java.util.ArrayList)7 Expectations (org.jmock.Expectations)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)5 Response (javax.ws.rs.core.Response)5 Matchers.anyString (org.mockito.Matchers.anyString)5 FileResource (org.pentaho.platform.web.http.api.resources.FileResource)5 FormDataContentDisposition (com.sun.jersey.core.header.FormDataContentDisposition)4 Domain (org.pentaho.metadata.model.Domain)4 XmiParser (org.pentaho.metadata.util.XmiParser)4