Markush DCR Exporter Button

    This button is used in the Markush demo data set on a form view for Inventsion, which has the V Examples table as a child. This is an older version which does not use a pop-up to ask which columns the user would like exported along with the structures. Instead, you must define these in the script itself. However, a pop-up would be very easy to incorporate (see the Table Standardizer script as an example for utilizing column inputs via a SwingBuilder).

    This script first prompts for the name and location of the outpu SDF (checking if the file already exists or if it has the right extension). It then identifies the exemplified structures associated with the selected patent, and exports then with the affiliated column data.

    
    /** Example Structures Exporter
     *
     * @author Tim Dudgeon (tdudgeon@chemaxon.com)
     */
    import com.im.commons.progress.*
    import chemaxon.formats.MolExporter
    import chemaxon.struc.Molecule
    import chemaxon.formats.MolImporter
    import javax.swing.JFileChooser
    import javax.swing.*
    
    evaluate = { widget ->
        def STRUCTURE_FIELD = 'Cd Structure' // field name of the structure field
        def FIELDS_FROM_CHILD = [ 'DCR Number', 'ROLE1 CODES', 'ROLE1 NAMES', 'ROLE2 CODES', 'ROLE2 NAMES' ] // list of field names from the child entity to export. Do not include structure field
    
        // Prompt for save file location
        def chooser = new JFileChooser()
        if (chooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION) {
            File fileName = chooser.getSelectedFile()
            name = fileName.getCanonicalPath()
    
            // Check to see if file has the correct extension
            if (!name.endsWith('.sdf')){
                FILE_NAME = name + '.sdf'
                println "if $fileName"
            } else {
                FILE_NAME = fileName.getCanonicalPath()
                println "else $fileName"
            }
    
            // See if file already exists
            File existFile = new File(FILE_NAME)
            if (existFile.exists () ) {
                def response = JOptionPane.showConfirmDialog (null, "$existFile exists \nOverwrite existing file?", "Confirm Overwrite", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE)
                if (response == JOptionPane.CANCEL_OPTION) {
                    return
                }
                println "fileName $FILE_NAME attained"
            }
        } else {
            return}
    
        def rs = widget.form.resultSet
        def dataTree = rs.dataTree
        def parent = dataTree.rootVertex.entity
        def molEdge = dataTree.rootVertex.edges.find { it.destination.entity.name == 'V Examples' }
        def molEntity = molEdge.destination.entity
        def fldMol = molEntity.fields.items.find { it.name == STRUCTURE_FIELD }
    
        def fieldsFromChild = [ ]
        FIELDS_FROM_CHILD.each { name ->
            def fld = molEntity.fields.items.find { it.name == name }
            if (fld) {
                fieldsFromChild << fld
                println "Found child field ${fld.id} for $name"
            } else {
                println "WARNING: field $name not found"
            }
        }
        def ors = parent.schema.dataProvider.getDefaultResultSet(dataTree, false, DFEnvironmentRO.DEV_NULL)
        def parentVS = ors.getVertexState(dataTree.rootVertex)
        def molVS = ors.getVertexState(molEdge.destination)
        def ids = parentVS.selectedRowsIds
    
        if (ids.size == 1) {
            println "Found one entry selected for export"
        } else {
            throw new RuntimeException('Must select single record for export')
        }
        def id = ids[0]
    
        // now read the data
        def good = 0
        def exporter = new MolExporter(FILE_NAME, 'sdf')
        def lock = ors.lockable.obtainLock('exporting')
        try {
            def envRW = new DFEnvironmentRW() {
                public DFLock getLock() {
                    return lock
                }
                public DFFeedback getFeedback() {
                    return DFEnvironmentRO.DEV_NULL.feedback
                }
            }
    
            def mol = null
            def childIDs = molVS.ids
            def values = [ : ]
            childIDs.eachWithIndex { childID, i ->
                println "Exporting ID $i $childID"
                def molData = molVS.getData([childID], DFEnvironmentRO.DEV_NULL)
                mol = molData[childID][fldMol.id]
                fieldsFromChild.each {
                    values.put(it, molData[childID][it.id])
                }
    
                def expMol
                if (!mol ) {
                    expMol = new Molecule()
                } else {
                    expMol = MolImporter.importMol(mol)
                }
                values.each { k,v ->
                    if (v) {
                        expMol.setProperty(k.name, v.toString())
                    }
                }
                exporter.write(expMol)
                good++
            }
        } finally {
            lock?.release()
            exporter.flush()
            exporter.close()
        }
        JOptionPane.showMessageDialog(null, "Finished exporting data to file $FILE_NAME \n $good structures exported", "Export Complete", JOptionPane.INFORMATION_MESSAGE)
        println "Finished exporting data to file $FILE_NAME"
        println "$good structures exported"