Find Entries with Duplicated Field Value

    This script find duplicates in a chosen field and save results as a new list. It searches the datatree containing the script. This is the version with field chooser, so you specify on which field should be run the search after the script is run. There is also a minimalistic version where the field is specified directly in the script. You can find it in the dedicated forum topic.

    
    import java.awt.Dimension
    import java.awt.BorderLayout
    import javax.swing.*
    import javax.swing.border.*
    import org.openide.*
    import com.im.ijc.core.api.lists.TemporaryListsAndQueries
    import com.im.df.util.ui.DFObjectListCellRenderer
    
    def ety = dataTree.rootVertex.entity
    def edp = ety.schema.dataProvider.getEntityDataProvider(ety)
    def field = selectFromList(ety.fields.items)
    if (field == null) {
        return
    }
    
    sort = SortDirective.create(field, true)
    ids = edp.queryForIds(DFTermExpression.ALL_DATA, sort, env)
    
    List duplicates = []
    List buffer = []
    lastValue = null
    ids.each { rowId ->
       value = edp.getData([ rowId ], env).get(rowId).get(field.id)
       if (lastValue != value) {
           if (buffer.size() > 1) {
               duplicates.addAll(buffer)
           }
           buffer.clear()
           lastValue = value
       }
       buffer.add(rowId)
    }
    println "Found " + duplicates.size() + " duplicates"
    if (!duplicates.empty) {
        TemporaryListsAndQueries.getListsFor(ety).addList(duplicates, "Duplicates in " + field.name + " field")
    }
    
    Object selectFromList(List values) {
        JList list = new JList(values.toArray())
        list.cellRenderer = new DFObjectListCellRenderer()
        list.selectionModel.selectionMode = ListSelectionModel.SINGLE_SELECTION
        list.visibleRowCount = 15
        panel = new JPanel()
        panel.layout = new BorderLayout()
        panel.add(new JScrollPane(list), BorderLayout.CENTER)
        panel.border = new EmptyBorder(5,5,5,5)
        panel.preferredSize = new Dimension(250, 300)
        DialogDescriptor dd = new DialogDescriptor(panel, "Select field")
        if (DialogDisplayer.getDefault().notify(dd) != DialogDescriptor.OK_OPTION) {
            println "  Cancel pressed => exit"
            return null
        }
        selected = list.selectedValue
        println "  Selected field: $selected"
        if (selected == null) {
            println "  No field selected => exit"
            return null
        }
        return selected
    }