removing old not versioned node
This commit is contained in:
parent
6b690caf56
commit
7591536a69
|
@ -7,6 +7,7 @@ import java.io.OutputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -24,6 +25,7 @@ import javax.jcr.RepositoryException;
|
||||||
import javax.jcr.Session;
|
import javax.jcr.Session;
|
||||||
import javax.jcr.lock.Lock;
|
import javax.jcr.lock.Lock;
|
||||||
import javax.jcr.lock.LockException;
|
import javax.jcr.lock.LockException;
|
||||||
|
import javax.jcr.nodetype.NodeType;
|
||||||
import javax.jcr.query.Query;
|
import javax.jcr.query.Query;
|
||||||
import javax.jcr.version.Version;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.bull.javamelody.internal.common.LOG;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
|
||||||
public final static String SERVICE_NAME = "home-library";
|
public final static String SERVICE_NAME = "home-library";
|
||||||
|
@ -132,20 +136,20 @@ public class Utils {
|
||||||
} catch (UserNotAuthorizedException | BackendGenericError | RepositoryException e) {
|
} catch (UserNotAuthorizedException | BackendGenericError | RepositoryException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return getItemListFromNodeIterator(checker, iterator , excludes, range, showHidden, excludeTrashed, nodeTypeToInclude);
|
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{
|
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);
|
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{
|
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());
|
logger.trace("getting children of node {}", parent.getIdentifier());
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
@ -153,10 +157,10 @@ public class Utils {
|
||||||
logger.trace("time to get iterator {}",(System.currentTimeMillis()-start));
|
logger.trace("time to get iterator {}",(System.currentTimeMillis()-start));
|
||||||
return getItemListFromNodeIterator(checker, iterator, excludes, range, showHidden, false, nodeTypeToInclude);
|
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{
|
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>();
|
List<T> returnList = new ArrayList<T>();
|
||||||
|
|
||||||
logger.trace("nodeType is {}",nodeTypeToInclude);
|
logger.trace("nodeType is {}",nodeTypeToInclude);
|
||||||
int count =0;
|
int count =0;
|
||||||
logger.trace("selected range is {}", range);
|
logger.trace("selected range is {}", range);
|
||||||
|
@ -164,20 +168,20 @@ public class Utils {
|
||||||
Set<String> duplicateId = new HashSet<String>();
|
Set<String> duplicateId = new HashSet<String>();
|
||||||
while (iterator.hasNext()){
|
while (iterator.hasNext()){
|
||||||
Node current = iterator.next();
|
Node current = iterator.next();
|
||||||
|
|
||||||
logger.debug("[SEARCH] evaluating node {} ",current.hasProperty(NodeProperty.TITLE.toString())? current.getProperty(NodeProperty.TITLE.toString()):current.getName());
|
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
|
//REMOVE duplicate nodes, in case the indexes are not working
|
||||||
if (duplicateId.contains(current.getIdentifier())) {
|
if (duplicateId.contains(current.getIdentifier())) {
|
||||||
logger.warn("duplicated node found");
|
logger.warn("duplicated node found");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//EXCLUDES node from predicate
|
//EXCLUDES node from predicate
|
||||||
if (checker!=null && !checker.test(current))
|
if (checker!=null && !checker.test(current))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
||||||
if (isToExclude(current, showHidden))
|
if (isToExclude(current, showHidden))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -299,18 +303,32 @@ public class Utils {
|
||||||
|
|
||||||
public static void getAllContentIds(Session ses, Set<String> idsToDelete, Item itemToDelete, VersionHandler versionHandler) throws Exception{
|
public static void getAllContentIds(Session ses, Set<String> idsToDelete, Item itemToDelete, VersionHandler versionHandler) throws Exception{
|
||||||
if (itemToDelete instanceof AbstractFileItem) {
|
if (itemToDelete instanceof AbstractFileItem) {
|
||||||
List<Version> versions = versionHandler.getContentVersionHistory(ses.getNodeByIdentifier(itemToDelete.getId()), ses);
|
Node currentNode = ses.getNodeByIdentifier(itemToDelete.getId());
|
||||||
|
List<NodeType> ntList = Arrays.asList(currentNode.getMixinNodeTypes());
|
||||||
versions.forEach(v -> {
|
boolean isVersioned = false;
|
||||||
try {
|
logger.debug("mixin node type are {}",ntList);
|
||||||
String storageId =v.getFrozenNode().getProperty(NodeProperty.STORAGE_ID.toString()).getString();
|
for (NodeType nt: ntList)
|
||||||
idsToDelete.add(storageId);
|
if(nt.getName().equals("mix:versionable")) {
|
||||||
logger.info("retrieved StorageId {} for version {}", storageId, v.getName());
|
isVersioned = true;
|
||||||
} catch (Exception e) {
|
break;
|
||||||
logger.warn("error retreiving sotrageId version for item with id {}",itemToDelete.getId(),e);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
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());
|
idsToDelete.add(((AbstractFileItem) itemToDelete).getContent().getStorageId());
|
||||||
}else if (itemToDelete instanceof FolderItem) {
|
}else if (itemToDelete instanceof FolderItem) {
|
||||||
List<Item> items = Utils.getItemList(ses.getNodeByIdentifier(itemToDelete.getId()), Excludes.GET_ONLY_CONTENT , null, true, null);
|
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 {
|
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);
|
logger.debug("creating folder {} in {}", name, destinationNode);
|
||||||
|
|
||||||
String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, destinationNode, name);
|
String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, destinationNode, name);
|
||||||
|
|
||||||
FolderItem item = new FolderItem();
|
FolderItem item = new FolderItem();
|
||||||
|
@ -367,12 +385,12 @@ public class Utils {
|
||||||
item.setTitle(uniqueName);
|
item.setTitle(uniqueName);
|
||||||
item.setDescription(description);
|
item.setDescription(description);
|
||||||
//item.setCreationTime(now);
|
//item.setCreationTime(now);
|
||||||
|
|
||||||
boolean hiddenDestNode= false;
|
boolean hiddenDestNode= false;
|
||||||
try {
|
try {
|
||||||
hiddenDestNode = destinationNode.getProperty(NodeProperty.HIDDEN.toString()).getBoolean();
|
hiddenDestNode = destinationNode.getProperty(NodeProperty.HIDDEN.toString()).getBoolean();
|
||||||
}catch (Throwable e) {}
|
}catch (Throwable e) {}
|
||||||
|
|
||||||
item.setHidden(hidden || hiddenDestNode);
|
item.setHidden(hidden || hiddenDestNode);
|
||||||
item.setLastAction(ItemAction.CREATED);
|
item.setLastAction(ItemAction.CREATED);
|
||||||
item.setLastModificationTime(now);
|
item.setLastModificationTime(now);
|
||||||
|
@ -387,7 +405,7 @@ public class Utils {
|
||||||
}
|
}
|
||||||
return newNode;
|
return newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Node createURLInternally(Session ses, Node destinationNode, String name, URL value, String description, String login, AccountingHandler accountingHandler) throws BackendGenericError {
|
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);
|
String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, destinationNode, name);
|
||||||
|
@ -410,7 +428,7 @@ public class Utils {
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
item.setHidden(false);
|
item.setHidden(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Node newNode = new Item2NodeConverter().getNode(destinationNode, item);
|
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_MODIFIED_BY.toString(), login);
|
||||||
node.setProperty(NodeProperty.LAST_ACTION.toString(), action.name());
|
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{
|
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);
|
org.gcube.common.storagehub.model.Path vrePath = Paths.append(Utils.getWorkspacePath(), Constants.VRE_FOLDER_PARENT_NAME);
|
||||||
ScopeBean bean = new ScopeBean(ScopeProvider.instance.get());
|
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{
|
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);
|
org.gcube.common.storagehub.model.Path vrePath = Paths.append(Utils.getWorkspacePath(userId), Constants.VRE_FOLDER_PARENT_NAME);
|
||||||
VRE vre = vreManager.getVRE(goupName);
|
VRE vre = vreManager.getVRE(goupName);
|
||||||
|
@ -485,5 +503,5 @@ public class Utils {
|
||||||
return vreManager.putVRE(vreFolder);
|
return vreManager.putVRE(vreFolder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,11 +65,18 @@ public class TrashHandler {
|
||||||
public void removeNodes(Session ses, List<Item> itemsToDelete) throws RepositoryException, StorageHubException{
|
public void removeNodes(Session ses, List<Item> itemsToDelete) throws RepositoryException, StorageHubException{
|
||||||
log.debug("defnitively removing nodes with ids {}",itemsToDelete);
|
log.debug("defnitively removing nodes with ids {}",itemsToDelete);
|
||||||
for (Item item: 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 {
|
try {
|
||||||
Set<String> contentIdsToDelete = new HashSet<>();
|
Set<String> contentIdsToDelete = new HashSet<>();
|
||||||
|
@ -83,7 +90,8 @@ public class TrashHandler {
|
||||||
} else {
|
} else {
|
||||||
Utils.getAllContentIds(ses, contentIdsToDelete, itemToDelete, versionHandler);
|
Utils.getAllContentIds(ses, contentIdsToDelete, itemToDelete, versionHandler);
|
||||||
}
|
}
|
||||||
nodeToDelete.remove();
|
if (!onlyContent)
|
||||||
|
nodeToDelete.remove();
|
||||||
|
|
||||||
log.debug("content ids to remove are {}",contentIdsToDelete);
|
log.debug("content ids to remove are {}",contentIdsToDelete);
|
||||||
|
|
||||||
|
@ -104,8 +112,8 @@ public class TrashHandler {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
executor.execute(deleteFromStorageRunnable);
|
executor.execute(deleteFromStorageRunnable);
|
||||||
|
if (!onlyContent)
|
||||||
ses.save();
|
ses.save();
|
||||||
}catch (LockException e) {
|
}catch (LockException e) {
|
||||||
throw new ItemLockedException("the selected node or his parent is locked", e);
|
throw new ItemLockedException("the selected node or his parent is locked", e);
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
|
|
|
@ -254,11 +254,6 @@ public class UserManager {
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
log.warn("error getting folder shared with ", 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 homePath = Utils.getHome(user);
|
||||||
org.gcube.common.storagehub.model.Path workspacePath = Utils.getWorkspacePath(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 workspaceNode = session.getNode(workspacePath.toPath());
|
||||||
Node homeNode = session.getNode(homePath.toPath());
|
Node homeNode = session.getNode(homePath.toPath());
|
||||||
List<Item> workspaceItems = Utils.getItemList(workspaceNode, Excludes.GET_ONLY_CONTENT, null, true, null);
|
List<Item> workspaceItems = Utils.getItemList(workspaceNode, Excludes.GET_ONLY_CONTENT, null, true, null);
|
||||||
trashHandler.removeNodes(session, workspaceItems);
|
trashHandler.removeOnlyNodesContent(session, workspaceItems);
|
||||||
homeNode.remove();
|
homeNode.remove();
|
||||||
} catch (PathNotFoundException e) {
|
} catch (PathNotFoundException e) {
|
||||||
log.warn("{} home dir was already deleted", user);
|
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();
|
session.save();
|
||||||
}catch(StorageHubException she ){
|
}catch(StorageHubException she ){
|
||||||
log.error(she.getErrorMessage(), she);
|
log.error(she.getErrorMessage(), she);
|
||||||
|
|
Loading…
Reference in New Issue