Project

General

Profile

« Previous | Next » 

Revision 1592

Added by Jing Tao about 21 years ago

This class will write triple to db.

View differences:

src/edu/ucsb/nceas/metacat/RelationHandler.java
31 31

  
32 32
public class RelationHandler //implements Runnable
33 33
{
34
  private Thread btThread = null;
35 34
  private DBConnection connection = null;
36 35
  private String docid = null;
37
  MetaCatUtil util = new MetaCatUtil();
38
  // The hashtable's key is String vector, subject parentid , relation ship
39
  // prarent id and object parent id. Element is a vetor too. subject, relation
40
  // and object
41
  private Hashtable parentNodeIdList = new Hashtable();
42
  private static final int SUBJECTINDEX = 0;
43
  private static final int RELATIONINDEX = 1;
44
  private static final int OBJECTINDEX = 2;
36
  private String docType = null;
45 37
  
38
  TripleCollection tripleForPackage = null;
39
   
46 40
  /** 
47 41
   * Constructor for this class.  finds all of the relations to a single xml
48 42
   * document and writes them to the database.  This includes transitive
49 43
   * relations.
50 44
   * @param docid the ID of the XML document to index.
45
   * @param doctype the doctype of this document
46
   * @param conn the db connection
47
   * @param list the triple list
51 48
   */
52
  public RelationHandler(String docid, DBConnection conn)
49
  public RelationHandler(String docid, String doctype, 
50
                        DBConnection conn, TripleCollection list)
53 51
              throws McdbException, SQLException, AccessionNumberException
54 52
  {
55
    System.out.println("docid is: "+docid);
56 53
    this.connection = conn;
57 54
    this.docid = docid;
58
    //putRelations();
55
    this.docType = doctype;
56
    tripleForPackage = list;
59 57
    createRelations();
60 58
  }
61 59
  
......
65 63
  private void createRelations() 
66 64
              throws McdbException, SQLException, AccessionNumberException
67 65
  {
68
    String packagetype = null;
66
    String packagetype = docType;
69 67
    String subject = null;
70 68
    String subjectParentId = null;
71 69
    String subDoctype = null;
......
76 74
    PreparedStatement tstmt = null; // to insert each relation into xml_relation
77 75
   
78 76
    MetaCatUtil.debugMessage("Running relation handler!", 40);
79
    
80

  
81
    /* 
82
     * PSEUDO-CODE ALGORITHM:
83
     * deletes all of the relations with a docid of @docid.
84
     * for each new relation r in xml_nodes
85
     *   put r into xml_relation
86
     */
87

  
88
    DocumentImpl xmldoc = new DocumentImpl(docid,true);
89
    packagetype = (new DocumentImpl(docid,false)).getDoctype();
77
   
90 78
    // first delete the relations for this package document if any
91 79
    deleteRelations(docid);
92
    
93
    
94
    //transfer to documentImpl object to string
95
    String xml = xmldoc.toString();
96
    
97
    //create a tripcollection object
98
    TripleCollection tripleForPackage = new 
99
                                     TripleCollection(new StringReader(xml));
80
 
100 81
    //get the vetor of triples 
101
    Vector tripleList=tripleForPackage.getCollection();
82
    Vector tripleList= new Vector();
83
    //get vector form tripleCollection
84
    if (tripleForPackage != null)
85
    {
86
      tripleList =tripleForPackage.getCollection();
87
    }
102 88
    
103 89
    if (tripleList != null && tripleList.size()>0) 
104 90
    {
......
107 93
                                    "docid,packagetype,subject,subdoctype," +
108 94
                                    "relationship, object, objdoctype) " + 
109 95
                                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
110
      //increase usage count
111
      connection.increaseUsageCount(1);
112
    }
113
    // go through tripe list 
114
    for (int i= 0; i<tripleList.size(); i++)
115
    {
116
      // Get the triple
117
      Triple triple = (Triple)tripleList.elementAt(i);
118
      subject = (new DocumentIdentifier(triple.getSubject())).getIdentifier();
119
      relationship = triple.getRelationship();
120
      object = (new DocumentIdentifier(triple.getObject())).getIdentifier();
96
      
97
      // go through tripe list 
98
      for (int i= 0; i<tripleList.size(); i++)
99
      {
100
        //increase usage count
101
        connection.increaseUsageCount(1);
102
        // Get the triple
103
        Triple triple = (Triple)tripleList.elementAt(i);
104
        MetaCatUtil.debugMessage("Info from triple: ", 30);
105
        MetaCatUtil.debugMessage("subject from triple:"+triple.getSubject(), 30);
106
        MetaCatUtil.debugMessage("relationship from triple:"+triple.getRelationship(),30);
107
        MetaCatUtil.debugMessage("object from triple: "+triple.getObject(),30);
108
        subject = (new DocumentIdentifier(triple.getSubject())).getIdentifier();
109
        relationship = triple.getRelationship();
110
        object = (new DocumentIdentifier(triple.getObject())).getIdentifier();
121 111
     
122
      //put the new relation into xml_relation
123
      MetaCatUtil.debugMessage("Insert into xml_relation table", 30);
124
      tstmt.setString(1, docid);
125
      MetaCatUtil.debugMessage("Insert docid into xml_relation table" + 
112
        //put the new relation into xml_relation
113
        MetaCatUtil.debugMessage("Insert into xml_relation table", 30);
114
        tstmt.setString(1, docid);
115
        MetaCatUtil.debugMessage("Insert docid into xml_relation table" + 
126 116
                                docid, 30);
127
      tstmt.setString(2, packagetype);
128
      tstmt.setString(3, subject);
129
      MetaCatUtil.debugMessage("Insert subject into xml_relation table" + 
117
        tstmt.setString(2, packagetype);
118
        tstmt.setString(3, subject);
119
        MetaCatUtil.debugMessage("Insert subject into xml_relation table" + 
130 120
                               subject, 30);
131
      tstmt.setString(4, subDoctype);
132
      tstmt.setString(5, relationship);
133
      MetaCatUtil.debugMessage("Insert relationship into xml_relation table" + 
121
        tstmt.setString(4, subDoctype);
122
        tstmt.setString(5, relationship);
123
        MetaCatUtil.debugMessage("Insert relationship into xml_relation table" + 
134 124
                                relationship, 30);
135
      tstmt.setString(6, object);
136
      tstmt.setString(7, objDoctype);
137
      tstmt.execute();  
125
        tstmt.setString(6, object);
126
        MetaCatUtil.debugMessage("Insert object into xml_relation table" + 
127
                                object, 30);
128
        tstmt.setString(7, objDoctype);
129
        tstmt.execute();  
138 130
     
139
    }//for
131
      }//for
132
    }//if
140 133
    
141 134
    if ( tstmt != null ) 
142 135
    {
......
145 138
  
146 139
   
147 140
  }
148
  
149
  
150 141
 
151
  
152
  /*
153
   * A method to check if a parentid is already in the paretnIdlist
154
   */
155
   public boolean hasSameId(String parentId)
156
   {
157
     boolean hasSame = false;
158
     if (parentId == null || parentId.equals(""))
159
     {
160
       return hasSame;
161
     }
162
     Enumeration en = parentNodeIdList.keys();
163
     // go throught hashtable
164
     while (en.hasMoreElements())
165
     {
166
       Vector keys = (Vector)en.nextElement();
167
       for (int i=0; i<keys.size(); i++)
168
       {
169
         String obj = (String)keys.elementAt(i);
170
         if ( obj != null && obj.equals(parentId) )
171
         {
172
           hasSame = true;
173
         }//if
174
       }//for
175
     }//while
176
     return hasSame;
177
   }
178
  
179
   /*
180
    * Get a value from parentidlist for a given paretnIdlist
181
    */
182
   public String getValueFromParentIdList(String parentId)
183
   {
184
     String value = null;
185
     int    index = -1; // index in vector keys which has same parentid to 
186
                        // given id
187
     // Check the parameter
188
     if ( parentId == null || parentId.equals("") )
189
     {
190
       return value;
191
     }
192
      
193
     Enumeration en = parentNodeIdList.keys();
194
     // go throught hashtable
195
     while (en.hasMoreElements())
196
     {
197
       Vector keys = (Vector)en.nextElement();
198
       for (int i=0; i<keys.size(); i++)
199
       {
200
         String obj = (String)keys.elementAt(i);
201
         if ( obj != null && obj.equals(parentId) )
202
         {
203
           index = i;
204
         }//if
205
         if (index != -1)
206
         {
207
           // get the element vector to the key
208
           Vector element = (Vector)parentNodeIdList.get(keys);
209
           // get string
210
           value = (String)element.elementAt(index);
211
         }
212
         // reset index
213
         index = -1;
214
       }//for
215
     }//while
216
     MetaCatUtil.debugMessage("The value for given parent id " + parentId +
217
                              "is " + value, 40);
218
     return value;
219
   }
220
   
221
   /*
222
    * A method to remove a entry in hashtable for given parentid
223
    */
224
   private void removeEntryFromParentIdList(String parentId)
225
   {
226
     if (parentId==null || parentId.equals(""))
227
     {
228
       return;
229
     }
230
     
231
     Enumeration en = parentNodeIdList.keys();
232
     // go throught hashtable
233
     while (en.hasMoreElements())
234
     {
235
       Vector keys = (Vector)en.nextElement();
236
       for (int i=0; i<keys.size(); i++)
237
       {
238
         String obj = (String)keys.elementAt(i);
239
         if ( obj != null && obj.equals(parentId) )
240
         {
241
            parentNodeIdList.remove(keys);
242
         }//if
243
       
244
       }//for
245
     }//while
246
   } 
247 142
  /**
248 143
   * Deletes all of the relations with a docid of 'docid'.
249 144
   * @param docid the docid of the package which relations to delete.
......
271 166
   * @param docid the document identifier of the package
272 167
   * @return the document identifier of the access file for that package
273 168
   */
274
  public String getAccessFileID(String docid) throws SQLException
169
  public static String getAccessFileID(String docid) throws SQLException
275 170
  {
276 171
    String aclid = null;
277 172
    PreparedStatement pstmt = null;
......
296 191
    sql.append("))");
297 192
    //System.out.println("new sql script: " + sql.toString());
298 193
    
299
    /*
300
    PreparedStatement pstmt = 
301
      conn.prepareStatement("SELECT docid FROM xml_documents " +
302
                            "WHERE docid in " +
303
                            "(SELECT subject FROM xml_relation " +
304
                            "WHERE docid = ?) " +
305
                            "AND doctype = ?");
306
    pstmt.setString(1, docid);
307
    pstmt.setString(2, MetaCatUtil.getOption("accessdoctype"));
308
    */
309 194
    try
310 195
    {
311 196
      dbConn=DBConnectionPool.

Also available in: Unified diff