]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/SingleActiveModel.java
Merge "Allow loading databoard serialized files with type adapting" into release...
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / SingleActiveModel.java
1 package org.simantics.db.layer0.request;
2
3 import java.util.Collection;
4
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.db.common.request.ResourceRead;
8 import org.simantics.db.common.utils.NameUtils;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.exception.NoSingleResultException;
11
12 /**
13  * Returns the active model or null if no single active model is available.
14  */
15 public class SingleActiveModel extends ResourceRead<Resource> {
16
17     public SingleActiveModel(Resource project) {
18         super(project);
19     }
20
21     @Override
22     public Resource perform(ReadGraph graph) throws DatabaseException {
23         Collection<Resource> actives = graph.syncRequest(new ActiveModels(resource));
24         if (actives.size() == 1)
25             return actives.iterator().next();
26
27         if (actives.size() == 0) 
28                 throw new NoSingleResultException("There are no active models.", actives.size()); 
29
30         throw new NoSingleResultException("There are many active models: " + NameUtils.getSafeName(graph, actives), actives.size()); 
31         
32     }
33
34 }