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;
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations