Search in sources :

Example 1 with IStreamListener

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

the class RepositoryFileOutputStream method flush.

@Override
public void flush() throws IOException {
    if (closed) {
        return;
    }
    super.flush();
    ByteArrayInputStream bis = new ByteArrayInputStream(toByteArray());
    // make an effort to determine the correct mime type, default to application/octet-stream
    String extension = RepositoryFilenameUtils.getExtension(path);
    // $NON-NLS-1$
    String mimeType = "application/octet-stream";
    if (extension != null) {
        // $NON-NLS-1$
        String tempMimeType = MimeHelper.getMimeTypeFromExtension("." + extension);
        if (tempMimeType != null) {
            mimeType = tempMimeType;
        }
    }
    if (charsetName == null) {
        charsetName = MimeHelper.getDefaultCharset(mimeType);
    }
    // FIXME: not a good idea that we assume we are dealing with text. Best if this is somehow moved to the
    // RepositoryFileWriter
    // but I couldn't figure out a clean way to do that. For now, charsetName is passed in here and we use it if
    // available.
    final IRepositoryFileData payload;
    Converter converter;
    if (TRANS_EXT.equalsIgnoreCase(extension)) {
        converter = PentahoSystem.get(Converter.class, null, Collections.singletonMap("name", "PDITransformationStreamConverter"));
        mimeType = "application/vnd.pentaho.transformation";
    } else if (JOB_EXT.equalsIgnoreCase(extension)) {
        converter = PentahoSystem.get(Converter.class, null, Collections.singletonMap("name", "PDIJobStreamConverter"));
        mimeType = "application/vnd.pentaho.job";
    } else {
        converter = null;
    }
    payload = convert(converter, bis, mimeType);
    if (!flushed) {
        RepositoryFile file = repository.getFile(path);
        RepositoryFile parentFolder = getParent(path);
        String baseFileName = RepositoryFilenameUtils.getBaseName(path);
        if (file == null) {
            if (autoCreateDirStructure) {
                ArrayList<String> foldersToCreate = new ArrayList<>();
                String parentPath = RepositoryFilenameUtils.getFullPathNoEndSeparator(path);
                // Make sure the parent path isn't the root
                while ((parentPath != null) && (parentPath.length() > 0 && !path.equals(parentPath)) && (repository.getFile(parentPath) == null)) {
                    foldersToCreate.add(RepositoryFilenameUtils.getName(parentPath));
                    parentPath = RepositoryFilenameUtils.getFullPathNoEndSeparator(parentPath);
                }
                Collections.reverse(foldersToCreate);
                parentFolder = ((parentPath != null) && (parentPath.length() > 0)) ? repository.getFile(parentPath) : repository.getFile("/");
                if (!parentFolder.isFolder()) {
                    throw new FileNotFoundException();
                }
                for (String folderName : foldersToCreate) {
                    parentFolder = repository.createFolder(parentFolder.getId(), new RepositoryFile.Builder(folderName).folder(true).build(), null);
                }
            } else {
                if (parentFolder == null) {
                    throw new FileNotFoundException();
                }
            }
            file = buildRepositoryFile(RepositoryFilenameUtils.getName(path), extension, baseFileName);
            repository.createFile(parentFolder.getId(), file, payload, // $NON-NLS-1$
            "commit from " + RepositoryFileOutputStream.class.getName());
            for (IStreamListener listener : listeners) {
                listener.fileCreated(path);
            }
        } else if (file.isFolder()) {
            throw new FileNotFoundException(MessageFormat.format("Repository file {0} is a directory", file.getPath()));
        } else {
            if (autoCreateUniqueFileName) {
                int nameCount = 1;
                String newFileName = null;
                String newBaseFileName = null;
                List<RepositoryFile> children = repository.getChildren(parentFolder.getId());
                boolean hasFile = true;
                while (hasFile) {
                    hasFile = false;
                    nameCount++;
                    newBaseFileName = baseFileName + "(" + nameCount + ")";
                    if ((extension != null) && (extension.length() > 0)) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        newFileName = newBaseFileName + "." + extension;
                    } else {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        newFileName = newBaseFileName;
                    }
                    for (RepositoryFile child : children) {
                        if (child.getPath().equals(parentFolder.getPath() + "/" + newFileName)) {
                            hasFile = true;
                            break;
                        }
                    }
                }
                file = buildRepositoryFile(newFileName, extension, newBaseFileName);
                // $NON-NLS-1$
                file = repository.createFile(parentFolder.getId(), file, payload, "New File");
                path = file.getPath();
                for (IStreamListener listener : listeners) {
                    listener.fileCreated(path);
                }
            } else {
                // $NON-NLS-1$
                repository.updateFile(file, payload, "New File");
                path = file.getPath();
                for (IStreamListener listener : listeners) {
                    listener.fileCreated(path);
                }
            }
        }
    } else {
        RepositoryFile file = repository.getFile(path);
        // $NON-NLS-1$
        repository.updateFile(file, payload, "New File");
    }
    flushed = true;
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) IStreamListener(org.pentaho.platform.api.repository2.unified.IStreamListener) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Converter(org.pentaho.platform.api.repository2.unified.Converter) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with IStreamListener

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

