@VisibleForTesting NameSpaceContainer deleteEntity(final NamespaceKey path, final Type type, String version, boolean deleteRoot) throws NamespaceException { final List<NameSpaceContainer> entitiesOnPath = getEntitiesOnPath(path); final NameSpaceContainer container = lastElement(entitiesOnPath); if (container == null) { throw new NamespaceNotFoundException(path, String.format("Entity %s not found", path)); } return doDeleteEntity(path, type, version, entitiesOnPath, deleteRoot); }
@Override public List<NameSpaceContainer> list(NamespaceKey root) throws NamespaceException { final List<NameSpaceContainer> entitiesOnPath = getEntitiesOnPath(root); final NameSpaceContainer rootContainer = lastElement(entitiesOnPath); if (rootContainer == null) { throw new NamespaceNotFoundException(root, "not found"); } if (!isListable(rootContainer.getType())) { throw new NamespaceNotFoundException(root, "no listable entity found"); } return doList(root, entitiesOnPath); }
/** * Helper method that returns the list of the namespace entities on the given path. Except the last entity on the path * all other entities should return a non-null value. * * @param entityPath * @return */ protected List<NameSpaceContainer> getEntitiesOnPath(NamespaceKey entityPath) throws NamespaceException { final List<byte[]> keys = Lists.newArrayListWithExpectedSize(entityPath.getPathComponents().size()); NamespaceKey currentPath = entityPath; for (int i = 0; i < entityPath.getPathComponents().size(); i++) { keys.add(new NamespaceInternalKey(currentPath, keyNormalization).getKey()); if (currentPath.hasParent()) { currentPath = currentPath.getParent(); } } // reverse the keys so that the order of keys is from root to leaf level entity. Collections.reverse(keys); final List<NameSpaceContainer> entitiesOnPath = namespace.get(keys); for (int i = 0; i < entitiesOnPath.size() - 1; i++) { if (entitiesOnPath.get(i) == null) { throw new NamespaceNotFoundException(entityPath, "one or more elements on the path are not found in namespace"); } } return entitiesOnPath; }
/** * Helper method which retrieves the entity with given key. No authorization check done. * * @param key * @return * @throws NamespaceException */ private NameSpaceContainer getEntity(final NamespaceKey key, Type type) throws NamespaceException { final List<NameSpaceContainer> entitiesOnPath = getEntitiesOnPath(key); NameSpaceContainer container = lastElement(entitiesOnPath); if (container == null || container.getType() != type) { throw new NamespaceNotFoundException(key, "not found"); } return doGetEntity(key, type, entitiesOnPath); }
protected NameSpaceContainer getEntityByIndex(IndexKey key, String index, Type type) throws NamespaceException { NameSpaceContainer namespaceContainer = getByIndex(key, index); if (namespaceContainer == null || namespaceContainer.getType() != type) { throw new NamespaceNotFoundException(new NamespaceKey(key.toString()), "not found"); } return namespaceContainer; }