Coverage Report - org.apache.torque.util.TransactionComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
TransactionComparator
68%
26/38
59%
13/22
2.727
 
 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  22
 public class TransactionComparator<T> implements Comparator<Transaction> {
 14  
 
 15  1
         String suffix = ".sql";
 16  1
         String constraints = "-constraints";
 17  
         String artifactId;
 18  
 
 19  
         public TransactionComparator() {
 20  0
                 this(null);
 21  0
         }
 22  
 
 23  
         public TransactionComparator(String artifactId) {
 24  1
                 super();
 25  1
                 this.artifactId = artifactId;
 26  1
         }
 27  
 
 28  
         @Override
 29  
         public int compare(Transaction one, Transaction two) {
 30  22
                 String loc1 = one.getResourceLocation();
 31  22
                 String loc2 = two.getResourceLocation();
 32  
                 // If loc1 is ks-embedded-db.sql, loc1 goes before loc2
 33  22
                 if (isSchemaSQL(loc1)) {
 34  1
                         return -1;
 35  
                 }
 36  
                 // If loc2 is ks-embedded-db.sql, loc1 goes after loc2
 37  21
                 if (isSchemaSQL(loc2)) {
 38  3
                         return 1;
 39  
                 }
 40  
                 // If loc1 is ks-embedded-db-constraints.sql, loc1 goes after loc2
 41  18
                 if (isSchemaConstraintsSQL(loc1)) {
 42  3
                         return 1;
 43  
                 }
 44  
                 // If loc2 is ks-embedded-db-constraints.sql, loc1 goes before loc2
 45  15
                 if (isSchemaConstraintsSQL(loc2)) {
 46  1
                         return -1;
 47  
                 }
 48  
                 // They are both empty, it is a tie
 49  14
                 if (isEmpty(loc1) && isEmpty(loc2)) {
 50  0
                         return 0;
 51  
                 }
 52  
                 // Loc2 is empty but loc1 is not, loc1 goes after loc2
 53  14
                 if (isEmpty(loc1) && !isEmpty(loc2)) {
 54  0
                         return 1;
 55  
                 }
 56  
                 // Loc1 is empty but loc2 is not, loc1 goes before loc2
 57  14
                 if (!isEmpty(loc1) && isEmpty(loc2)) {
 58  0
                         return -1;
 59  
                 }
 60  
                 // Fall through to the normal string compare
 61  14
                 return loc1.compareTo(loc2);
 62  
         }
 63  
 
 64  
         protected boolean isSchemaSQL(String location) {
 65  43
                 if (isEmpty(location)) {
 66  0
                         return false;
 67  
                 } else {
 68  43
                         return location.endsWith(getArtifactId() + getSuffix());
 69  
                 }
 70  
         }
 71  
 
 72  
         protected boolean isSchemaConstraintsSQL(String location) {
 73  33
                 return location.endsWith(getArtifactId() + getConstraints() + getSuffix());
 74  
         }
 75  
 
 76  
         public String getArtifactId() {
 77  76
                 return artifactId;
 78  
         }
 79  
 
 80  
         public void setArtifactId(String schema) {
 81  0
                 this.artifactId = schema;
 82  0
         }
 83  
 
 84  
         public String getSuffix() {
 85  76
                 return suffix;
 86  
         }
 87  
 
 88  
         public void setSuffix(String suffix) {
 89  0
                 this.suffix = suffix;
 90  0
         }
 91  
 
 92  
         public String getConstraints() {
 93  33
                 return constraints;
 94  
         }
 95  
 
 96  
         public void setConstraints(String constraints) {
 97  0
                 this.constraints = constraints;
 98  0
         }
 99  
 
 100  
 }