Project

General

Profile

« Previous | Next » 

Revision 1324

Added by Jing Tao over 21 years ago

Add code to update tripes in data set file.

View differences:

src/edu/ucsb/nceas/metacat/CleanupAccessTable.java
66 66
   *                      into the data set doc
67 67
   */
68 68
  private static final String TRIPLESTAG = "//triple";
69
  private static final String IDENTIFIER = "//identifier";
70
  private static final String RELATIONSHIP = "provides access control rules for";
71
  public static final String DELIMITER = "!";
72
  
69 73
  public static void addNewTripleIntoDataset(String accessionNumber, 
70 74
                                             Vector newTripleVector)
71 75
  {
76
    String docidWithoutVersion = null;
77
    int rev = 0;
72 78
    // check parameter
73 79
    if (newTripleVector.size()==0 || accessionNumber == null || 
74 80
        accessionNumber.equals(""))
75 81
    {
76 82
      return;
77 83
    }
84
    
78 85
    DocumentImpl doc = null;
79 86
    try
80 87
    {
88
      docidWithoutVersion = MetaCatUtil.getDocIdFromString(accessionNumber); 
89
      rev = MetaCatUtil.getVersionFromString(accessionNumber);
81 90
      doc = new DocumentImpl(accessionNumber);
82 91
    }
83 92
    catch (Exception e)
......
97 106
      newTripleForDataSet.addTriple(triple);
98 107
    }
99 108
    xmlDoc = addTriplesToTriplesString(newTripleForDataSet, xmlDoc, TRIPLESTAG);
109
    System.out.println("add triple xml file: "+xmlDoc);
110
    // Update accessNumber
111
    Vector oldAccessNumberVector = new Vector();
112
    Vector newAccessNumberVector = new Vector();
113
    oldAccessNumberVector.addElement(accessionNumber);
114
    //increase rev number
115
    rev++;
116
    accessionNumber = docidWithoutVersion + "." + rev;
117
    newAccessNumberVector.addElement(accessionNumber);
118
    xmlDoc = incRevInTriples(xmlDoc, oldAccessNumberVector, 
119
                             newAccessNumberVector, TRIPLESTAG);
100 120
    
101
    System.out.println("add triple xml file: "+xmlDoc);
121
    System.out.println("increate rev xml file: "+xmlDoc);
122
    
102 123
  }
103 124
  
104 125
    /**
......
106 127
   * searches for any triples already in the string and appends the new
107 128
   * ones after the existing ones.  
108 129
   * @param triples the collection of triples to add
130
   * @param dataPackageString the xml document of data set
109 131
   * @param triplesTag the trip tag in xml docment
110 132
   */
111 133
  private static String addTriplesToTriplesString(TripleCollection triples,
......
173 195
    return docString;
174 196
  }
175 197
  
198
  
199
  
200
  private static String incRevInTriples(String xml, Vector oldid, 
201
                                        Vector newid, String triplePath)
