Search in sources :

Example 1 with IRepositoryExporter

use of org.pentaho.di.repository.IRepositoryExporter in project pentaho-kettle by pentaho.

the class JobEntryExportRepository method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setNrErrors(1);
    result.setResult(false);
    String realrepName = environmentSubstitute(repositoryname);
    String realusername = environmentSubstitute(username);
    String realpassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password));
    String realfoldername = environmentSubstitute(directoryPath);
    String realoutfilename = environmentSubstitute(targetfilename);
    if (export_type.equals(Export_All) || export_type.equals(Export_Jobs) || export_type.equals(Export_Trans) || export_type.equals(Export_One_Folder)) {
        realoutfilename = buildFilename(realoutfilename);
    }
    NrErrors = 0;
    successConditionBroken = false;
    limitErr = Const.toInt(environmentSubstitute(getNrLimit()), 10);
    try {
        file = KettleVFS.getFileObject(realoutfilename, this);
        if (file.exists()) {
            if (export_type.equals(Export_All) || export_type.equals(Export_Jobs) || export_type.equals(Export_Trans) || export_type.equals(Export_One_Folder)) {
                if (iffileexists.equals(If_FileExists_Fail)) {
                    logError(BaseMessages.getString(PKG, "JobExportRepository.Log.Failing", realoutfilename));
                    return result;
                } else if (iffileexists.equals(If_FileExists_Skip)) {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.Exit", realoutfilename));
                    }
                    result.setResult(true);
                    result.setNrErrors(0);
                    return result;
                } else if (iffileexists.equals(If_FileExists_Uniquename)) {
                    String parentFolder = KettleVFS.getFilename(file.getParent());
                    String shortFilename = file.getName().getBaseName();
                    shortFilename = buildUniqueFilename(shortFilename);
                    file = KettleVFS.getFileObject(parentFolder + Const.FILE_SEPARATOR + shortFilename, this);
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.NewFilename", file.toString()));
                    }
                }
            } else if (export_type.equals(Export_By_Folder)) {
                if (file.getType() != FileType.FOLDER) {
                    logError(BaseMessages.getString(PKG, "JobExportRepository.Log.NotFolder", "" + file.getName()));
                    return result;
                }
            }
        } else {
            if (export_type.equals(Export_By_Folder)) {
                // create folder?
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.FolderNotExists", "" + file.getName()));
                }
                if (!createfolder) {
                    return result;
                }
                file.createFolder();
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.FolderCreated", file.toString()));
                }
            } else if (export_type.equals(Export_All) || export_type.equals(Export_Jobs) || export_type.equals(Export_Trans) || export_type.equals(Export_One_Folder)) {
                // create parent folder?
                if (!file.getParent().exists()) {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.FolderNotExists", "" + file.getParent().toString()));
                    }
                    if (createfolder) {
                        file.getParent().createFolder();
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.FolderCreated", file.getParent().toString()));
                        }
                    } else {
                        return result;
                    }
                }
            }
        }
        realoutfilename = KettleVFS.getFilename(this.file);
        // connect to repository
        connectRep(log, realrepName, realusername, realpassword);
        IRepositoryExporter exporter = repository.getExporter();
        if (export_type.equals(Export_All)) {
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.StartingExportAllRep", realoutfilename));
            }
            exporter.exportAllObjects(null, realoutfilename, null, "all");
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.EndExportAllRep", realoutfilename));
            }
            if (add_result_filesname) {
                addFileToResultFilenames(realoutfilename, log, result, parentJob);
            }
        } else if (export_type.equals(Export_Jobs)) {
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.StartingExportJobsRep", realoutfilename));
            }
            exporter.exportAllObjects(null, realoutfilename, null, "jobs");
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.EndExportJobsRep", realoutfilename));
            }
            if (add_result_filesname) {
                addFileToResultFilenames(realoutfilename, log, result, parentJob);
            }
        } else if (export_type.equals(Export_Trans)) {
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.StartingExportTransRep", realoutfilename));
            }
            exporter.exportAllObjects(null, realoutfilename, null, "trans");
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.EndExportTransRep", realoutfilename));
            }
            if (add_result_filesname) {
                addFileToResultFilenames(realoutfilename, log, result, parentJob);
            }
        } else if (export_type.equals(Export_One_Folder)) {
            RepositoryDirectoryInterface directory = new RepositoryDirectory();
            directory = repository.findDirectory(realfoldername);
            if (directory != null) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.ExpAllFolderRep", directoryPath, realoutfilename));
                }
                exporter.exportAllObjects(null, realoutfilename, directory, "all");
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.EndExpAllFolderRep", directoryPath, realoutfilename));
                }
                if (add_result_filesname) {
                    addFileToResultFilenames(realoutfilename, log, result, parentJob);
                }
            } else {
                logError(BaseMessages.getString(PKG, "JobExportRepository.Error.CanNotFindFolderInRep", realfoldername, realrepName));
                return result;
            }
        } else if (export_type.equals(Export_By_Folder)) {
            // User must give a destination folder..
            RepositoryDirectoryInterface directory = new RepositoryDirectory();
            directory = this.repository.loadRepositoryDirectoryTree().findRoot();
            // Loop over all the directory id's
            ObjectId[] dirids = directory.getDirectoryIDs();
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobExportRepository.Log.TotalFolders", "" + dirids.length));
            }
            for (int d = 0; d < dirids.length && !parentJob.isStopped(); d++) {
                // Success condition broken?
                if (successConditionBroken) {
                    logError(BaseMessages.getString(PKG, "JobExportRepository.Error.SuccessConditionbroken", "" + NrErrors));
                    throw new Exception(BaseMessages.getString(PKG, "JobExportRepository.Error.SuccessConditionbroken", "" + NrErrors));
                }
                RepositoryDirectoryInterface repdir = directory.findDirectory(dirids[d]);
                if (!processOneFolder(parentJob, result, log, repdir, realoutfilename, d, dirids.length)) {
                    // updateErrors
                    updateErrors();
                }
            }
        // end for
        }
    } catch (Exception e) {
        updateErrors();
        logError(BaseMessages.getString(PKG, "JobExportRepository.UnExpectedError", e.toString()));
        logError("Stack trace: " + Const.CR + Const.getStackTracker(e));
    } finally {
        if (this.repository != null) {
            this.repository.disconnect();
            this.repository = null;
        }
        if (this.repositoryMeta != null) {
            this.repositoryMeta = null;
        }
        if (this.repsinfo != null) {
            this.repsinfo.clear();
            this.repsinfo = null;
        }
        if (this.file != null) {
            try {
                this.file.close();
                this.file = null;
            } catch (Exception e) {
            // Ignore close errors
            }
        }
    }
    // Success Condition
    result.setNrErrors(NrErrors);
    if (getSuccessStatus()) {
        result.setResult(true);
    }
    return result;
}
Also used : RepositoryDirectoryInterface(org.pentaho.di.repository.RepositoryDirectoryInterface) RepositoryDirectory(org.pentaho.di.repository.RepositoryDirectory) ObjectId(org.pentaho.di.repository.ObjectId) IRepositoryExporter(org.pentaho.di.repository.IRepositoryExporter) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) Result(org.pentaho.di.core.Result)

