Project

General

Profile

« Previous | Next » 

Revision 1577

Added by Jing Tao over 21 years ago

Using triple rather than query to get relation.

View differences:

src/edu/ucsb/nceas/metacat/RelationHandler.java
20 20
 
21 21
package edu.ucsb.nceas.metacat;
22 22

  
23
import edu.ucsb.nceas.morpho.datapackage.Triple;
24
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
25

  
26
import java.io.StringReader;
23 27
import java.sql.*;
24 28
import java.util.*;
25 29
import java.net.*;
26 30

  
31

  
27 32
public class RelationHandler //implements Runnable
28 33
{
29 34
  private Thread btThread = null;
......
47 52
  public RelationHandler(String docid, DBConnection conn)
48 53
              throws McdbException, SQLException, AccessionNumberException
49 54
  {
55
    System.out.println("docid is: "+docid);
50 56
    this.connection = conn;
51 57
    this.docid = docid;
52
    putRelations();
58
    //putRelations();
59
    createRelations();
53 60
  }
54 61
  
55
  /**
62
   /**
56 63
   * insert the relations specified in the triples into xml_relation table
57 64
   */ 
58
  private void putRelations() 
65
  private void createRelations() 
59 66
              throws McdbException, SQLException, AccessionNumberException
60 67
  {
61 68
    String packagetype = null;
......
65 72
    String relationship = null;
66 73
    String relationshipParentId = null;
67 74
    String object = null;
68
    String objectParentId = null;
69 75
    String objDoctype = null;
70
    PreparedStatement pstmt = null; // to get the relations from xml_nodes
71 76
    PreparedStatement tstmt = null; // to insert each relation into xml_relation
72 77
   
73 78
    MetaCatUtil.debugMessage("Running relation handler!", 40);
......
80 85
     *   put r into xml_relation
81 86
     */
82 87

  
83
    //DocumentImpl xmldoc = new DocumentImpl(conn,docid,false);
88
    DocumentImpl xmldoc = new DocumentImpl(docid,true);
84 89
    packagetype = (new DocumentImpl(docid,false)).getDoctype();
85

  
86 90
    // first delete the relations for this package document if any
87 91
    deleteRelations(docid);
88

  
89
    // to put the new relations get them out of xml_nodes
90
    pstmt = connection.prepareStatement
91
                                (QuerySpecification.printPackageSQL(docid));
92
   
93
    //increase usage cont
94
    connection.increaseUsageCount(1);
95
    pstmt.execute();
96
    ResultSet rs = pstmt.getResultSet();
97
    boolean hasmorerows = rs.next();
98
   
99
    while(hasmorerows) {
100
      Vector parentId = new Vector();
101
      Vector sbjReObj = new Vector();
102
      boolean mergeSubject = false;
103
      boolean mergeRelation = false;
104
      boolean mergerObject = false;
105
      subject = rs.getString(1);
106
      subjectParentId = rs.getString(2);
107
      relationship = rs.getString(3);
108
      relationshipParentId = rs.getString(4);
109
      object = rs.getString(5);
110
      objectParentId = rs.getString(6);
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));
100
    //get the vetor of triples 
101
    Vector tripleList=tripleForPackage.getCollection();
102
    
103
    if (tripleList != null && tripleList.size()>0) 
104
    {
111 105
      
112
     
113
      MetaCatUtil.debugMessage("oringal subjectparentid: " + 
114
                                                subjectParentId, 40);
115
      MetaCatUtil.debugMessage("oringinal subject: "+subject, 40);
116
      MetaCatUtil.debugMessage("oringal relation parentid: "+ 
117
                                relationshipParentId, 40);
118
      MetaCatUtil.debugMessage("oringinal relationship: "+relationship, 40);
119
      MetaCatUtil.debugMessage("oringal objectparentid: "+ 
120
                                objectParentId, 40);
121
      MetaCatUtil.debugMessage("oringinal object: "+object, 40);
122
      
123
      if ( hasSameId(subjectParentId) )
124
      {
125
        // subject was splited and need to conbined
126
        String prefixSubject = getValueFromParentIdList(subjectParentId);
127
        if (prefixSubject !=null && !prefixSubject.equals(subject))
128
        {
129
          MetaCatUtil.debugMessage("merger subject: "+prefixSubject + 
130
                                    subject, 40);
131
          subject = prefixSubject + subject;
132
          mergeSubject = true;
133
        }
134
      }
135
      
136
      if ( hasSameId(relationshipParentId) )
137
      {
138
        // relationship need to be combined
139
        String relationPrefix = getValueFromParentIdList(relationshipParentId);
140
        if (relationPrefix != null && !relationPrefix.equals(relationship))
141
        {
142
          MetaCatUtil.debugMessage("merge relation: "+ relationPrefix + 
143
                                    relationship, 40);
144
          relationship = relationPrefix + relationship;
145
          mergeRelation = true;
146
        }
147
      }
148
      
149
      if ( hasSameId(objectParentId) )
150
      {
151
        String objectPrefix = getValueFromParentIdList(objectParentId);
152
        if (objectPrefix != null && !objectPrefix.equals(object))
153
        {
154
          MetaCatUtil.debugMessage("merge object: "+ objectPrefix + object, 40);
155
          object = objectPrefix + object;
156
          mergerObject = true;
157
        }
158
      }
159
      
160
      if (mergeSubject)
161
      {
162
        removeEntryFromParentIdList(subjectParentId);
163
      }
164
      if (mergeRelation)
165
      {
166
        removeEntryFromParentIdList(relationshipParentId);
167
      }
168
      if (mergerObject)
169
      {
170
        removeEntryFromParentIdList(objectParentId);
171
      }
172
      // Store the parentids into parentId vector
173
      parentId.addElement(subjectParentId);
174
      parentId.addElement(relationshipParentId);
175
      parentId.addElement(objectParentId);
176
      // Store the value into sbjRelObj vector
177
      sbjReObj.addElement(subject);
178
      sbjReObj.addElement(relationship);
179
      sbjReObj.addElement(object);
180
      // Store the parent id and value into hashtable
181
      parentNodeIdList.put(parentId, sbjReObj);
182
      hasmorerows = rs.next();
183
    }
184
    Enumeration en = parentNodeIdList.elements();
185
    if (en.hasMoreElements()) {
186
      tstmt = connection.prepareStatement("INSERT INTO xml_relation (" +
106
       tstmt = connection.prepareStatement("INSERT INTO xml_relation (" +
187 107
                                    "docid,packagetype,subject,subdoctype," +
188 108
                                    "relationship, object, objdoctype) " + 
189 109
                                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
190 110
      //increase usage count
191 111
      connection.increaseUsageCount(1);
192 112
    }
193
 
194
    while (en.hasMoreElements())
113
    // go through tripe list 
114
    for (int i= 0; i<tripleList.size(); i++)
195 115
    {
196
      Vector values = (Vector)en.nextElement();
197
      // cut out the revision number for subject and object
198
      subject = (new DocumentIdentifier((String)values.elementAt(SUBJECTINDEX)))
199
                                                        .getIdentifier();
200
      relationship = (String)values.elementAt(RELATIONINDEX);
201
      object = (new DocumentIdentifier((String)values.elementAt(OBJECTINDEX)))
202
                                                      .getIdentifier();
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();
203 121
     
204
      //subDoctype and objDoctype are N/A
205
      subDoctype = null;
206
      objDoctype = null;
207 122
      //put the new relation into xml_relation
208 123
      MetaCatUtil.debugMessage("Insert into xml_relation table", 30);
209 124
      tstmt.setString(1, docid);
......
219 134
                                relationship, 30);
220 135
      tstmt.setString(6, object);
221 136
      tstmt.setString(7, objDoctype);
222
      tstmt.execute(); 
223
    }
224
    if ( tstmt != null ) {
137
      tstmt.execute();  
138
     
139
    }//for
140
    
141
    if ( tstmt != null ) 
142
    {
225 143
      tstmt.close();
226 144
    }
227
    pstmt.close();
228
    connection.commit();
145
  
146
   
229 147
  }
230 148
  
149
  
150
 
151
  
231 152
  /*
232 153
   * A method to check if a parentid is already in the paretnIdlist
233 154
   */

Also available in: Unified diff