Search in sources :

Example 1 with ReferenceFileItem

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

the class ImportItemUtil method copyReferenceFiles.

private boolean copyReferenceFiles(ResourcesManager manager, Item tmpItem, IPath pathToRead) throws IOException {
    OutputStream os = null;
    InputStream is = null;
    boolean haveRef = false;
    List<ReferenceFileItem> refItems = tmpItem.getReferenceResources();
    URI propertyResourceURI = EcoreUtil.getURI(tmpItem.getProperty());
    for (ReferenceFileItem refItem : refItems) {
        haveRef = true;
        URI relativePlateformDestUri = propertyResourceURI.trimFileExtension().appendFileExtension(refItem.getExtension());
        try {
            URL fileURL = FileLocator.toFileURL(new java.net.URL(//$NON-NLS-1$
            "platform:/resource" + relativePlateformDestUri.toPlatformString(true)));
            os = new FileOutputStream(fileURL.getFile());
            is = manager.getStream(pathToRead.removeFileExtension().addFileExtension(refItem.getExtension()));
            FileCopyUtils.copyStreams(is, os);
        } finally {
            if (os != null) {
                os.close();
            }
            if (is != null) {
                is.close();
            }
        }
    }
    return haveRef;
}
Also used : ReferenceFileItem(org.talend.core.model.properties.ReferenceFileItem) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URL(java.net.URL) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) URI(org.eclipse.emf.common.util.URI) URL(java.net.URL)

Example 2 with ReferenceFileItem

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

the class AddCopyBookXc2jFileMigrationTask method updateXc2jFilePath.

private boolean updateXc2jFilePath(Item item) throws PersistenceException, IOException {
    IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
    boolean update = false;
    if (item instanceof ConnectionItem) {
        ConnectionItem connectionItem = (ConnectionItem) item;
        Connection con = connectionItem.getConnection();
        if (con instanceof EbcdicConnection) {
            ReferenceFileItem createReferenceFileItem = null;
            // the old copybook version depands MidFile
            String midFile = ((EbcdicConnection) con).getMidFile();
            if (midFile != null) {
                File xc2jMidFile = new File(midFile);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                if (xc2jMidFile.exists()) {
                    readXc2jFile(xc2jMidFile, baos);
                }
                // create a referenceItem first,maybe it has content,maybe is empty,decide by the midFile
                if (connectionItem.getReferenceResources().isEmpty()) {
                    createReferenceFileItem = PropertiesFactory.eINSTANCE.createReferenceFileItem();
                    ByteArray byteArray = PropertiesFactory.eINSTANCE.createByteArray();
                    createReferenceFileItem.setContent(byteArray);
                    createReferenceFileItem.setExtension("xc2j");
                    connectionItem.getReferenceResources().add(createReferenceFileItem);
                } else {
                    createReferenceFileItem = (ReferenceFileItem) connectionItem.getReferenceResources().get(0);
                }
                createReferenceFileItem.getContent().setInnerContent(baos.toByteArray());
                // create the phyhical x2cj file and set referenceFileItem content for it
                String xc2jFilePath = getReferenceXc2jFile(connectionItem).getLocation().makeAbsolute().toFile().getAbsolutePath();
                getXc2jFileFromBytes(createReferenceFileItem.getContent().getInnerContent(), xc2jFilePath);
                update = true;
            }
        }
        if (update) {
            factory.save(connectionItem, true);
        }
    }
    return update;
}
Also used : EbcdicConnection(org.talend.core.model.metadata.builder.connection.EbcdicConnection) ReferenceFileItem(org.talend.core.model.properties.ReferenceFileItem) ConnectionItem(org.talend.core.model.properties.ConnectionItem) Connection(org.talend.core.model.metadata.builder.connection.Connection) EbcdicConnection(org.talend.core.model.metadata.builder.connection.EbcdicConnection) ByteArray(org.talend.core.model.properties.ByteArray) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory)

Example 3 with ReferenceFileItem

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

the class RemoveRefNameMT method execute.

@Override
public ExecutionResult execute(Item item) {
    Property property = item.getProperty();
    if (property != null) {
        boolean needSave = false;
        for (Object itemRefObj : property.getItem().getReferenceResources()) {
            ReferenceFileItem refItem = (ReferenceFileItem) itemRefObj;
            if (refItem.getName() != null) {
                needSave = true;
                refItem.setName(null);
            }
        }
        if (needSave) {
            try {
                saveItem(item);
                return ExecutionResult.SUCCESS_NO_ALERT;
            } catch (PersistenceException e) {
                ExceptionHandler.process(e);
                return ExecutionResult.FAILURE;
            }
        }
    }
    return ExecutionResult.NOTHING_TO_DO;
}
Also used : ReferenceFileItem(org.talend.core.model.properties.ReferenceFileItem) PersistenceException(org.talend.commons.exception.PersistenceException) Property(org.talend.core.model.properties.Property)