Example 2 with IRepositoryExporter

use of org.pentaho.di.repository.IRepositoryExporter in project pentaho-kettle by pentaho.

the class PurRepositoryIT method testExportWithRules.

@Test
public void testExportWithRules() throws Exception {
    String fileName = "testExportWithRuled.xml";
    // $NON-NLS-1$
    final String exportFileName = new File(fileName).getAbsolutePath();
    RepositoryDirectoryInterface rootDir = initRepo();
    String transWithoutNoteName = "2" + EXP_DBMETA_NAME;
    TransMeta transWithoutNote = createTransMeta(transWithoutNoteName);
    String transUniqueName = EXP_TRANS_NAME.concat(transWithoutNoteName);
    RepositoryDirectoryInterface transDir = rootDir.findDirectory(DIR_TRANSFORMATIONS);
    repository.save(transWithoutNote, VERSION_COMMENT_V1, null);
    // So this transformation is cleaned up afterward
    deleteStack.push(transWithoutNote);
    assertNotNull(transWithoutNote.getObjectId());
    assertTrue(hasVersionWithComment(transWithoutNote, VERSION_COMMENT_V1));
    assertTrue(repository.exists(transUniqueName, transDir, RepositoryObjectType.TRANSFORMATION));
    // Second transformation (contained note)
    String transWithNoteName = "1" + EXP_DBMETA_NAME;
    TransMeta transWithNote = createTransMeta(transWithNoteName);
    transUniqueName = EXP_TRANS_NAME.concat(EXP_DBMETA_NAME);
    TransMeta transWithRules = createTransMeta(EXP_DBMETA_NAME);
    NotePadMeta note = new NotePadMeta("Note Message", 1, 1, 100, 5);
    transWithRules.addNote(note);
    repository.save(transWithRules, VERSION_COMMENT_V1, null);
    // So this transformation is cleaned up afterward
    deleteStack.push(transWithRules);
    assertNotNull(transWithRules.getObjectId());
    assertTrue(hasVersionWithComment(transWithRules, VERSION_COMMENT_V1));
    assertTrue(repository.exists(transUniqueName, transDir, RepositoryObjectType.TRANSFORMATION));
    // create rules for export to .xml file
    List<ImportRuleInterface> rules = new AbstractList<ImportRuleInterface>() {

        @Override
        public ImportRuleInterface get(int index) {
            TransformationHasANoteImportRule rule = new TransformationHasANoteImportRule();
            rule.setEnabled(true);
            return rule;
        }

        @Override
        public int size() {
            return 1;
        }
    };
    ImportRules importRules = new ImportRules();
    importRules.setRules(rules);
    // create exporter
    IRepositoryExporter exporter = repository.getExporter();
    exporter.setImportRulesToValidate(importRules);
    // export itself
    try {
        // $NON-NLS-1$
        exporter.exportAllObjects(new MockProgressMonitorListener(), exportFileName, null, "all");
        FileObject exportFile = KettleVFS.getFileObject(exportFileName);
        assertNotNull(exportFile);
        MockRepositoryExportParser parser = new MockRepositoryExportParser();
        SAXParserFactory.newInstance().newSAXParser().parse(KettleVFS.getInputStream(exportFile), parser);
        if (parser.getFatalError() != null) {
            throw parser.getFatalError();
        }
        // assumed transformation with note will be here and only it
        assertEquals("Incorrect number of transformations", 1, parser.getNodesWithName(RepositoryObjectType.TRANSFORMATION.getTypeDescription()).size());
    } finally {
        KettleVFS.getFileObject(exportFileName).delete();
    }
}
Also used : AbstractList(java.util.AbstractList) RepositoryDirectoryInterface(org.pentaho.di.repository.RepositoryDirectoryInterface) ImportRuleInterface(org.pentaho.di.imp.rule.ImportRuleInterface) TransMeta(org.pentaho.di.trans.TransMeta) Matchers.anyString(org.mockito.Matchers.anyString) ImportRules(org.pentaho.di.imp.ImportRules) TransformationHasANoteImportRule(org.pentaho.di.imp.rules.TransformationHasANoteImportRule) NotePadMeta(org.pentaho.di.core.NotePadMeta) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) IRepositoryExporter(org.pentaho.di.repository.IRepositoryExporter) Test(org.junit.Test)

