View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.pppm.readerwriter.database;
17  
18  import java.net.URI;
19  import java.util.Map;
20  
21  import org.jamesii.SimSystem;
22  import org.jamesii.core.data.DBConnectionData;
23  import org.jamesii.core.data.model.IModelReader;
24  import org.jamesii.core.model.IModel;
25  import org.jamesii.core.model.symbolic.ISymbolicModel;
26  
27  import p3j.database.IP3MDatabase;
28  import p3j.database.hibernate.P3MDatabase;
29  import p3j.gui.P3J;
30  import p3j.misc.gui.GUI;
31  import p3j.pppm.ProjectionModel;
32  import p3j.pppm.SymbolicProjectionModel;
33  
34  /**
35   * Model reader to access the PPP model database.
36   * 
37   * @author Christina Bohk
38   * @author Roland Ewald
39   */
40  public class PPPMDatabaseReader implements IModelReader {
41  
42    /** The data about the connection to the database. */
43    private final DBConnectionData connData;
44  
45    /** The ID of the projection to be loaded. */
46    private final int projID;
47  
48    /**
49     * Instantiates a new PPPM database reader.
50     * 
51     * @param connectionData
52     *          the connection data
53     * @param projectionID
54     *          the projection id
55     */
56    public PPPMDatabaseReader(DBConnectionData connectionData,
57        Integer projectionID) {
58      connData = connectionData;
59      projID = projectionID;
60    }
61  
62    @Override
63    public ISymbolicModel<?> read(URI ident) {
64  
65      ProjectionModel model = null;
66  
67      try {
68        IP3MDatabase sqlDatabase = new P3MDatabase();
69        sqlDatabase.init(connData, P3J.getInstance().getConfigFile());
70        try {
71          sqlDatabase.open();
72          model = sqlDatabase.getProjectionByID(projID);
73        } catch (Exception ex) {
74          SimSystem.report(ex);
75          return null;
76        }
77        if (model == null) {
78          throw new IllegalArgumentException("PPP Model with ID '" + projID
79              + "' was not found.");
80        }
81      } catch (Throwable t) {
82        GUI.printErrorMessage("Could not load model", t);
83      }
84      return new SymbolicProjectionModel(model);
85    }
86  
87    @Override
88    public IModel read(URI source, Map<String, ?> parameters) {
89      return (IModel) read(source).getAsDataStructure();
90    }
91  
92  }