View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  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  		// If loc1 is ks-embedded-db.sql, loc1 goes before loc2
48  		if (isSchemaSQL(loc1)) {
49  			return -1;
50  		}
51  		// If loc2 is ks-embedded-db.sql, loc1 goes after loc2
52  		if (isSchemaSQL(loc2)) {
53  			return 1;
54  		}
55  		// If loc1 is ks-embedded-db-constraints.sql, loc1 goes after loc2
56  		if (isSchemaConstraintsSQL(loc1)) {
57  			return 1;
58  		}
59  		// If loc2 is ks-embedded-db-constraints.sql, loc1 goes before loc2
60  		if (isSchemaConstraintsSQL(loc2)) {
61  			return -1;
62  		}
63  		// They are both empty, it is a tie
64  		if (isEmpty(loc1) && isEmpty(loc2)) {
65  			return 0;
66  		}
67  		// Loc2 is empty but loc1 is not, loc1 goes after loc2
68  		if (isEmpty(loc1) && !isEmpty(loc2)) {
69  			return 1;
70  		}
71  		// Loc1 is empty but loc2 is not, loc1 goes before loc2
72  		if (!isEmpty(loc1) && isEmpty(loc2)) {
73  			return -1;
74  		}
75  		// Fall through to the normal string compare
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 }