Project

General

Profile

« Previous | Next » 

Revision 465

Added by berkley over 23 years ago

Added backtrack functionality. Backtracking works by passing a returndoc parameter. There can be more than one. If a document that is hit by a query is not of type returndoc then it searches the database for a related file of type returndoc. If one is found it is displayed, if no relation is found, the original is displayed.

Support was also added for an index of relations. the table xml_relation handles the all of the relation indexing.

View differences:

DBQuery.java
41 41

  
42 42
  private Connection	conn = null;
43 43
  private String	parserName = null;
44

  
44
  private MetaCatUtil util = new MetaCatUtil();
45 45
  /**
46 46
   * the main routine used to test the DBQuery utility.
47 47
   * <p>
......
116 116
    this.parserName = parserName;
117 117
  }
118 118
  
119
  public Hashtable findDocuments(Reader xmlquery, String user, String group)
120
  {
121
    return findDocuments(xmlquery, user, group, null);
122
  }
123
  
119 124
  /** 
120 125
   * routine to search the elements and attributes looking to match query
121 126
   *
122 127
   * @param xmlquery the xml serialization of the query (@see pathquery.dtd)
128
   * @param user the username of the user
129
   * @param group the group of the user
130
   * @param returndoc an array of document types to backtrack against.
123 131
   */
124
  public Hashtable findDocuments(Reader xmlquery, String user, String group)
125
         //throws Exception
132
  public Hashtable findDocuments(Reader xmlquery, String user, String group,
133
                                 String[] returndoc)
