Search in sources :

Example 1 with Cell

use of cn.devezhao.commons.excel.Cell in project rebuild by getrebuild.

the class DataImporter method exec.

@Override
protected Integer exec() {
    final List<Cell[]> rows = new DataFileParser(rule.getSourceFile()).parse();
    this.setTotal(rows.size() - 1);
    owningUser = rule.getDefaultOwningUser() != null ? rule.getDefaultOwningUser() : getUser();
    GeneralEntityServiceContextHolder.setSkipSeriesValue();
    for (final Cell[] row : rows) {
        if (isInterrupt()) {
            this.setInterrupted();
            break;
        }
        Cell fc = row == null || row.length == 0 ? null : row[0];
        if (fc == null || fc.getRowNo() == 0) {
            continue;
        }
        try {
            Record record = checkoutRecord(row);
            if (record == null) {
                traceLogs.add(new Object[] { fc.getRowNo(), "SKIP" });
            } else {
                boolean isNew = record.getPrimary() == null;
                record = Application.getEntityService(rule.getToEntity().getEntityCode()).createOrUpdate(record);
                this.addSucceeded();
                traceLogs.add(new Object[] { fc.getRowNo(), isNew ? "CREATED" : "UPDATED", record.getPrimary(), cellTraces });
            }
        } catch (Exception ex) {
            traceLogs.add(new Object[] { fc.getRowNo(), "ERROR", ex.getLocalizedMessage() });
            log.error("ROW#{} > {}", fc.getRowNo(), ex.getLocalizedMessage());
        }
        this.addCompleted();
    }
    return this.getSucceeded();
}
Also used : Record(cn.devezhao.persist4j.Record) Cell(cn.devezhao.commons.excel.Cell)

Example 2 with Cell

use of cn.devezhao.commons.excel.Cell in project rebuild by getrebuild.

the class RecordCheckout method checkoutN2NReferenceValue.

protected ID[] checkoutN2NReferenceValue(Field field, Cell cell) {
    final String val = cell.asString();
    Set<ID> ids = new LinkedHashSet<>();
    for (String s : val.split("[,,;;]")) {
        ID id = checkoutReferenceValue(field, new Cell(s, cell.getRowNo(), cell.getColumnNo()));
        if (id != null)
            ids.add(id);
    }
    return ids.toArray(new ID[0]);
}
Also used : ID(cn.devezhao.persist4j.engine.ID) Cell(cn.devezhao.commons.excel.Cell)

Example 3 with Cell

use of cn.devezhao.commons.excel.Cell in project rebuild by getrebuild.

the class RecordCheckout method checkout.

/**
 * @param record
 * @param row
 * @return
 */
public Record checkout(Record record, Cell[] row) {
    for (Map.Entry<Field, Integer> e : this.fieldsMapping.entrySet()) {
        int cellIndex = e.getValue();
        if (cellIndex >= row.length)
            continue;
        Cell cellValue = row[cellIndex];
        if (cellValue == Cell.NULL || cellValue.isEmpty()) {
            continue;
        }
        Field field = e.getKey();
        Object value = checkoutFieldValue(field, cellValue, true);
        if (value != null) {
            record.setObjectValue(field.getName(), value);
        } else {
            putTraceLog(cellValue, Language.L(EasyMetaFactory.getDisplayType(field)));
        }
    }
    return record;
}
Also used : Field(cn.devezhao.persist4j.Field) Cell(cn.devezhao.commons.excel.Cell)

Example 4 with Cell

use of cn.devezhao.commons.excel.Cell in project rebuild by getrebuild.

the class ClassificationFileImporter method exec.

@Override
protected Integer exec() throws Exception {
    List<Cell[]> rows = new DataFileParser(file).parse();
    this.setTotal(rows.size() - 1);
    boolean first = true;
    for (Cell[] row : rows) {
        if (first) {
            first = false;
            continue;
        }
        if (row.length == 0) {
            continue;
        }
        this.addCompleted();
        String L1 = row[0].asString();
        if (StringUtils.isBlank(L1)) {
            continue;
        }
        ID L1Id = findOrCreate(L1, null, null, LEVEL_BEGIN);
        String L2 = row.length > 1 ? row[1].asString() : null;
        if (StringUtils.isBlank(L2)) {
            continue;
        }
        ID L2Id = findOrCreate(L2, null, L1Id, LEVEL_BEGIN + 1);
        String L3 = row.length > 2 ? row[2].asString() : null;
        if (StringUtils.isBlank(L3)) {
            continue;
        }
        ID L3Id = findOrCreate(L3, null, L2Id, LEVEL_BEGIN + 2);
        String L4 = row.length > 3 ? row[3].asString() : null;
        if (StringUtils.isBlank(L4)) {
            continue;
        }
        findOrCreate(L4, null, L3Id, LEVEL_BEGIN + 3);
    }
    return this.getSucceeded();
}
Also used : DataFileParser(com.rebuild.core.service.dataimport.DataFileParser) ID(cn.devezhao.persist4j.engine.ID) Cell(cn.devezhao.commons.excel.Cell)

Example 5 with Cell

use of cn.devezhao.commons.excel.Cell in project rebuild by getrebuild.

the class RecordCheckoutTest method checkoutMultiSelectValue.

@Test
void checkoutMultiSelectValue() {
    RecordCheckout checkout = new RecordCheckout(null);
    Field MULTISELECT = MetadataHelper.getEntity(TestAllFields).getField("MULTISELECT");
    checkout.checkoutMultiSelectValue(MULTISELECT, new Cell("1哈哈; 3"));
}
Also used : Field(cn.devezhao.persist4j.Field) Cell(cn.devezhao.commons.excel.Cell) Test(org.junit.jupiter.api.Test)

Aggregations

Cell (cn.devezhao.commons.excel.Cell)9 Field (cn.devezhao.persist4j.Field)3 ID (cn.devezhao.persist4j.engine.ID)3 Record (cn.devezhao.persist4j.Record)2 Test (org.junit.jupiter.api.Test)2 AnalysisContext (com.alibaba.excel.context.AnalysisContext)1 AnalysisEventListener (com.alibaba.excel.event.AnalysisEventListener)1 RebuildException (com.rebuild.core.RebuildException)1 DataFileParser (com.rebuild.core.service.dataimport.DataFileParser)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Matcher (java.util.regex.Matcher)1