]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.project/src/org/simantics/project/adapters/ProjectAdapter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / adapters / ProjectAdapter.java
diff --git a/bundles/org.simantics.project/src/org/simantics/project/adapters/ProjectAdapter.java b/bundles/org.simantics.project/src/org/simantics/project/adapters/ProjectAdapter.java
new file mode 100644 (file)
index 0000000..441f913
--- /dev/null
@@ -0,0 +1,65 @@
+/*******************************************************************************\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.adapters;\r
+\r
+import java.util.Collection;\r
+\r
+import org.eclipse.core.runtime.CoreException;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.adaption.SimpleAdapter;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.db.exception.ValidationException;\r
+import org.simantics.project.IProject;\r
+import org.simantics.project.ProjectFeatures;\r
+import org.simantics.project.exception.ProjectException;\r
+import org.simantics.project.features.IProjectFeature;\r
+import org.simantics.project.features.registry.IProjectFeatureExtension;\r
+import org.simantics.project.impl.Project;\r
+import org.simantics.project.ontology.ProjectResource;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ProjectAdapter extends SimpleAdapter<IProject> {\r
+\r
+    @Override\r
+    public IProject adapt(ReadGraph g, Resource resource) throws DatabaseException {\r
+        ProjectResource PROJ = ProjectResource.getInstance(g);\r
+        if (!g.isInstanceOf(resource, PROJ.Project))\r
+            throw new ValidationException("Cannot adapt resource " + NameUtils.getSafeName(g, resource) + " to IProject. Resource is not an instance of " + ProjectResource.URIs.Project);\r
+\r
+        Project p = new Project(g.getSession(), resource);\r
+        //System.out.println("NEW PROJECT: " + p + " (  " + System.identityHashCode(p) + ")");\r
+        try {\r
+            Collection<IProjectFeatureExtension> sorted = ProjectFeatures.getTopologicallySortedFeatures(g, p.get());\r
+            loadFeatures(g, p, sorted);\r
+            return p;\r
+        } catch (ProjectException e) {\r
+            throw new ValidationException("project feature loading failed", e);\r
+        }\r
+    }\r
+\r
+    private void loadFeatures(ReadGraph g, Project p, Collection<IProjectFeatureExtension> sortedFeatures) throws ProjectException, ValidationException, ServiceException {\r
+        for (IProjectFeatureExtension feature : sortedFeatures) {\r
+            try {\r
+                IProjectFeature pf = feature.newInstance();\r
+                p.addFeature(pf);\r
+            } catch (CoreException e) {\r
+                throw new ValidationException(e);\r
+            }\r
+        }\r
+    }\r
+\r
+}\r