public void generatePath(DMEntity entity) throws ConfigException, DataSourceException { DMEntity p = entity; StringBuilder path = new StringBuilder(); switch (p.getType()) { case DMEntityType.WORKSPACE: path.append(DMEntityImpl.DM_PATH_SEPARATOR); path.append(p.getName()); break; case DMEntityType.FOLDER: path.append(DMEntityImpl.DM_PATH_SEPARATOR); path.append(p.getName()); p = initializeAndUnproxy(p); p = ((Folder) p).getParent(); while (p != null) { path.insert(0, DMEntityImpl.DM_PATH_SEPARATOR); path.insert(1, p.getName()); if (p.getType() == DMEntityType.FOLDER) { p = initializeAndUnproxy(p); p = ((Folder) p).getParent(); } else { p = null; } } break; case DMEntityType.DOCUMENT: path.append(DMEntityImpl.DM_PATH_SEPARATOR); path.append(p.getName()); p = initializeAndUnproxy(p); if (((Document) p).getExtension() != null) { path.append(DMEntityImpl.DM_EXTENSION_SEPARATOR); path.append(((Document) p).getExtension().toLowerCase()); } p = ((Document) p).getFolder(); while (p != null) { path.insert(0, DMEntityImpl.DM_PATH_SEPARATOR); path.insert(1, p.getName()); if (p.getType() == DMEntityType.FOLDER) { p = initializeAndUnproxy(p); p = ((Folder) p).getParent(); } else { p = null; } } break; } log.debug("Entity " + entity.getUid() + " generated path " + path); entity.setPath(path.toString()); }
public void updatePath(DMEntity entity, String newName) throws ConfigException, DataSourceException { DMEntity p = entity; String oldPath = p.getPath(); /* * Setting name */ p.setName(newName); /* * Generate new path */ StringBuilder path = new StringBuilder(); switch (p.getType()) { case DMEntityType.WORKSPACE: path.append(DMEntityImpl.DM_PATH_SEPARATOR); path.append(p.getName()); break; case DMEntityType.FOLDER: path.append(DMEntityImpl.DM_PATH_SEPARATOR); path.append(p.getName()); p = initializeAndUnproxy(p); p = ((Folder) p).getParent(); while (p != null) { path.insert(0, DMEntityImpl.DM_PATH_SEPARATOR); path.insert(1, p.getName()); if (p.getType() == DMEntityType.FOLDER) { p = initializeAndUnproxy(p); p = ((Folder) p).getParent(); } else { p = null; } } break; case DMEntityType.DOCUMENT: path.append(DMEntityImpl.DM_PATH_SEPARATOR); path.append(p.getName()); p = initializeAndUnproxy(p); if (((Document) p).getExtension() != null) { path.append(DMEntityImpl.DM_EXTENSION_SEPARATOR); path.append(((Document) p).getExtension().toLowerCase()); } p = ((Document) p).getFolder(); while (p != null) { path.insert(0, DMEntityImpl.DM_PATH_SEPARATOR); path.insert(1, p.getName()); if (p.getType() == DMEntityType.FOLDER) { p = initializeAndUnproxy(p); p = ((Folder) p).getParent(); } else { p = null; } } break; } log.debug("oldPath: " + oldPath + " / newPath: " + path); entity.setPath(path.toString()); /* * Update entity path */ String query = "update DMEntityImpl set path = " + ":newPath where path = :oldPath"; getSession() .createQuery(query) .setString("oldPath", oldPath) .setString("newPath", entity.getPath()) .executeUpdate(); /* * Update children path */ if (entity.getType() != 3) { query = "update DMEntityImpl set path = " + "concat(:newPath,substring(path, :oldPathLength + 1, length(path))) " + "where substring(path,1, :oldPathLength) = :oldPath"; getSession() .createQuery(query) .setInteger("oldPathLength", oldPath.length() + 1) .setString("oldPath", oldPath + "/") .setString("newPath", entity.getPath() + "/") .executeUpdate(); } }