Search in sources :

Example 6 with DomainAlreadyExistsException

use of org.pentaho.metadata.repository.DomainAlreadyExistsException in project pentaho-platform by pentaho.

the class MetadataImportHandler method processMetadataFile.

/**
 * Processes the file as a metadata file and returns the domain name. It will import the file into the Pentaho
 * Metadata Domain Repository.
 *
 * @param bundle
 * @return
 */
protected String processMetadataFile(final IPlatformImportBundle bundle) throws PlatformImportException {
    final String domainId = (String) bundle.getProperty("domain-id");
    if (domainId == null) {
        throw new PlatformImportException("Bundle missing required domain-id property");
    }
    try {
        log.debug("Importing as metadata - [domain=" + domainId + "]");
        final InputStream inputStream;
        if (bundle.isPreserveDsw()) {
            // storeDomain needs to be able to close the stream
            inputStream = cloneStream(bundle.getInputStream());
        } else {
            inputStream = StripDswFromStream(bundle.getInputStream());
        }
        if (metadataRepositoryImporter instanceof IAclAwarePentahoMetadataDomainRepositoryImporter) {
            IAclAwarePentahoMetadataDomainRepositoryImporter importer = (IAclAwarePentahoMetadataDomainRepositoryImporter) metadataRepositoryImporter;
            importer.storeDomain(inputStream, domainId, bundle.overwriteInRepository(), bundle.isApplyAclSettings() ? bundle.getAcl() : null);
        } else {
            metadataRepositoryImporter.storeDomain(inputStream, domainId, bundle.overwriteInRepository());
        }
        if (metadataRepositoryImporter instanceof IModelAnnotationsAwareMetadataDomainRepositoryImporter) {
            // Store annotations xml with the domain if it exists
            final String annotationsXml = (String) bundle.getProperty(IModelAnnotationsAwareMetadataDomainRepositoryImporter.PROPERTY_NAME_ANNOTATIONS);
            if (StringUtils.isNotBlank(annotationsXml)) {
                // Save annotations
                IModelAnnotationsAwareMetadataDomainRepositoryImporter importer = (IModelAnnotationsAwareMetadataDomainRepositoryImporter) metadataRepositoryImporter;
                importer.storeAnnotationsXml(domainId, annotationsXml);
            }
        }
        return domainId;
    } catch (DomainIdNullException dine) {
        throw new PlatformImportException(dine.getMessage(), PlatformImportException.PUBLISH_TO_SERVER_FAILED, dine);
    } catch (DomainStorageException dse) {
        throw new PlatformImportException(dse.getMessage(), PlatformImportException.PUBLISH_TO_SERVER_FAILED, dse);
    } catch (DomainAlreadyExistsException daee) {
        throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0007_PUBLISH_SCHEMA_EXISTS_ERROR"), PlatformImportException.PUBLISH_SCHEMA_EXISTS_ERROR, daee);
    } catch (Exception e) {
        final String errorMessage = messages.getErrorString("MetadataImportHandler.ERROR_0001_IMPORTING_METADATA", domainId, e.getLocalizedMessage());
        log.error(errorMessage, e);
        throw new PlatformImportException(errorMessage, e);
    }
}
Also used : DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) IModelAnnotationsAwareMetadataDomainRepositoryImporter(org.pentaho.platform.plugin.services.metadata.IModelAnnotationsAwareMetadataDomainRepositoryImporter) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IAclAwarePentahoMetadataDomainRepositoryImporter(org.pentaho.platform.plugin.services.metadata.IAclAwarePentahoMetadataDomainRepositoryImporter) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException)

Example 7 with DomainAlreadyExistsException

use of org.pentaho.metadata.repository.DomainAlreadyExistsException 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 8 with DomainAlreadyExistsException

use of org.pentaho.metadata.repository.DomainAlreadyExistsException in project pentaho-platform by pentaho.

the class MockSessionAwareMetadataDomainRepository method removeModel.

