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