【仅供内部供应商使用,不提供对外解答和培训】

Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
title处理数组元素的示例代码
collapsetrue
import org.bson.Document;

import java.util.*;

public class ArrayColumnResolver extends AbstractColumnResolver {

    @Override
    public boolean accept(Object cell) {
        return cell instanceof List;
    }

    @Override
    public void expandData(Document doc, List<String> columnNames, List<Integer> waitingColumnsIndex, List<List<Object>> rowDataCollections, List<Object> rowData) {
        fillRowDataByArray(doc, columnNames, waitingColumnsIndex, rowDataCollections, rowData);
    }

    private void fillRowDataByArray(Document doc, List<String> columnNames, List<Integer> waitingColumnsIndex, List<List<Object>> rowDataCollections, List<Object> rowData) {
        Map<Integer, List<Object>> group = new HashMap<Integer, List<Object>>();
        List<Object> standard = null;
        int maxLength = 0;
        for (int index : waitingColumnsIndex) {
            Object data = doc.get(columnNames.get(index));
            if (data instanceof List) {
                List<Object> array = (List<Object>) data;
                if (standard == null) {
                    standard = array;
                }
                maxLength = Math.max(maxLength, array.size());
                group.put(index, array);
            }
        }

        if (standard != null) {
            for (int i = 0; i < maxLength; i++) {
                List<Object> row = new ArrayList<Object>(Arrays.asList(new Object[rowData.size()]));
                Collections.copy(row, rowData);
                for (int index : waitingColumnsIndex) {
                    List<Object> array = group.get(index);
                    row.set(index, array.size() > i ? array.get(i) : null);
                }
                rowDataCollections.add(row);
            }
        }
    }
}

注册方式

Code Block
<dependence>
    <Item key="com.fr.solution.plugin.db.mongo" type="plugin"/>
</dependence>
<extra-core>
    <ColumnResolver class="com.fr.plugin.db.mongo.expand.impl.ArrayColumnResolver"/>
</extra-core>