the class ActionRunner method callImpl.

private ExecutionResult callImpl() throws Exception {
    boolean executionStatus = true;
    final Object locale = params.get(LocaleHelper.USER_LOCALE_PARAM);
    if (locale instanceof Locale) {
        LocaleHelper.setLocaleOverride((Locale) locale);
    } else {
        LocaleHelper.setLocaleOverride(new Locale((String) locale));
    }
    // sync job params to the action bean
    ActionHarness actionHarness = new ActionHarness(actionBean);
    boolean updateJob = false;
    final Map<String, Object> actionParams = new HashMap<String, Object>();
    actionParams.putAll(params);
    if (streamProvider != null) {
        actionParams.put("inputStream", streamProvider.getInputStream());
    }
    actionHarness.setValues(actionParams, new ActionSequenceCompatibilityFormatter());
    if (actionBean instanceof IVarArgsAction) {
        actionParams.remove("inputStream");
        actionParams.remove("outputStream");
        ((IVarArgsAction) actionBean).setVarArgs(actionParams);
    }
    boolean waitForFileCreated = false;
    OutputStream stream = null;
    if (streamProvider != null) {
        actionParams.remove("inputStream");
        if (actionBean instanceof IStreamingAction) {
            streamProvider.setStreamingAction((IStreamingAction) actionBean);
        }
        // BISERVER-9414 - validate that output path still exist
        SchedulerOutputPathResolver resolver = new SchedulerOutputPathResolver(streamProvider.getOutputPath(), actionUser);
        String outputPath = resolver.resolveOutputFilePath();
        actionParams.put("useJcr", Boolean.TRUE);
        actionParams.put("jcrOutputPath", outputPath.substring(0, outputPath.lastIndexOf("/")));
        if (!outputPath.equals(streamProvider.getOutputPath())) {
            // set fallback path
            streamProvider.setOutputFilePath(outputPath);
            // job needs to be deleted and recreated with the new output path
            updateJob = true;
        }
        stream = streamProvider.getOutputStream();
        if (stream instanceof ISourcesStreamEvents) {
            ((ISourcesStreamEvents) stream).addListener(new IStreamListener() {

                public void fileCreated(final String filePath) {
                    synchronized (lock) {
                        outputFilePath = filePath;
                        lock.notifyAll();
                    }
                }
            });
            waitForFileCreated = true;
        }
        actionParams.put("outputStream", stream);
        // The lineage_id is only useful for the metadata and not needed at this level see PDI-10171
        ActionUtil.removeKeyFromMap(actionParams, ActionUtil.QUARTZ_LINEAGE_ID);
        actionHarness.setValues(actionParams);
    }
    actionBean.execute();
    executionStatus = actionBean.isExecutionSuccessful();
    if (stream != null) {
        IOUtils.closeQuietly(stream);
    }
    if (waitForFileCreated) {
        synchronized (lock) {
            if (outputFilePath == null) {
                lock.wait();
            }
        }
        ActionUtil.sendEmail(actionParams, params, outputFilePath);
        deleteEmptyFile();
    }
    if (actionBean instanceof IPostProcessingAction) {
        closeContentOutputStreams((IPostProcessingAction) actionBean);
        markContentAsGenerated((IPostProcessingAction) actionBean);
    }
    // Create the ExecutionResult to return the status and whether the update is required or not
    ExecutionResult executionResult = new ExecutionResult(updateJob, executionStatus);
    return executionResult;
}
Also used : Locale(java.util.Locale) IStreamListener(org.pentaho.platform.api.repository2.unified.IStreamListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) SchedulerOutputPathResolver(org.pentaho.platform.scheduler2.quartz.SchedulerOutputPathResolver) ISourcesStreamEvents(org.pentaho.platform.api.repository2.unified.ISourcesStreamEvents) IStreamingAction(org.pentaho.platform.api.action.IStreamingAction) IVarArgsAction(org.pentaho.platform.api.action.IVarArgsAction) ActionSequenceCompatibilityFormatter(org.pentaho.platform.engine.services.solution.ActionSequenceCompatibilityFormatter) ActionHarness(org.pentaho.platform.util.beans.ActionHarness) IPostProcessingAction(org.pentaho.platform.api.action.IPostProcessingAction)

Aggregations

IStreamListener (org.pentaho.platform.api.repository2.unified.IStreamListener)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 IPostProcessingAction (org.pentaho.platform.api.action.IPostProcessingAction)1 IStreamingAction (org.pentaho.platform.api.action.IStreamingAction)1 IVarArgsAction (org.pentaho.platform.api.action.IVarArgsAction)1 Converter (org.pentaho.platform.api.repository2.unified.Converter)1 IRepositoryFileData (org.pentaho.platform.api.repository2.unified.IRepositoryFileData)1 ISourcesStreamEvents (org.pentaho.platform.api.repository2.unified.ISourcesStreamEvents)1 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)1 ActionSequenceCompatibilityFormatter (org.pentaho.platform.engine.services.solution.ActionSequenceCompatibilityFormatter)1 SchedulerOutputPathResolver (org.pentaho.platform.scheduler2.quartz.SchedulerOutputPathResolver)1 ActionHarness (org.pentaho.platform.util.beans.ActionHarness)1