Search in sources :

Example 1 with EMFUtil

use of org.talend.commons.emf.EMFUtil in project tdq-studio-se by Talend.

the class UpdatePropertiesFileTask method doExecute.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.dataprofiler.core.migration.AMigrationTask#doExecute()
     */
@Override
protected boolean doExecute() throws Exception {
    boolean ok = true;
    EMFUtil emfUtil = new EMFUtil();
    for (File file : fileList) {
        if (file.isFile()) {
            URI uri = URI.createFileURI(file.getAbsolutePath());
            if (log.isDebugEnabled()) {
                // $NON-NLS-1$
                log.debug("---------Translate " + uri.toString());
            }
            EObject eObject = null;
            try {
                Resource resource = emfUtil.getResourceSet().getResource(uri, true);
                ResourceFileMap resourceFileMap = ModelElementFileFactory.getResourceFileMap(uri.fileExtension());
                if (resourceFileMap != null) {
                    eObject = resourceFileMap.getModelElement(resource);
                }
            } catch (Exception e) {
                // $NON-NLS-1$
                log.warn(DefaultMessagesImpl.getString("UpdatePropertiesFileTask_2", file.getAbsolutePath()), e);
                ok = false;
            }
            if (eObject != null) {
                if (eObject instanceof ModelElement) {
                    try {
                        saveObject(emfUtil, file, uri, eObject);
                    } catch (Exception e) {
                        // $NON-NLS-1$
                        log.error(DefaultMessagesImpl.getString("UpdatePropertiesFileTask_3", eObject, file, uri), e);
                        ok = false;
                    }
                } else {
                    // $NON-NLS-1$
                    log.warn(DefaultMessagesImpl.getString("UpdatePropertiesFileTask_4", eObject.toString()));
                    ok = false;
                }
            }
        }
    }
    emfUtil = null;
    return ok;
}
Also used : ModelElement(orgomg.cwm.objectmodel.core.ModelElement) EMFUtil(org.talend.commons.emf.EMFUtil) ResourceFileMap(org.talend.dq.helper.resourcehelper.ResourceFileMap) EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) File(java.io.File) URI(org.eclipse.emf.common.util.URI)

Example 2 with EMFUtil

use of org.talend.commons.emf.EMFUtil in project tdq-studio-se by Talend.

the class LoadSerialDataMain method main.

/**
 * DOC scorreia Comment method "main".
 *
 * @param args
 */
public static void main(String[] args) {
    EMFUtil util = new EMFUtil();
    File file = new File("out/columnTest_0.1.ana");
    System.out.println("Loading file " + file.getAbsolutePath());
    ResourceSet rs = util.getResourceSet();
    Resource r = rs.getResource(URI.createFileURI(file.getAbsolutePath()), true);
    EList<EObject> contents = r.getContents();
    if (contents.isEmpty()) {
        System.err.println("No content in " + r);
    }
    System.out.println("Nb elements in contents " + contents.size());
    IndicatorsSwitch<FrequencyIndicator> mySwitch = new IndicatorsSwitch<FrequencyIndicator>() {

        @Override
        public FrequencyIndicator caseFrequencyIndicator(FrequencyIndicator object) {
            return object;
        }
    };
    for (EObject object : contents) {
        FrequencyIndicator freqI = mySwitch.doSwitch(object);
        if (freqI != null) {
            Long uniqueValueCount = freqI.getUniqueValueCount();
            System.out.println("nb unique values = " + uniqueValueCount);
            EList<Object> uniqueValues = freqI.getUniqueValues();
            for (Object data : uniqueValues) {
                System.out.println("unique value= " + data + " " + freqI.getCount(data));
            }
        }
    }
}
Also used : EMFUtil(org.talend.commons.emf.EMFUtil) EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) IndicatorsSwitch(org.talend.dataquality.indicators.util.IndicatorsSwitch) EObject(org.eclipse.emf.ecore.EObject) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) File(java.io.File) FrequencyIndicator(org.talend.dataquality.indicators.FrequencyIndicator)

Example 3 with EMFUtil

use of org.talend.commons.emf.EMFUtil in project tdq-studio-se by Talend.

the class ResourceViewLabelProviderTest method initProxyRepository.

/**
 * DOC talend Comment method "initProxyRepository".
 */
