View Javadoc
1   /**
2    * Copyright 2014 Internet2
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17   * @author mchyzer
18   * $Id: PrintTest.java,v 1.1 2009-06-10 05:31:35 mchyzer Exp $
19   */
20  package edu.internet2.middleware.grouper.changeLog.consumer;
21  
22  import java.util.List;
23  
24  import edu.internet2.middleware.grouper.changeLog.ChangeLogConsumerBase;
25  import edu.internet2.middleware.grouper.changeLog.ChangeLogEntry;
26  import edu.internet2.middleware.grouper.changeLog.ChangeLogLabels;
27  import edu.internet2.middleware.grouper.changeLog.ChangeLogProcessorMetadata;
28  import edu.internet2.middleware.grouper.changeLog.ChangeLogTypeBuiltin;
29  import edu.internet2.middleware.grouper.util.GrouperUtil;
30  
31  
32  /**
33   * just print out some of the events
34   */
35  public class PrintTest extends ChangeLogConsumerBase {
36  
37    /**
38     * @see edu.internet2.middleware.grouper.changeLog.ChangeLogConsumerBase#processChangeLogEntries(List, ChangeLogProcessorMetadata)
39     */
40    @Override
41    public long processChangeLogEntries(List<ChangeLogEntry> changeLogEntryList,
42        ChangeLogProcessorMetadata changeLogProcessorMetadata) {
43      
44      long currentId = -1;
45  
46      //try catch so we can track that we made some progress
47      try {
48        for (ChangeLogEntry changeLogEntry : GrouperUtil.nonNull(changeLogEntryList)) {
49          currentId = changeLogEntry.getSequenceNumber();
50          
51          //if this is a group add action and category
52          if (changeLogEntry.equalsCategoryAndAction(ChangeLogTypeBuiltin.GROUP_ADD)) {
53            
54            //print the name from the entry
55            System.out.println("Group add, name: " 
56                + changeLogEntry.retrieveValueForLabel(ChangeLogLabels.GROUP_ADD.name));
57          } else if (changeLogEntry.equalsCategoryAndAction(ChangeLogTypeBuiltin.GROUP_DELETE)) {
58            
59            //print the name from the entry
60            System.out.println("Group delete, name: " 
61                + changeLogEntry.retrieveValueForLabel(ChangeLogLabels.GROUP_DELETE.name));
62          } else if (changeLogEntry.equalsCategoryAndAction(ChangeLogTypeBuiltin.GROUP_UPDATE)) {
63            
64            //print the name from the entry
65            System.out.println("Group update, name: " 
66                + changeLogEntry.retrieveValueForLabel(ChangeLogLabels.GROUP_UPDATE.name)
67                + ", property: " + changeLogEntry.retrieveValueForLabel(ChangeLogLabels.GROUP_UPDATE.propertyChanged)
68                + ", from: '" + changeLogEntry.retrieveValueForLabel(ChangeLogLabels.GROUP_UPDATE.propertyOldValue)
69                + "', to: '" + changeLogEntry.retrieveValueForLabel(ChangeLogLabels.GROUP_UPDATE.propertyNewValue) + "'");
70          } else {
71            System.out.println("Change log entry: " + changeLogEntry.getChangeLogType().getChangeLogCategory() +
72                " -> " + changeLogEntry.getChangeLogType().getActionName() + ", " + changeLogEntry.getSequenceNumber());
73  
74          }
75          
76          //we successfully processed this record
77        }
78      } catch (Exception e) {
79        changeLogProcessorMetadata.registerProblem(e, "Error processing record", currentId);
80        //we made it to this -1
81        return currentId-1;
82      }
83      if (currentId == -1) {
84        throw new RuntimeException("Couldnt process any records");
85      }
86      
87      return currentId;
88    }
89  
90  }