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