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();
}
}
}
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);
}
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;
}
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);
}
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);
}
}
Aggregations