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