1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.apache.torque.util;
17  
18  import java.util.Comparator;
19  
20  import static org.apache.commons.lang.StringUtils.*;
21  
22  import org.kuali.db.Transaction;
23  
24  
25  
26  
27  
28  public class TransactionComparator<T> implements Comparator<Transaction> {
29  
30  	String suffix = ".sql";
31  	String constraints = "-constraints";
32  	String artifactId;
33  
34  	public TransactionComparator() {
35  		this(null);
36  	}
37  
38  	public TransactionComparator(String artifactId) {
39  		super();
40  		this.artifactId = artifactId;
41  	}
42  
43  	@Override
44  	public int compare(Transaction one, Transaction two) {
45  		String loc1 = one.getResourceLocation();
46  		String loc2 = two.getResourceLocation();
47  		
48  		if (isSchemaSQL(loc1)) {
49  			return -1;
50  		}
51  		
52  		if (isSchemaSQL(loc2)) {
53  			return 1;
54  		}
55  		
56  		if (isSchemaConstraintsSQL(loc1)) {
57  			return 1;
58  		}
59  		
60  		if (isSchemaConstraintsSQL(loc2)) {
61  			return -1;
62  		}
63  		
64  		if (isEmpty(loc1) && isEmpty(loc2)) {
65  			return 0;
66  		}
67  		
68  		if (isEmpty(loc1) && !isEmpty(loc2)) {
69  			return 1;
70  		}
71  		
72  		if (!isEmpty(loc1) && isEmpty(loc2)) {
73  			return -1;
74  		}
75  		
76  		return loc1.compareTo(loc2);
77  	}
78  
79  	protected boolean isSchemaSQL(String location) {
80  		if (isEmpty(location)) {
81  			return false;
82  		} else {
83  			return location.endsWith(getArtifactId() + getSuffix());
84  		}
85  	}
86  
87  	protected boolean isSchemaConstraintsSQL(String location) {
88  		return location.endsWith(getArtifactId() + getConstraints() + getSuffix());
89  	}
90  
91  	public String getArtifactId() {
92  		return artifactId;
93  	}
94  
95  	public void setArtifactId(String schema) {
96  		this.artifactId = schema;
97  	}
98  
99  	public String getSuffix() {
100 		return suffix;
101 	}
102 
103 	public void setSuffix(String suffix) {
104 		this.suffix = suffix;
105 	}
106 
107 	public String getConstraints() {
108 		return constraints;
109 	}
110 
111 	public void setConstraints(String constraints) {
112 		this.constraints = constraints;
113 	}
114 
115 }