]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.project.adapters;\r
13 \r
14 import java.util.Collection;\r
15 \r
16 import org.eclipse.core.runtime.CoreException;\r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.common.adaption.SimpleAdapter;\r
20 import org.simantics.db.common.utils.NameUtils;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.exception.ServiceException;\r
23 import org.simantics.db.exception.ValidationException;\r
24 import org.simantics.project.IProject;\r
25 import org.simantics.project.ProjectFeatures;\r
26 import org.simantics.project.exception.ProjectException;\r
27 import org.simantics.project.features.IProjectFeature;\r
28 import org.simantics.project.features.registry.IProjectFeatureExtension;\r
29 import org.simantics.project.impl.Project;\r
30 import org.simantics.project.ontology.ProjectResource;\r
31 \r
32 /**\r
33  * @author Tuukka Lehtonen\r
34  */\r
35 public class ProjectAdapter extends SimpleAdapter<IProject> {\r
36 \r
37     @Override\r
38     public IProject adapt(ReadGraph g, Resource resource) throws DatabaseException {\r
39         ProjectResource PROJ = ProjectResource.getInstance(g);\r
40         if (!g.isInstanceOf(resource, PROJ.Project))\r
41             throw new ValidationException("Cannot adapt resource " + NameUtils.getSafeName(g, resource) + " to IProject. Resource is not an instance of " + ProjectResource.URIs.Project);\r
42 \r
43         Project p = new Project(g.getSession(), resource);\r
44         //System.out.println("NEW PROJECT: " + p + " (  " + System.identityHashCode(p) + ")");\r
45         try {\r
46             Collection<IProjectFeatureExtension> sorted = ProjectFeatures.getTopologicallySortedFeatures(g, p.get());\r
47             loadFeatures(g, p, sorted);\r
48             return p;\r
49         } catch (ProjectException e) {\r
50             throw new ValidationException("project feature loading failed", e);\r
51         }\r
52     }\r
53 \r
54     private void loadFeatures(ReadGraph g, Project p, Collection<IProjectFeatureExtension> sortedFeatures) throws ProjectException, ValidationException, ServiceException {\r
55         for (IProjectFeatureExtension feature : sortedFeatures) {\r
56             try {\r
57                 IProjectFeature pf = feature.newInstance();\r
58                 p.addFeature(pf);\r
59             } catch (CoreException e) {\r
60                 throw new ValidationException(e);\r
61             }\r
62         }\r
63     }\r
64 \r
65 }\r