context-manager/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextTree.java

91 lines
3.0 KiB
Java
Raw Normal View History

2020-12-14 20:32:46 +01:00
package org.gcube.vremanagement.contextmanager.handlers.impl;
2020-12-03 18:13:34 +01:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
2020-12-14 20:32:46 +01:00
import javax.inject.Inject;
2020-12-23 10:42:07 +01:00
import javax.inject.Singleton;
2020-12-14 20:32:46 +01:00
2020-12-23 10:42:07 +01:00
import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException;
2020-12-04 19:57:56 +01:00
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
2020-12-03 18:13:34 +01:00
import org.gcube.vremanagement.contextmanager.model.types.Context;
2020-12-04 19:57:56 +01:00
import org.gcube.vremanagement.contextmanager.model.types.Context.Type;
2020-12-03 18:13:34 +01:00
import org.slf4j.Logger;
2020-12-23 10:42:07 +01:00
@Singleton
public class ContextTree {
2020-12-03 18:13:34 +01:00
2020-12-14 20:32:46 +01:00
@Inject Logger log;
2020-12-03 18:13:34 +01:00
2021-01-04 19:55:29 +01:00
private TreeItem root;
2020-12-03 18:13:34 +01:00
2020-12-04 19:57:56 +01:00
private Map<String, TreeItem> values = new HashMap<>();
2020-12-03 18:13:34 +01:00
2020-12-23 10:42:07 +01:00
public void init() {};
2020-12-03 18:13:34 +01:00
public synchronized TreeItem removeItem(String itemId) {
TreeItem item = values.get(itemId);
2020-12-04 19:57:56 +01:00
if (item.isLeaf()) {
values.remove(itemId);
item.parent.removeChild(item);
return item;
} else
throw new IllegalArgumentException("item is not a leaf");
2020-12-03 18:13:34 +01:00
}
2020-12-23 10:42:07 +01:00
public synchronized TreeItem createItem(String parentId, Context context) throws InvalidContextException, ContextAlreadyExistsException {
if (values.containsKey(context.getId())) throw new ContextAlreadyExistsException("context with id "+context.getId()+" already exist");
2020-12-04 19:57:56 +01:00
log.debug("creating item {} with parentId {}", context, parentId);
2020-12-03 18:13:34 +01:00
TreeItem item;
if (parentId==null) {
2021-01-04 19:55:29 +01:00
if (root!=null) throw new InvalidContextException("root is already set");
2020-12-04 19:57:56 +01:00
if (context.getType()!=Type.INFRASTRUCTURE) throw new InvalidContextException("this context is not a root context");
2020-12-03 18:13:34 +01:00
item = new TreeItem(null, context);
root= item;
} else {
TreeItem parentItem = values.get(parentId);
2021-01-04 19:55:29 +01:00
log.debug("parent id is {} and is present ? {} and values {}", parentId, parentItem!=null, values.keySet());
2020-12-03 18:13:34 +01:00
if (context.getType().getPossibleParent()!=parentItem.getContext().getType())
2021-01-04 19:55:29 +01:00
throw new InvalidContextException("parent not valid");
2020-12-03 18:13:34 +01:00
item = new TreeItem(parentItem, context);
parentItem.addChild(item);
}
values.put(context.getId(), item);
return item;
}
2020-12-04 19:57:56 +01:00
public Context getContext(String id) throws InvalidContextException {
TreeItem item = values.get(id);
if (item==null) throw new InvalidContextException("invalid context "+id);
return item.getContext();
2020-12-03 18:13:34 +01:00
}
public List<String> getContexts(){
2020-12-23 10:42:07 +01:00
return new ArrayList<>(values.keySet());
/*log.debug("searching for contexts");
2020-12-03 18:13:34 +01:00
List<String> toReturn = new ArrayList<>();
String rootString = "/"+root.getContext().getId();
toReturn.add(rootString);
if (!root.isLeaf())
toReturn.addAll(deepSearch(root.getChildren(), rootString));
log.debug("found {} contexts", toReturn.size());
2020-12-23 10:42:07 +01:00
return toReturn;*/
2020-12-03 18:13:34 +01:00
}
2020-12-23 10:42:07 +01:00
/*
2020-12-03 18:13:34 +01:00
private List<String> deepSearch(Set<TreeItem> children, String parentString) {
List<String> toReturn = new ArrayList<>();
for (TreeItem item : children) {
String itemString = String.format("%s/%s", parentString, item.getContext().getId());
toReturn.add(itemString);
if (!item.isLeaf())
toReturn.addAll(deepSearch(item.getChildren(), itemString));
}
return toReturn;
2020-12-23 10:42:07 +01:00
}*/
2020-12-03 18:13:34 +01:00
}