Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that handles the SAX XML events as they
4
 *             are generated from XML documents
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Matt Jones, Jivka Bojilova
8
 *    Release: @release@
9
 *
10
 *   '$Author: tao $'
11
 *     '$Date: 2003-02-26 09:48:42 -0800 (Wed, 26 Feb 2003) $'
12
 * '$Revision: 1423 $'
13
 *
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
 */
28

    
29
package edu.ucsb.nceas.metacat;
30

    
31
import java.sql.*;
32
import java.io.StringReader;
33
import java.util.Stack;
34
import java.util.Vector;
35
import java.util.Hashtable;
36
import java.util.Enumeration;
37
import java.util.EmptyStackException;
38

    
39
import org.xml.sax.Attributes;
40
import org.xml.sax.SAXException;
41
import org.xml.sax.SAXParseException;
42
import org.xml.sax.ext.DeclHandler;
43
import org.xml.sax.ext.LexicalHandler;
44
import org.xml.sax.helpers.DefaultHandler;
45

    
46
/**
47
 * A database aware Class implementing callback bethods for the SAX parser to
48
 * call when processing the XML stream and generating events
49
 */
50
public class EmlSAXHandler extends DBSAXHandler implements 
51
                                                      AccessControlInterface
52
{
53
   private Vector allowRules = new Vector();
54
   private Vector denyRules = new Vector();
55
   private String documentId = null;
56
   private Vector subDocumentIdList = new Vector();
57
   private boolean processTopLeverAccess = false;
58
   private boolean processAdditionalAccess = false;
59
   private AccessSection accessObject= null;
60
   private AccessRule accessRule = null;
61
   private Vector accessObjectList = new Vector(); // store every access rule
62
   private Hashtable topLevelAccessControlMap = new Hashtable();
63
   private Hashtable additionalAccessControlMap = new Hashtable();
64
   private Vector describesId = new Vector(); // store the ids in
65
                                      //additionalmetadata/describes
66
   private Stack subTreeInfoStack = new Stack();
67
   private Vector subTreeList = new Vector();// store the final subtree
68
 
69
 
70
   // Constant
71
   private static final String DESCRIBES = "describes";
72
   private static final String ADDITIONALMETADATA = "additionalMetadata";
73
   private static final String ORDER = "order";
74
   private static final String ID ="id";
75
   private static final String REFERENCES = "references";
76
      
77
   /** Construct an instance of the handler class
78
    *
79
    * @param conn the JDBC connection to which information is written
80
    * @param action - "INSERT" or "UPDATE"
81
    * @param docid to be inserted or updated into JDBC connection
82
    * @param user the user connected to MetaCat servlet and owns the document
83
    * @param groups the groups to which user belongs
84
    * @param pub flag for public "read" access on document
85
    * @param serverCode the serverid from xml_replication on which this document
86
    *        resides.
87
    *
88
    */
89
   public EmlSAXHandler(DBConnection conn, String action, String docid,
90
                      String user, String[] groups, String pub, int serverCode)
91
   {
92
     super(conn, action, docid, user, groups, pub, serverCode);
93
   }
94
   
95
    /** Construct an instance of the handler class
96
    * In this constructor, user can specify the version need to upadate
97
    *
98
    * @param conn the JDBC connection to which information is written
99
    * @param action - "INSERT" or "UPDATE"
100
    * @param docid to be inserted or updated into JDBC connection
101
    * @param revision, the user specified the revision need to be update
102
    * @param user the user connected to MetaCat servlet and owns the document
103
    * @param groups the groups to which user belongs
104
    * @param pub flag for public "read" access on document
105
    * @param serverCode the serverid from xml_replication on which this document
106
    *        resides.
107
    *
108
    */
109
   public EmlSAXHandler(DBConnection conn, String action, String docid,
110
     String revision, String user, String[] groups, String pub, int serverCode)
111
   {
112
     super(conn, action, docid, revision, user, groups, pub, serverCode);
113
   }
114
   
115
   /** SAX Handler that is called at the start of each XML element */
116
   public void startElement(String uri, String localName,
117
                            String qName, Attributes atts)
118
               throws SAXException 
119
  {
120
      // for element <eml:eml...> qname is "eml:eml", local name is "eml"            
121
     // for element <acl....> both qname and local name is "eml"
122
     // uri is namesapce
123
     MetaCatUtil.debugMessage("Start ELEMENT(qName) " + qName, 50);
124
     MetaCatUtil.debugMessage("Start ELEMENT(localName) " + localName, 50);
125
     MetaCatUtil.debugMessage("Start ELEMENT(uri) " + uri, 50);
126
     
127
     
128
     DBSAXNode parentNode = null;
129
     DBSAXNode currentNode = null;
130

    
131
     // Get a reference to the parent node for the id
132
     try 
133
     {
134
       parentNode = (DBSAXNode)nodeStack.peek();
135
     } 
136
     catch (EmptyStackException e) 
137
     {
138
       parentNode = null;
139
     }
140
     
141
     // If hit a text node, we need write this text for current's parent node
142
     // This will happend if the element is mixted
143
     if (hitTextNode && parentNode != null)
144
     {
145
       // write the textbuffer into db for parent node.
146
        endNodeId = writeTextForDBSAXNode(textBuffer, parentNode);
147
        // rest hitTextNode
148
        hitTextNode =false;
149
        // reset textbuffer
150
        textBuffer = null;
151
        textBuffer = new StringBuffer();
152
       
153
     }
154
     
155
  
156
     // Document representation that points to the root document node
157
     if (atFirstElement) 
158
     {
159
       atFirstElement = false;
160
       // If no DOCTYPE declaration: docname = root element
161
       // doctype = root element name or name space
162
       if (docname == null) 
163
       {
164
         docname = localName;
165
         // if uri isn't null doctype = uri(namespace)
166
         // othewise root element
167
         if (uri != null && !(uri.trim()).equals(""))
168
         {
169
           doctype = uri;
170
         }
171
         else
172
         {
173
           doctype = docname;
174
         }
175
         MetaCatUtil.debugMessage("DOCNAME-a: " + docname, 30);
176
         MetaCatUtil.debugMessage("DOCTYPE-a: " + doctype, 30);
177
       } 
178
       else if (doctype == null) 
179
       {
180
         // because docname is not null and it is declared in dtd
181
         // so could not be in schema, no namespace
182
         doctype = docname;
183
         MetaCatUtil.debugMessage("DOCTYPE-b: " + doctype, 30);
184
       }
185
       rootNode.writeNodename(docname);
186
       try 
187
       {
188
         // for validated XML Documents store a reference to XML DB Catalog
189
         // Because this is select statement and it needn't to roll back if
190
         // insert document action fialed.
191
         // In order to decrease DBConnection usage count, we get a new
192
         // dbconnection from pool
193
         String catalogid = null;
194
         DBConnection dbConn = null;
195
         int serialNumber = -1;
196

    
197
         if ( systemid != null ) 
198
         {
199
           try
200
           {
201
            // Get dbconnection
202
            dbConn=DBConnectionPool.getDBConnection
203
                                          ("DBSAXHandler.startElement");
204
            serialNumber=dbConn.getCheckOutSerialNumber();
205

    
206
            Statement stmt = dbConn.createStatement();
207
            ResultSet rs = stmt.executeQuery(
208
                          "SELECT catalog_id FROM xml_catalog " +
209
                          "WHERE entry_type = 'DTD' " +
210
                          "AND public_id = '" + doctype + "'");
211
            boolean hasRow = rs.next();
212
            if ( hasRow ) {
213
              catalogid = rs.getString(1);
214
            }
215
            stmt.close();
216
           }//try
217
           finally
218
           {
219
             // Return dbconnection
220
             DBConnectionPool.returnDBConnection(dbConn, serialNumber);
221
           }//finally
222
         }
223

    
224
         //create documentImpl object by the constructor which can specify
225
         //the revision
226
         currentDocument = new DocumentImpl(connection, rootNode.getNodeID(),
227
                               docname, doctype, docid, revision, action, user,
228
                               this.pub, catalogid, this.serverCode);
229

    
230

    
231
       } 
232
       catch (Exception ane) 
233
       {
234
         throw (new SAXException("Error in DBSaxHandler.startElement " +
235
                                 action, ane));
236
       }
237
     }
238

    
239
     // Create the current node representation
240
     currentNode = new DBSAXNode(connection, qName, localName, parentNode,
241
                                 currentDocument.getRootNodeID(),docid,
242
                                 currentDocument.getDoctype());
243
     // Use a local variable to store the element node id
244
     // If this element is a start point of subtree(section), it will be stored
245
     // otherwise, it will be discated
246
     long startNodeId = currentNode.getNodeID();
247
     // Add all of the namespaces
248
     String prefix;
249
     String nsuri;
250
     Enumeration prefixes = namespaces.keys();
251
     while ( prefixes.hasMoreElements() ) 
252
     {
253
       prefix = (String)prefixes.nextElement();
254
       nsuri = (String)namespaces.get(prefix);
255
       endNodeId = currentNode.setNamespace(prefix, nsuri, docid);
256
     }
257
     namespaces = null;
258
     namespaces = new Hashtable();
259

    
260
     // Add all of the attributes
261
     for (int i=0; i<atts.getLength(); i++) 
262
     {
263
       String attributeName = atts.getQName(i);
264
       String attributeValue = atts.getValue(i);
265
       endNodeId = 
266
                currentNode.setAttribute(attributeName, attributeValue, docid);
267
       
268
       // To handle name space and schema location if the attribute name is
269
       // xsi:schemaLocation. If the name space is in not in catalog table
270
       // it will be regeistered.
271
       if (attributeName != null && 
272
           attributeName.indexOf(MetaCatServlet.SCHEMALOCATIONKEYWORD) != -1)
273
       {
274
         SchemaLocationResolver resolver = 
275
                                  new SchemaLocationResolver(attributeValue);
276
         resolver.resolveNameSpace();
277
         
278
       }
279
       else if (attributeName !=null && attributeName.equals(ID))
280
       {
281
         // handle subtree info
282
         SubTree subTress = new SubTree();
283
         // set sub tree id
284
         subTress.setSubTreeId(attributeValue);
285
         // set sub tree sstart lement name
286
         subTress.setStartElementName(currentNode.getTagName());
287
         // set start node number
288
         subTress.setStartNodeId(startNodeId);
289
         // add to stack, but it didn't get end node id yet
290
         subTreeInfoStack.push(subTress);
291
         
292
       }
293
     }
294
   
295
     // handle access stuff
296
     if (localName.equals(ACCESS))
297
     {
298
       // if it is in addtionalmetacat
299
       if (parentNode.getTagName() == ADDITIONALMETADATA)
300
       {
301
         processAdditionalAccess = true;
302
        
303
       }
304
       else
305
       {
306
         processTopLeverAccess = true;
307
        
308
       }
309
       // create access object 
310
        accessObject = new AccessSection();
311
         // set permission order
312
       String permOrder = currentNode.getAttribute(ORDER);
313
       accessObject.setPermissionOrder(permOrder);
314
       // set access id
315
       String accessId = currentNode.getAttribute(ID);
316
       accessObject.setAccessSectionId(accessId);
317
       
318
     }
319
     // Set up a access rule for allow
320
     else if (parentNode.getTagName() != null && 
321
       (parentNode.getTagName()).equals(ACCESS) && localName.equals(ALLOW))
322
     {
323
      
324
       accessRule = new AccessRule(); 
325
      
326
       //set permission type "allow"
327
       accessRule.setPermissionType(ALLOW);
328
      
329
     }
330
     // set up an access rule for den
331
     else if (parentNode.getTagName() != null 
332
       && (parentNode.getTagName()).equals(ACCESS) && localName.equals(DENY))
333
     {
334
       accessRule = new AccessRule();
335
       //set permission type "allow"
336
       accessRule.setPermissionType(DENY);
337
     }
338
     // refernce access, then set up access object (setcion) null
339
     else if (parentNode.getTagName() != null 
340
           && (parentNode.getTagName()).equals(ACCESS) 
341
           && localName.equals(REFERENCES))
342
     {
343
       accessObject = null;
344
     }
345
   
346
     // Add the node to the stack, so that any text data can be
347
     // added as it is encountered
348
     nodeStack.push(currentNode);
349
     // Add the node to the vector used by thread for writing XML Index
350
     nodeIndex.addElement(currentNode);
351
    
352

    
353
  }
354
  
355
   
356
  /** SAX Handler that is called at the end of each XML element */
357
   public void endElement(String uri, String localName,
358
                          String qName) throws SAXException 
359
  {
360
     MetaCatUtil.debugMessage("End ELEMENT " + qName, 50);
361

    
362
     // Get the node from the stack
363
     DBSAXNode currentNode = (DBSAXNode)nodeStack.pop();
364
     String currentTag = currentNode.getTagName();
365
     
366
    
367
     // If before the end element, the parser hit text nodes and store them
368
     // into the buffer, write the buffer to data base. The reason we put
369
     // write database here is for xerces some time split text node
370
     if (hitTextNode)
371
     {
372
        // get access value
373
        String data = null;
374
        // add principal
375
       if (currentTag.equals(PRINCIPAL) && accessRule != null) 
376
       {
377
          data = (textBuffer.toString()).trim();
378
          accessRule.addPrincipal(data);
379

    
380
       } 
381
       else if (currentTag.equals(PERMISSION) && accessRule != null) 
382
       {
383
         data = (textBuffer.toString()).trim();
384
         // we conbine different a permission into one value
385
         int permission = accessRule.getPermission();
386
         // add permision
387
         if ( data.toUpperCase().equals("READ") ) 
388
         {
389
           permission = permission | READ;
390
         } 
391
         else if ( data.toUpperCase().equals("WRITE") ) 
392
         {
393
           permission = permission | WRITE;
394
         } 
395
         else if ( data.toUpperCase().equals("CHANGEPERMISSION")) 
396
         {
397
           permission = permission | CHMOD;
398
         } 
399
         else if ( data.toUpperCase().equals("ALL") ) 
400
         {
401
          permission = permission | ALL;
402
         }
403
         accessRule.setPermission(permission);
404
       }
405
       // put additionalmetadata/describes into vector
406
       else if (currentTag.equals(DESCRIBES))
407
       {
408
          data = (textBuffer.toString()).trim();
409
          describesId.add(data);
410
       }
411
       else if (currentTag.equals(REFERENCES) && processTopLeverAccess)
412
       {
413
         // get reference 
414
         data = (textBuffer.toString()).trim();
415
         // insert docid and reference id into toplevlerAccess
416
         topLevelAccessControlMap.put(docid, data);
417
         
418
       }
419
       else if (currentTag.equals(REFERENCES) && processAdditionalAccess)
420
       {
421
       
422
          // get reference 
423
          data = (textBuffer.toString()).trim();
424
          // put describeids and reference into additional access mapping
425
          for ( int i=0; i<describesId.size(); i++)
426
          {
427
            String subId = (String)describesId.elementAt(i);
428
            MetaCatUtil.debugMessage("sub id in seting additional mapping: "
429
                                    +subId, 25);
430
            MetaCatUtil.debugMessage("refrence id in setting additional mapping: "
431
                                    +data, 25);
432
            if (subId != null)
433
            {
434
              additionalAccessControlMap.put(subId, data);
435
            }
436
          }
437
          
438
       }
439
       // write text to db
440
       MetaCatUtil.debugMessage("Write text into DB in End Element", 50);
441
       endNodeId = writeTextForDBSAXNode(textBuffer, currentNode);
442
     }//if
443
     
444
     //set hitText false
445
     hitTextNode = false;
446
     // reset textbuff
447
     textBuffer = null;
448
     textBuffer = new StringBuffer();
449
     
450
     // hand sub stree stuff
451
     if (!subTreeInfoStack.empty())
452
     {
453
       SubTree tree = (SubTree)subTreeInfoStack.peek();// get last subtree
454
       if (tree != null && tree.getStartElementName() != null && 
455
         (tree.getStartElementName()).equals(currentTag))
456
       {
457
         // find the end of sub tree and set the end node id
458
         tree.setEndNodeId(endNodeId);
459
         // add the subtree into the final store palace
460
         subTreeList.add(tree);
461
         // get rid of it from stack
462
         subTreeInfoStack.pop();
463
       }//if
464
     }//if
465

    
466
     // access stuff
467
     if (currentTag.equals(ALLOW) || currentTag.equals(DENY))
468
     {
469
       // finish parser a ccess rule and  assign it to new one
470
       AccessRule newRule = accessRule;
471
       //add the new rule to access section object
472
       accessObject.addAccessRule(newRule);
473
       // reset access rule
474
       accessRule = null;
475
     }
476
     else if (currentTag.equals(ACCESS))
477
     {
478
       // finish parse a access setction and assign it to new one
479
       AccessSection newAccessObject = accessObject;
480
       if (newAccessObject != null)
481
       {
482
        // add the accessSection into a vector to store it
483
        accessObjectList.add(newAccessObject);
484
        if (processTopLeverAccess)
485
        {
486
          // top level access control will handle whole document -docid
487
          topLevelAccessControlMap.put(docid, newAccessObject);
488
          // reset processtopleveraccess tag
489
          
490
        }//if
491
        else if (processAdditionalAccess)
492
        {
493
          // for additional control
494
          // put everything in describes value and access object into hash
495
          for ( int i=0; i<describesId.size(); i++)
496
          {
497
            String subId = (String)describesId.elementAt(i);
498
            if (subId != null)
499
            {
500
              additionalAccessControlMap.put(subId, newAccessObject);
501
            }//if
502
          }//for
503
         
504
        }//if
505
       }//if
506
       //reset access section object
507
       accessObject = null;
508
       // reset flag
509
       processAdditionalAccess =false;
510
       processTopLeverAccess =false;
511
     }
512
     else if (currentTag.equals(ADDITIONALMETADATA))
513
     {
514
        //reset describesId
515
        describesId = null;
516
        describesId = new Vector();
517
     }
518
   }
519
   
520
   /** SAX Handler that receives notification of end of the document */
521
   public void endDocument() throws SAXException 
522
   {
523
     MetaCatUtil.debugMessage("end Document", 50);
524
     // write access rule to db
525
     writeAccessRuleToDB();
526
     // Starting new thread for writing XML Index.
527
     // It calls the run method of the thread.
528
     try 
529
     {
530
       xmlIndex.start();
531
     } 
532
     catch (NullPointerException e) 
533
     {
534
       xmlIndex = null;
535
       throw new
536
       SAXException("Problem with starting thread for writing XML Index. " +
537
                    e.getMessage());
538
     }
539
   }
540
   
541
  /* The method to write all access rule intodb */
542
  private void writeAccessRuleToDB() throws SAXException
543
  {
544
    //Delete old permssion
545
    deletePermissionsInAccessTable(docid);
546
    //write top leve access rule
547
    writeTopLevelAccessRuleToDB();
548
    //write additional access rule
549
    writeAddtionalAccessRuleToDB();
550
  }//writeAccessRuleToDB
551
   
552
  /* The method to write top level access rule into db. */
553
  private void writeTopLevelAccessRuleToDB() throws SAXException
554
  {
555
    
556
    // for top document level
557
    Object accessSection = topLevelAccessControlMap.get(docid);
558
    boolean top = true;
559
    String subSectionId = null;
560
    // if accessSection is not null and isnot reference
561
    if( accessSection != null && (accessSection instanceof AccessSection) )
562
    {
563
       AccessSection accessObj = (AccessSection)accessSection;
564
       writeGivenAccessRuleIntoDB(accessObj, top, subSectionId);
565
    }//if
566
    else
567
    {
568
      // this is a reference and go trough the vector which contains all
569
      // access object
570
      String referenceId = (String) accessSection;
571
      MetaCatUtil.debugMessage("referered id for top access: "+ 
572
                               referenceId, 25);
573
      for (int i=0; i<accessObjectList.size(); i++)
574
      {
575
        AccessSection accessObj = (AccessSection)accessObjectList.elementAt(i);
576
        String accessObjId = accessObj.getAccessSectionId();
577
        if (referenceId != null && accessObj != null &&
578
            referenceId.equals(accessObjId))
579
        {
580
          writeGivenAccessRuleIntoDB(accessObj, top, subSectionId);
581
        }
582
      }
583
      
584
    }
585
    
586
  }//writeTopLevelAccessRuletoDB
587
   
588
   /* The method to write addtional access rule into db. */
589
  private void writeAddtionalAccessRuleToDB() throws SAXException
590
  {
591
    
592
     PreparedStatement pstmt = null;
593
     // additional access rule
594
     Enumeration en = additionalAccessControlMap.keys();
595
     boolean topLevel =false;
596
   
597
     while(en.hasMoreElements())
598
    {
599
       try
600
       {
601
         // Get subsection id
602
          String subSectionId = (String)en.nextElement();
603
          MetaCatUtil.debugMessage("sub section id in additional access mapping"
604
                                   +"(go through: "+ subSectionId);
605
          
606
          if (subSectionId == null)
607
          {
608
            // if id is null ignore the following lines
609
            continue;
610
          }
611
          // Get AccessSection Object
612
          Object accessSection = additionalAccessControlMap.get(subSectionId);
613
          if (accessSection == null)
614
          {
615
            // if accesssection is null ignore the following lines
616
            continue;
617
          }
618
          else if ( accessSection instanceof AccessSection )
619
          {
620
            // it is not reference
621
            AccessSection accessObj = (AccessSection)accessSection;
622
            writeGivenAccessRuleIntoDB(accessObj, topLevel, subSectionId);
623
          }
624
          else
625
          {
626
            //this is reference
627
            // this is a reference and go trough the vector which contains all
628
            // access object
629
            String referenceId = (String) accessSection;
630
            MetaCatUtil.debugMessage("referered id for additional access "+
631
                                     "mapping(go through): "+ referenceId, 25);
632
            for (int i=0; i<accessObjectList.size(); i++)
633
            {
634
              AccessSection accessObj = 
635
                                (AccessSection)accessObjectList.elementAt(i);
636
              String accessObjId = accessObj.getAccessSectionId();
637
              MetaCatUtil.debugMessage("access obj id in the list(go through): "
638
                                        + accessObjId, 25);
639
              if (referenceId != null && accessObj != null &&
640
                  referenceId.equals(accessObjId))
641
              {
642
                writeGivenAccessRuleIntoDB(accessObj, topLevel, subSectionId);
643
              }//if
644
           }//for
645
      
646
         }//else
647
       }//try
648
       catch (Exception e)
649
       {
650
         
651
         MetaCatUtil.debugMessage("error in EmlSAXHandler.writeAddtionalAccess"
652
                                   + ": "+e.getMessage(), 30);
653
         throw new SAXException(e.getMessage());
654
       }
655
    }//while
656
  }//writeAccessRuletoDB
657
  
658
  /* Write a gaven access rule into db*/
659
  private void writeGivenAccessRuleIntoDB(AccessSection accessSection, 
660
                                         boolean topLevel, String subSectionId) 
661
                                         throws SAXException
662
  {
663
     if (accessSection == null)
664
     {
665
       throw new SAXException("The access object is null");
666
     }
667
     
668
      String permOrder = accessSection.getPermissionOrder();
669
      String sql = null;
670
      PreparedStatement pstmt = null;
671
      if (topLevel)
672
      {
673
        sql = "INSERT INTO xml_access (docid, principal_name, permission, "+
674
                 "perm_type, perm_order, accessfileid) VALUES " +
675
                 " (?, ?, ?, ?, ?, ?)";
676
      }
677
      else
678
      {
679
        sql ="INSERT INTO xml_access (docid,principal_name, "+ 
680
             "permission, perm_type, perm_order, accessfileid, subtreeid, "+
681
             " startnodeid, endnodeid) VALUES" +
682
             " (?, ?, ?, ?, ?, ?, ?, ?, ?)";
683
      }
684
      try 
685
      {
686
     
687
        pstmt = connection.prepareStatement(sql);
688
        // Increase DBConnection usage count
689
        connection.increaseUsageCount(1);
690
        // Bind the values to the query
691
        pstmt.setString(1, docid);
692
        MetaCatUtil.debugMessage("Docid in accesstable: "+ docid, 25);
693
        pstmt.setString(6, docid);
694
        MetaCatUtil.debugMessage("Accessfileid in accesstable: "+ docid, 25);
695
        pstmt.setString(5, permOrder);
696
        MetaCatUtil.debugMessage("PermOder in accesstable: "+ permOrder, 25);
697
        // if it is not top level, set subsection id
698
        if (!topLevel)
699
        {
700
          long startNodeId = 0;
701
          long endNodeId = 0;
702
          // for subtree should specify the
703
          if (subSectionId == null)
704
          {
705
            throw new SAXException("The subsection is null");
706
          }
707
          // go through the substree list vector and found the start node id
708
          // and stop node id for this subtree id
709
          for (int i=0; i<subTreeList.size(); i++)
710
          {
711
            SubTree tree = (SubTree)subTreeList.elementAt(i);
712
            String subTreeId = tree.getSubTreeId();
713
            MetaCatUtil.debugMessage("subtree id in the list(go throug: "+
714
                                      subTreeId, 25);
715
            if (subSectionId.equals(subTreeId))
716
            {
717
              startNodeId = tree.getStartNodeId();
718
              endNodeId = tree.getEndNodeId(); 
719
            }//if
720
          }//for
721
          if (startNodeId == 0 || endNodeId == 0)
722
          {
723
            throw new SAXException("Could find the subtree"
724
                                   + "for this id: "+subSectionId);
725
          }
726
          pstmt.setString(7, subSectionId);
727
          MetaCatUtil.debugMessage("SubSectionId in accesstable: "+ 
728
                                    subSectionId, 25);
729
          pstmt.setLong(8, startNodeId);
730
          MetaCatUtil.debugMessage("Start node id is: " + startNodeId, 25);
731
          pstmt.setLong(9, endNodeId);
732
          MetaCatUtil.debugMessage("End node id is: " + endNodeId, 25);
733
          
734
        }
735
      
736
        Vector accessRules = accessSection.getAccessRules();
737
        // go through every rule
738
        for (int i=0; i<accessRules.size(); i++)
739
        {
740
          AccessRule rule = (AccessRule)accessRules.elementAt(i);
741
          String permType = rule.getPermissionType();
742
          int permission = rule.getPermission();
743
          pstmt.setInt(3, permission);
744
          MetaCatUtil.debugMessage("permission in accesstable: "+ permission, 25);
745
          pstmt.setString(4, permType);
746
          MetaCatUtil.debugMessage("Permtype in accesstable: "+ permType, 25);
747
          // go through every principle in rule
748
          Vector nameVector = rule.getPrincipal();
749
          for ( int j = 0; j < nameVector.size(); j++ ) 
750
          {
751
            String prName = (String)nameVector.elementAt(j);
752
            pstmt.setString(2, prName);
753
            MetaCatUtil.debugMessage("Principal in accesstable: "+prName, 25);
754
            pstmt.execute();
755
          }//for
756
        }//for
757
        pstmt.close();
758
      }//try 
759
      catch (SQLException e) 
760
      {
761
        throw new 
762
        SAXException("EMLSAXHandler.writeAccessRuletoDB(): " + e.getMessage());
763
      }//catch
764
      finally
765
      {
766
        try
767
        {
768
          pstmt.close();
769
        }
770
        catch(SQLException ee)
771
        {
772
          throw new 
773
          SAXException("EMLSAXHandler.writeAccessRuletoDB(): " + 
774
          ee.getMessage());
775
        }
776
      }//finally
777
     
778
  }//writeGivenAccessRuleIntoDB
779
  
780
  /* Delete from db all permission for resources related to @aclid if any.*/
781
  private void deletePermissionsInAccessTable(String aclid) 
782
          throws SAXException 
783
  {
784
    Statement stmt = null;
785
    try
786
    {
787
      // delete all acl records for resources related to @aclid if any
788
      stmt = connection.createStatement();
789
      // Increase DBConnection usage count
790
      connection.increaseUsageCount(1);
791
      stmt.execute("DELETE FROM xml_access WHERE accessfileid = '" + aclid +
792
                   "'"); 
793
    
794
    }
795
    catch (SQLException e)
796
    {
797
      throw new SAXException(e.getMessage());
798
    }
799
    finally
800
    {
801
      try
802
      {
803
        stmt.close();
804
      }
805
      catch (SQLException ee)
806
      {
807
        throw new SAXException(ee.getMessage());
808
      }
809
    }
810
  }
811
 
812
}
(32-32/53)