Search in sources :

Example 11 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 12 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 13 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
     * @return
     */
public List<Message> doParse(String filePath, String startChar, String endChar) {
    List<Message> messageList = new ArrayList<Message>();
    String filePathNoQuotes = TalendTextUtils.removeQuotes(filePath);
    File file = Path.fromOSString(filePathNoQuotes).toFile();
    if (file.exists()) {
        ByteArray array = PropertiesFactory.eINSTANCE.createByteArray();
        try {
            array.setInnerContentFromFile(file);
            TalendHL7Reader talendHL7Reader = new TalendHL7Reader(new java.io.FileInputStream(file), "ISO-8859-15");
            if (startChar != null) {
                talendHL7Reader.setStartMsgChar(stringParse2Char(startChar));
            }
            if (endChar != null) {
                talendHL7Reader.setEndMsgChar(stringParse2Char(endChar));
            }
            String HL7InputTem = null;
            String messageText = "";
            while ((HL7InputTem = talendHL7Reader.getMessage()) != null) {
                Message message = getHL7MessageInput(HL7InputTem);
                messageList.add(message);
                messageText = messageText + HL7InputTem + "\r";
            }
            if (messageText == null || "".equals(messageText)) {
                messageText = new String(array.getInnerContent());
            }
            talendHL7Reader.close();
        } catch (IOException ex) {
            ExceptionHandler.process(ex);
        }
    } else {
        MessageBox message = new MessageBox(new Shell(), SWT.APPLICATION_MODAL | SWT.OK);
        //$NON-NLS-1$
        message.setText("The file is not exist");
        //$NON-NLS-1$
        message.setMessage("Please check the file path and select the file again");
        if (message.open() == SWT.OK) {
            message.getParent().getShell().close();
        }
    }
    return messageList;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) 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) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 14 with ByteArray

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

the class ChangeJobscriptContentValueMigrationTask method execute.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.core.model.migration.AbstractItemMigrationTask#execute(org.talend.core.model.properties.Item)
     */
@Override
public ExecutionResult execute(Item item) {
    ProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
    if (item instanceof JobScriptItem) {
        byte[] bytes = ((JobScriptItem) item).getContent().getInnerContent();
        String message = new String(bytes);
        message = message.replaceAll("PATTREN", "PATTERN");
        try {
            ByteArray byteArray = PropertiesFactory.eINSTANCE.createByteArray();
            byteArray.setInnerContent(message.getBytes());
            ((JobScriptItem) item).setContent(byteArray);
            factory.save(item, true);
        } catch (PersistenceException e) {
            ExceptionHandler.process(e);
            return ExecutionResult.FAILURE;
        }
        return ExecutionResult.SUCCESS_NO_ALERT;
    }
    return ExecutionResult.NOTHING_TO_DO;
}
Also used : ProxyRepositoryFactory(org.talend.core.repository.model.ProxyRepositoryFactory) PersistenceException(org.talend.commons.exception.PersistenceException) JobScriptItem(org.talend.core.model.properties.JobScriptItem) ByteArray(org.talend.core.model.properties.ByteArray)

Example 15 with ByteArray

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

the class RenameRoutinesClassName method execute.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.core.model.migration.AbstractItemMigrationTask#execute(org.talend.core.model.properties.Item)
     */
@Override
public ExecutionResult execute(Item item) {
    if (getProject().getLanguage() == ECodeLanguage.JAVA) {
        RoutineItem routineItem = (RoutineItem) item;
        if (routineItem.isBuiltIn()) {
            // if it's a system routine, no migration needed.
            return ExecutionResult.NOTHING_TO_DO;
        }
        try {
            IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
            ByteArray content = routineItem.getContent();
            String str = new String(content.getInnerContent());
            String string = item.getProperty().getLabel();
            //$NON-NLS-1$
            str = str.replaceAll("__CLASS_NAME__", string);
            content.setInnerContent(str.getBytes());
            routineItem.setContent(content);
            factory.save(routineItem);
            return ExecutionResult.SUCCESS_NO_ALERT;
        } catch (Exception e) {
            ExceptionHandler.process(e);
            return ExecutionResult.FAILURE;
        }
    } else {
        return ExecutionResult.NOTHING_TO_DO;
    }
}
Also used : ByteArray(org.talend.core.model.properties.ByteArray) RoutineItem(org.talend.core.model.properties.RoutineItem) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory)

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