View Javadoc

1   /*
2    *  $Id: Course.java,v 1.13 2004/07/25 19:10:09 roku Exp $ 
3    *
4    *  Copyright 2004 University of Hannover
5    *
6    *  Licensed under the Apache License, Version 2.0 (the "License");
7    *  you may not use this file except in compliance with the License.
8    *  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package olr.om;
20  
21  import java.sql.ResultSet;
22  
23  import olr.rdf.Definitions;
24  import olr.rdf.OLR3Definitions;
25  
26  import org.apache.torque.TorqueException;
27  
28  /***
29   * This is an artificial entity. 
30   * It does not exists as a relation in the database.
31   * A coursee is defined as a statement about a model
32   * with the predciate "hasCourse".
33   */
34  public class Course extends RdfStatement
35  {
36      private Model model;
37      private String course;
38      private String name;
39      
40      Course(Model model) throws Exception
41      {
42          this.setRdfUser(OmContext.getUser()); 
43          this.setModel(model);
44          this.setRes(IS_OBJECT_RESOURCE_TRUE);
45      }
46      
47      Course(ResultSet rs)
48      {
49          try
50          {
51              setModel(new Model());
52              model.setId(rs.getInt("modelID"));
53              course = rs.getString("course");
54              name = rs.getString("name");
55          }
56          catch(Exception e)
57          {
58              e.printStackTrace();
59          }
60      }
61  
62      private void setModel(Model model)
63      {
64          this.model = model;
65      }
66      
67      public Model getModel()
68      {
69          return model;
70      }
71  
72      public String getCourse()
73      {
74          return course;
75      }
76  
77      public String getName()
78      {
79          return name;
80      }
81  
82      public void setName(String name)
83      {
84          this.name = name;
85      }
86  
87      public String getRelativeUrl() {
88          return getCourse().substring(getCourse().indexOf("#")+1);
89      }    
90      
91      /***
92       * Creates all statements needed to describe/attach a course to a model.
93       */
94      public void save() throws TorqueException 
95      {
96          try {
97              this.setRdfResourceRelatedBySubject(RdfResourcePeer.selectElseInsert(Definitions.HOME_NS, OLR3Definitions.OLR3_MODEL_NAME+getModel().getId()));
98              this.setRdfResourceRelatedByPredicate(RdfResourcePeer.getHasCourseResource());
99              this.setRdfResourceRelatedByObjResource(RdfResourcePeer.selectElseInsert(Definitions.HOME_NS, String.valueOf(DBUtils.getInstanceId())));
100             super.save(OmContext.getConnection());
101             
102             ModelStatement modelStatement = new ModelStatement(getModel(), this);
103             modelStatement.save(OmContext.getConnection());
104             
105             final RdfStatement courseTypeStatement = new RdfStatement(
106                     this.getRdfResourceRelatedByObjResource(),
107                     RdfResourcePeer.getTypeResource(),
108                     RdfResourcePeer.getCourseResource());
109             
110             courseTypeStatement.save(OmContext.getConnection());
111             
112             modelStatement = new ModelStatement(getModel(), courseTypeStatement);
113             modelStatement.save(OmContext.getConnection());
114             
115             ModelPeer.createLabel(getModel(), this.getRdfResourceRelatedByObjResource(), getModel().getName());
116         }
117         catch(Exception e) 
118         {
119             throw new TorqueException(e);
120         }
121     }
122 }