@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()); }