Search in sources :

Example 1 with ByteArray

use of org.talend.core.model.properties.ByteArray in project tdi-studio-se by Talend.

the class GenerateGrammarController method persistInRoutine.

/**
     * Persist item in routines
     * 
     * DOC ytao Comment method "persistInRoutine".
     * 
     * @param path, sub folder named with job id
     * @param label, java file name without suffix
     * @param initFile, File handler
     * @param name, job id as package name
     * @return
     */
private RoutineItem persistInRoutine(IPath inFolder, File fileToFill, String label) {
    // item property to be set
    Property property = PropertiesFactory.eINSTANCE.createProperty();
    property.setAuthor(((RepositoryContext) CorePlugin.getContext().getProperty(Context.REPOSITORY_CONTEXT_KEY)).getUser());
    property.setVersion(VersionUtils.DEFAULT_VERSION);
    //$NON-NLS-1$
    property.setStatusCode("");
    // Label must match pattern ^[a-zA-Z\_]+[a-zA-Z0-9\_]*$
    // Must be composed with JAVA_PORJECT_NAME + JOB NAME + COMPONENT NAME,
    // since all projects share with the same routines
    property.setLabel(label);
    // add properties to item
    RoutineItem routineItem = PropertiesFactory.eINSTANCE.createRoutineItem();
    routineItem.setProperty(property);
    // get the content of java file as byte array.
    ByteArray byteArray = PropertiesFactory.eINSTANCE.createByteArray();
    InputStream stream = null;
    try {
        stream = new FileInputStream(fileToFill);
        byte[] bytes = new byte[stream.available()];
        stream.read(bytes);
        byteArray.setInnerContent(bytes);
    } catch (IOException e) {
        ExceptionHandler.process(e);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    routineItem.setContent(byteArray);
    // persist item in routines
    IProxyRepositoryFactory repositoryFactory = ProxyRepositoryFactory.getInstance();
    try {
        property.setId(repositoryFactory.getNextId());
        // create folder with name job id: routines/JOBID (seems from TOS)
        repositoryFactory.createParentFoldersRecursively(ERepositoryObjectType.getItemType(routineItem), inFolder);
        // add the item
        repositoryFactory.create(routineItem, inFolder);
    } catch (Exception e) {
        ExceptionHandler.process(e);
    }
    return routineItem;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArray(org.talend.core.model.properties.ByteArray) RoutineItem(org.talend.core.model.properties.RoutineItem) IOException(java.io.IOException) IDynamicProperty(org.talend.core.ui.properties.tab.IDynamicProperty) Property(org.talend.core.model.properties.Property) FileInputStream(java.io.FileInputStream) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) SystemException(org.talend.commons.exception.SystemException) IOException(java.io.IOException)

Example 2 with ByteArray

use of org.talend.core.model.properties.ByteArray in project tdi-studio-se by Talend.

the class StandAloneTalendJavaEditor method doSave.

@Override
public void doSave(final IProgressMonitor monitor) {
    IRepositoryService service = CorePlugin.getDefault().getRepositoryService();
    final IProxyRepositoryFactory repFactory = service.getProxyRepositoryFactory();
    try {
        repFactory.lock(item);
    } catch (Exception e) {
        ExceptionHandler.process(e);
    }
    ERepositoryStatus status = repFactory.getStatus(item);
    if (!status.equals(ERepositoryStatus.LOCK_BY_USER) && !repFactory.getRepositoryContext().isEditableAsReadOnly()) {
        MessageDialog.openWarning(getEditorSite().getShell(), Messages.getString("AbstractMultiPageTalendEditor.canNotSaveTitle"), Messages.getString("AbstractMultiPageTalendEditor.canNotSaveMessage.routine"));
        return;
    }
    EList adapters = item.getProperty().eAdapters();
    adapters.remove(dirtyListener);
    super.doSave(monitor);
    try {
        resetItem();
        ByteArray byteArray = item.getContent();
        byteArray.setInnerContentFromFile(((FileEditorInput) getEditorInput()).getFile());
        final IRunProcessService runProcessService = CorePlugin.getDefault().getRunProcessService();
        runProcessService.buildJavaProject();
        // check syntax error
        addProblems();
        //$NON-NLS-1$
        String name = "Save Routine";
        RepositoryWorkUnit<Object> repositoryWorkUnit = new RepositoryWorkUnit<Object>(name, this) {

            @Override
            protected void run() throws LoginException, PersistenceException {
                refreshJobAndSave(repFactory);
            }
        };
        repositoryWorkUnit.setAvoidSvnUpdate(true);
        repositoryWorkUnit.setAvoidUnloadResources(true);
        repFactory.executeRepositoryWorkUnit(repositoryWorkUnit);
        repositoryWorkUnit.throwPersistenceExceptionIfAny();
    // for bug 11930: Unable to save Routines.* in db project
    // repFactory.save(item);
    // startRefreshJob(repFactory);
    } catch (Exception e) {
        // e.printStackTrace();
        ExceptionHandler.process(e);
    }
}
Also used : ERepositoryStatus(org.talend.commons.runtime.model.repository.ERepositoryStatus) EList(org.eclipse.emf.common.util.EList) IRunProcessService(org.talend.designer.runprocess.IRunProcessService) ByteArray(org.talend.core.model.properties.ByteArray) RepositoryWorkUnit(org.talend.repository.RepositoryWorkUnit) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) LoginException(org.talend.commons.exception.LoginException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.talend.commons.exception.PersistenceException) IRepositoryService(org.talend.repository.model.IRepositoryService)

