Search in sources :

Example 6 with RebuildException

use of com.rebuild.core.RebuildException in project rebuild by getrebuild.

the class UserHelper method getCreatedBy.

private static ID getCreatedBy(ID anyRecordId) {
    final String ckey = "CreatedBy-" + anyRecordId;
    ID createdBy = (ID) Application.getCommonsCache().getx(ckey);
    if (createdBy != null) {
        return createdBy;
    }
    Entity entity = MetadataHelper.getEntity(anyRecordId.getEntityCode());
    if (!entity.containsField(EntityHelper.CreatedBy)) {
        log.warn("No [createdBy] field in [{}]", entity.getEntityCode());
        return null;
    }
    Object[] c = Application.getQueryFactory().uniqueNoFilter(anyRecordId, EntityHelper.CreatedBy);
    if (c == null) {
        throw new RebuildException("No record found : " + anyRecordId);
    }
    createdBy = (ID) c[0];
    Application.getCommonsCache().putx(ckey, createdBy);
    return createdBy;
}
Also used : Entity(cn.devezhao.persist4j.Entity) RebuildException(com.rebuild.core.RebuildException) ID(cn.devezhao.persist4j.engine.ID)

Example 7 with RebuildException

use of com.rebuild.core.RebuildException in project rebuild by getrebuild.

the class EasyMetaFactory method valueOf.

/**
 * @param field
 * @return
 */
public static EasyField valueOf(Field field) {
    String displayType = field.getExtraAttrs() == null ? null : field.getExtraAttrs().getString("displayType");
    DisplayType dt = displayType == null ? convertBuiltinFieldType(field) : DisplayType.valueOf(displayType);
    if (dt == null) {
        throw new RebuildException("Unsupported field type : " + field);
    }
    try {
        Constructor<?> c = ReflectionUtils.accessibleConstructor(dt.getEasyClass(), Field.class, DisplayType.class);
        return (EasyField) c.newInstance(field, dt);
    } catch (Exception ex) {
        throw new RebuildException(ex);
    }
}
Also used : DisplayType(com.rebuild.core.metadata.easymeta.DisplayType) RebuildException(com.rebuild.core.RebuildException) RebuildException(com.rebuild.core.RebuildException) MetadataException(cn.devezhao.persist4j.metadata.MetadataException)

Example 8 with RebuildException

use of com.rebuild.core.RebuildException in project rebuild by getrebuild.

the class DataExporter method exportCsv.

/**
 * 导出到指定文件
 *
 * @param dest
 */
protected void exportCsv(File dest) {
    DataListBuilderImpl control = new DataListBuilderImpl(queryData, getUser());
    List<String> head = this.buildHead(control);
    try (FileOutputStream fos = new FileOutputStream(dest, true)) {
        try (OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
            try (BufferedWriter writer = new BufferedWriter(osw)) {
                writer.write("\ufeff");
                writer.write(mergeLine(head));
                for (List<String> row : this.buildData(control)) {
                    writer.newLine();
                    writer.write(mergeLine(row));
                    count++;
                }
                writer.flush();
            }
        }
    } catch (IOException e) {
        throw new RebuildException("Cannot write .csv file", e);
    }
}
Also used : DataListBuilderImpl(com.rebuild.core.support.general.DataListBuilderImpl) RebuildException(com.rebuild.core.RebuildException)

Example 9 with RebuildException

use of com.rebuild.core.RebuildException in project rebuild by getrebuild.

the class RecordDifference method diffMerge.

protected JSON diffMerge(Record after, boolean diffCommons) {
    if (record == null && after == null) {
        throw new RebuildException("Both records cannot be null");
    }
    if (record != null && after != null && !record.getEntity().equals(after.getEntity())) {
        throw new RebuildException("Both records must be the same entity");
    }
    Entity entity = record != null ? record.getEntity() : after.getEntity();
    Map<String, Object[]> merged = new CaseInsensitiveMap<>();
    if (record != null) {
        JSONObject recordSerialize = (JSONObject) record.serialize();
        for (Map.Entry<String, Object> e : recordSerialize.entrySet()) {
            String field = e.getKey();
            if (!diffCommons && isIgnoreField(entity.getField(field)))
                continue;
            Object beforeVal = e.getValue();
            if (NullValue.is(beforeVal))
                beforeVal = null;
            merged.put(field, new Object[] { beforeVal, null });
        }
    }
    if (after != null) {
        JSONObject afterSerialize = (JSONObject) after.serialize();
        for (Map.Entry<String, Object> e : afterSerialize.entrySet()) {
            String field = e.getKey();
            if (!diffCommons && isIgnoreField(entity.getField(field)))
                continue;
            Object afterVal = e.getValue();
            if (NullValue.is(afterVal))
                continue;
            Object[] mergedValue = merged.computeIfAbsent(field, k -> new Object[] { null, null });
            mergedValue[1] = afterVal;
        }
    }
    JSONArray result = new JSONArray();
    for (Map.Entry<String, Object[]> e : merged.entrySet()) {
        Object[] vals = e.getValue();
        if (vals[0] == null && vals[1] == null)
            continue;
        if (Objects.equals(vals[0], vals[1]))
            continue;
        JSON item = JSONUtils.toJSONObject(new String[] { "field", "before", "after" }, new Object[] { e.getKey(), vals[0], vals[1] });
        result.add(item);
    }
    return result;
}
Also used : Entity(cn.devezhao.persist4j.Entity) JSONArray(com.alibaba.fastjson.JSONArray) RebuildException(com.rebuild.core.RebuildException) JSON(com.alibaba.fastjson.JSON) CaseInsensitiveMap(org.apache.commons.collections4.map.CaseInsensitiveMap) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) CaseInsensitiveMap(org.apache.commons.collections4.map.CaseInsensitiveMap) Map(java.util.Map)

