import org.simantics.db.ReadGraph;
import org.simantics.db.RequestProcessor;
import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.UnaryRead;
import org.simantics.db.common.utils.NameUtils;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.exception.ResourceNotFoundException;
import org.simantics.db.layer0.variable.Variable;
-import org.simantics.db.request.Read;
import org.simantics.scl.reflection.OntologyVersions;
import org.simantics.viewpoint.ontology.ViewpointResource;
import org.slf4j.Logger;
private static final Logger LOGGER = LoggerFactory.getLogger(BrowseContext.class);
public static final boolean DEBUG = false;
- NodeTypeMultiMap<ChildContribution> childContributions = new NodeTypeMultiMap<ChildContribution>();
- NodeTypeMultiMap<ChildContribution> parentContributions = new NodeTypeMultiMap<ChildContribution>();
- OrderedNodeTypeMultiMap<LabelContribution> labelContributions = new OrderedNodeTypeMultiMap<LabelContribution>();
- OrderedNodeTypeMultiMap<ImageContribution> imageContributions = new OrderedNodeTypeMultiMap<ImageContribution>();
- OrderedNodeTypeMultiMap<CheckedStateContribution> checkedStateContributions = new OrderedNodeTypeMultiMap<CheckedStateContribution>();
- OrderedNodeTypeMultiMap<LabelDecorationContribution> labelDecorationContributions = new OrderedNodeTypeMultiMap<LabelDecorationContribution>();
- OrderedNodeTypeMultiMap<ImageDecorationContribution> imageDecorationContributions = new OrderedNodeTypeMultiMap<ImageDecorationContribution>();
- OrderedNodeTypeMultiMap<ModifierContribution> modifierContributions = new OrderedNodeTypeMultiMap<ModifierContribution>();
- OrderedNodeTypeMultiMap<SorterContribution> sorterContributions = new OrderedNodeTypeMultiMap<SorterContribution>();
- OrderedNodeTypeMultiMap<FlatNodeContribution> flatNodeContributions = new OrderedNodeTypeMultiMap<FlatNodeContribution>();
+ NodeTypeMultiMap<ChildContribution> childContributions = new NodeTypeMultiMap<>();
+ NodeTypeMultiMap<ChildContribution> parentContributions = new NodeTypeMultiMap<>();
+ OrderedNodeTypeMultiMap<LabelContribution> labelContributions = new OrderedNodeTypeMultiMap<>();
+ OrderedNodeTypeMultiMap<ImageContribution> imageContributions = new OrderedNodeTypeMultiMap<>();
+ OrderedNodeTypeMultiMap<CheckedStateContribution> checkedStateContributions = new OrderedNodeTypeMultiMap<>();
+ OrderedNodeTypeMultiMap<LabelDecorationContribution> labelDecorationContributions = new OrderedNodeTypeMultiMap<>();
+ OrderedNodeTypeMultiMap<ImageDecorationContribution> imageDecorationContributions = new OrderedNodeTypeMultiMap<>();
+ OrderedNodeTypeMultiMap<ModifierContribution> modifierContributions = new OrderedNodeTypeMultiMap<>();
+ OrderedNodeTypeMultiMap<SorterContribution> sorterContributions = new OrderedNodeTypeMultiMap<>();
+ OrderedNodeTypeMultiMap<FlatNodeContribution> flatNodeContributions = new OrderedNodeTypeMultiMap<>();
OrderedNodeTypeMultiMap<TooltipContribution> tooltipContributions = new OrderedNodeTypeMultiMap<>();
private final String[] uris;
- private BrowseContext(String[] uris) {
+ private BrowseContext(String... uris) {
if (uris == null)
throw new NullPointerException("null URIs");
this.uris = uris;
return defaultContext;
}
+ private static BrowseContext loadCachedVisuals(ReadGraph g, Resource visualsContributionResource) throws DatabaseException, InvalidContribution {
+ try {
+ return g.syncRequest(new ResourceRead<BrowseContext>(visualsContributionResource) {
+ @Override
+ public BrowseContext perform(ReadGraph graph) throws DatabaseException {
+ try {
+ BrowseContext bc = new BrowseContext();
+ VisualsContribution.load(g, visualsContributionResource,
+ bc.labelContributions,
+ bc.imageContributions,
+ bc.checkedStateContributions,
+ bc.labelDecorationContributions,
+ bc.imageDecorationContributions,
+ bc.modifierContributions,
+ bc.sorterContributions,
+ bc.flatNodeContributions,
+ bc.tooltipContributions
+ );
+ return bc;
+ } catch (InvalidContribution e) {
+ throw new DatabaseException(e);
+ }
+ }
+ }, TransientCacheAsyncListener.instance());
+ } catch (DatabaseException e) {
+ Throwable c = e.getCause();
+ if (c instanceof InvalidContribution)
+ throw (InvalidContribution) c;
+ throw e;
+ }
+ }
+
/**
* Creates a new BrowseContext for the given Collection of {@link Resource}s.
*
for(Resource childContributionResource :
g.getObjects(browseContextResource, vr.BrowseContext_HasChildContribution)) {
try {
- ChildContribution contribution = ChildContribution.create(g, childContributionResource);
+ ChildContribution contribution = ChildContribution.createCached(g, childContributionResource);
browseContext.childContributions.put(contribution.getParentNodeType(), contribution);
browseContext.parentContributions.put(contribution.getChildNodeType(), contribution);
} catch (DatabaseException e) {
for(Resource visualsContributionResource :
g.getObjects(browseContextResource, vr.BrowseContext_HasVisualsContribution)) {
try {
- VisualsContribution.load(g, visualsContributionResource,
- browseContext.labelContributions,
- browseContext.imageContributions,
- browseContext.checkedStateContributions,
- browseContext.labelDecorationContributions,
- browseContext.imageDecorationContributions,
- browseContext.modifierContributions,
- browseContext.sorterContributions,
- browseContext.flatNodeContributions,
- browseContext.tooltipContributions
- );
+ BrowseContext visuals = loadCachedVisuals(g, visualsContributionResource);
+ visuals.labelContributions.appendTo(browseContext.labelContributions);
+ visuals.imageContributions.appendTo(browseContext.imageContributions);
+ visuals.checkedStateContributions.appendTo(browseContext.checkedStateContributions);
+ visuals.labelDecorationContributions.appendTo(browseContext.labelDecorationContributions);
+ visuals.imageDecorationContributions.appendTo(browseContext.imageDecorationContributions);
+ visuals.modifierContributions.appendTo(browseContext.modifierContributions);
+ visuals.sorterContributions.appendTo(browseContext.sorterContributions);
+ visuals.flatNodeContributions.appendTo(browseContext.flatNodeContributions);
+ visuals.tooltipContributions.appendTo(browseContext.tooltipContributions);
} catch (DatabaseException e) {
LOGGER.error("Failed to load visuals contribution " + NameUtils.getSafeName(g, visualsContributionResource), e);
}
}
public static Set<String> getBrowseContextClosure(RequestProcessor processor, final Set<String> browseContexts) throws DatabaseException {
- return processor.syncRequest(new Read<Set<String>>() {
+ return processor.syncRequest(new UnaryRead<Set<String>, Set<String>>(browseContexts) {
@Override
public Set<String> perform(ReadGraph graph) throws DatabaseException {
- Collection<Resource> browseContextResources = new ArrayList<Resource>(browseContexts.size());
- for (String browseContext : browseContexts) {
+ Collection<Resource> browseContextResources = new ArrayList<>(parameter.size());
+ for (String browseContext : parameter) {
try {
browseContextResources.add(graph.getResource(browseContext));
} catch (ResourceNotFoundException e) {
}
}
Collection<Resource> allBrowseContextResources = BrowseContext.findSubcontexts(graph, browseContextResources);
- Set<String> result = new HashSet<String>();
+ Set<String> result = new HashSet<>();
for (Resource r : allBrowseContextResources)
result.add(graph.getURI(r));
- return result;
+ return result;
}
- });
+ }, TransientCacheAsyncListener.instance());
}
- public static Collection<Resource> findSubcontexts(ReadGraph g,
+ public static Collection<Resource> findSubcontexts(ReadGraph g, Collection<Resource> browseContexts)
+ throws DatabaseException {
+ return g.syncRequest(new UnaryRead<Collection<Resource>, Collection<Resource>>(browseContexts) {
+ @Override
+ public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
+ return findSubcontexts0(graph, parameter);
+ }
+ }, TransientCacheAsyncListener.instance());
+ }
+
+ private static Collection<Resource> findSubcontexts0(ReadGraph g,
Collection<Resource> browseContexts) throws DatabaseException {
ViewpointResource vr = ViewpointResource.getInstance(g);
- HashSet<Resource> result = new HashSet<Resource>(browseContexts);
- ArrayList<Resource> stack = new ArrayList<Resource>(browseContexts);
+ HashSet<Resource> result = new HashSet<>(browseContexts);
+ ArrayList<Resource> stack = new ArrayList<>(browseContexts);
while(!stack.isEmpty()) {
Resource cur = stack.remove(stack.size()-1);
for(Resource sc : g.getObjects(cur, vr.BrowseContext_Includes))
}
return result;
}
-
+
/**
* Finds the possible children of the given {@link NodeContext} parameter.
*