View Javadoc

1   package olr.om;
2   
3   import java.sql.Connection;
4   import java.sql.ResultSet;
5   import java.util.ArrayList;
6   import java.util.Date;
7   import java.util.List;
8   
9   import org.apache.log4j.Logger;
10  import org.apache.torque.util.Criteria;
11  
12  /***
13   * The skeleton for this class was autogenerated by Torque on:
14   * 
15   * [Fri Jun 18 10:10:23 CEST 2004]
16   * 
17   * You should add additional methods to this class to meet the application requirements. This class
18   * will only be generated as long as it does not already exist in the output directory.
19   */
20  public class ModelPeer extends olr.om.BaseModelPeer {
21  
22      public static List getAllModels() {
23  
24          List models = new ArrayList();
25  
26          try {
27              final Criteria criteria = new Criteria();
28              criteria.addAscendingOrderByColumn(ModelPeer.ID);
29              models = ModelPeer.doSelect(criteria);
30          } catch (Exception e) {
31              Logger.getLogger(ModelPeer.class).error(e);
32          }
33          return models;
34      }
35  
36      public static void delete(Model model, RdfUser user) throws Exception {
37          Connection connection = null;
38          try {
39              final int[] statementIds = model.getModelStatementIds();
40              connection = DBUtils.getConnection();
41              OmContext.setConnection(connection);
42  
43              ModelStatementPeer.deleteAll(connection, ModelStatementPeer.TABLE_NAME,
44                      ModelStatementPeer.MODEL_ID, model.getId());
45              ModelPeer.doDelete(model.getPrimaryKey(), connection);
46              RdfStatementPeer.delete(statementIds);
47  
48              DBUtils.releaseConnection(connection);
49          } catch (Exception e) {
50              if (connection != null)
51                  DBUtils.releaseConnection(connection, e);
52              throw e;
53          }
54      }
55  
56      public static int addNewModel(String name, RdfUser user, boolean isCourse) throws Exception {
57          Connection connection = null;
58  
59          try {
60              connection = DBUtils.getConnection();
61  
62              final Model model = new Model(name, isCourse);
63              model.save(connection);
64  
65              final RdfResource subject = RdfResourcePeer.createModelResource(model);
66              final RdfResource predicate = RdfResourcePeer.getTypeResource();
67              final RdfResource object = RdfResourcePeer.getModelRootResource();
68  
69              final RdfStatement statement = new RdfStatement(subject, predicate, object);
70              statement.setModified(new Date());
71              statement.save(connection);
72  
73              final ModelStatement modelStatement = new ModelStatement(model, statement);
74              modelStatement.setModified(new Date());
75              modelStatement.save(connection);
76  
77              ModelPeer.createLabel(model, statement.getRdfResourceRelatedBySubject(), model
78                      .getName());
79  
80              if (isCourse)
81                  createCourse(model, name);
82  
83              DBUtils.releaseConnection(connection);
84  
85              return model.getId();
86          } catch (Exception e) {
87              if (connection != null)
88                  DBUtils.releaseConnection(connection, e);
89              throw e;
90          }
91      }
92  
93      private static void createCourse(Model model, String name) throws Exception {
94          final RdfStatement hasCourseStatement = new Course(model);
95          hasCourseStatement.save();
96      }
97  
98      static void createLabel(Model model, RdfResource subject, String label) throws Exception {
99          final RdfLiteral courseLabel = new RdfLiteral(label);
100         courseLabel.save(OmContext.getConnection());
101 
102         final RdfStatement courseLabelStatement = new RdfStatement(subject, RdfResourcePeer
103                 .getLabelResource(), courseLabel);
104 
105         courseLabelStatement.save(OmContext.getConnection());
106         final ModelStatement modelStatement = new ModelStatement(model, courseLabelStatement);
107         modelStatement.save(OmContext.getConnection());
108     }
109 
110     /***
111      * @deprecated @param modelID
112      * @return
113      */
114     public static int getModelOwner(int modelID) {
115         Connection dbConn = null;
116         int usr = 0;
117         try {
118             dbConn = DBUtils.getConnection();
119             String sql = String.valueOf(String.valueOf((new StringBuffer(
120                     "SELECT usr FROM model_statement WHERE model_id=")).append(modelID)));
121             ResultSet rs = dbConn.createStatement().executeQuery(sql);
122             if (rs.next())
123                 try {
124                     usr = rs.getInt("USR");
125                 } catch (Exception e) {
126                     e.printStackTrace();
127                 }
128             rs.close();
129         } catch (Exception e) {
130             e.printStackTrace();
131         }
132         DBUtils.releaseConnection(dbConn);
133         return usr;
134     }
135 }