private void initProxyRepository(IProject rootProject) {
    Project project = null;
    ProxyRepositoryFactory proxyRepository = ProxyRepositoryFactory.getInstance();
    IRepositoryFactory repository = RepositoryFactoryProvider.getRepositoriyById(RepositoryConstants.REPOSITORY_LOCAL_ID);
    if (repository == null) {
        log.fatal(DefaultMessagesImpl.getString(// $NON-NLS-1$
        "No local Repository found! Probably due to a missing plugin in the product."));
    }
    proxyRepository.setRepositoryFactoryFromProvider(repository);
    try {
        proxyRepository.checkAvailability();
        proxyRepository.initialize();
        XmiResourceManager xmiResourceManager = new XmiResourceManager();
        if (rootProject.getFile(FileConstants.LOCAL_PROJECT_FILENAME).exists()) {
            // Initialize TDQ EMF model packages.
            new EMFUtil();
            project = new Project(xmiResourceManager.loadProject(rootProject));
        } else {
            User user = org.talend.core.model.properties.impl.PropertiesFactoryImpl.eINSTANCE.createUser();
            // $NON-NLS-1$
            user.setLogin("talend@talend.com");
            // $NON-NLS-1$
            user.setPassword("talend@talend.com".getBytes());
            String projectName = ResourceManager.getRootProjectName();
            String projectDesc = ResourcesPlugin.getWorkspace().newProjectDescription(projectName).getComment();
            Project projectInfor = ProjectHelper.createProject(projectName, projectDesc, ECodeLanguage.JAVA.getName(), user);
            // MOD zshen create project by proxyRepository
            checkFileName(projectInfor.getLabel(), RepositoryConstants.PROJECT_PATTERN);
            project = proxyRepository.getRepositoryFactoryFromProvider().createProject(projectInfor);
        }
        if (project != null) {
            initRepositoryContext(project);
            // add status
            // $NON-NLS-1$
            String defaultTechnicalStatusList = "DEV development;TEST testing;PROD production";
            List<Status> statusList = StatusHelper.parse(defaultTechnicalStatusList);
            proxyRepository.setTechnicalStatus(statusList);
        }
    } catch (PersistenceException e) {
        ExceptionHandler.process(e);
        log.error(e, e);
        Assert.fail(e.getMessage());
    }
}
Also used : Status(org.talend.core.model.properties.Status) Project(org.talend.core.model.general.Project) IProject(org.eclipse.core.resources.IProject) IRepositoryFactory(org.talend.core.repository.model.IRepositoryFactory) ProxyRepositoryFactory(org.talend.core.repository.model.ProxyRepositoryFactory) User(org.talend.core.model.properties.User) EMFUtil(org.talend.commons.emf.EMFUtil) PersistenceException(org.talend.commons.exception.PersistenceException) XmiResourceManager(org.talend.core.repository.utils.XmiResourceManager)

Example 4 with EMFUtil

use of org.talend.commons.emf.EMFUtil in project tdq-studio-se by Talend.

the class TDQJrxmlItemImplTest method setUp.

