Project

General

Profile

« Previous | Next » 

Revision 5384

Added by berkley over 14 years ago

implementing getLogRecords

View differences:

test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java
86 86
	public static Test suite() 
87 87
	{
88 88
		TestSuite suite = new TestSuite();
89
		/*suite.addTest(new CrudServiceTest("initialize"));
89
		suite.addTest(new CrudServiceTest("initialize"));
90 90
		suite.addTest(new CrudServiceTest("testSingletonAccessor"));
91 91
		suite.addTest(new CrudServiceTest("testCreateAndGet"));
92 92
		suite.addTest(new CrudServiceTest("testGetSystemMetadata"));
93 93
		suite.addTest(new CrudServiceTest("testUpdate"));
94 94
		suite.addTest(new CrudServiceTest("testListObjects"));
95
		suite.addTest(new CrudServiceTest("testAccessControl"));*/
95
		suite.addTest(new CrudServiceTest("testAccessControl"));
96 96
		suite.addTest(new CrudServiceTest("testGenerateMissingSystemMetadata"));
97
		suite.addTest(new CrudServiceTest("testGetLogRecords"));
97 98
		return suite;
98 99
	}
99 100
	
100 101
	/**
102
	 * test CrudService.getLogRecords
103
	 */
104
	public void testGetLogRecords()
105
	{
106
	    printTestHeader("testGetLogRecords");
107
	    try
108
	    {
109
	        CrudService cs = CrudService.getInstance();
110
	        AuthToken token = getToken();
111
	        Date fromDate = new Date();
112
	        Identifier id = createDoc(token, getTestDoc());
113
	        Date toDate = new Date();
114
	        LogRecordSet lrs = cs.getLogRecords(token, fromDate, toDate);
115
	        Set s = lrs.getRecords();
116
	        assertNotNull(s);
117
	        assertTrue(lrs.getRecords().size() == 1);
118
	        LogRecord lrLogRecord = (LogRecord)s.iterator().next();
119
	        
120
	    }
121
	    catch(Exception e)
122
	    {
123
	        e.printStackTrace();
124
	        fail("testGetLogRecords threw an unexpected exception: " + e.getMessage());
125
	    }
126
	}
127
	
128
	/**
101 129
	 * test the generation of system metadata for docs that don't already 
102 130
	 * have it.  This will be used for migration of existing object stores
103 131
	 * to dataone.
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
187 187
            sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
188 188
            AuthToken token = new AuthToken(sessionid);
189 189
            EventLog.getInstance().log(metacatUrl,
190
                    username, null, "CrudService.authenticate");
190
                    username, null, "authenticate");
191 191
            return token;
192 192
        }
193 193
        catch(Exception e)
......
229 229
            {
230 230
                //generate required system metadata fields from the document
231 231
                SystemMetadata sm = createSystemMetadata(localId, token);
232
                System.out.println("sm: " + new String(serializeSystemMetadata(sm).toByteArray()));
233 232
                //insert the systemmetadata object
234 233
                SessionData sessionData = getSessionData(token);
235 234
                insertSystemMetadata(sm, sessionData);
236 235
                
237 236
                String username = sessionData.getUserName();
238 237
                EventLog.getInstance().log(metacatUrl,
239
                        username, localId, "CrudService.generateMissingSystemMetadata");
238
                        username, localId, "generateMissingSystemMetadata");
240 239
            }
241 240
            catch(Exception e)
242 241
            {
......
315 314

  
316 315
        logMetacat.debug("Returning from CrudService.create()");
317 316
        EventLog.getInstance().log(metacatUrl,
318
                username, localId, "CrudService.create");
317
                username, localId, "create");
319 318
        return guid;
320 319
    }
321 320
    
......
361 360
            IdentifierManager im = IdentifierManager.getInstance();
362 361
            String username = sessionData.getUserName();
363 362
            EventLog.getInstance().log(metacatUrl,
364
                    username, im.getLocalId(guid.getValue()), "CrudService.update");
363
                    username, im.getLocalId(guid.getValue()), "update");
365 364
            return guid;
366 365
        }
367 366
        catch(Exception e)
......
402 401
            
403 402
            String username = sessionData.getUserName();
404 403
            EventLog.getInstance().log(metacatUrl,
405
                    username, im.getLocalId(id.getValue()), "CrudService.setAccess");
404
                    username, im.getLocalId(id.getValue()), "setAccess");
406 405
        }
407 406
        catch(Exception e)
408 407
        {
......
467 466
              //get the document from the results
468 467
              Document d = (Document)docs.get(i);
469 468
              ObjectFormat returnedObjectFormat = ObjectFormat.convert(d.getField("objectFormat"));
469
              if(returnedObjectFormat == null)
470
              {
471
                  continue;
472
              }
470 473
              if(objectFormat != null && !objectFormat.toString().trim().equals(returnedObjectFormat.toString().trim()))
471 474
              { //make sure the objectFormat is the one specified
472 475
                  continue;
473 476
              }
474
              Date dateSysMetadataModified = parseDate(d.getField("dateSysMetadataModified"));
477
              
478
              String dateSMM = d.getField("dateSysMetadataModified");
479
              if(dateSMM == null)
480
              {
481
                  continue;
482
              }
483
              Date dateSysMetadataModified = parseDate(dateSMM);
475 484
              int startDateComparison = 0;
476 485
              int endDateComparison = 0;
477 486
              if(startTime != null)
......
520 529
      }
521 530
      String username = sessionData.getUserName();
522 531
      EventLog.getInstance().log(metacatUrl,
523
              username, null, "CrudService.listObjects");
532
              username, null, "read");
524 533
      return ol;
525 534
    }
526 535
    
......
619 628
            
620 629
            String username = sessionData.getUserName();
621 630
            EventLog.getInstance().log(metacatUrl,
622
                    username, im.getLocalId(guid.getValue()), "CrudService.get");
631
                    username, im.getLocalId(guid.getValue()), "read");
623 632
            return objectStream;
624 633

  
625 634
        } catch (McdbDocNotFoundException e) {
......
646 655
    }
647 656

  
648 657
    /**
649
     * get log records.  NOT IMPLEMENTED
658
     * get log records.  
650 659
     */