202
  {
203
    System.out.println("oldid: " + oldid.toString() + " newid: " + newid.toString());
204
    Document doc = null;
205
    DOMParser parser = new DOMParser();
206
    InputSource in;
207
    StringReader sr;
208
     try
209
    { //parse the wizard created file with existing triples
210
      sr = new StringReader(xml);
211
      in = new InputSource(sr);
212
    }
213
    catch(Exception fnf)
214
    {
215
      fnf.printStackTrace();
216
      return null;
217
    }
218
    try
219
    {
220
      parser.parse(in);
221
      sr.close();
222
    }
223
    catch(Exception e1)
224
    {
225
      System.err.println(e1.toString());
226
    }
227
    
228
    doc = parser.getDocument();
229
    NodeList tripleList = null;
230
    try
231
    {
232
      //find where the triples go in the file
233
      tripleList = XPathAPI.selectNodeList(doc, triplePath);
234
    }
235
    catch(Exception se)
236
    {
237
      System.err.println("incRevInTriples() : parse threw: " + 
238
                         se.toString());
239
    }
240
    
241
    for(int i=0; i<tripleList.getLength(); i++)
242
    {
243
      Node triple = tripleList.item(i);
244
      NodeList children = triple.getChildNodes();
245
      String sub = null;
246
      String rel = null;
247
      String obj = null;
248
      if(children.getLength() > 2)
249
      {
250
        for(int j=0; j<children.getLength(); j++)
251
        {
252
          Node childNode = children.item(j);
253
          String nodename = childNode.getNodeName().trim().toUpperCase();
254
          if(nodename.equals("SUBJECT") || nodename.equals("OBJECT"))
255
          {
256
            String nodeval;
257
            try
258
            {
259
              nodeval = childNode.getFirstChild().getNodeValue().trim();
260
            }
261
            catch(NullPointerException npe)
262
            {
263
              continue;
264
            }
265
            
266
            if(oldid.contains(nodeval.trim()))
267
            {
268
              String newidS = "";
269
              for(int k=0; k<newid.size(); k++)
270
              {
271
                newidS = (String)newid.elementAt(k);
272
                if(nodeval.trim().equals(oldid.elementAt(k)))
273
                {
274
                  break;
275
                }
276
              }
277
              System.out.println("replacing: " + nodeval + " with " + newidS);
278
              childNode.getFirstChild().setNodeValue(newidS);
279
            }
280
          }
281
        }
282
      }
283
    }
284
    
285
    //increase the identifier
286
    NodeList identiferList = null; 
287
    try
288
    {
289
      //find where the triples go in the file
290
      identiferList = XPathAPI.selectNodeList(doc, IDENTIFIER);
291
    }
292
    catch(Exception se)
293
    {
294
      System.err.println("incRevInTriples() : parse threw: " + 
295
                         se.toString());
296
    }
297
    
298
    for(int i=0; i<identiferList.getLength(); i++)
299
    {
300
      Node identifierNode = identiferList.item(i);
301
      String nodeName = identifierNode.getNodeName().trim().toUpperCase();
302
      if (nodeName.equals("IDENTIFIER"))
303
      {
304
        String nodeValue = null;
305
        try
306
        {
307
           nodeValue = identifierNode.getFirstChild().getNodeValue().trim();
308
        }
309
        catch(NullPointerException npe)
310
        {
311
          continue;
312
        }
313
        
314
        if(oldid.contains(nodeValue.trim()))
315
        {
316
           String newidS = "";
317
            for(int k=0; k<newid.size(); k++)
318
            {
319
               newidS = (String)newid.elementAt(k);
320
               if(nodeValue.trim().equals(oldid.elementAt(k)))
321
               {
322
                  break;
323
               }
324
            }
325
            System.out.println("replacing: " + nodeValue + " with " + newidS);
326
            identifierNode.getFirstChild().setNodeValue(newidS);
327
         }
328
      
329
     
330
      }
331
    }
332
    
333
    return PackageUtil.printDoctype(doc) + 
334
           PackageUtil.print(doc.getDocumentElement());
335
  }
336
  
337
 
176 338
  /*
177 339
   *Give a docid to find a package accesssionNumber for it.
178 340
   */
179
   private static String getDataPackageId(String docId)
341
   private static String getDataPackageAccessionNumber(String docId)
180 342
  {
181 343
    String accessNumber = null;
182 344
    String packageIdWithoutVersion = null;
......
203 365
        packageIdWithoutVersion=rs.getString(1);//get data package id
204 366
      }
205 367
      pStmt.close();
206
      
207
      query = "SELECT rev from xml_documents where docid = ?";
208
      pStmt=dbConn.prepareStatement(query);
209
      //bind the value to query
210
      pStmt.setString(1, packageIdWithoutVersion);
211
      //execute the query
212
      pStmt.execute();
213
      rs=pStmt.getResultSet();
214
      //process the result
215
      if (rs.next()) //There are some records for the id in docId fields
216
      {
217
        rev=rs.getString(1);//get data package id
218
      }