@Override
public void removeModel(String domainId, String modelId) throws DomainIdNullException, DomainStorageException {
    // $NON-NLS-1$
    incrementInvocationCount("removeModel");
    // don't actually do anything
    Domain domain = getDomain(domainId);
    removeDomain(domainId);
    try {
        storeDomain(domain, true);
    } catch (DomainAlreadyExistsException ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) Domain(org.pentaho.metadata.model.Domain)

Example 9 with DomainAlreadyExistsException

use of org.pentaho.metadata.repository.DomainAlreadyExistsException in project pentaho-platform by pentaho.

the class PentahoMetadataDomainRepository method storeDomain.

@Override
public void storeDomain(InputStream inputStream, String domainId, boolean overwrite, RepositoryFileAcl acl) throws DomainIdNullException, DomainAlreadyExistsException, DomainStorageException {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("storeDomain(inputStream, %s, %s, %s)", domainId, overwrite, acl));
    }
    if (null == inputStream) {
        throw new IllegalArgumentException();
    }
    if (StringUtils.isEmpty(domainId)) {
        throw new DomainIdNullException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0001_DOMAIN_ID_NULL"));
    }
    // Check to see if the domain already exists
    RepositoryFile domainFile = getMetadataRepositoryFile(domainId);
    if (domainFile == null && domainId.endsWith(XMI_EXTENSION)) {
        domainFile = getMetadataRepositoryFile(domainId.substring(0, domainId.length() - XMI_EXTENSION.length()));
    }
    if (!overwrite && domainFile != null) {
        final String errorString = messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0002_DOMAIN_ALREADY_EXISTS", domainId);
        logger.error(errorString);
        throw new DomainAlreadyExistsException(errorString);
    }
    // Check if this is valid xml
    InputStream inputStream2;
    String xmi;
    try {
        // try to see if the xmi can be parsed (ie, check if it's valid xmi)
        // first, convert our input stream to a string
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_ENCODING));
        try {
            while ((xmi = reader.readLine()) != null) {
                stringBuilder.append(xmi);
            }
        } finally {
            inputStream.close();
        }
        if (!isDomainIdXmiEqualsOrNotPresent(domainId, getDomainIdFromXmi(stringBuilder))) {
            domainId = replaceDomainId(stringBuilder, domainId);
        }
        xmi = stringBuilder.toString();
        // now, try to see if the xmi can be parsed (ie, check if it's valid xmi)
        byte[] xmiBytes = xmi.getBytes(DEFAULT_ENCODING);
        inputStream2 = new java.io.ByteArrayInputStream(xmiBytes);
        xmiParser.parseXmi(inputStream2);
        // xmi is valid. Create a new inputstream for the actual import action.
        inputStream2.reset();
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        // throw new
        // DomainStorageException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0010_ERROR_PARSING_XMI"),
        // ex);
        java.io.ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ex.printStackTrace(new java.io.PrintStream(byteArrayOutputStream));
        throw new DomainStorageException(byteArrayOutputStream.toString(), ex);
    }
    final SimpleRepositoryFileData data = new SimpleRepositoryFileData(inputStream2, DEFAULT_ENCODING, DOMAIN_MIME_TYPE);
    final RepositoryFile newDomainFile;
    if (domainFile == null) {
        newDomainFile = createUniqueFile(domainId, null, data);
    } else {
        newDomainFile = repository.updateFile(domainFile, data, null);
    }
    // This invalidates any caching
    flushDomains();
    getAclHelper().setAclFor(newDomainFile, acl);
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) BufferedReader(java.io.BufferedReader) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 10 with DomainAlreadyExistsException

use of org.pentaho.metadata.repository.DomainAlreadyExistsException in project pentaho-platform by pentaho.

the class SessionCachingMetadataDomainRepositoryIT method testStoreDomain.

public void testStoreDomain() throws Exception {
    // $NON-NLS-1$
    final String ID = "1";
    MockSessionAwareMetadataDomainRepository mock = new MockSessionAwareMetadataDomainRepository();
    SessionCachingMetadataDomainRepository repo = new SessionCachingMetadataDomainRepository(mock);
    // $NON-NLS-1$ //$NON-NLS-2$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", "1"));
    repo.storeDomain(getTestDomain(ID), false);
    // No cache values when storing a domain
    assertEquals(0, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // Cache one domain
    repo.getDomain(ID);
    // Storing a domain under a different session should wipe out all cached domains with the same id
    // $NON-NLS-1$ //$NON-NLS-2$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", "2"));
    try {
        repo.storeDomain(getTestDomain(ID), false);
        // $NON-NLS-1$
        fail("Should have thrown a " + DomainAlreadyExistsException.class.getSimpleName());
    } catch (DomainAlreadyExistsException ex) {
    // expected
    }
    repo.storeDomain(getTestDomain(ID), true);
    // Storing a domain under a different session should wipe out all cached domains with the same id
    assertEquals(0, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    assertEquals(1, repo.getDomainIds().size());
    repo.getDomain(ID);
    assertEquals(2, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // Storing a domain should only wipe out the cached domains with the same id
    // $NON-NLS-1$
    repo.storeDomain(getTestDomain("2"), false);
    assertEquals(2, repo.getDomainIds().size());
    assertEquals(2, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) SessionCachingMetadataDomainRepository(org.pentaho.platform.plugin.services.metadata.SessionCachingMetadataDomainRepository) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException)

Aggregations

DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)10 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)7 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)7 IOException (java.io.IOException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 Domain (org.pentaho.metadata.model.Domain)3 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)3 LogicalModel (org.pentaho.metadata.model.LogicalModel)2 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)2 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)2 RepositoryFileInputStream (org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream)2 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ZipInputStream (java.util.zip.ZipInputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Test (org.junit.Test)1