126 134
  {
127 135
      Hashtable	 docListResult = new Hashtable();
128 136
      PreparedStatement pstmt;
......
136 144
      String fielddata = null;
137 145
      String relation = null;
138 146
      StringBuffer document = null; 
139

  
147
      Vector returndocVec = new Vector();
148
      
149
      if(returndoc != null)
150
      {//add the returndoc elements to a vector for easier manipulation
151
        for(int i=0; i<returndoc.length; i++)
152
        {
153
          returndocVec.add(new String((String)returndoc[i]));
154
        }
155
      }
156
      
140 157
      try {
141 158
        // Get the XML query and covert it into a SQL statment
142 159
        QuerySpecification qspec = new QuerySpecification(xmlquery, 
......
157 174
          createDate = rs.getString(5);
158 175
          updateDate = rs.getString(6);
159 176
          
177
          if(returndocVec.size() != 0 && !returndocVec.contains(doctype))
178
          { //there are returndocs to match (backtracking can now be performed). 
179
            //System.out.println("olddoctype: " + doctype);
180
            StringBuffer btBuf = new StringBuffer();
181
            btBuf.append("select object from xml_relation where ");
182
            btBuf.append("objdoctype in (");
183
            //build the doctype list for the backtracking sql statement
184
            for(int i=0; i<returndocVec.size(); i++)
185
            {
186
              btBuf.append("'").append((String)returndocVec.get(i)).append("'");
187
              if(i != (returndocVec.size() - 1))
188
              {
189
                btBuf.append(", ");
190
              }
191
            }
192
            btBuf.append(") ");
193
            btBuf.append("and subject like '");
194
            btBuf.append("metacat://docid#").append(docid).append("'");
195
            pstmt = conn.prepareStatement(btBuf.toString());
196
            pstmt.execute();
197
            ResultSet btrs = pstmt.getResultSet();
198
            boolean hasBtRows = btrs.next();
199
            if(hasBtRows)
200
            { //there was a backtrackable document found
201
              DocumentImpl xmldoc = null;
202
              //System.out.println("document found is: " + btrs.getString(1));
203
              metacatURL objURL = new metacatURL(btrs.getString(1));
204
              try
205
              {
206
                xmldoc = new DocumentImpl(conn, objURL.getParam(0)[1]);
207
              }
208
              catch(Exception e)
209
              {
210
                System.out.println("Error getting document: " + e.getMessage());
211
              }
212
              
213
              docid   = xmldoc.getDocID();
214
              docname = xmldoc.getDocname();
215
              doctype = xmldoc.getDoctype();
216
              doctitle = xmldoc.getDocTitle();
217
              createDate = xmldoc.getCreateDate();
218
              updateDate = xmldoc.getUpdateDate();
219
              //System.out.println("docname: " + docname + " doctype: " + doctype + 
220
              //                   " doctitle: " + doctitle + " createdate: " +
221
              //                   createDate + " updatedate: " + updateDate);
222
            }
223
            btrs.close();
224
          }
225
          
160 226
          document = new StringBuffer();
161
          
162
          document.append("<docid>").append(docid).append("</docid>");
163
          if (docname != null) {
164
            document.append("<docname>" + docname + "</docname>");
227
          //System.out.println("packagdoctype: " + util.getOption("packagedoctype"));
228
          if(!doctype.equals(util.getOption("packagedoctype")))
229
          {
230
            document.append("<docid>").append(docid).append("</docid>");
231
            if (docname != null) {
232
              document.append("<docname>" + docname + "</docname>");
233
            }
234
            if (doctype != null) {
235
              document.append("<doctype>" + doctype + "</doctype>");
236
            }
237
            if (doctitle != null) {
238
              document.append("<doctitle>" + doctitle + "</doctitle>");
239
            }
240
            if(createDate != null) {
241
              document.append("<createdate>" + createDate + "</createdate>");
242
            }
243
            if(updateDate != null) {
244
              document.append("<updatedate>" + updateDate + "</updatedate>");
245
            }
246
            // Store the document id and the root node id
247
            docListResult.put(docid,(String)document.toString());
165 248
          }
166
          if (doctype != null) {
167
            document.append("<doctype>" + doctype + "</doctype>");
168
          }
169
          if (doctitle != null) {
170
            document.append("<doctitle>" + doctitle + "</doctitle>");
171
          }
172
          if(createDate != null) {
173
            document.append("<createdate>" + createDate + "</createdate>");
174
          }
175
          if(updateDate != null) {
176
            document.append("<updatedate>" + updateDate + "</updatedate>");
177
          }
178 249

  
179
          // Store the document id and the root node id
180
          docListResult.put(docid,(String)document.toString());
181

  
182 250
          // Advance to the next record in the cursor
183 251
          tableHasRows = rs.next();
184 252
        }
......
187 255
        {
188 256
          Vector extendedFields = new Vector(qspec.getReturnFieldList());
189 257
          Vector results = new Vector();
190
          pstmt = conn.prepareStatement(qspec.printExtendedSQL());
258
          Enumeration keylist = docListResult.keys();
259
          StringBuffer doclist = new StringBuffer();
260
          while(keylist.hasMoreElements())
261
          {
262
            doclist.append("'");
263
            doclist.append((String)keylist.nextElement());
264
            doclist.append("',");
265
          }
266
          doclist.deleteCharAt(doclist.length()-1); //remove the last comma
267
          pstmt = conn.prepareStatement(qspec.printExtendedSQL(
268
                                        doclist.toString()));
191 269
          pstmt.execute();
192 270
          rs = pstmt.getResultSet();
193 271
          tableHasRows = rs.next();
......
219 297
          }
220 298
        }
221 299

  
222
        pstmt = conn.prepareStatement(qspec.printPackageSQL());
223
        pstmt.execute();
224
        rs = pstmt.getResultSet();
225
        tableHasRows = rs.next();
226
        String[][] relations = new String[2000][3];
227
        int relLength=0;
228
        while(tableHasRows)
300
        //this loop adds the relation data to the resultdoc
301
        //this code might be able to be added to the backtracking code above
302
        Enumeration docidkeys = docListResult.keys();
303
        while(docidkeys.hasMoreElements())
229 304
        {
230
          relations[relLength][0] = rs.getString(1).trim();
231
          relations[relLength][1] = rs.getString(2).trim();
232
          relations[relLength][2] = rs.getString(3).trim();
233
          relLength++;
234
          
235
          //this get's direct relations from the data.  i.e. those relations
236
          //where the docid of the current document is in the subject tag.
237
          
238
          docid = rs.getString(1);
239
          metacatURL murl = new metacatURL(docid);
240
          if(murl.getURLType().equals("metacat"))
241
          {//we only want to process metacat urls here.
242
            String[] tempparam = murl.getParam(0);
243
            if(tempparam[0].equals("docid"))
244
            {
245
              docid = tempparam[1]; 
246
              document = new StringBuffer();
247
              document.append("<relation>");
248
              document.append("<relationtype>").append(rs.getString(2));
249
              document.append("</relationtype>");
250
              document.append("<relationdoc>").append(rs.getString(3));
251
              document.append("</relationdoc>");
252
              document.append("</relation>");
253
              tableHasRows = rs.next();
254
              if(docListResult.containsKey(docid))
305
          String docidkey = (String)docidkeys.nextElement();
306
          pstmt = conn.prepareStatement(
307
                  qspec.printRelationSQL("metacat://docid#" + docidkey));
308
          pstmt.execute();
309
          rs = pstmt.getResultSet();
310
          tableHasRows = rs.next();
311
          while(tableHasRows)
312
          {
313
            String sub = rs.getString(1);
314
            String rel = rs.getString(2);
315
            String obj = rs.getString(3);
316
            metacatURL murl = new metacatURL(sub);
317
            if(murl.getURLType().equals("metacat"))
318
            {//we only want to process metacat urls here.
319
              String[] tempparam = murl.getParam(0);
320
              if(tempparam[0].equals("docid") && tempparam[1].equals(docidkey))
255 321
              {
256
                String removedelement = (String)docListResult.remove(docid);
257
                docListResult.put(docid, removedelement + document.toString());
322
                document = new StringBuffer();
323
                document.append("<relation>");
324
                document.append("<relationtype>").append(rel);
325
                document.append("</relationtype>");
326
                document.append("<relationdoc>").append(obj);
327
                document.append("</relationdoc>");
328
                document.append("</relation>");
329
                
330
                String removedelement = (String)docListResult.remove(docidkey);
331
                docListResult.put(docidkey, removedelement + document.toString());
332
                
258 333
              }
259
              else
260
              {
261
                docListResult.put(docid, document.toString()); 
262
              }
263 334
            }
264
            else
265
            {
266
              //throw new Exception("Error in url. The first parameter must " +
267
              //                    "be the docid.");
268
              System.err.println("DBQuery: error in url");
269
            }
335
            tableHasRows = rs.next();
270 336
          }
271 337
        }
272
        
273
        //this loop handles transitive relations. i.e. if two relation tags
274
        //both have the same object then the subject of each are related.
275
        for(int i=0; i<relLength; i++)
276
        {
277
          for(int j=0; j<relLength; j++)
278
          {
279
            if(i != j)
280
            {
281
              if(relations[i][2].equals(relations[j][2]))
282
              {//the objects are the same.
283
                metacatURL murl = new metacatURL(relations[i][0]);
284
                //relations[i][0] contains the docid of document that 
285
                //the new document is related to.
286
                String[] tempparam = murl.getParam(0);
287
                if(tempparam[0].equals("docid"))
288
                {
289
                  docid = tempparam[1]; 
290
                  document = new StringBuffer();
291
                  document.append("<relation>");
292
                  document.append("<relationtype>").append(relations[j][1]);
293
                  document.append("</relationtype>");
294
                  document.append("<relationdoc>").append(relations[j][0]);
295
                  //the relation is to the subject of the new document
296
                  //instead of the object.
297
                  //   direct relation    transitive relation
298
                  // |-----------------|  |-----------------|
299
                  // subject -> (object = object) -> subject
300
                  document.append("</relationdoc>");
301
                  document.append("</relation>");
302
                  if(docListResult.containsKey(docid))
303
                  {
304
                    String removedelement = (String)docListResult.remove(docid);
305
                    docListResult.put(docid, removedelement + 
306
                                      document.toString());
307
                  }
308
                  else
309
                  {
310
                    docListResult.put(docid, document.toString()); 
311
                  }
312
                }
313
              }
314
            }
315
          }
316
        }
317

  
318 338
        pstmt.close();
319 339
      } catch (SQLException e) {
320 340
        System.err.println("Error getting id: " + e.getMessage());
......
695 715

  
696 716
/**
697 717
 * '$Log$
718
 * 'Revision 1.20  2000/09/15 19:52:11  berkley
719
 * 'Added functionality for package specifications.  metacatservlet now contains a new action called getrelateddocument that handles retrieving related documents using the metacatURL specification (metacatURL.java).  DBQuery contains new code in runQuery that embeds relation tags in the returned hashtable describing the documents related to each docid.  querySpecification contains a new method which prints the sql that does the relation query.
720
 * '
698 721
 * 'Revision 1.19  2000/09/12 17:37:07  bojilova
699 722
 * 'added check from "read" permission on "query" and "squery" actions
700 723
 * 'for connected user or for "public" connection

Also available in: Unified diff