Search in sources :

Example 6 with DomainIdNullException

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

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

use of org.pentaho.metadata.repository.DomainIdNullException in project data-access by pentaho.

the class DSWDatasourceServiceImpl method deleteLogicalModel.

public boolean deleteLogicalModel(String domainId, String modelName) throws DatasourceServiceException {
    if (!hasDataAccessPermission()) {
        // $NON-NLS-1$
        logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0001_PERMISSION_DENIED"));
        return false;
    }
    String catalogRef = null;
    String targetTable = null;
    try {
        // first load the model
        Domain domain = getMetadataDomainRepository().getDomain(domainId);
        ModelerWorkspace model = createModelerWorkspace();
        model.setDomain(domain);
        LogicalModel logicalModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
        if (logicalModel == null) {
            logicalModel = model.getLogicalModel(ModelerPerspective.REPORTING);
        }
        LogicalModel logicalModelRep = model.getLogicalModel(ModelerPerspective.REPORTING);
        // CSV related data is bounded to reporting model so need to perform some additional clean up here
        if (logicalModelRep != null) {
            String modelState = (String) logicalModelRep.getProperty(LM_PROP_DATASOURCE_MODEL);
            // TODO: use the edit story's stored info to do this
            if ("CSV".equals(logicalModelRep.getProperty(LM_PROP_DATASOURCE_TYPE)) || "true".equalsIgnoreCase((String) logicalModelRep.getProperty(LogicalModel.PROPERTY_TARGET_TABLE_STAGED))) {
                targetTable = ((SqlPhysicalTable) domain.getPhysicalModels().get(0).getPhysicalTables().get(0)).getTargetTable();
                DatasourceDTO datasource = null;
                if (modelState != null) {
                    datasource = deSerializeModelState(modelState);
                }
                if (datasource != null) {
                    CsvTransformGenerator csvTransformGenerator = new CsvTransformGenerator(datasource.getCsvModelInfo(), AgileHelper.getDatabaseMeta());
                    try {
                        csvTransformGenerator.dropTable(targetTable);
                    } catch (CsvTransformGeneratorException e) {
                        // table might not be there, it's OK that is what we were trying to do anyway
                        logger.warn(Messages.getErrorString("DatasourceServiceImpl.ERROR_0019_UNABLE_TO_DROP_TABLE", targetTable, domainId, e.getLocalizedMessage()), // $NON-NLS-1$
                        e);
                    }
                    String fileName = datasource.getCsvModelInfo().getFileInfo().getFilename();
                    FileUtils fileService = new FileUtils();
                    if (fileName != null) {
                        fileService.deleteFile(fileName);
                    }
                }
            }
        }
        // if associated mondrian file, delete
        if (logicalModel.getProperty(LM_PROP_MONDRIAN_CATALOG_REF) != null) {
            // remove Mondrian schema
            IMondrianCatalogService service = PentahoSystem.get(IMondrianCatalogService.class, null);
            catalogRef = (String) logicalModel.getProperty(LM_PROP_MONDRIAN_CATALOG_REF);
            // check if the model is not already removed
            if (service.getCatalog(catalogRef, PentahoSessionHolder.getSession()) != null) {
                service.removeCatalog(catalogRef, PentahoSessionHolder.getSession());
            }
        }
        getMetadataDomainRepository().removeModel(domainId, logicalModel.getId());
        if (logicalModelRep != null && !logicalModelRep.getId().equals(logicalModel.getId())) {
            getMetadataDomainRepository().removeModel(domainId, logicalModelRep.getId());
        }
        // get updated domain
        domain = getMetadataDomainRepository().getDomain(domainId);
        if (domain == null) {
            // already deleted
            return true;
        }
        if (domain.getLogicalModels() == null || domain.getLogicalModels().isEmpty()) {
            getMetadataDomainRepository().removeDomain(domainId);
        }
    } catch (MondrianCatalogServiceException me) {
        logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0020_UNABLE_TO_DELETE_CATALOG", catalogRef, domainId, me.getLocalizedMessage()), // $NON-NLS-1$
        me);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceServiceImpl.ERROR_0020_UNABLE_TO_DELETE_CATALOG", catalogRef, domainId, me.getLocalizedMessage()), // $NON-NLS-1$
        me);
    } catch (DomainStorageException dse) {
        logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0017_UNABLE_TO_STORE_DOMAIN", domainId, dse.getLocalizedMessage()), // $NON-NLS-1$
        dse);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceServiceImpl.ERROR_0016_UNABLE_TO_STORE_DOMAIN", domainId, dse.getLocalizedMessage()), // $NON-NLS-1$
        dse);
    } catch (DomainIdNullException dne) {
        logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0019_DOMAIN_IS_NULL", dne.getLocalizedMessage()), // $NON-NLS-1$
        dne);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceServiceImpl.ERROR_0019_DOMAIN_IS_NULL", dne.getLocalizedMessage()), // $NON-NLS-1$
        dne);
    }
    return true;
}
Also used : CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) FileUtils(org.pentaho.platform.dataaccess.datasource.wizard.csv.FileUtils) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DatasourceDTO(org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) LogicalModel(org.pentaho.metadata.model.LogicalModel) MondrianCatalogServiceException(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) CsvTransformGenerator(org.pentaho.platform.dataaccess.datasource.wizard.service.agile.CsvTransformGenerator) Domain(org.pentaho.metadata.model.Domain) ModelerWorkspace(org.pentaho.agilebi.modeler.ModelerWorkspace) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)

Aggregations

DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)8 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)8 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)7 IOException (java.io.IOException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)3 Domain (org.pentaho.metadata.model.Domain)2 LogicalModel (org.pentaho.metadata.model.LogicalModel)2 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)2 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)2 MondrianCatalogServiceException (org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException)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