Search in sources :

Example 1 with DeleteRecord

use of com.rebuild.core.metadata.DeleteRecord in project rebuild by getrebuild.

the class GeneralEntityService method createOrUpdate.

// 此方法具备明细实体批处理能力
// 此方法具备重复检查能力
@Override
public Record createOrUpdate(Record record) {
    @SuppressWarnings("unchecked") final List<Record> details = (List<Record>) record.removeValue(HAS_DETAILS);
    final int rcm = GeneralEntityServiceContextHolder.getRepeatedCheckModeOnce();
    if (rcm == GeneralEntityServiceContextHolder.RCM_CHECK_MAIN || rcm == GeneralEntityServiceContextHolder.RCM_CHECK_ALL) {
        List<Record> repeated = getAndCheckRepeated(record, 20);
        if (!repeated.isEmpty()) {
            throw new RepeatedRecordsException(repeated);
        }
    }
    record = super.createOrUpdate(record);
    if (details == null || details.isEmpty())
        return record;
    // 主记录+明细记录处理
    String dtf = MetadataHelper.getDetailToMainField(record.getEntity().getDetailEntity()).getName();
    ID mainid = record.getPrimary();
    boolean checkDetailsRepeated = rcm == GeneralEntityServiceContextHolder.RCM_CHECK_DETAILS || rcm == GeneralEntityServiceContextHolder.RCM_CHECK_ALL;
    for (Record d : details) {
        if (d instanceof DeleteRecord) {
            delete(d.getPrimary());
            continue;
        }
        if (checkDetailsRepeated) {
            // for check
            d.setID(dtf, mainid);
            List<Record> repeated = getAndCheckRepeated(d, 20);
            if (!repeated.isEmpty()) {
                throw new RepeatedRecordsException(repeated);
            }
        }
        if (d.getPrimary() == null) {
            d.setID(dtf, mainid);
            create(d);
        } else {
            update(d);
        }
    }
    return record;
}
Also used : DeleteRecord(com.rebuild.core.metadata.DeleteRecord) DeleteRecord(com.rebuild.core.metadata.DeleteRecord) ID(cn.devezhao.persist4j.engine.ID)

Aggregations

ID (cn.devezhao.persist4j.engine.ID)1 DeleteRecord (com.rebuild.core.metadata.DeleteRecord)1