Example 3 with ByteArray

use of org.talend.core.model.properties.ByteArray in project tdi-studio-se by Talend.

the class SaveAsSQLPatternWizard method performFinish.

public boolean performFinish() {
    boolean ok = false;
    try {
        isUpdate = isUpdate();
        if (isUpdate) {
            assginVlaues(oldProperty, property);
            repositoryFactory.save(oldSqlpatternItem);
            // assign value
            sqlpatternItem = oldSqlpatternItem;
        } else {
            property.setId(repositoryFactory.getNextId());
            // copy the byte[] content, the new routineItem get the old saved content, it is not the newest.
            SQLPatternItem oldItem = (SQLPatternItem) repositoryEditorInput.getItem();
            ByteArray byteArray = PropertiesFactory.eINSTANCE.createByteArray();
            byteArray.setInnerContent(oldItem.getContent().getInnerContent());
            sqlpatternItem.setContent(byteArray);
            // don't need to add depended routines.
            repositoryFactory.create(sqlpatternItem, mainPage.getDestinationPath());
        }
        ok = true;
    } catch (Exception e) {
        MessageDialog.openError(getShell(), "Error", "SQLTemplate could not be saved" + " : " + e.getMessage());
        ExceptionHandler.process(e);
    }
    return ok;
}
Also used : ByteArray(org.talend.core.model.properties.ByteArray) SQLPatternItem(org.talend.core.model.properties.SQLPatternItem)

Example 4 with ByteArray

use of org.talend.core.model.properties.ByteArray in project tdi-studio-se by Talend.

the class EditPropertiesAction method processRename.

