]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.project/src/org/simantics/project/Projects.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / Projects.java
diff --git a/bundles/org.simantics.project/src/org/simantics/project/Projects.java b/bundles/org.simantics.project/src/org/simantics/project/Projects.java
new file mode 100644 (file)
index 0000000..574a448
--- /dev/null
@@ -0,0 +1,286 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.project;\r
+\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.Queries;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.impl.EntityRemover;\r
+import org.simantics.db.layer0.util.RemoverUtil;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.project.features.IProjectFeature;\r
+import org.simantics.project.features.registry.GroupReference;\r
+import org.simantics.project.internal.ProjectPolicy;\r
+import org.simantics.project.internal.SafeName;\r
+import org.simantics.project.ontology.ProjectResource;\r
+\r
+/**\r
+ * Utilities for project life-cycle management and configuration in a Simantics\r
+ * graph database.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class Projects {\r
+\r
+    /**\r
+     * @param processor\r
+     * @param project\r
+     * @return\r
+     * @throws DatabaseException\r
+     */\r
+    public static String getName(RequestProcessor processor, IProject project) throws DatabaseException {\r
+        return processor.syncRequest(new SafeName(project.get()));\r
+    }\r
+\r
+    /**\r
+     * @param graph\r
+     * @param name\r
+     * @return\r
+     * @throws DatabaseException\r
+     */\r
+    public static Resource createProject(WriteGraph graph, String name) throws DatabaseException {\r
+        Resource root = graph.getResource("http://Projects");\r
+\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        ProjectResource PROJ = ProjectResource.getInstance(graph);\r
+\r
+        Resource project = graph.newResource();\r
+        graph.claim(project, L0.InstanceOf, null, PROJ.Project);\r
+        graph.claim(project, L0.PartOf, root);\r
+        graph.claimLiteral(project, L0.HasName, name);\r
+\r
+        return project;\r
+    }\r
+\r
+    /**\r
+     * Creates a new project into the database with the specified name and\r
+     * specified features.\r
+     * \r
+     * @param graph writable graph for creating the project\r
+     * @param name name of the new project\r
+     * @param features the features to attach to the new project\r
+     * @return the resource of the new project\r
+     * @throws DatabaseException\r
+     */\r
+    public static Resource createProject(WriteGraph graph, String name, Collection<GroupReference> features) throws DatabaseException {\r
+        // Create the new project instance.\r
+        Resource project = createProject(graph, name);\r
+        setProjectInstalledGroups(graph, project, features);\r
+        return project;\r
+    }\r
+\r
+    /**\r
+     * @param graph\r
+     * @param project\r
+     * @return map of versionid to feature spec resource\r
+     * @throws DatabaseException\r
+     */\r
+    public static Map<String, Resource> getInstalledFeatures(ReadGraph graph, Resource project) throws DatabaseException {\r
+        ProjectResource PROJ = ProjectResource.getInstance(graph);\r
+\r
+        if (ProjectPolicy.TRACE_PROJECT_MANAGEMENT)\r
+            System.out.println("Looking for installed groups in project '" + NameUtils.getSafeName(graph, project)+ "'");\r
+\r
+        Map<String, Resource> result = new HashMap<String, Resource>();\r
+        // Remove previous project feature references\r
+        for (Resource featureSpec : graph.getObjects(project, PROJ.HasFeature)) {\r
+               Resource group = graph.getSingleObject(featureSpec, PROJ.HasGroupId);\r
+            String groupId = graph.getPossibleValue(group, Bindings.STRING);\r
+            // Re-use existing HasFeature definitions if possible.\r
+            if (groupId == null) continue;\r
+            if (ProjectPolicy.TRACE_PROJECT_MANAGEMENT)\r
+                System.out.println("\t+ found existing feature group definition '" + NameUtils.getSafeName(graph, group) + "'");\r
+            result.put(groupId, featureSpec);            \r
+        }\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * @param graph write transaction handle\r
+     * @param project the project to modify\r
+     * @param features the features\r
+     * @return\r
+     * @throws DatabaseException\r
+     */\r
+    public static Resource setProjectInstalledGroups(WriteGraph graph, Resource project, Collection<GroupReference> groups) throws DatabaseException {\r
+        if (ProjectPolicy.TRACE_PROJECT_MANAGEMENT)\r
+            System.out.println("Setting installed groups for project '" + NameUtils.getSafeName(graph, project) + "' to " + groups);\r
+\r
+        Set<String> groupStringsToAdd = new HashSet<String>();\r
+        for (GroupReference ref : groups)\r
+            groupStringsToAdd.add(ref.toString());\r
+\r
+        Map<String, Resource> existing = getInstalledFeatures(graph, project);\r
+        Set<Resource> specsToRemove = new HashSet<Resource>();\r
+\r
+        for (Map.Entry<String, Resource> entry : existing.entrySet()) {\r
+            // Re-use existing HasFeature definitions if possible.\r
+            if (groupStringsToAdd.remove(entry.getKey())) {\r
+                if (ProjectPolicy.TRACE_PROJECT_MANAGEMENT)\r
+                    System.out.println("\t= reusing existing definition: " + entry.getKey());\r
+                continue;\r
+            }\r
+\r
+            if (ProjectPolicy.TRACE_PROJECT_MANAGEMENT)\r
+                System.out.println("\t- marking for removal: " + entry.getKey());\r
+            specsToRemove.add(entry.getValue());\r
+        }\r
+\r
+        for (Resource groupToRemove : specsToRemove) {\r
+            uninstallGroup(graph, project, groupToRemove);\r
+        }\r
+\r
+        // Install the specified features to the project.\r
+        for (String groupString : groupStringsToAdd) {\r
+            installGroup(graph, project, groupString);\r
+        }\r
+\r
+        return project;\r
+    }\r
+\r
+    /**\r
+     * @param graph write transaction handle\r
+     * @param project the project to install the group id to\r
+     * @param groupId the group id to install\r
+     * @return the new group resource\r
+     * @throws DatabaseException\r
+     */\r
+    public static Resource installGroup(WriteGraph graph, Resource project, String groupId) throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        ProjectResource PROJ = ProjectResource.getInstance(graph);\r
+\r
+        if (ProjectPolicy.TRACE_PROJECT_MANAGEMENT)\r
+            System.out.println("+ Installing group '" + groupId+ "' to project '" + NameUtils.getSafeName(graph, project) + "'");\r
+\r
+        Resource groupIdRes = graph.newResource();\r
+        graph.claim(groupIdRes, L0.InstanceOf, null, L0.String);\r
+        graph.claimValue(groupIdRes, groupId);\r
+\r
+        Resource isRequiredRes = graph.newResource();\r
+        graph.claim(isRequiredRes, L0.InstanceOf, null, L0.Boolean);\r
+        graph.claimValue(isRequiredRes, true);\r
+\r
+        Resource featureSpec = graph.newResource();\r
+        graph.claim(featureSpec, L0.InstanceOf, null, PROJ.FeatureSpec);\r
+        graph.claim(project, PROJ.HasFeature, featureSpec);\r
+        graph.claim(featureSpec, PROJ.HasGroupId, groupIdRes);\r
+        graph.claim(featureSpec, PROJ.IsRequired, isRequiredRes);\r
+\r
+        return groupIdRes;\r
+    }\r
+\r
+    /**\r
+     * @param graph write transaction handle\r
+     * @param project the project to uninstall the group id from\r
+     * @param groupId the group id to uninstall\r
+     * @return <code>true</code> if successfully uninstalled, <code>false</code>\r
+     *         if group id not found\r
+     * @throws DatabaseException\r
+     */\r
+    public static boolean uninstallGroup(WriteGraph graph, Resource project, String groupId) throws DatabaseException {\r
+        ProjectResource PROJ = ProjectResource.getInstance(graph);\r
+\r
+        if (ProjectPolicy.TRACE_PROJECT_MANAGEMENT)\r
+            System.out.println("- Uninstalling group '" + groupId+ "' from project '" + NameUtils.getSafeName(graph, project) + "'");\r
+\r
+        for (Resource featureSpec : graph.getObjects(project, PROJ.HasFeature)) {\r
+               Resource group = graph.getSingleObject(featureSpec, PROJ.HasGroupId); \r
+               String existingGroup = graph.getPossibleValue(group, Bindings.STRING);\r
+            // Re-use existing HasFeature definitions if possible.\r
+            if (groupId.equals(existingGroup)) {\r
+                if (ProjectPolicy.TRACE_PROJECT_MANAGEMENT)\r
+                    System.out.println("\t - found it, removing");\r
+                graph.deny(featureSpec, PROJ.HasGroupId, group);\r
+                EntityRemover.remove(graph, group, false);\r
+                return true;\r
+            }\r
+            graph.deny(project, PROJ.HasFeature, featureSpec);\r
+            EntityRemover.remove(graph, featureSpec, false);\r
+        }\r
+\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * @param graph write transaction handle\r
+     * @param project the project to uninstall the group id from\r
+     * @param featureSpec the feature specification to uninstall\r
+     * @return <code>true</code> if successfully uninstalled, <code>false</code>\r
+     *         if group id not found\r
+     * @throws DatabaseException\r
+     */\r
+    public static boolean uninstallGroup(WriteGraph graph, Resource project, Resource featureSpec) throws DatabaseException {\r
+        ProjectResource PROJ = ProjectResource.getInstance(graph);\r
+\r
+        if (ProjectPolicy.TRACE_PROJECT_MANAGEMENT)\r
+            System.out.println("- Uninstalling group '" + NameUtils.getSafeName(graph, featureSpec) + "' from project '" + NameUtils.getSafeName(graph, project) + "'");\r
+\r
+        Resource groupId = graph.getPossibleObject(featureSpec, PROJ.HasGroupId);\r
+        if (groupId!=null) {\r
+            graph.deny(featureSpec, PROJ.HasGroupId, groupId);\r
+            EntityRemover.remove(graph, groupId, false);\r
+            return true;\r
+        }\r
+        \r
+        if (graph.hasStatement(project, PROJ.HasFeature, featureSpec)) {\r
+            graph.deny(project, PROJ.HasFeature, featureSpec);\r
+            EntityRemover.remove(graph, featureSpec, false);\r
+            return true;\r
+        }\r
+        \r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * Tries to load the specified project from a database.\r
+     * \r
+     * <p>\r
+     * After this method completes, the project knows all its project features\r
+     * (see {@link IProjectFeature}). The list of features should be available\r
+     * through {@link IProject#getFeatures()}.\r
+     * </p>\r
+     * \r
+     * @param graph readable graph for loading the project\r
+     * @param project the project resource to load\r
+     * @param activate <code>true</code> to invoke <code>onActivated</code> for\r
+     *        all {@link IProjectLifecycle}'s of this project.\r
+     * @return the loaded project.\r
+     */\r
+    public static IProject loadProject(RequestProcessor processor, Resource project) throws DatabaseException {\r
+        IProject p = processor.syncRequest( Queries.adapt(project, IProject.class, false, true) );\r
+        return p;\r
+    }\r
+\r
+    /**\r
+     * Destroys the specified project from the database.\r
+     * \r
+     * @param g writable graph for deleting the project\r
+     * @param project the project to destroy\r
+     * @throws DatabaseException\r
+     */\r
+    public static void deleteProject(WriteGraph g, Resource project) throws DatabaseException {\r
+        // NOTE: this will throw ServiceException if adapters are not initialized!\r
+        RemoverUtil.remove(g, project);\r
+    }\r
+\r
+}\r