219
      pStmt.close();
368
      rev = getRevNumber(packageIdWithoutVersion);
220 369
      accessNumber = packageIdWithoutVersion+"."+rev;
221 370
      MetaCatUtil.debugMessage("DataPackageId: "+accessNumber, 20);
222 371
    }//try
......
244 393
    return accessNumber;
245 394
  }//isDataPackageId()
246 395
  
396
  /**
397
   * Get a dev number from a given docid(without dev)
398
   */
399
  public static String getRevNumber(String docId) 
400
  {
401
    DBConnection dbConn = null;
402
    int serialNumber = -1;
403
    String query = "SELECT rev from xml_documents where docid = ?";
404
    PreparedStatement pStmt = null;
405
    ResultSet rs = null;
406
    String rev = null;
407
    try
408
    {
409
      dbConn=DBConnectionPool.
410
                  getDBConnection("DBQuery.getDataPackageId");
411
      serialNumber=dbConn.getCheckOutSerialNumber();
412
      pStmt=dbConn.prepareStatement(query);
413
      //bind the value to query
414
      pStmt.setString(1, docId);
415
      //execute the query
416
      pStmt.execute();
417
      rs=pStmt.getResultSet();
418
      //process the result
419
      if (rs.next()) //There are some records for the id in docId fields
420
      {
421
        rev=rs.getString(1);//get data package id
422
      }
423
      pStmt.close();
424
    }
425
    catch (SQLException e)
426
    {
427
      System.out.println("the error in getRevNumber: "+e.getMessage());
428
    }
429
    finally
430
    {
431
      try
432
      {
433
        pStmt.close();
434
      }
435
      catch (SQLException ee)
436
      {
437
        System.out.println("the error in getRevNumber2: "+ee.getMessage());
438
      }
439
      finally
440
      {
441
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
442
        return rev;
443
      }
444
    }
445
   
446
  }
447
  /**
448
   * Delete a access control rule which is not in access xml document 
449
   */
450
  public static void deleteAccessRule(String aclDocid, String accessRule)
451
  {
452
    String principal  = null;
453
    String allowType  = null;
454
    String permission = null;
455
    String allowOrder = null;
456
    String [] parseAccessRuleArray = null;
457
     DBConnection dbConn = null;
458
    int serialNumber = -1;
459
    String query = "delete from xml_access where accessfileid = ? " +
460
                   "and principal_name = ? and permission_type = ? "+
461
                   "and permission = ? and permission_order = ?";
462
    PreparedStatement pStmt = null;
463
    String rev = null;
464
    // parse the access rule 
465
    parseAccessRuleArray = parseAccessRule(accessRule);
466
    if ( parseAccessRuleArray == null)
467
    {
468
      System.out.println("couldn't parse docid: "+aclDocid);
469
      return;
470
    }
471
    else
472
    {
473
      principal  = parseAccessRuleArray[0];
474
      System.out.println("principal: "+principal);
475
      allowType  = parseAccessRuleArray[1];
476
      System.out.println("allowType: "+allowType);
477
      permission = parseAccessRuleArray[2];
478
      System.out.println("permission: "+permission);
479
      allowOrder = parseAccessRuleArray[3];
480
      System.out.println("allowOrder: "+allowOrder);
481
    }
482
    
483
    /*try
484
    {
485
      dbConn=DBConnectionPool.
486
                  getDBConnection("DBQuery.getDataPackageId");
487
      serialNumber=dbConn.getCheckOutSerialNumber();
488
      //pStmt=dbConn.prepareStatement(query);
489
      //bind the value to query
490
      //pStmt.setString(1, docId);
491
      //execute the query
492
      //pStmt.execute();
493
      //pStmt.close();
494
    }
495
    catch (SQLException e)
496
    {
497
      System.out.println("the error in getRevNumber: "+e.getMessage());
498
    }
499
    finally
500
    {
501
      try
502
      {
503
        //pStmt.close();
504
      }
505
      catch (SQLException ee)
506
      {
507
        System.out.println("the error in getRevNumber2: "+ee.getMessage());
508
      }
509
      finally
510
      {
511
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
512
       
513
      }
514
    }*/
515
  }