651 660
    public LogRecordSet getLogRecords(AuthToken token, Date fromDate, Date toDate)
652 661
            throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, 
653
            NotImplemented {
654
        throw new NotImplemented("1000", "This method not yet implemented.");
662
            NotImplemented 
663
    {
664
        IdentifierManager im = IdentifierManager.getInstance();
665
        HashSet<LogRecord> logs = new HashSet<LogRecord>();
666
        EventLog el = EventLog.getInstance();
667
        String report = el.getReport(null, null, null, null, 
668
                new java.sql.Timestamp(fromDate.getTime()), 
669
                new java.sql.Timestamp(toDate.getTime()));
670
        
671
        String logEntry = "<logEntry>";
672
        String endLogEntry = "</logEntry>";
673
        int startIndex = 0;
674
        int foundIndex = report.indexOf(logEntry, startIndex);
675
        while(foundIndex != -1)
676
        {
677
            //parse out each entry
678
            int endEntryIndex = report.indexOf(endLogEntry, foundIndex);
679
            String entry = report.substring(foundIndex, endEntryIndex);
680
            //System.out.println("entry: " + entry);
681
            startIndex = endEntryIndex + endLogEntry.length();
682
            foundIndex = report.indexOf(logEntry, startIndex);
683
            
684
            String entryId = getLogEntryField("entryid", entry);
685
            String ipAddress = getLogEntryField("ipAddress", entry);
686
            String principal = getLogEntryField("principal", entry);
687
            String docid = getLogEntryField("docid", entry);
688
            String event = getLogEntryField("event", entry);
689
            String dateLogged = getLogEntryField("dateLogged", entry);
690
            
691
            LogRecord lr = new LogRecord();
692
            
693
            Event e = Event.convert(event);
694
            if(e == null)
695
            { //skip any events that are not Dataone Crud events
696
                continue;
697
            }
698
            lr.setEvent(e);
699
            Identifier entryid = new Identifier();
700
            entryid.setValue(entryId);
701
            lr.setEntryId(entryid);
702
            Identifier identifier = new Identifier();
703
            try
704
            {
705
                identifier.setValue(im.getGUID(docid, im.getLatestRevForLocalId(docid)));
706
            }
707
            catch(Exception ex)
708
            { //try to get the guid, if that doesn't work, just use the local id
709
                identifier.setValue(docid);
710
            }
711
            
712
            lr.setIdentifier(identifier);
713
            lr.setIpAddress(ipAddress);
714
            Calendar c = Calendar.getInstance();
715
            String year = dateLogged.substring(0, 4);
716
            String month = dateLogged.substring(5, 7);
717
            String date = dateLogged.substring(8, 10);
718
            //System.out.println("year: " + year + " month: " + month + " day: " + date);
719
            c.set(new Integer(year).intValue(), new Integer(month).intValue(), new Integer(date).intValue());
720
            Date logDate = c.getTime();
721
            lr.setLogDate(logDate);
722
            NodeReference memberNode = new NodeReference();
723
            memberNode.setValue(ipAddress);
724
            lr.setMemberNode(memberNode);
725
            Principal princ = new Principal();
726
            princ.setValue(principal);
727
            lr.setPrincipal(princ);
728
            lr.setUserAgent("metacat/RESTService");
729
            logs.add(lr);
730
        }
731
        
732
        LogRecordSet set = new LogRecordSet(logs);
733
        set.setRecords(logs);
734
        return set;
655 735
    }
736
    
737
    /**
738
     * parse a logEntry and get the relavent field from it
739
     * @param fieldname
740
     * @param entry
741
     * @return
742
     */
743
    private String getLogEntryField(String fieldname, String entry)
744
    {
745
        String begin = "<" + fieldname + ">";
746
        String end = "</" + fieldname + ">";
747
        //System.out.println("looking for " + begin + " and " + end + " in entry " + entry);
748
        String s = entry.substring(entry.indexOf(begin) + begin.length(), entry.indexOf(end));
749
        //System.out.println("entry " + fieldname + " : " + s);
750
        return s;
751
    }
656 752

  
657 753
    /**
658 754
     * get the system metadata for a document with a specified guid.
......
715 811
            SystemMetadata sysmeta = deserializeSystemMetadata(objectStream);
716 812
            String username = sessionData.getUserName();
717 813
            EventLog.getInstance().log(metacatUrl,
718
                    username, im.getLocalId(guid.getValue()), "CrudService.getSystemMetadata");
814
                    username, im.getLocalId(guid.getValue()), "read");
719 815
            return sysmeta;
720 816
            
721 817
        } catch (McdbDocNotFoundException e) {
......
890 986
    
891 987
                    logMetacat.debug("Logging the creation event.");
892 988
                    EventLog.getInstance().log(metacatUrl,
893
                            username, localId, "CrudService.insertDataObject");
989
                            username, localId, "create");
894 990
    
895 991
                    // Schedule replication for this data file
896 992
                    logMetacat.debug("Scheduling replication.");
897 993
                    ForceReplicationHandler frh = new ForceReplicationHandler(
898
                            localId, "insert", false, null);
994
                            localId, "create", false, null);
899 995
    
900 996
                } catch (PropertyNotFoundException e) {
901 997
                    throw new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());

Also available in: Unified diff