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 james.SimSystem;
19  import james.core.data.DBConnectionData;
20  import james.core.data.model.IModelReader;
21  import james.core.model.IModel;
22  import james.core.model.symbolic.ISymbolicModel;
23  
24  import java.net.URI;
25  import java.util.Map;
26  
27  import p3j.database.IP3MDatabase;
28  import p3j.database.hibernate.P3MDatabase;
29  import p3j.misc.gui.GUI;
30  import p3j.pppm.ProjectionModel;
31  import p3j.pppm.SymbolicProjectionModel;
32  
33  /**
34   * Model reader to access the PPP model database.
35   * 
36   * @author Christina Bohk
37   * @author Roland Ewald
38   */
39  public class PPPMDatabaseReader implements IModelReader {
40  
41    /** The data about the connection to the database. */
42    private final DBConnectionData connData;
43  
44    /** The ID of the projection to be loaded. */
45    private final int projID;
46  
47    /**
48     * Instantiates a new PPPM database reader.
49     * 
50     * @param connectionData
51     *          the connection data
52     * @param projectionID
53     *          the projection id
54     */
55    public PPPMDatabaseReader(DBConnectionData connectionData,
56        Integer projectionID) {
57      connData = connectionData;
58      projID = projectionID;
59    }
60  
61    @Override
62    public ISymbolicModel<?> read(URI ident) {
63  
64      ProjectionModel model = null;
65  
66      try {
67        IP3MDatabase sqlDatabase = new P3MDatabase();
68        sqlDatabase.init(connData);
69        try {
70          sqlDatabase.open();
71          model = sqlDatabase.getProjectionByID(projID);
72        } catch (Exception ex) {
73          SimSystem.report(ex);
74          return null;
75        }
76        if (model == null) {
77          throw new IllegalArgumentException("PPP Model with ID '" + projID
78              + "' was not found.");
79        }
80      } catch (Throwable t) {
81        GUI.printErrorMessage("Could not load model", t);
82      }
83      return new SymbolicProjectionModel(model);
84    }
85  
86    @Override
87    public IModel read(URI source, Map<String, ?> parameters) {
88      return (IModel) read(source).getAsDataStructure();
89    }
90  
91  }