@Override public synchronized void delete(AEntity entity) { String alias = aliases.get(entity.getClass()); File file = new File(dir + "/" + alias + "/" + entity.getId() + ".xml"); // backup if (file.exists() && !(entity instanceof BackupHostile)) { backup(file, entity.getDao().getEntityName()); } // delete if (!file.delete() && file.exists()) throw new RuntimeException("Deleting entity file failed: " + file.getAbsolutePath()); getDao(entity.getClass()).remove(entity.getId()); LOG.debug("Entity deleted:", file.getPath(), entity.getClass().getSimpleName(), entity); }
@Override public synchronized void save(AEntity entity) { if (!versionSaved) saveVersion(); String alias = aliases.get(entity.getClass()); File tmpFile = new File(dir + "/tmp/" + entity.getId() + ".xml"); if (!tmpFile.getParentFile().exists()) { tmpFile.getParentFile().mkdirs(); } // save BufferedOutputStream out; try { out = new BufferedOutputStream(new FileOutputStream(tmpFile)); } catch (IOException ex) { throw new RuntimeException(ex); } beanSerializer.serialize(entity, out); try { out.close(); } catch (IOException ex) { throw new RuntimeException(ex); } File file = new File(dir + "/" + alias + "/" + entity.getId() + ".xml"); // backup if (file.exists() && !(entity instanceof BackupHostile)) { backup(file, entity.getDao().getEntityName()); } IO.move(tmpFile, file, true); getDao(entity.getClass()).put(entity.getId(), entity); LOG.debug("Entity saved:", entity, "->", file.getPath()); }
private boolean loadObject(File file, Map<String, AEntity> entities, Class type, String alias) { String name = file.getName(); if (!name.endsWith(".xml")) { LOG.warn("Unsupported file. Skipping:", name); return false; } if (entityfilePreparator != null) entityfilePreparator.prepareEntityfile(file, type, alias); BufferedInputStream in; try { in = new BufferedInputStream(new FileInputStream(file)); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } AEntity entity = (AEntity) beanSerializer.deserialize(in); entities.put(entity.getId(), entity); try { in.close(); } catch (IOException ex) { throw new RuntimeException(ex); } return true; }