| 1 | |
package org.kuali.db.impex; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.List; |
| 5 | |
|
| 6 | |
import org.apache.torque.engine.database.model.Database; |
| 7 | |
import org.apache.torque.engine.database.model.Table; |
| 8 | |
import org.kuali.db.jdbc.Transaction; |
| 9 | |
import org.slf4j.Logger; |
| 10 | |
import org.slf4j.LoggerFactory; |
| 11 | |
import org.springframework.core.io.DefaultResourceLoader; |
| 12 | |
import org.springframework.core.io.Resource; |
| 13 | |
import org.springframework.core.io.ResourceLoader; |
| 14 | |
|
| 15 | 0 | public class TransactionGenerator { |
| 16 | 0 | final Logger logger = LoggerFactory.getLogger(TransactionGenerator.class); |
| 17 | |
|
| 18 | 0 | ResourceLoader loader = new DefaultResourceLoader(); |
| 19 | |
String prefix; |
| 20 | |
String suffix; |
| 21 | |
String constraintsSuffix; |
| 22 | |
SchemaParser parser; |
| 23 | |
List<Transaction> transactions; |
| 24 | |
|
| 25 | |
public SchemaParser getParser() { |
| 26 | 0 | return parser; |
| 27 | |
} |
| 28 | |
|
| 29 | |
public void setParser(SchemaParser parser) { |
| 30 | 0 | this.parser = parser; |
| 31 | 0 | } |
| 32 | |
|
| 33 | |
protected Transaction getResourceTransaction(String resourceLocation) { |
| 34 | 0 | Transaction t = new Transaction(); |
| 35 | 0 | t.setResourceLocation(resourceLocation); |
| 36 | 0 | return t; |
| 37 | |
} |
| 38 | |
|
| 39 | |
protected String getImpexClasspathResourceLocation(String filename) { |
| 40 | 0 | StringBuilder sb = new StringBuilder(); |
| 41 | 0 | sb.append("classpath:"); |
| 42 | 0 | sb.append(getPrefix()); |
| 43 | 0 | sb.append("/"); |
| 44 | 0 | sb.append(getParser().getImpexMetadata().getPlatform()); |
| 45 | 0 | sb.append("/"); |
| 46 | 0 | sb.append(filename); |
| 47 | 0 | return sb.toString(); |
| 48 | |
} |
| 49 | |
|
| 50 | |
protected boolean exists(String location) { |
| 51 | 0 | Resource resource = loader.getResource(location); |
| 52 | 0 | return resource.exists(); |
| 53 | |
} |
| 54 | |
|
| 55 | |
public void generate() { |
| 56 | 0 | transactions = new ArrayList<Transaction>(); |
| 57 | 0 | Database database = parser.getDatabase(); |
| 58 | 0 | String name = database.getName(); |
| 59 | 0 | String ddlResource = getImpexClasspathResourceLocation(name + getSuffix()); |
| 60 | 0 | String ddlConstraintsResource = getImpexClasspathResourceLocation(name + getConstraintsSuffix() + getSuffix()); |
| 61 | 0 | transactions.add(getResourceTransaction(ddlResource)); |
| 62 | 0 | List<Table> tables = database.getTables(); |
| 63 | 0 | for (Table table : tables) { |
| 64 | 0 | String tablename = table.getName(); |
| 65 | 0 | String tableResource = getImpexClasspathResourceLocation(tablename + getSuffix()); |
| 66 | 0 | if (exists(tableResource)) { |
| 67 | 0 | transactions.add(getResourceTransaction(tableResource)); |
| 68 | 0 | logger.debug("Adding " + tableResource); |
| 69 | |
} else { |
| 70 | 0 | logger.debug("Skipping '" + tableResource + "' Resource not found"); |
| 71 | |
} |
| 72 | |
|
| 73 | 0 | } |
| 74 | 0 | transactions.add(getResourceTransaction(ddlConstraintsResource)); |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
public String getSuffix() { |
| 78 | 0 | return suffix; |
| 79 | |
} |
| 80 | |
|
| 81 | |
public void setSuffix(String suffix) { |
| 82 | 0 | this.suffix = suffix; |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
public List<Transaction> getTransactions() { |
| 86 | 0 | return transactions; |
| 87 | |
} |
| 88 | |
|
| 89 | |
public void setTransactions(List<Transaction> transactions) { |
| 90 | 0 | this.transactions = transactions; |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
public String getConstraintsSuffix() { |
| 94 | 0 | return constraintsSuffix; |
| 95 | |
} |
| 96 | |
|
| 97 | |
public void setConstraintsSuffix(String constraintsSuffix) { |
| 98 | 0 | this.constraintsSuffix = constraintsSuffix; |
| 99 | 0 | } |
| 100 | |
|
| 101 | |
public String getPrefix() { |
| 102 | 0 | return prefix; |
| 103 | |
} |
| 104 | |
|
| 105 | |
public void setPrefix(String prefix) { |
| 106 | 0 | this.prefix = prefix; |
| 107 | 0 | } |
| 108 | |
} |