Create Relational Data


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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.