001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.torque.util;
017    
018    import java.util.Comparator;
019    
020    import static org.apache.commons.lang.StringUtils.*;
021    
022    import org.kuali.db.Transaction;
023    
024    /**
025     * 
026     *
027     */
028    public class TransactionComparator<T> implements Comparator<Transaction> {
029    
030            String suffix = ".sql";
031            String constraints = "-constraints";
032            String artifactId;
033    
034            public TransactionComparator() {
035                    this(null);
036            }
037    
038            public TransactionComparator(String artifactId) {
039                    super();
040                    this.artifactId = artifactId;
041            }
042    
043            @Override
044            public int compare(Transaction one, Transaction two) {
045                    String loc1 = one.getResourceLocation();
046                    String loc2 = two.getResourceLocation();
047                    // If loc1 is ks-embedded-db.sql, loc1 goes before loc2
048                    if (isSchemaSQL(loc1)) {
049                            return -1;
050                    }
051                    // If loc2 is ks-embedded-db.sql, loc1 goes after loc2
052                    if (isSchemaSQL(loc2)) {
053                            return 1;
054                    }
055                    // If loc1 is ks-embedded-db-constraints.sql, loc1 goes after loc2
056                    if (isSchemaConstraintsSQL(loc1)) {
057                            return 1;
058                    }
059                    // If loc2 is ks-embedded-db-constraints.sql, loc1 goes before loc2
060                    if (isSchemaConstraintsSQL(loc2)) {
061                            return -1;
062                    }
063                    // They are both empty, it is a tie
064                    if (isEmpty(loc1) && isEmpty(loc2)) {
065                            return 0;
066                    }
067                    // Loc2 is empty but loc1 is not, loc1 goes after loc2
068                    if (isEmpty(loc1) && !isEmpty(loc2)) {
069                            return 1;
070                    }
071                    // Loc1 is empty but loc2 is not, loc1 goes before loc2
072                    if (!isEmpty(loc1) && isEmpty(loc2)) {
073                            return -1;
074                    }
075                    // Fall through to the normal string compare
076                    return loc1.compareTo(loc2);
077            }
078    
079            protected boolean isSchemaSQL(String location) {
080                    if (isEmpty(location)) {
081                            return false;
082                    } else {
083                            return location.endsWith(getArtifactId() + getSuffix());
084                    }
085            }
086    
087            protected boolean isSchemaConstraintsSQL(String location) {
088                    return location.endsWith(getArtifactId() + getConstraints() + getSuffix());
089            }
090    
091            public String getArtifactId() {
092                    return artifactId;
093            }
094    
095            public void setArtifactId(String schema) {
096                    this.artifactId = schema;
097            }
098    
099            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    }