1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.cdm.database.update;
|
5
|
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.Arrays;
|
8
|
import java.util.List;
|
9
|
|
10
|
import org.apache.commons.lang.StringUtils;
|
11
|
|
12
|
/**
|
13
|
* @author a.mueller
|
14
|
*
|
15
|
*/
|
16
|
public class MnTableCreator extends TableCreator {
|
17
|
|
18
|
private String firstTableName;
|
19
|
private String firstTableAlias;
|
20
|
private String secondTableName;
|
21
|
private String secondTableAlias;
|
22
|
private boolean hasSortIndex;
|
23
|
private boolean secondTableInKey;
|
24
|
|
25
|
public static MnTableCreator NewMnInstance(String stepName, String firstTableName, String secondTableName, boolean includeAudTable){
|
26
|
MnTableCreator result = new MnTableCreator(stepName, firstTableName, null, secondTableName, null, new String[]{}, new String[]{}, null, null, includeAudTable, false, true, false, false, false);
|
27
|
return result;
|
28
|
}
|
29
|
|
30
|
public static MnTableCreator NewMnInstance(String stepName, String firstTableName, String firstTableAlias, String secondTableName, String secondTableAlias, boolean includeAudTable, boolean hasSortIndex, boolean secondTableInKey){
|
31
|
MnTableCreator result = new MnTableCreator(stepName, firstTableName, firstTableAlias, secondTableName, secondTableAlias, new String[]{}, new String[]{}, null, null, includeAudTable, hasSortIndex, secondTableInKey, false, false, false);
|
32
|
return result;
|
33
|
}
|
34
|
|
35
|
|
36
|
protected MnTableCreator(String stepName, String firstTableName, String firstTableAlias, String secondTableName, String secondTableAlias, String[] columnNames, String[] columnTypes,
|
37
|
List<Object> defaultValues, List<Boolean> isNull, boolean includeAudTable, boolean hasSortIndex, boolean secondTableInKey, boolean includeCdmBaseAttributes,boolean includeAnnotatableEntity, boolean includeIdentifiableEntity) {
|
38
|
super(stepName, firstTableName + "_" + secondTableName, Arrays.asList(columnNames), Arrays.asList(columnTypes), defaultValues, isNull, new ArrayList<String>(), includeAudTable, includeCdmBaseAttributes, includeAnnotatableEntity, includeIdentifiableEntity, false);
|
39
|
this.firstTableName = firstTableName;
|
40
|
this.secondTableName = secondTableName;
|
41
|
this.firstTableAlias = (firstTableAlias != null )? firstTableAlias : firstTableName ;
|
42
|
this.secondTableAlias = (secondTableAlias != null)? secondTableAlias : secondTableName ;
|
43
|
this.hasSortIndex = hasSortIndex;
|
44
|
this.secondTableInKey = secondTableInKey;
|
45
|
addMyColumns();
|
46
|
}
|
47
|
|
48
|
|
49
|
protected void addMyColumns(){
|
50
|
this.columnAdders.add(ColumnAdder.NewIntegerInstance(stepName, tableName, getFirstIdColumn(), false, true, this.firstTableName));
|
51
|
this.columnAdders.add(ColumnAdder.NewIntegerInstance(stepName, tableName, getSecondIdColumn(), false, true, this.secondTableName));
|
52
|
if (this.hasSortIndex){
|
53
|
this.columnAdders.add(ColumnAdder.NewIntegerInstance(stepName, tableName, "sortIndex", false, true, null));
|
54
|
}
|
55
|
|
56
|
}
|
57
|
|
58
|
protected String primaryKey(boolean isAudit){
|
59
|
String result = "";
|
60
|
if (! isAudit){
|
61
|
result = getFirstIdColumn() + ",";
|
62
|
result += secondTableInKey ? getSecondIdColumn() + "," : "";
|
63
|
result += hasSortIndex ? "sortIndex," : "";
|
64
|
}else{
|
65
|
result = "REV, " + primaryKey(false);
|
66
|
result += (!secondTableInKey) ? ","+getSecondIdColumn() + "," : "";
|
67
|
}
|
68
|
result = StringUtils.chomp(result.trim(), ",");
|
69
|
return result;
|
70
|
}
|
71
|
|
72
|
protected String unique(boolean isAudit){
|
73
|
if (! isAudit){
|
74
|
return getSecondIdColumn();
|
75
|
}else{
|
76
|
return null;
|
77
|
}
|
78
|
}
|
79
|
|
80
|
private String getFirstIdColumn(){
|
81
|
return this.firstTableAlias + "_id";
|
82
|
}
|
83
|
|
84
|
private String getSecondIdColumn(){
|
85
|
String result = this.secondTableAlias.toLowerCase();
|
86
|
|
87
|
if (this.secondTableAlias.equalsIgnoreCase(this.secondTableName) ){
|
88
|
if (! result.endsWith("s")){
|
89
|
result += "s";
|
90
|
}
|
91
|
}
|
92
|
result += "_id";
|
93
|
return result;
|
94
|
}
|
95
|
}
|