Example 10 with RebuildException

use of com.rebuild.core.RebuildException in project rebuild by getrebuild.

the class RecycleRestore method restore.

/**
 * 恢复数据
 *
 * @param cascade 恢复关联删除的数据
 * @return
 */
public int restore(boolean cascade) {
    Object[] main = Application.createQueryNoFilter("select recordContent,recordId,recycleId from RecycleBin where recycleId = ?").setParameter(1, this.recycleId).unique();
    // 可能已经(关联)恢复了
    if (main == null) {
        log.warn("No recycle found! Maybe restored : " + this.recycleId);
        return 0;
    }
    final List<ID> recycleIds = new ArrayList<>();
    final List<Record> willRestores = new ArrayList<>(conver2Record(JSON.parseObject((String) main[0]), (ID) main[1]));
    if (willRestores.isEmpty()) {
        throw new RebuildException("Record entity not exists");
    }
    recycleIds.add((ID) main[2]);
    if (cascade) {
        Object[][] array = Application.createQueryNoFilter("select recordContent,recordId,recycleId from RecycleBin where channelWith = ?").setParameter(1, main[1]).array();
        for (Object[] o : array) {
            List<Record> records = conver2Record(JSON.parseObject((String) o[0]), (ID) o[1]);
            if (!records.isEmpty()) {
                willRestores.addAll(records);
                recycleIds.add((ID) o[2]);
            }
        }
    }
    // 启动事物
    final TransactionStatus status = TransactionManual.newTransaction();
    int restored = 0;
    PersistManagerImpl PM = (PersistManagerImpl) Application.getPersistManagerFactory().createPersistManager();
    try {
        for (Record r : willRestores) {
            String primaryName = r.getEntity().getPrimaryField().getName();
            ID primaryId = (ID) r.removeValue(primaryName);
            PM.saveInternal(r, primaryId);
            restoreAttachment(PM, primaryId);
            if (primaryId.getEntityCode() == EntityHelper.Feeds)
                restoreFeedsMention(r);
            restored++;
        }
        // 从回收站删除
        PM.delete(recycleIds.toArray(new ID[0]));
        TransactionManual.commit(status);
        return restored;
    } catch (Throwable ex) {
        TransactionManual.rollback(status);
        throw new RebuildException("Failed to restore data", ex);
    }
}
Also used : ArrayList(java.util.ArrayList) RebuildException(com.rebuild.core.RebuildException) TransactionStatus(org.springframework.transaction.TransactionStatus) PersistManagerImpl(cn.devezhao.persist4j.engine.PersistManagerImpl) JSONObject(com.alibaba.fastjson.JSONObject) Record(cn.devezhao.persist4j.Record) ID(cn.devezhao.persist4j.engine.ID)

Aggregations

RebuildException (com.rebuild.core.RebuildException)16 ID (cn.devezhao.persist4j.engine.ID)4 Entity (cn.devezhao.persist4j.Entity)2 JSONObject (com.alibaba.fastjson.JSONObject)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Cipher (javax.crypto.Cipher)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 Cell (cn.devezhao.commons.excel.Cell)1 Field (cn.devezhao.persist4j.Field)1 Record (cn.devezhao.persist4j.Record)1 PersistManagerImpl (cn.devezhao.persist4j.engine.PersistManagerImpl)1 MetadataException (cn.devezhao.persist4j.metadata.MetadataException)1 MissingMetaExcetion (cn.devezhao.persist4j.metadata.MissingMetaExcetion)1 AnalysisContext (com.alibaba.excel.context.AnalysisContext)1 AnalysisEventListener (com.alibaba.excel.event.AnalysisEventListener)1 JSON (com.alibaba.fastjson.JSON)1 JSONArray (com.alibaba.fastjson.JSONArray)1 EncodeHintType (com.google.zxing.EncodeHintType)1