View Javadoc

1   package liquibase.changelog;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.Collections;
6   import java.util.List;
7   
8   import liquibase.changelog.filter.ChangeSetFilter;
9   import liquibase.changelog.visitor.ChangeSetVisitor;
10  import liquibase.database.Database;
11  import liquibase.exception.LiquibaseException;
12  
13  public class ChangeLogIterator {
14      private DatabaseChangeLog databaseChangeLog;
15      private List<ChangeSetFilter> changeSetFilters;
16  
17      public ChangeLogIterator(DatabaseChangeLog databaseChangeLog, ChangeSetFilter... changeSetFilters) {
18          this.databaseChangeLog = databaseChangeLog;
19          this.changeSetFilters = Arrays.asList(changeSetFilters);
20      }
21  
22      public ChangeLogIterator(List<RanChangeSet> changeSetList, DatabaseChangeLog changeLog,
23              ChangeSetFilter... changeSetFilters) {
24          final List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
25          for (RanChangeSet ranChangeSet : changeSetList) {
26              ChangeSet changeSet = changeLog.getChangeSet(ranChangeSet);
27              if (changeSet != null) {
28                  changeSets.add(changeSet);
29              }
30          }
31          this.databaseChangeLog = (new DatabaseChangeLog() {
32              @Override
33              public List<ChangeSet> getChangeSets() {
34                  return changeSets;
35              }
36          });
37  
38          this.changeSetFilters = Arrays.asList(changeSetFilters);
39      }
40  
41      public void run(ChangeSetVisitor visitor, Database database) throws LiquibaseException {
42          List<ChangeSet> changeSetList = databaseChangeLog.getChangeSets();
43          if (visitor.getDirection().equals(ChangeSetVisitor.Direction.REVERSE)) {
44              Collections.reverse(changeSetList);
45          }
46  
47          for (ChangeSet changeSet : changeSetList) {
48              boolean shouldVisit = true;
49              if (changeSetFilters != null) {
50                  for (ChangeSetFilter filter : changeSetFilters) {
51                      if (!filter.accepts(changeSet)) {
52                          shouldVisit = false;
53                          break;
54                      }
55                  }
56              }
57  
58              if (shouldVisit) {
59                  visitor.visit(changeSet, databaseChangeLog, database);
60              }
61          }
62      }
63  }