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
|
import java.util.Map;
|
10
|
|
11
|
/**
|
12
|
* Creates an MN table
|
13
|
*
|
14
|
* @author a.mueller
|
15
|
*/
|
16
|
public class MnTableCreator extends TableCreator {
|
17
|
|
18
|
private String firstTableName;
|
19
|
private String firstTableAlias;
|
20
|
private String firstColumnName;
|
21
|
private String secondTableName;
|
22
|
private String secondTableAlias;
|
23
|
private String secondColumnName;
|
24
|
//is the MN table used for a list, if yes, a sortIndex column is needed
|
25
|
//and the and the sortindex column needs to be in the key instead of second table column.
|
26
|
private boolean isList;
|
27
|
private boolean is1toM;
|
28
|
|
29
|
public static MnTableCreator NewMnInstance(String stepName, String firstTableName, String secondTableName, boolean includeAudTable, boolean isList, boolean is1toM){
|
30
|
MnTableCreator result = new MnTableCreator(stepName, firstTableName, null, null, secondTableName, null, null, new String[]{}, new String[]{}, null, null, includeAudTable, isList, is1toM, false, false, false);
|
31
|
return result;
|
32
|
}
|
33
|
|
34
|
/**
|
35
|
*
|
36
|
* @param stepName The step name
|
37
|
* @param firstTableName The name of the first table
|
38
|
* @param firstTableAlias The alias for the first table as used in the MN table name
|
39
|
* @param secondTableName The name of the second table
|
40
|
* @param secondTableAlias The alias for the second table as used in the MN table name
|
41
|
* @param attributeName The name of the attribute pointing to the second table (this is used for the column name for the
|
42
|
* column pointing to the second table)
|
43
|
* @param includeAudTable <code>true</code> if also the Audit (_AUD) table should be created
|
44
|
* @param hasSortIndex by default <code>false</code> but true for {@link Map maps} (or maybe user defined MN-tables)
|
45
|
* @param secondTableInKey should the column that links to the second table also be in the key? This is by default
|
46
|
* <code>true</code> but for {@link List lists} should be <code>false</code>.
|
47
|
* @return
|
48
|
*/
|
49
|
public static MnTableCreator NewMnInstance(String stepName, String firstTableName, String firstTableAlias, String secondTableName, String secondTableAlias, String attributeName,
|
50
|
boolean includeAudTable, boolean isList, boolean is1toM){
|
51
|
MnTableCreator result = new MnTableCreator(stepName, firstTableName, firstTableAlias, null, secondTableName, secondTableAlias, attributeName,
|
52
|
new String[]{}, new String[]{}, null, null,
|
53
|
includeAudTable, isList, is1toM, false, false, false);
|
54
|
return result;
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
*
|
59
|
* @param stepName The step name
|
60
|
* @param firstTableName The name of the first table
|
61
|
* @param firstTableAlias The alias for the first table as used in the MN table name
|
62
|
* @param firstColumnName The name of the attribute pointing to the first table (this is used for the column name for the
|
63
|
* column pointing to the first table)
|
64
|
* @param secondTableName The name of the second table
|
65
|
* @param secondTableAlias The alias for the second table as used in the MN table name
|
66
|
* @param secondColumnName The name of the attribute pointing to the second table (this is used for the column name for the
|
67
|
* column pointing to the second table)
|
68
|
* @param includeAudTable <code>true</code> if also the Audit (_AUD) table should be created
|
69
|
* @param hasSortIndex by default <code>false</code> but true for {@link Map maps} (or maybe user defined MN-tables)
|
70
|
* @param secondTableInKey should the column that links to the second table also be in the key? This is by default
|
71
|
* <code>true</code> but for {@link List lists} should be <code>false</code>.
|
72
|
* @return
|
73
|
*/
|
74
|
public static MnTableCreator NewMnInstance(String stepName, String firstTableName, String firstTableAlias, String firstColumnName, String secondTableName, String secondTableAlias, String secondColumnName,
|
75
|
boolean includeAudTable, boolean isList, boolean is1toM){
|
76
|
MnTableCreator result = new MnTableCreator(stepName, firstTableName, firstTableAlias, firstColumnName, secondTableName, secondTableAlias, secondColumnName,
|
77
|
new String[]{}, new String[]{}, null, null,
|
78
|
includeAudTable, isList, is1toM, false, false, false);
|
79
|
return result;
|
80
|
}
|
81
|
|
82
|
// ****************************** CONSTRUCTOR *********************************/
|
83
|
|
84
|
protected MnTableCreator(String stepName, String firstTableName, String firstTableAlias, String firstColumnName, String secondTableName, String secondTableAlias, String secondColumnName,
|
85
|
String[] columnNames, String[] columnTypes, List<Object> defaultValues, List<Boolean> isNull,
|
86
|
boolean includeAudTable, boolean isList, boolean is1toM,
|
87
|
boolean includeCdmBaseAttributes, boolean includeAnnotatableEntity, boolean includeIdentifiableEntity) {
|
88
|
super(stepName, makeAlias(firstTableName, firstTableAlias) + "_" + makeAlias(secondTableName, secondTableAlias),
|
89
|
Arrays.asList(columnNames), Arrays.asList(columnTypes), defaultValues,
|
90
|
isNull, new ArrayList<>(), includeAudTable,
|
91
|
includeCdmBaseAttributes, includeAnnotatableEntity, includeIdentifiableEntity, false);
|
92
|
this.firstTableName = firstTableName;
|
93
|
this.secondTableName = secondTableName;
|
94
|
this.firstTableAlias = makeAlias(firstTableName, firstTableAlias) ;
|
95
|
this.secondTableAlias = makeAlias(secondTableName, secondTableAlias) ;
|
96
|
this.firstColumnName = (firstColumnName != null) ? firstColumnName : this.firstTableAlias;
|
97
|
this.secondColumnName = (secondColumnName != null) ? secondColumnName : this.secondTableAlias;
|
98
|
this.isList = isList;
|
99
|
this.is1toM = is1toM;
|
100
|
addMyColumns();
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* @param secondTableName
|
105
|
* @param secondTableAlias
|
106
|
* @return
|
107
|
*/
|
108
|
private static String makeAlias(String tableName, String alias) {
|
109
|
return (alias != null) ? alias : tableName;
|
110
|
}
|
111
|
|
112
|
|
113
|
protected void addMyColumns(){
|
114
|
ColumnAdder firstColAdder = ColumnAdder.NewIntegerInstance(stepName, tableName, getFirstIdColumn(), false, true, this.firstTableName);
|
115
|
this.columnAdders.add(firstColAdder);
|
116
|
ColumnAdder secondColAdder = ColumnAdder.NewIntegerInstance(stepName, tableName, getSecondIdColumn(), false, true, this.secondTableName);
|
117
|
// secondColAdder.addIndex(tableName+"_"+getSecondIdColumn(), null);
|
118
|
this.columnAdders.add(secondColAdder);
|
119
|
if (this.isList){
|
120
|
this.columnAdders.add(ColumnAdder.NewIntegerInstance(stepName, tableName, "sortIndex", false, true, null));
|
121
|
}
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
protected String primaryKey(boolean isAudit){
|
126
|
String result = "";
|
127
|
if (! isAudit){
|
128
|
result = getFirstIdColumn() + ",";
|
129
|
result += (isList ? "sortIndex" : getSecondIdColumn());
|
130
|
}else{
|
131
|
result = "REV, " + primaryKey(false);
|
132
|
//for AUDIT also the second table column is in PK
|
133
|
result += (isList) ? ","+getSecondIdColumn() : "";
|
134
|
}
|
135
|
return result;
|
136
|
}
|
137
|
|
138
|
@Override
|
139
|
protected String unique(boolean isAudit){
|
140
|
if (! isAudit && is1toM){
|
141
|
return getSecondIdColumn();
|
142
|
}else{
|
143
|
return null;
|
144
|
}
|
145
|
}
|
146
|
|
147
|
private String getFirstIdColumn(){
|
148
|
return this.firstColumnName + "_id";
|
149
|
}
|
150
|
|
151
|
private String getSecondIdColumn(){
|
152
|
String result = this.secondColumnName.toLowerCase();
|
153
|
|
154
|
if (this.secondColumnName.equalsIgnoreCase(this.secondTableName) ){
|
155
|
if (! result.endsWith("s")){
|
156
|
result += "s";
|
157
|
}
|
158
|
}
|
159
|
result += "_id";
|
160
|
return result;
|
161
|
}
|
162
|
}
|