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