Example 3 with IRepositoryExporter

use of org.pentaho.di.repository.IRepositoryExporter in project pentaho-kettle by pentaho.

the class RepositoryExportProgressDialog method open.

public boolean open() {
    boolean retval = true;
    final List<ExportFeedback> list = new ArrayList<ExportFeedback>();
    IRepositoryExporter tmpExporter = null;
    try {
        tmpExporter = rep.getExporter();
    } catch (KettleException e) {
        log.logError(RepositoryExportProgressDialog.class.toString(), "Error creating repository: " + e.toString());
        log.logError(Const.getStackTracker(e));
        new ErrorDialog(shell, BaseMessages.getString(PKG, "RepositoryExportDialog.ErrorExport.Title"), BaseMessages.getString(PKG, "RepositoryExportDialog.ErrorExport.Message"), e);
        return false;
    }
    final IRepositoryExporter exporter = tmpExporter;
    // this hack is only to support dog-nail build process for <...>
    // and keep base interfaces without changes - getExporter should
    // directly return IRepositoryExporterFeedback.
    final boolean isFeedback = (exporter instanceof IRepositoryExporterFeedback) ? true : false;
    IRunnableWithProgress op = new IRunnableWithProgress() {

        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                exporter.setImportRulesToValidate(importRules);
                ProgressMonitorAdapter pMonitor = new ProgressMonitorAdapter(monitor);
                if (isFeedback) {
                    IRepositoryExporterFeedback fExporter = IRepositoryExporterFeedback.class.cast(exporter);
                    List<ExportFeedback> ret = fExporter.exportAllObjectsWithFeedback(pMonitor, filename, dir, "all");
                    list.addAll(ret);
                } else {
                    exporter.exportAllObjects(pMonitor, filename, dir, "all");
                }
            } catch (KettleException e) {
                throw new InvocationTargetException(e, BaseMessages.getString(PKG, "RepositoryExportDialog.Error.CreateUpdate", Const.getStackTracker(e)));
            }
        }
    };
    try {
        ProgressMonitorDialog pmd = new ProgressMonitorDialog(shell);
        pmd.run(true, true, op);
        if (!pmd.getProgressMonitor().isCanceled() && isFeedback) {
            // show some results here.
            IRepositoryExporterFeedback fExporter = IRepositoryExporterFeedback.class.cast(exporter);
            showExportResultStatus(list, fExporter.isRulesViolation());
        }
    } catch (InvocationTargetException e) {
        log.logError(RepositoryExportProgressDialog.class.toString(), "Error creating repository: " + e.toString());
        log.logError(Const.getStackTracker(e));
        new ErrorDialog(shell, BaseMessages.getString(PKG, "RepositoryExportDialog.ErrorExport.Title"), BaseMessages.getString(PKG, "RepositoryExportDialog.ErrorExport.Message"), e);
        retval = false;
    } catch (InterruptedException e) {
        log.logError(RepositoryExportProgressDialog.class.toString(), "Error creating repository: " + e.toString());
        log.logError(Const.getStackTracker(e));
        new ErrorDialog(shell, BaseMessages.getString(PKG, "RepositoryExportDialog.ErrorExport.Title"), BaseMessages.getString(PKG, "RepositoryExportDialog.ErrorExport.Message"), e);
        retval = false;
    }
    return retval;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) ExportFeedback(org.pentaho.di.repository.ExportFeedback) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) ArrayList(java.util.ArrayList) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IRepositoryExporterFeedback(org.pentaho.di.repository.IRepositoryExporterFeedback) ProgressMonitorAdapter(org.pentaho.di.core.ProgressMonitorAdapter) IRepositoryExporter(org.pentaho.di.repository.IRepositoryExporter)

Aggregations

IRepositoryExporter (org.pentaho.di.repository.IRepositoryExporter)3 KettleException (org.pentaho.di.core.exception.KettleException)2 RepositoryDirectoryInterface (org.pentaho.di.repository.RepositoryDirectoryInterface)2 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AbstractList (java.util.AbstractList)1 ArrayList (java.util.ArrayList)1 FileObject (org.apache.commons.vfs2.FileObject)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 NotePadMeta (org.pentaho.di.core.NotePadMeta)1 ProgressMonitorAdapter (org.pentaho.di.core.ProgressMonitorAdapter)1 Result (org.pentaho.di.core.Result)1 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)1 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)1 ImportRules (org.pentaho.di.imp.ImportRules)1 ImportRuleInterface (org.pentaho.di.imp.rule.ImportRuleInterface)1