247 516
  
517
  /*
518
   * parse the string and get a array cotains principle name(index 0),
519
   * allow type (index1), permission (index2), and allow order(index, 4)
520
   */
521
  private static String[] parseAccessRule(String accessRule)
522
  {
523
    String principal  = null;
524
    String allowType  = null;
525
    String permission = null;
526
    String allowOrder = null;
527
    String [] result  = new String[4];
528
    int count = 0;
529
    int index = 0;
530
    char delimiter = DELIMITER.charAt(0);
531
    
532
    if (accessRule== null)
533
    {
534
      return result;
535
    }
536
    for (int i=0; i<accessRule.length(); i++)
537
    {
538
      
539
      char charInString = accessRule.charAt(i);
540
      // find a symbol !
541
      if (charInString == delimiter)
542
      {
543
        count ++;
544
      }
545
      else
546
      {
547
        continue;
548
      }
549
      
550
      switch (count)
551
      {
552
        case 1:
553
        {
554
          // get principal
555
          principal = accessRule.substring(0, i);
556
          // remeber the index
557
          index = i;
558
          break;
559
        }
560
        case 2:
561
        {
562
          allowType = accessRule.substring(index+1, i);
563
          index = i;
564
          break;
565
        }
566
        case 3:
567
        {
568
          permission = accessRule.substring(index+1, i);
569
          index = i;
570
          allowOrder = accessRule.substring(index+1, accessRule.length());
571
          break;
572
        }
573
      }//switch
574
    }//for
575
    
576
    // if the string exactly has 3 !, add it to array
577
    if ( count == 3)
578
    {
579
      result[0] = principal;
580
      result[1] = allowType;
581
      result[2] = permission;
582
      result[3] = allowOrder;
583
    }//if
584
    return result;
585
  }
586
  
248 587
  public static void main(String[] agus)
249 588
  {
250 589
    if(agus.length == 0)
......
253 592
      return;
254 593
    }
255 594
    String docID = agus[0];
595
    Vector newTripleVector = new Vector();
596
    
256 597
    try
257 598
    {
258 599
      DBConnectionPool pool = DBConnectionPool.getInstance();
600
      String packagAccessionNumber=getDataPackageAccessionNumber(docID);
259 601
      AccessRulesFromDocument xmlDocument = new AccessRulesFromDocument(docID);
260 602
      Vector rules = xmlDocument.getAccessRuleVector();
261 603
      Vector docid = xmlDocument.getACLObjects();
......
268 610
        if (!rules.contains(rulesFromDB.elementAt(i)))
269 611
        {
270 612
          System.out.println("find a new rule!!!");
613
          deleteAccessRule(docID, (String)rulesFromDB.elementAt(i));
271 614
        }
272 615
      }
273 616
      for (int i = 0; i<docidFromDB.size(); i++)
274 617
      {
275
        System.out.println("docid: "+(String)docidFromDB.elementAt(i));
276
        if (!docid.contains(docidFromDB.elementAt(i)))
618
        String objectId = (String)docidFromDB.elementAt(i);
619
        System.out.println("docid: "+objectId);
620
        if (!docid.contains(objectId))
277 621
        {
622
          String revs = getRevNumber(objectId);
623
          objectId = objectId+"."+revs;
624
          revs = getRevNumber(docID);
625
          docID = docID+"."+revs;
626
          Triple newTriple = new Triple(docID, RELATIONSHIP, objectId);
627
          newTripleVector.add(newTriple);
278 628
          System.out.println("find a new triple");
279 629
          
280 630
        }
281 631
      }
632
      // add new triple vector to xml
633
      addNewTripleIntoDataset(packagAccessionNumber, newTripleVector);
282 634
    }
283 635
    catch(Exception e)
284 636
    {

Also available in: Unified diff