Create Relational Data


import com.im.commons.progress.*
import com.im.df.api.capabilities.*
import com.im.df.api.util.*;
import com.im.df.api.ddl.*;
import com.im.commons.db.util.SchemaQualifiedName
import com.im.df.api.chem.MarvinStructure

schema.lockable.withLock('create new jchem entity'){ env ->
    def Class[] req = [ JChemBaseEntityCapability.class ]
    def nt = DIFUtilities.findFirstAppropriateNewType(schema.getEntities().getNewTypes(), false, req, new Class[0]);
    nt.options.newDFItemNameSafe = 'STRUCTURES'
    assert nt.options.valid : 'Error: ' + nt.options.errorMessage
    def jchemEntity = nt.create(env).iterator().next()

    req = [ DFFieldBinaryCapability.class ]
    nt = DIFUtilities.findFirstAppropriateNewType(jchemEntity.getFields().getNewTypes(), false, req, new Class[0]);
    nt.options.newDFItemNameSafe = 'Binary'
    assert nt.options.valid : 'Error: ' + nt.options.errorMessage
    nt.create(env).iterator().next()

    def Class[] excl = [ JChemBaseEntityCapability.class ]
    nt = DIFUtilities.findFirstAppropriateNewType(schema.getEntities().getNewTypes(), false, new Class[0], excl);
    nt.options.newDFItemNameSafe = 'ANNOTATIONS'
    assert nt.options.valid : 'Error: ' + nt.options.errorMessage
    def stdEntity = nt.create(env).iterator().next()

    req = [ DFFieldTextCapability.class ]
    nt = DIFUtilities.findFirstAppropriateNewType(stdEntity.getFields().getNewTypes(), false, req, new Class[0]);
    nt.options.newDFItemNameSafe = 'Annotation'
    assert nt.options.valid : 'Error: ' + nt.options.errorMessage
    def annotationField = nt.create(env).iterator().next()

    req = [ DBRelationshipMtoN.class ]
    nt = DIFUtilities.findFirstAppropriateNewType(schema.getRelationships().getNewTypes(), false, req, new Class[0]);
    nt.options.newDFItemNameSafe = 'STRUCTURES to ANNOTATIONS'
    nt.options.srcField = jchemEntity.idField
    nt.options.dstField = stdEntity.idField
    nt.options.relType = DFRelationship.Type.MANY_TO_MANY
    assert nt.options.valid : 'Error: ' + nt.options.errorMessage
    def rel = nt.create(env).iterator().next()

    def dt1 = DIFUtilities.createDataTreeForEntity(jchemEntity, env)
    dt1.rootVertex.addEdge(rel.forward, env)
    DIFUtilities.createViewForDataTree(dt1, 'Gridview', true, env)
    
    def dt2 = DIFUtilities.createDataTreeForEntity(stdEntity, env)
    dt2.rootVertex.addEdge(rel.reverse, env)
    DIFUtilities.createViewForDataTree(dt2, 'Gridview', true, env)

    insertData(stdEntity, annotationField, [ 'aaa', 'bbb', 'ccc' ], env)
    
    def structField = DFItems.findItemsWithCapability(jchemEntity.fields.items, DFFieldStructureCapability.class).iterator().next()
    insertData(jchemEntity, structField, [ 
        new MarvinStructure('CCCC1=NN(C2=C1NC(=NC2=O)C3=C(C=CC(=C3)S(=O)(=O)N4CCN(CC4)C)OCC)C'),
        new MarvinStructure('CN1C=NC2=C1C(=O)N(C(=O)N2C)C'),
        new MarvinStructure('Clc1ccc(cc1)C(c2ccc(Cl)cc2)C(Cl)(Cl)Cl')
        ], env)

    // supppose these new rows were numbered 1,2,3 in both tables. Let's connect some of them:
    def links = [ 1:[1,2,3],2:[1], 3:[2,3] ]
    for ( x in links ) {
        for ( y in x.value ) {
            DIFUtilities.connectRelationalData(rel.forward, x.key, y, env)
        }
    }

} 

DFLock switchLock(DFEnvironmentRW env, DFLockable lockable, String newMessage) {
    env.lock.release() 
    env.lock = lockable.obtainLock(msg)
    return env.lock
}

void insertData(DFEntity entity, DFField field, List values, DFEnvironmentRW env) {
    def origLock = env.lock
    def edp = DIFUtilities.findEntityDataProvider(entity)
    edp.lockable.withLock('inserting data into ' + entity.name) { env2 ->
        values.each {
            def map = new HashMap()
            map.put( field.getId(), it)
            edp.insert(map, null, env2);
        }
    }
}

Versions: This script has been tested on IJC version 6.0.



Copyright © 1999-2013 ChemAxon Ltd.    All rights reserved.