Search in sources :

Example 21 with IPlatformImportBundle

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

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

the class SolutionImportHandler method importFile.

@Override
public void importFile(IPlatformImportBundle bundle) throws PlatformImportException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException, IOException {
    RepositoryFileImportBundle importBundle = (RepositoryFileImportBundle) bundle;
    ZipInputStream zipImportStream = new ZipInputStream(bundle.getInputStream());
    SolutionRepositoryImportSource importSource = new SolutionRepositoryImportSource(zipImportStream);
    LocaleFilesProcessor localeFilesProcessor = new LocaleFilesProcessor();
    setOverwriteFile(bundle.overwriteInRepository());
    // importSession.set(ImportSession.getSession());
    IPlatformImporter importer = PentahoSystem.get(IPlatformImporter.class);
    cachedImports = new HashMap<String, RepositoryFileImportBundle.Builder>();
    // Process Manifest Settings
    ExportManifest manifest = getImportSession().getManifest();
    String manifestVersion = null;
    if (manifest != null) {
        manifestVersion = manifest.getManifestInformation().getManifestVersion();
    }
    // Process Metadata
    if (manifest != null) {
        // import the users
        Map<String, List<String>> roleToUserMap = importUsers(manifest.getUserExports());
        // import the roles
        importRoles(manifest.getRoleExports(), roleToUserMap);
        List<ExportManifestMetadata> metadataList = manifest.getMetadataList();
        for (ExportManifestMetadata exportManifestMetadata : metadataList) {
            String domainId = exportManifestMetadata.getDomainId();
            if (domainId != null && !domainId.endsWith(XMI_EXTENSION)) {
                domainId = domainId + XMI_EXTENSION;
            }
            boolean overWriteInRepository = isOverwriteFile();
            RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder().charSet("UTF-8").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).preserveDsw(bundle.isPreserveDsw()).overwriteFile(overWriteInRepository).mime("text/xmi+xml").withParam("domain-id", domainId);
            cachedImports.put(exportManifestMetadata.getFile(), bundleBuilder);
        }
        // Process Mondrian
        List<ExportManifestMondrian> mondrianList = manifest.getMondrianList();
        for (ExportManifestMondrian exportManifestMondrian : mondrianList) {
            String catName = exportManifestMondrian.getCatalogName();
            Parameters parametersMap = exportManifestMondrian.getParameters();
            StringBuilder parametersStr = new StringBuilder();
            for (String s : parametersMap.keySet()) {
                parametersStr.append(s).append("=").append(parametersMap.get(s)).append(sep);
            }
            RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder().charSet("UTF_8").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).name(catName).overwriteFile(isOverwriteFile()).mime("application/vnd.pentaho.mondrian+xml").withParam("parameters", parametersStr.toString()).withParam("domain-id", // TODO: this is
            catName);
            // definitely
            // named wrong
            // at the very
            // least.
            // pass as param if not in parameters string
            String xmlaEnabled = "" + exportManifestMondrian.isXmlaEnabled();
            bundleBuilder.withParam("EnableXmla", xmlaEnabled);
            cachedImports.put(exportManifestMondrian.getFile(), bundleBuilder);
            String annotationsFile = exportManifestMondrian.getAnnotationsFile();
            if (annotationsFile != null) {
                RepositoryFileImportBundle.Builder annotationsBundle = new RepositoryFileImportBundle.Builder().path(MondrianCatalogRepositoryHelper.ETC_MONDRIAN_JCR_FOLDER + RepositoryFile.SEPARATOR + catName).name("annotations.xml").charSet("UTF_8").overwriteFile(isOverwriteFile()).mime("text/xml").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).withParam("domain-id", catName);
                cachedImports.put(annotationsFile, annotationsBundle);
            }
        }
    }
    importMetaStore(manifest, bundle.overwriteInRepository());
    for (IRepositoryFileBundle fileBundle : importSource.getFiles()) {
        String fileName = fileBundle.getFile().getName();
        String actualFilePath = fileBundle.getPath();
        if (manifestVersion != null) {
            fileName = ExportFileNameEncoder.decodeZipFileName(fileName);
            actualFilePath = ExportFileNameEncoder.decodeZipFileName(actualFilePath);
        }
        String repositoryFilePath = RepositoryFilenameUtils.concat(PentahoPlatformImporter.computeBundlePath(actualFilePath), fileName);
        if (this.cachedImports.containsKey(repositoryFilePath)) {
            byte[] bytes = IOUtils.toByteArray(fileBundle.getInputStream());
            RepositoryFileImportBundle.Builder builder = cachedImports.get(repositoryFilePath);
            builder.input(new ByteArrayInputStream(bytes));
            importer.importFile(build(builder));
            continue;
        }
        RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder();
        InputStream bundleInputStream = null;
        String decodedFilePath = fileBundle.getPath();
        RepositoryFile decodedFile = fileBundle.getFile();
        if (manifestVersion != null) {
            decodedFile = new RepositoryFile.Builder(decodedFile).path(decodedFilePath).name(fileName).title(fileName).build();
            decodedFilePath = ExportFileNameEncoder.decodeZipFileName(fileBundle.getPath());
        }
        if (fileBundle.getFile().isFolder()) {
            bundleBuilder.mime("text/directory");
            bundleBuilder.file(decodedFile);
            fileName = repositoryFilePath;
            repositoryFilePath = importBundle.getPath();
        } else {
            byte[] bytes = IOUtils.toByteArray(fileBundle.getInputStream());
            bundleInputStream = new ByteArrayInputStream(bytes);
            // If is locale file store it for later processing.
            if (localeFilesProcessor.isLocaleFile(fileBundle, importBundle.getPath(), bytes)) {
                log.trace("Skipping [" + repositoryFilePath + "], it is a locale property file");
                continue;
            }
            bundleBuilder.input(bundleInputStream);
            bundleBuilder.mime(solutionHelper.getMime(fileName));
            String filePath = (decodedFilePath.equals("/") || decodedFilePath.equals("\\")) ? "" : decodedFilePath;
            repositoryFilePath = RepositoryFilenameUtils.concat(importBundle.getPath(), filePath);
        }
        bundleBuilder.name(fileName);
        bundleBuilder.path(repositoryFilePath);
        String sourcePath;
        if (fileBundle.getFile().isFolder()) {
            sourcePath = fileName;
        } else {
            sourcePath = RepositoryFilenameUtils.concat(PentahoPlatformImporter.computeBundlePath(actualFilePath), fileName);
        }
        // may not have rights to such as /home or /public
        if (manifest != null && manifest.getExportManifestEntity(sourcePath) == null && fileBundle.getFile().isFolder()) {
            continue;
        }
        getImportSession().setCurrentManifestKey(sourcePath);
        bundleBuilder.charSet(bundle.getCharset());
        bundleBuilder.overwriteFile(bundle.overwriteInRepository());
        bundleBuilder.applyAclSettings(bundle.isApplyAclSettings());
        bundleBuilder.retainOwnership(bundle.isRetainOwnership());
        bundleBuilder.overwriteAclSettings(bundle.isOverwriteAclSettings());
        bundleBuilder.acl(getImportSession().processAclForFile(sourcePath));
        RepositoryFile file = getFile(importBundle, fileBundle);
        ManifestFile manifestFile = getImportSession().getManifestFile(sourcePath, file != null);
        bundleBuilder.hidden(isFileHidden(file, manifestFile, sourcePath));
        boolean isSchedulable = isSchedulable(file, manifestFile);
        if (isSchedulable) {
            bundleBuilder.schedulable(isSchedulable);
        } else {
            bundleBuilder.schedulable(fileIsScheduleInputSource(manifest, sourcePath));
        }
        IPlatformImportBundle platformImportBundle = build(bundleBuilder);
        importer.importFile(platformImportBundle);
        if (bundleInputStream != null) {
            bundleInputStream.close();
            bundleInputStream = null;
        }
    }
    if (manifest != null) {
        importSchedules(manifest.getScheduleList());
        // Add Hitachi Vantara Connections
        List<org.pentaho.database.model.DatabaseConnection> datasourceList = manifest.getDatasourceList();
        if (datasourceList != null) {
            IDatasourceMgmtService datasourceMgmtSvc = PentahoSystem.get(IDatasourceMgmtService.class);
            for (org.pentaho.database.model.DatabaseConnection databaseConnection : datasourceList) {
                if (databaseConnection.getDatabaseType() == null) {
                    // don't try to import the connection if there is no type it will cause an error
                    // However, if this is the DI Server, and the connection is defined in a ktr, it will import automatically
                    log.warn("Can't import connection " + databaseConnection.getName() + " because it doesn't have a databaseType");
                    continue;
                }
                try {
                    IDatabaseConnection existingDBConnection = datasourceMgmtSvc.getDatasourceByName(databaseConnection.getName());
                    if (existingDBConnection != null && existingDBConnection.getName() != null) {
                        if (isOverwriteFile()) {
                            databaseConnection.setId(existingDBConnection.getId());
                            datasourceMgmtSvc.updateDatasourceByName(databaseConnection.getName(), databaseConnection);
                        }
                    } else {
                        datasourceMgmtSvc.createDatasource(databaseConnection);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    // Process locale files.
    localeFilesProcessor.processLocaleFiles(importer);
}
Also used : ExportManifestMetadata(org.pentaho.platform.plugin.services.importexport.exportManifest.bindings.ExportManifestMetadata) ExportManifest(org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest) IDatasourceMgmtService(org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) List(java.util.List) ArrayList(java.util.ArrayList) ExportManifestMondrian(org.pentaho.platform.plugin.services.importexport.exportManifest.bindings.ExportManifestMondrian) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) IRepositoryFileBundle(org.pentaho.platform.plugin.services.importexport.ImportSource.IRepositoryFileBundle) Parameters(org.pentaho.platform.plugin.services.importexport.exportManifest.Parameters) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ManifestFile(org.pentaho.platform.plugin.services.importexport.ImportSession.ManifestFile) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection)

Example 23 with IPlatformImportBundle

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

the class LocaleImportHandler method importFile.

public void importFile(IPlatformImportBundle bundle) throws PlatformImportException {
    RepositoryFileImportBundle localeBundle = (RepositoryFileImportBundle) bundle;
    Properties localePropertiesFromIndex = loadPropertiesByXml(localeBundle);
    RepositoryFile localeParent = getLocaleParent(localeBundle, localePropertiesFromIndex);
    Properties localeProperties = buildLocaleProperties(localeBundle, localePropertiesFromIndex);
    String bundleFileName = localeBundle.getFile() != null ? localeBundle.getFile().getName() : localeBundle.getName();
    if (localeParent != null && unifiedRepository != null && bundleFileName != null) {
        // If the parent file (content) got skipped because it existed then we will not import the locale information
        String fullPath = RepositoryFilenameUtils.concat(localeBundle.getPath(), localeParent.getName());
        if (ImportSession.getSession().getSkippedFiles().contains(fullPath)) {
            getLogger().trace("Not importing Locale [" + bundleFileName + "] since parent file not written ");
        } else {
            getLogger().trace("Processing Locale [" + bundleFileName + "]");
            unifiedRepository.setLocalePropertiesForFile(localeParent, extractLocaleCode(localeBundle), localeProperties);
        }
    }
}
Also used : RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Properties(java.util.Properties)

Example 24 with IPlatformImportBundle

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

the class MondrianImportHandler method importFile.

/**
 * **************************************** Main entry point from the Spring Interface
 *
 * @param IPlatformImportBundle
 * @throws IOException
 * @throws DomainStorageException
 * @throws DomainAlreadyExistsException
 * @throws DomainIdNullException
 * @throws PlatformImportException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
public void importFile(IPlatformImportBundle bundle) throws PlatformImportException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException, IOException {
    boolean overwriteInRepossitory = bundle.overwriteInRepository();
    boolean xmla = "false".equalsIgnoreCase(findParameterPropertyValue(bundle, ENABLE_XMLA)) ? false : true;
    final String domainId = (String) bundle.getProperty(DOMAIN_ID);
    if (domainId == null) {
        throw new PlatformImportException("Bundle missing required domain-id property");
    }
    try {
        InputStream is = bundle.getInputStream();
        MondrianCatalog catalog = this.createCatalogObject(domainId, xmla, bundle);
        IPentahoSession session = PentahoSessionHolder.getSession();
        if (mondrianRepositoryImporter instanceof IAclAwareMondrianCatalogService) {
            RepositoryFileAcl acl = bundle.isApplyAclSettings() ? bundle.getAcl() : null;
            IAclAwareMondrianCatalogService aware = (IAclAwareMondrianCatalogService) mondrianRepositoryImporter;
            aware.addCatalog(is, catalog, overwriteInRepossitory, acl, session);
        } else {
            mondrianRepositoryImporter.addCatalog(is, catalog, overwriteInRepossitory, session);
        }
    } catch (MondrianCatalogServiceException mse) {
        int statusCode = convertExceptionToStatus(mse);
        throw new PlatformImportException(mse.getMessage(), statusCode);
    } catch (Exception e) {
        throw new PlatformImportException(e.getMessage(), PlatformImportException.PUBLISH_GENERAL_ERROR);
    }
}
Also used : MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) MondrianCatalogServiceException(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException) IAclAwareMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IAclAwareMondrianCatalogService) InputStream(java.io.InputStream) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) IOException(java.io.IOException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MondrianCatalogServiceException(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException) SAXException(org.xml.sax.SAXException)

Example 25 with IPlatformImportBundle

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

the class RepositoryFileImportFileHandler method importFile.

@Override
public void importFile(IPlatformImportBundle bnd) throws PlatformImportException {
    if (bnd instanceof RepositoryFileImportBundle == false) {
        throw new PlatformImportException("Error importing bundle. RepositoryFileImportBundle expected");
    }
    RepositoryFileImportBundle bundle = (RepositoryFileImportBundle) bnd;
    if (bundle.isSchedulable() == null) {
        bundle.setSchedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT);
    }
    String repositoryFilePath = RepositoryFilenameUtils.concat(bundle.getPath(), bundle.getName());
    getLogger().trace("Processing [" + repositoryFilePath + "]");
    // Verify if destination already exists in the repository.
    RepositoryFile file = repository.getFile(repositoryFilePath);
    if (file != null) {
        if (file.isFolder() && getImportSession().getFoldersCreatedImplicitly().contains(repositoryFilePath)) {
            getLogger().trace("Skipping entry for folder [" + repositoryFilePath + "]. It was already processed implicitly.");
        } else {
            if (bundle.overwriteInRepossitory()) {
                // If file exists, overwrite is true and is not a folder then update it.
                if (!file.isFolder()) {
                    file = finalAdjustFile(bundle, file);
                    copyFileToRepository(bundle, repositoryFilePath, file);
                } else {
                    // The folder exists. Possible ACL changes.
                    getLogger().trace("Existing folder [" + repositoryFilePath + "]");
                    file = finalAdjustFolder(bundle, file.getId());
                    repository.updateFolder(file, null);
                    if (bundle.getAcl() != null) {
                        updateAclFromBundle(false, bundle, file);
                    }
                }
            } else {
                if (getImportSession().getIsNotRunningImport()) {
                    throw new PlatformImportException(messages.getString("DefaultImportHandler.ERROR_0009_OVERWRITE_CONTENT", repositoryFilePath), PlatformImportException.PUBLISH_CONTENT_EXISTS_ERROR);
                } else {
                    getLogger().trace("Not importing existing file [" + repositoryFilePath + "]");
                    ImportSession importSession = ImportSession.getSession();
                    importSession.getSkippedFiles().add(repositoryFilePath);
                }
            }
        }
    } else {
        if (bundle.isFolder()) {
            // The file doesn't exist and it is a folder. Create folder.
            getLogger().trace("Creating folder [" + repositoryFilePath + "]");
            final Serializable parentId = getParentId(repositoryFilePath);
            bundle.setFile(bundle.getFile());
            RepositoryFile repoFile = finalAdjustFolder(bundle, null);
            if (bundle.getAcl() != null) {
                repoFile = repository.createFolder(parentId, repoFile, bundle.getAcl(), null);
                updateAclFromBundle(true, bundle, repoFile);
            } else {
                repository.createFolder(parentId, repoFile, null);
            }
        } else {
            // The file doesn't exist. Create file.
            getLogger().trace("Creating file [" + repositoryFilePath + "]");
            copyFileToRepository(bundle, repositoryFilePath, null);
        }
    }
}
Also used : ImportSession(org.pentaho.platform.plugin.services.importexport.ImportSession) Serializable(java.io.Serializable) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

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