removing old not versioned node

This commit is contained in:
lucio.lelii 2021-03-15 16:01:14 +01:00
parent 6b690caf56
commit 7591536a69
3 changed files with 71 additions and 43 deletions

View File

@ -7,6 +7,7 @@ import java.io.OutputStream;
import java.net.URL;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Deque;
import java.util.HashSet;
@ -24,6 +25,7 @@ import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.lock.Lock;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.NodeType;
import javax.jcr.query.Query;
import javax.jcr.version.Version;
@ -57,6 +59,8 @@ import org.gcube.data.access.storagehub.handlers.items.Node2ItemConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.bull.javamelody.internal.common.LOG;
public class Utils {
public final static String SERVICE_NAME = "home-library";
@ -132,20 +136,20 @@ public class Utils {
} catch (UserNotAuthorizedException | BackendGenericError | RepositoryException e) {
return false;
}
}
};
return getItemListFromNodeIterator(checker, iterator , excludes, range, showHidden, excludeTrashed, nodeTypeToInclude);
}
public static <T extends Item> List<T> getItemList(Node parent, List<String> excludes, Range range, boolean showHidden, Class<? extends Item> nodeTypeToInclude) throws RepositoryException, BackendGenericError{
return getItemList(null, parent, excludes, range, showHidden, nodeTypeToInclude);
}
public static <T extends Item> List<T> getItemList(Predicate<Node> checker, Node parent, List<String> excludes, Range range, boolean showHidden, Class<? extends Item> nodeTypeToInclude) throws RepositoryException, BackendGenericError{
logger.trace("getting children of node {}", parent.getIdentifier());
long start = System.currentTimeMillis();
@ -153,10 +157,10 @@ public class Utils {
logger.trace("time to get iterator {}",(System.currentTimeMillis()-start));
return getItemListFromNodeIterator(checker, iterator, excludes, range, showHidden, false, nodeTypeToInclude);
}
private static <T extends Item> List<T> getItemListFromNodeIterator(Predicate<Node> checker, NodeChildrenFilterIterator iterator, List<String> excludes, Range range, boolean showHidden, boolean excludeTrashed, Class<? extends Item> nodeTypeToInclude) throws RepositoryException, BackendGenericError{
List<T> returnList = new ArrayList<T>();
logger.trace("nodeType is {}",nodeTypeToInclude);
int count =0;
logger.trace("selected range is {}", range);
@ -164,20 +168,20 @@ public class Utils {
Set<String> duplicateId = new HashSet<String>();
while (iterator.hasNext()){
Node current = iterator.next();
logger.debug("[SEARCH] evaluating node {} ",current.hasProperty(NodeProperty.TITLE.toString())? current.getProperty(NodeProperty.TITLE.toString()):current.getName());
//REMOVE duplicate nodes, in case the indexes are not working
if (duplicateId.contains(current.getIdentifier())) {
logger.warn("duplicated node found");
continue;
}
//EXCLUDES node from predicate
if (checker!=null && !checker.test(current))
continue;
if (isToExclude(current, showHidden))
continue;
@ -299,18 +303,32 @@ public class Utils {
public static void getAllContentIds(Session ses, Set<String> idsToDelete, Item itemToDelete, VersionHandler versionHandler) throws Exception{
if (itemToDelete instanceof AbstractFileItem) {
List<Version> versions = versionHandler.getContentVersionHistory(ses.getNodeByIdentifier(itemToDelete.getId()), ses);
versions.forEach(v -> {
try {
String storageId =v.getFrozenNode().getProperty(NodeProperty.STORAGE_ID.toString()).getString();
idsToDelete.add(storageId);
logger.info("retrieved StorageId {} for version {}", storageId, v.getName());
} catch (Exception e) {
logger.warn("error retreiving sotrageId version for item with id {}",itemToDelete.getId(),e);
Node currentNode = ses.getNodeByIdentifier(itemToDelete.getId());
List<NodeType> ntList = Arrays.asList(currentNode.getMixinNodeTypes());
boolean isVersioned = false;
logger.debug("mixin node type are {}",ntList);
for (NodeType nt: ntList)
if(nt.getName().equals("mix:versionable")) {
isVersioned = true;
break;
}
});
if (isVersioned) {
List<Version> versions = versionHandler.getContentVersionHistory(currentNode, ses);
versions.forEach(v -> {
try {
String storageId =v.getFrozenNode().getProperty(NodeProperty.STORAGE_ID.toString()).getString();
idsToDelete.add(storageId);
logger.info("retrieved StorageId {} for version {}", storageId, v.getName());
} catch (Exception e) {
logger.warn("error retreiving sotrageId version for item with id {}",itemToDelete.getId(),e);
}
});
} else
logger.info("not versionable node type found of type {}", currentNode.getPrimaryNodeType().toString());
idsToDelete.add(((AbstractFileItem) itemToDelete).getContent().getStorageId());
}else if (itemToDelete instanceof FolderItem) {
List<Item> items = Utils.getItemList(ses.getNodeByIdentifier(itemToDelete.getId()), Excludes.GET_ONLY_CONTENT , null, true, null);
@ -355,10 +373,10 @@ public class Utils {
}
}
public static Node createFolderInternally(Session ses, Node destinationNode, String name, String description, boolean hidden, String login, AccountingHandler accountingHandler) throws BackendGenericError {
logger.debug("creating folder {} in {}", name, destinationNode);
String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, destinationNode, name);
FolderItem item = new FolderItem();
@ -367,12 +385,12 @@ public class Utils {
item.setTitle(uniqueName);
item.setDescription(description);
//item.setCreationTime(now);
boolean hiddenDestNode= false;
try {
hiddenDestNode = destinationNode.getProperty(NodeProperty.HIDDEN.toString()).getBoolean();
}catch (Throwable e) {}
item.setHidden(hidden || hiddenDestNode);
item.setLastAction(ItemAction.CREATED);
item.setLastModificationTime(now);
@ -387,7 +405,7 @@ public class Utils {
}
return newNode;
}
public static Node createURLInternally(Session ses, Node destinationNode, String name, URL value, String description, String login, AccountingHandler accountingHandler) throws BackendGenericError {
String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, destinationNode, name);
@ -410,7 +428,7 @@ public class Utils {
} catch (Throwable e) {
item.setHidden(false);
}
Node newNode = new Item2NodeConverter().getNode(destinationNode, item);
@ -447,7 +465,7 @@ public class Utils {
node.setProperty(NodeProperty.LAST_MODIFIED_BY.toString(), login);
node.setProperty(NodeProperty.LAST_ACTION.toString(), action.name());
}
public static synchronized VRE getVreFolderItem(Session ses, Node2ItemConverter node2Item, VREManager vreManager, List<String> excludes ) throws RepositoryException, BackendGenericError{
org.gcube.common.storagehub.model.Path vrePath = Paths.append(Utils.getWorkspacePath(), Constants.VRE_FOLDER_PARENT_NAME);
ScopeBean bean = new ScopeBean(ScopeProvider.instance.get());
@ -468,7 +486,7 @@ public class Utils {
}
}
public static synchronized VRE getVreFolderItemByGroupNameAndUser(Session ses, String goupName, String userId, Node2ItemConverter node2Item, VREManager vreManager, List<String> excludes ) throws RepositoryException, BackendGenericError{
org.gcube.common.storagehub.model.Path vrePath = Paths.append(Utils.getWorkspacePath(userId), Constants.VRE_FOLDER_PARENT_NAME);
VRE vre = vreManager.getVRE(goupName);
@ -485,5 +503,5 @@ public class Utils {
return vreManager.putVRE(vreFolder);
}
}
}

View File

@ -65,11 +65,18 @@ public class TrashHandler {
public void removeNodes(Session ses, List<Item> itemsToDelete) throws RepositoryException, StorageHubException{
log.debug("defnitively removing nodes with ids {}",itemsToDelete);
for (Item item: itemsToDelete) {
removeNodesInternally(ses, item);
removeNodesInternally(ses, item, false);
}
}
private void removeNodesInternally(Session ses, Item itemToDelete) throws RepositoryException, StorageHubException {
public void removeOnlyNodesContent(Session ses, List<Item> itemsToDelete) throws RepositoryException, StorageHubException{
log.debug("defnitively removing nodes with ids {}",itemsToDelete);
for (Item item: itemsToDelete) {
removeNodesInternally(ses, item, true);
}
}
private void removeNodesInternally(Session ses, Item itemToDelete, boolean onlyContent) throws RepositoryException, StorageHubException {
try {
Set<String> contentIdsToDelete = new HashSet<>();
@ -83,7 +90,8 @@ public class TrashHandler {
} else {
Utils.getAllContentIds(ses, contentIdsToDelete, itemToDelete, versionHandler);
}
nodeToDelete.remove();
if (!onlyContent)
nodeToDelete.remove();
log.debug("content ids to remove are {}",contentIdsToDelete);
@ -104,8 +112,8 @@ public class TrashHandler {
}
});
executor.execute(deleteFromStorageRunnable);
ses.save();
if (!onlyContent)
ses.save();
}catch (LockException e) {
throw new ItemLockedException("the selected node or his parent is locked", e);
}catch (Exception e) {

View File

@ -254,11 +254,6 @@ public class UserManager {
} catch (Throwable t) {
log.warn("error getting folder shared with ", t);
}
Authorizable authorizable = usrManager.getAuthorizable(new PrincipalImpl(user));
if (authorizable!=null && !authorizable.isGroup()) {
log.info("removing user {}", user);
authorizable.remove();
} else log.warn("the user {} was already deleted", user);
org.gcube.common.storagehub.model.Path homePath = Utils.getHome(user);
org.gcube.common.storagehub.model.Path workspacePath = Utils.getWorkspacePath(user);
@ -267,11 +262,18 @@ public class UserManager {
Node workspaceNode = session.getNode(workspacePath.toPath());
Node homeNode = session.getNode(homePath.toPath());
List<Item> workspaceItems = Utils.getItemList(workspaceNode, Excludes.GET_ONLY_CONTENT, null, true, null);
trashHandler.removeNodes(session, workspaceItems);
trashHandler.removeOnlyNodesContent(session, workspaceItems);
homeNode.remove();
} catch (PathNotFoundException e) {
log.warn("{} home dir was already deleted", user);
}
Authorizable authorizable = usrManager.getAuthorizable(new PrincipalImpl(user));
if (authorizable!=null && !authorizable.isGroup()) {
log.info("removing user {}", user);
authorizable.remove();
} else log.warn("the user {} was already deleted", user);
session.save();
}catch(StorageHubException she ){
log.error(she.getErrorMessage(), she);