protected void processRename(IRepositoryNode node, String originalName) {
    try {
        IRunProcessService runProcessService = CorePlugin.getDefault().getRunProcessService();
        ITalendProcessJavaProject talendProcessJavaProject = runProcessService.getTalendProcessJavaProject();
        if (talendProcessJavaProject == null) {
            return;
        }
        IFolder srcFolder = talendProcessJavaProject.getSrcFolder();
        IPackageFragmentRoot root = talendProcessJavaProject.getJavaProject().getPackageFragmentRoot(srcFolder);
        // add for bug TDI-24379 on August 23, 2013.
        IFolder srcInterFolder = srcFolder.getFolder(JavaUtils.JAVA_INTERNAL_DIRECTORY);
        if (srcInterFolder.exists()) {
            File file = new File(srcInterFolder.getLocationURI());
            for (File f : file.listFiles()) {
                if (f.isFile()) {
                    f.delete();
                }
            }
        }
        // qli modified to fix the bug 5400 and 6185.
        // update for fix [TESB-6784]
        IPackageFragment routinesPkg = getPackageFragment(root, node);
        // ICompilationUnit unit = routinesPkg.getCompilationUnit(originalName +
        // SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        ICompilationUnit unit = routinesPkg.getCompilationUnit(originalName + ".java");
        if (unit == null) {
            return;
        }
        String newName = node.getObject().getProperty().getLabel();
        JavaRenameProcessor processor = new RenameCompilationUnitProcessor(unit);
        // processor.setNewElementName(newName + SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        processor.setNewElementName(newName + ".java");
        RenameRefactoring ref = new RenameRefactoring(processor);
        final PerformRefactoringOperation operation = new PerformRefactoringOperation(ref, CheckConditionsOperation.ALL_CONDITIONS);
        IRunnableWithProgress r = new IRunnableWithProgress() {

            @Override
            public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            operation.run(monitor);
                        } catch (CoreException e) {
                            ExceptionHandler.process(e);
                        }
                    }
                });
            }
        };
        PlatformUI.getWorkbench().getProgressService().run(true, true, r);
        RefactoringStatus conditionStatus = operation.getConditionStatus();
        if (conditionStatus.hasError()) {
            //$NON-NLS-1$
            String errorMessage = Messages.getString("EditPropertiesAction.renameError", unit.getElementName(), newName);
            RefactoringStatusEntry[] entries = conditionStatus.getEntries();
            for (RefactoringStatusEntry entry : entries) {
                //$NON-NLS-1$
                errorMessage += "\n>>>" + entry.getMessage();
            }
            Shell shell = null;
            IRepositoryView viewPart = getViewPart();
            if (viewPart != null) {
                shell = viewPart.getViewSite().getShell();
            } else {
                shell = Display.getCurrent().getActiveShell();
            }
            //$NON-NLS-1$
            MessageDialog.openError(shell, Messages.getString("EditPropertiesAction.warning"), errorMessage);
            return;
        }
        // ICompilationUnit newUnit = routinesPkg.getCompilationUnit(newName + SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        ICompilationUnit newUnit = routinesPkg.getCompilationUnit(newName + ".java");
        if (newUnit == null) {
            return;
        }
        RoutineItem item = (RoutineItem) node.getObject().getProperty().getItem();
        IFile javaFile = (IFile) newUnit.getAdapter(IResource.class);
        try {
            ByteArray byteArray = item.getContent();
            byteArray.setInnerContentFromFile(javaFile);
            IRepositoryService service = CorePlugin.getDefault().getRepositoryService();
            IProxyRepositoryFactory repFactory = service.getProxyRepositoryFactory();
            repFactory.save(item);
        } catch (Exception e) {
            // e.printStackTrace();
            ExceptionHandler.process(e);
        }
    } catch (Exception e) {
        // e.printStackTrace();
        ExceptionHandler.process(e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IRunProcessService(org.talend.designer.runprocess.IRunProcessService) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) IRepositoryView(org.talend.repository.ui.views.IRepositoryView) ITalendProcessJavaProject(org.talend.core.runtime.process.ITalendProcessJavaProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IRepositoryService(org.talend.repository.model.IRepositoryService) Shell(org.eclipse.swt.widgets.Shell) ByteArray(org.talend.core.model.properties.ByteArray) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RefactoringStatusEntry(org.eclipse.ltk.core.refactoring.RefactoringStatusEntry) RoutineItem(org.talend.core.model.properties.RoutineItem) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RenameCompilationUnitProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor) CoreException(org.eclipse.core.runtime.CoreException) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 5 with ByteArray

use of org.talend.core.model.properties.ByteArray in project tdi-studio-se by Talend.

the class HL7Parse method doParse.

/**
     * DOC gcui Comment method "doParse".
     * 
     * @param filePath
     * @param hl7Manager
     * @param msgContentList
     * @return
     */
public List<Message> doParse(String filePath, HL7Manager hl7Manager, List<String> msgContentList, String startChar, String endChar) {
    List<Message> messageList = new ArrayList<Message>();
    String filePathNoQuotes = TalendTextUtils.removeQuotes(filePath);
    File file = Path.fromOSString(filePathNoQuotes).toFile();
    if (file.exists()) {
        msgContentList.clear();
        ByteArray array = PropertiesFactory.eINSTANCE.createByteArray();
        try {
            array.setInnerContentFromFile(file);
            TalendHL7Reader talendHL7Reader = new TalendHL7Reader(new java.io.FileInputStream(file), "ISO-8859-15");
            String HL7InputTem = null;
            if (startChar != null) {
                talendHL7Reader.setStartMsgChar(stringParse2Char(startChar));
            }
            if (endChar != null) {
                talendHL7Reader.setEndMsgChar(stringParse2Char(endChar));
            }
            while ((HL7InputTem = talendHL7Reader.getMessage()) != null) {
                msgContentList.add(HL7InputTem);
                Message message = getHL7MessageInput(HL7InputTem);
                messageList.add(message);
            }
            if (msgContentList == null || msgContentList.size() == 0) {
                String msgText = new String(array.getInnerContent());
                msgContentList.add(msgText);
                Message message = getHL7MessageInput(msgText);
                messageList.add(message);
            }
            hl7Manager.getUiManager().getHl7UI().getContentProvider().getAllSegmentsForMessage().clear();
            hl7Manager.getUiManager().getHl7UI().getMessageViewer().setInput(messageList.toArray());
            hl7Manager.getUiManager().getHl7UI().getHeader().setMsgIsChange(true);
            hl7Manager.getUiManager().getHl7UI().initSchemaCombo(null);
            hl7Manager.getUiManager().getHl7UI().getMetadataEditor().removeAll();
            hl7Manager.getUiManager().getHl7UI().redrawLinkers();
        } catch (IOException ex) {
            ExceptionHandler.process(ex);
        }
    }
    return messageList;
}
Also used : Message(ca.uhn.hl7v2.model.Message) ArrayList(java.util.ArrayList) ByteArray(org.talend.core.model.properties.ByteArray) IOException(java.io.IOException) File(java.io.File)

Aggregations

ByteArray (org.talend.core.model.properties.ByteArray)19 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)8 File (java.io.File)7 IOException (java.io.IOException)7 RoutineItem (org.talend.core.model.properties.RoutineItem)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 IFile (org.eclipse.core.resources.IFile)4 PersistenceException (org.talend.commons.exception.PersistenceException)4 Message (ca.uhn.hl7v2.model.Message)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 PartInitException (org.eclipse.ui.PartInitException)3 CoreException (org.eclipse.core.runtime.CoreException)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 WizardDialog (org.eclipse.jface.wizard.WizardDialog)2 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)2 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)2 Property (org.talend.core.model.properties.Property)2 ReferenceFileItem (org.talend.core.model.properties.ReferenceFileItem)2 SQLPatternItem (org.talend.core.model.properties.SQLPatternItem)2