Search in sources :

Example 6 with ReferenceFileItem

use of org.talend.core.model.properties.ReferenceFileItem in project tesb-studio-se by Talend.

the class NewRouteResourceWizard method performFinish.

/**
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
@Override
public boolean performFinish() {
    property.setId(repositoryFactory.getNextId());
    property.setLabel(property.getDisplayName());
    URL url = mainPage.getUrl();
    Path p = new Path(property.getLabel());
    String itemName = p.removeFileExtension().lastSegment();
    // String refName = item.getProperty().getLabel() + LOW_DASH + VersionUtils.DEFAULT_VERSION;
    String fileExtension = null;
    if (url != null) {
        p = new Path(url.getPath());
        if (p.getFileExtension() != null) {
            fileExtension = p.getFileExtension();
        }
    } else {
        fileExtension = p.getFileExtension();
    }
    // https://jira.talendforge.org/browse/TESB-6853
    if (fileExtension == null || fileExtension.isEmpty()) {
        fileExtension = "txt";
    }
    // In case the source file extension is "item"
    if (fileExtension.equals(FileConstants.ITEM_EXTENSION)) {
        fileExtension += LOW_DASH;
    }
    // In case the source file extension is "properties"
    if (fileExtension.equals(FileConstants.PROPERTIES_EXTENSION)) {
        fileExtension += LOW_DASH;
    }
    ByteArray byteArray = PropertiesFactory.eINSTANCE.createByteArray();
    if (url == null) {
        byteArray.setInnerContent(new byte[0]);
    } else {
        try {
            InputStream inputStream = url.openStream();
            byteArray.setInnerContent(IOUtils.toByteArray(inputStream));
            inputStream.close();
        } catch (Exception e) {
            MessageBoxExceptionHandler.process(e);
            ExceptionHandler.process(e);
            return false;
        }
    }
    ReferenceFileItem refItem = PropertiesFactory.eINSTANCE.createReferenceFileItem();
    refItem.setContent(byteArray);
    refItem.setExtension(fileExtension);
    // refItem.setName(refName);
    item.setName(itemName);
    item.setBindingExtension(fileExtension);
    byteArray = PropertiesFactory.eINSTANCE.createByteArray();
    byteArray.setInnerContent(new byte[0]);
    item.setContent(byteArray);
    item.getReferenceResources().add(refItem);
    RepositoryWorkUnit<Object> workUnit = new RepositoryWorkUnit<Object>(this.getWindowTitle(), this) {

        @Override
        protected void run() throws LoginException, PersistenceException {
            repositoryFactory.create(item, mainPage.getDestinationPath());
            RelationshipItemBuilder.getInstance().addOrUpdateItem(item);
        }
    };
    workUnit.setAvoidUnloadResources(true);
    repositoryFactory.executeRepositoryWorkUnit(workUnit);
    //		return item != null;
    return true;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ReferenceFileItem(org.talend.core.model.properties.ReferenceFileItem) InputStream(java.io.InputStream) ByteArray(org.talend.core.model.properties.ByteArray) RepositoryWorkUnit(org.talend.repository.RepositoryWorkUnit) URL(java.net.URL) LoginException(org.talend.commons.exception.LoginException) PersistenceException(org.talend.commons.exception.PersistenceException)

Example 7 with ReferenceFileItem

use of org.talend.core.model.properties.ReferenceFileItem in project tesb-studio-se by Talend.

the class RouteResourcesHelper method getRouteResourcesLocation.

public static String getRouteResourcesLocation(IRepositoryViewObject repositoryObject) {
    if (repositoryObject == null) {
        return null;
    }
    Item item = repositoryObject.getProperty().getItem();
    if (item.getReferenceResources().isEmpty()) {
        return null;
    }
    ReferenceFileItem refFileItem = (ReferenceFileItem) item.getReferenceResources().get(0);
    URI uri = refFileItem.getContent().eResource().getURI();
    try {
        return FileLocator.toFileURL(new URL(uri.toString())).getFile();
    } catch (IOException e) {
        ExceptionHandler.process(e);
    }
    return null;
}
Also used : ReferenceFileItem(org.talend.core.model.properties.ReferenceFileItem) Item(org.talend.core.model.properties.Item) ReferenceFileItem(org.talend.core.model.properties.ReferenceFileItem) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI) URL(java.net.URL)

Example 8 with ReferenceFileItem

use of org.talend.core.model.properties.ReferenceFileItem in project tesb-studio-se by Talend.

the class RouteResourceEditor method saveContentsToItem.

/**
	 * Save the file content to EMF item.
	 * 
	 * @param rrInput
	 */
public static void saveContentsToItem(RouteResourceInput rrInput) {
    try {
        RouteResourceItem item = (RouteResourceItem) rrInput.getItem();
        ReferenceFileItem refFile = (ReferenceFileItem) item.getReferenceResources().get(0);
        InputStream inputstream = rrInput.getFile().getContents();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream));
        String line = bufferedReader.readLine();
        StringBuffer sb = new StringBuffer();
        String lineSeparator = System.getProperty("line.separator");
        while (line != null) {
            sb.append(line).append(lineSeparator);
            line = bufferedReader.readLine();
        }
        bufferedReader.close();
        inputstream.close();
        ByteArray content = refFile.getContent();
        content.setInnerContent(sb.toString().getBytes());
    } catch (Exception e) {
        ExceptionHandler.process(e);
    }
}
Also used : ReferenceFileItem(org.talend.core.model.properties.ReferenceFileItem) RouteResourceItem(org.talend.camel.core.model.camelProperties.RouteResourceItem) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ByteArray(org.talend.core.model.properties.ByteArray) PartInitException(org.eclipse.ui.PartInitException)

Aggregations

ReferenceFileItem (org.talend.core.model.properties.ReferenceFileItem)8 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 URL (java.net.URL)3 IPath (org.eclipse.core.runtime.IPath)3 ByteArray (org.talend.core.model.properties.ByteArray)3 IFile (org.eclipse.core.resources.IFile)2 Path (org.eclipse.core.runtime.Path)2 URI (org.eclipse.emf.common.util.URI)2 RouteResourceItem (org.talend.camel.core.model.camelProperties.RouteResourceItem)2 ConnectionItem (org.talend.core.model.properties.ConnectionItem)2 Item (org.talend.core.model.properties.Item)2 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1