Example 4 with ReferenceFileItem

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

the class DatamodelOperatorUpdator method updateConditionOperator.

public boolean updateConditionOperator(Item item) {
    boolean modified = false;
    if (item != null && item instanceof WSDataModelItem) {
        WSDataModelItem modelItem = (WSDataModelItem) item;
        EList<ReferenceFileItem> resources = modelItem.getReferenceResources();
        for (ReferenceFileItem fileItem : resources) {
            if (fileItem.getExtension().equals("xsd")) {
                // $NON-NLS-1$
                ByteArray content = fileItem.getContent();
                byte[] byteContent = content.getInnerContent();
                String xsdSchema = updateOperator(byteContent);
                if (xsdSchema != null) {
                    try {
                        // $NON-NLS-1$
                        byteContent = xsdSchema.getBytes("utf-8");
                        content.setInnerContent(byteContent);
                        // $NON-NLS-1$
                        modelItem.getWsDataModel().setXsdSchema(new String(byteContent, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        log.error(e.getMessage(), e);
                    }
                    modified = true;
                }
                break;
            }
        }
        if (modified) {
            RepositoryResourceUtil.saveItem(item);
        }
    }
    return modified;
}
Also used : ReferenceFileItem(org.talend.core.model.properties.ReferenceFileItem) WSDataModelItem(org.talend.mdm.repository.model.mdmproperties.WSDataModelItem) ByteArray(org.talend.core.model.properties.ByteArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with ReferenceFileItem

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

the class DatamodelOperatorUpdatorTest method testUpdateConditionOperator.

@Test
public void testUpdateConditionOperator() {
    // $NON-NLS-1$
    String filename = "Product_0.1.xsd";
    // $NON-NLS-1$
    File file = new File("temp/" + filename);
    try {
        byte[] fbyteArray = IOUtils.toByteArray(new FileInputStream(file));
        WSDataModelItem wsdataModelItem = MdmpropertiesFactory.eINSTANCE.createWSDataModelItem();
        ReferenceFileItem xsdFileItem = PropertiesFactory.eINSTANCE.createReferenceFileItem();
        ByteArray byteArray = PropertiesFactory.eINSTANCE.createByteArray();
        // 
        byteArray = spy(byteArray);
        // 
        when(byteArray.getInnerContent()).thenReturn(fbyteArray);
        xsdFileItem.setContent(byteArray);
        xsdFileItem.setExtension("xsd");
        wsdataModelItem.getReferenceResources().add(xsdFileItem);
        WSDataModelE wsdataModelE = MdmserverobjectFactory.eINSTANCE.createWSDataModelE();
        wsdataModelE.setXsdSchema(null);
        wsdataModelItem.setWsDataModel(wsdataModelE);
        PowerMockito.mockStatic(RepositoryResourceUtil.class);
        boolean updated = new DatamodelOperatorUpdator().updateConditionOperator(wsdataModelItem);
        String xsdSchema = wsdataModelItem.getWsDataModel().getXsdSchema();
        assertTrue(updated);
        assertNotNull(xsdSchema);
        // $NON-NLS-1$
        assertFalse(xsdSchema.contains("Strict Contains"));
        // $NON-NLS-1$
        assertFalse(xsdSchema.contains("Contains Text Of"));
        // $NON-NLS-1$
        assertTrue(xsdSchema.contains("Contains"));
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : ReferenceFileItem(org.talend.core.model.properties.ReferenceFileItem) WSDataModelItem(org.talend.mdm.repository.model.mdmproperties.WSDataModelItem) WSDataModelE(org.talend.mdm.repository.model.mdmserverobject.WSDataModelE) ByteArray(org.talend.core.model.properties.ByteArray) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ReferenceFileItem (org.talend.core.model.properties.ReferenceFileItem)14 ByteArray (org.talend.core.model.properties.ByteArray)7 InputStream (java.io.InputStream)5 IOException (java.io.IOException)4 IPath (org.eclipse.core.runtime.IPath)4 URI (org.eclipse.emf.common.util.URI)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 URL (java.net.URL)3 IFile (org.eclipse.core.resources.IFile)3 Path (org.eclipse.core.runtime.Path)3 EList (org.eclipse.emf.common.util.EList)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CoreException (org.eclipse.core.runtime.CoreException)2 PersistenceException (org.talend.commons.exception.PersistenceException)2 ConnectionItem (org.talend.core.model.properties.ConnectionItem)2 Item (org.talend.core.model.properties.Item)2 WSDataModelItem (org.talend.mdm.repository.model.mdmproperties.WSDataModelItem)2