/**
 * DOC yyin Comment method "setUp".
 *
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
    jrxmlItem = PropertiesFactory.eINSTANCE.createTDQJrxmlItem();
    EMFUtil emfUtil = new EMFUtil();
    // File position = new File(""); //$NON-NLS-1$
    // $NON-NLS-1$
    file = new File("test_0.1.jrxml");
    // $NON-NLS-1$
    writeToFile("original jrxml content");
    ResourceSet rs = emfUtil.getResourceSet();
    // init the bytearray resource
    ByteArray byteArray = org.talend.core.model.properties.PropertiesFactory.eINSTANCE.createByteArray();
    byteArray.setInnerContentFromFile(file);
    byteArrayResource = new ByteArrayResource(URI.createFileURI(file.getAbsolutePath()));
    byteArrayResource.getContents().add(byteArray);
    rs.getResources().add(byteArrayResource);
    org.talend.core.model.properties.Property fileProperty = org.talend.core.model.properties.PropertiesFactory.eINSTANCE.createProperty();
    fileProperty.setId(EcoreUtil.generateUUID());
    fileProperty.setItem(jrxmlItem);
    // $NON-NLS-1$
    fileProperty.setLabel("test1");
    // $NON-NLS-1$
    jrxmlItem.setName("test1");
    jrxmlItem.setProperty(fileProperty);
    ItemState itemState = org.talend.core.model.properties.PropertiesFactory.eINSTANCE.createItemState();
    // $NON-NLS-1$
    itemState.setPath("data");
    jrxmlItem.setState(itemState);
    // get the eresource of the item without creating the real project
    // $NON-NLS-1$
    File property = new File("test_0.1.property");
    Resource propertyResource = emfUtil.getResourceSet().createResource(URI.createFileURI(property.getAbsolutePath()));
    propertyResource.getContents().add(jrxmlItem.getProperty());
    propertyResource.getContents().add(jrxmlItem.getState());
    propertyResource.getContents().add(jrxmlItem);
    rs.getResources().add(propertyResource);
    byteArrayResource.load(null);
    jrxmlItem.setContent(byteArray);
    emfUtil.save();
}
Also used : EMFUtil(org.talend.commons.emf.EMFUtil) ItemState(org.talend.core.model.properties.ItemState) ByteArrayResource(org.talend.core.model.properties.helper.ByteArrayResource) Resource(org.eclipse.emf.ecore.resource.Resource) ByteArray(org.talend.core.model.properties.ByteArray) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ByteArrayResource(org.talend.core.model.properties.helper.ByteArrayResource) File(java.io.File) Before(org.junit.Before)

Example 5 with EMFUtil

use of org.talend.commons.emf.EMFUtil in project tdq-studio-se by Talend.

the class ReadItemConnectionFile method test.

@Test
public void test() {
    EMFUtil emfUtil = new EMFUtil();
    File file = new File("data/my_0.1.item");
    System.out.println("Loading file " + file.getAbsolutePath());
    ResourceSet rs = emfUtil.getResourceSet();
    Resource r = rs.getResource(URI.createFileURI(file.getAbsolutePath()), true);
    // TreeIterator<EObject> allContents = r.getAllContents();
    // while (allContents.hasNext()) {
    // EObject metadata = allContents.next();
    // System.out.println(metadata);
    // }
    // should contain the connection, then catalog/schema, then tables, then columns
    EList<EObject> contents = r.getContents();
    // switch class that returns a connection when it finds one.
    ConnectionSwitch<Connection> connectionFinder = new ConnectionSwitch<Connection>() {

        /*
             * (non-Javadoc)
             * 
             * @see
             * org.talend.core.model.metadata.builder.connection.util.ConnectionSwitch#caseConnection(org.talend.core
             * .model.metadata.builder.connection.Connection)
             */
        @Override
        public Connection caseConnection(Connection object) {
            return object;
        }
    };
    // loop on all the content of the resource
    for (EObject eObject : contents) {
        Connection connection = connectionFinder.doSwitch(eObject);
        if (connection != null) {
            Set<Catalog> allCatalogs = ConnectionHelper.getAllCatalogs(connection);
            for (Catalog catalog : allCatalogs) {
                System.out.println("## Catalog: " + catalog.getName());
                System.out.println("\t## Tables");
                List<TdTable> tables = CatalogHelper.getTables(catalog);
                for (TdTable tdTable : tables) {
                    System.out.println("\t\t" + tdTable.getName());
                    List<TdColumn> columns = TableHelper.getColumns(tdTable);
                    if (!columns.isEmpty()) {
                        System.out.println("\t\t\t## Columns");
                    }
                    for (TdColumn tdColumn : columns) {
                        System.out.println("\t\t\t\t" + tdColumn.getName());
                    }
                }
                System.out.println("\t## Views");
                List<TdView> views = CatalogHelper.getViews(catalog);
                for (TdView tdView : views) {
                    System.out.println("\t\t" + tdView.getName());
                    List<TdColumn> columns = ViewHelper.getColumns(tdView);
                    if (!columns.isEmpty()) {
                        System.out.println("\t\t\t## Columns");
                    }
                    for (TdColumn tdColumn : columns) {
                        System.out.println("\t\t\t\t" + tdColumn.getName());
                    }
                }
            }
        }
    }
}
Also used : TdTable(org.talend.cwm.relational.TdTable) Resource(org.eclipse.emf.ecore.resource.Resource) Connection(org.talend.core.model.metadata.builder.connection.Connection) ConnectionSwitch(org.talend.core.model.metadata.builder.connection.util.ConnectionSwitch) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Catalog(orgomg.cwm.resource.relational.Catalog) TdColumn(org.talend.cwm.relational.TdColumn) EMFUtil(org.talend.commons.emf.EMFUtil) TdView(org.talend.cwm.relational.TdView) EObject(org.eclipse.emf.ecore.EObject) File(java.io.File) Test(org.junit.Test)

Aggregations

EMFUtil (org.talend.commons.emf.EMFUtil)18 Resource (org.eclipse.emf.ecore.resource.Resource)11 File (java.io.File)8 URI (org.eclipse.emf.common.util.URI)8 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 IProject (org.eclipse.core.resources.IProject)6 PersistenceException (org.talend.commons.exception.PersistenceException)6 Project (org.talend.core.model.general.Project)6 Status (org.talend.core.model.properties.Status)6 User (org.talend.core.model.properties.User)6 IRepositoryFactory (org.talend.core.repository.model.IRepositoryFactory)6 ProxyRepositoryFactory (org.talend.core.repository.model.ProxyRepositoryFactory)6 XmiResourceManager (org.talend.core.repository.utils.XmiResourceManager)6 EObject (org.eclipse.emf.ecore.EObject)5 IPath (org.eclipse.core.runtime.IPath)2 Property (org.talend.core.model.properties.Property)2 ByteArrayResource (org.talend.core.model.properties.helper.ByteArrayResource)2 FrequencyIndicator (org.talend.dataquality.indicators.FrequencyIndicator)2 FilenameFilter (java.io.FilenameFilter)1 IOException (java.io.IOException)1