-/**\r
- *\r
- */\r
-package eu.etaxonomy.cdm.database.update;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-/**\r
- * Creates an MN table\r
- *\r
- * @author a.mueller\r
- */\r
-public class MnTableCreator extends TableCreator {\r
-\r
- private String firstTableName;\r
- private String firstTableAlias;\r
- private String firstColumnName;\r
- private String secondTableName;\r
- private String secondTableAlias;\r
- private String secondColumnName;\r
- //is the MN table used for a list, if yes, a sortIndex column is needed\r
- //and the sortindex column needs to be in the key instead of second table column.\r
- private boolean isList;\r
- private boolean is1toM;\r
- private String sortIndexOrMapkeyColName;\r
-\r
- public static MnTableCreator NewMnInstance(List<ISchemaUpdaterStep> stepList, String stepName, String firstTableName,\r
- String secondTableName, boolean includeAudTable, boolean isList, boolean is1toM){\r
- MnTableCreator result = new MnTableCreator(stepList, stepName, firstTableName, null, null,\r
- secondTableName, null, null, new String[]{}, new String[]{}, null, null,\r
- includeAudTable, isList, is1toM, false, false, false, null);\r
- return result;\r
- }\r
-\r
- /**\r
- *\r
- * @param stepName The step name\r
- * @param firstTableName The name of the first table\r
- * @param firstTableAlias The alias for the first table as used in the MN table name\r
- * @param secondTableName The name of the second table\r
- * @param secondTableAlias The alias for the second table as used in the MN table name\r
- * @param attributeName The name of the attribute pointing to the second table (this is used for the column name for the\r
- * column pointing to the second table)\r
- * @param includeAudTable <code>true</code> if also the Audit (_AUD) table should be created\r
- * @param hasSortIndex by default <code>false</code> but true for {@link Map maps} (or maybe user defined MN-tables)\r
- * @param secondTableInKey should the column that links to the second table also be in the key? This is by default\r
- * <code>true</code> but for {@link List lists} should be <code>false</code>.\r
- * @return\r
- */\r
- public static MnTableCreator NewMnInstance(List<ISchemaUpdaterStep> stepList, String stepName, String firstTableName, String firstTableAlias,\r
- String secondTableName, String secondTableAlias, String attributeName,\r
- boolean includeAudTable, boolean isList, boolean is1toM){\r
- MnTableCreator result = new MnTableCreator(stepList, stepName, firstTableName, firstTableAlias, null, secondTableName, secondTableAlias, attributeName,\r
- new String[]{}, new String[]{}, null, null,\r
- includeAudTable, isList, is1toM, false, false, false, null);\r
- return result;\r
- }\r
- public static MnTableCreator NewDescriptionInstance(List<ISchemaUpdaterStep> stepList, String stepName, String firstTableName, String firstTableAlias,\r
- String attributeName, boolean includeAudTable){\r
- MnTableCreator result = new MnTableCreator(stepList, stepName, firstTableName, firstTableAlias, null, "LanguageString", null, attributeName,\r
- new String[]{}, new String[]{}, null, null,\r
- includeAudTable, true, true, false, false, false, attributeName + "_mapkey_id");\r
- return result;\r
- }\r
-\r
- /**\r
- *\r
- * @param stepName The step name\r
- * @param firstTableName The name of the first table\r
- * @param firstTableAlias The alias for the first table as used in the MN table name\r
- * @param firstColumnName The name of the attribute pointing to the first table (this is used for the column name for the\r
- * column pointing to the first table)\r
- * @param secondTableName The name of the second table\r
- * @param secondTableAlias The alias for the second table as used in the MN table name\r
- * @param secondColumnName The name of the attribute pointing to the second table (this is used for the column name for the\r
- * column pointing to the second table)\r
- * @param includeAudTable <code>true</code> if also the Audit (_AUD) table should be created\r
- * @param isList by default <code>false</code> but true for {@link Map maps} (or maybe user defined MN-tables)\r
- * @param is1ToM should the column that links to the second table also be in the key? This is by default\r
- * <code>true</code> but for {@link List lists} should be <code>false</code>.\r
- * @return\r
- */\r
- public static MnTableCreator NewMnInstance(List<ISchemaUpdaterStep> stepList, String stepName, String firstTableName, String firstTableAlias, String firstColumnName, String secondTableName, String secondTableAlias, String secondColumnName,\r
- boolean includeAudTable, boolean isList, boolean is1toM){\r
- MnTableCreator result = new MnTableCreator(stepList, stepName, firstTableName, firstTableAlias, firstColumnName, secondTableName, secondTableAlias, secondColumnName,\r
- new String[]{}, new String[]{}, null, null,\r
- includeAudTable, isList, is1toM, false, false, false, null);\r
- return result;\r
- }\r
-\r
-// ****************************** CONSTRUCTOR *********************************/\r
-\r
- protected MnTableCreator(List<ISchemaUpdaterStep> stepList, String stepName, String firstTableName, String firstTableAlias,\r
- String firstColumnName, String secondTableName, String secondTableAlias, String secondColumnName,\r
- String[] columnNames, String[] columnTypes, List<Object> defaultValues, List<Boolean> isNull,\r
- boolean includeAudTable, boolean isList, boolean is1toM,\r
- boolean includeCdmBaseAttributes, boolean includeAnnotatableEntity, boolean includeIdentifiableEntity,\r
- String description_mapkey_id) {\r
- super(stepList, stepName, makeAlias(firstTableName, firstTableAlias) + "_" + makeAlias(secondTableName, secondTableAlias),\r
- Arrays.asList(columnNames), Arrays.asList(columnTypes), defaultValues,\r
- isNull, new ArrayList<>(), includeAudTable,\r
- includeCdmBaseAttributes, includeAnnotatableEntity, includeIdentifiableEntity, false, false);\r
- this.firstTableName = firstTableName;\r
- this.secondTableName = secondTableName;\r
- this.firstTableAlias = makeAlias(firstTableName, firstTableAlias) ;\r
- this.secondTableAlias = makeAlias(secondTableName, secondTableAlias) ;\r
- this.firstColumnName = (firstColumnName != null) ? firstColumnName : this.firstTableAlias;\r
- this.secondColumnName = (secondColumnName != null) ? secondColumnName : this.secondTableAlias;\r
- this.isList = isList;\r
- this.is1toM = is1toM;\r
- this.sortIndexOrMapkeyColName = (description_mapkey_id == null) ? "sortIndex" : description_mapkey_id;\r
- addMyColumns();\r
- }\r
-\r
- /**\r
- * @param secondTableName\r
- * @param secondTableAlias\r
- * @return\r
- */\r
- private static String makeAlias(String tableName, String alias) {\r
- return (alias != null) ? alias : tableName;\r
- }\r
-\r
-\r
- protected void addMyColumns(){\r
- ColumnAdder.NewIntegerInstance(columnAdders, stepName, tableName, getFirstIdColumn(), false, true, this.firstTableName);\r
- ColumnAdder.NewIntegerInstance(columnAdders, stepName, tableName, getSecondIdColumn(), false, true, this.secondTableName);\r
-// secondColAdder.addIndex(tableName+"_"+getSecondIdColumn(), null);\r
- if (this.isList){\r
- ColumnAdder.NewIntegerInstance(columnAdders, stepName, tableName, this.sortIndexOrMapkeyColName, false, true, null);\r
- }\r
- }\r
-\r
- @Override\r
- protected String primaryKey(boolean isAudit){\r
- String result = "";\r
- if (! isAudit){\r
- result = getFirstIdColumn() + ",";\r
- result += (isList ? sortIndexOrMapkeyColName : getSecondIdColumn());\r
- }else{\r
- result = "REV, " + primaryKey(false);\r
- //for AUDIT also the second table column is in PK\r
- result += (isList) ? ","+getSecondIdColumn() : "";\r
- }\r
- return result;\r
- }\r
-\r
- @Override\r
- protected String unique(boolean isAudit){\r
- if (! isAudit && is1toM){\r
- return getSecondIdColumn();\r
- }else{\r
- return null;\r
- }\r
- }\r
-\r
- private String getFirstIdColumn(){\r
- return this.firstColumnName + "_id";\r
- }\r
-\r
- private String getSecondIdColumn(){\r
- String result = this.secondColumnName.toLowerCase();\r
-\r
- if (this.secondColumnName.equalsIgnoreCase(this.secondTableName) ){\r
- if (! result.endsWith("s")){\r
- result += "s";\r
- }\r
- }\r
- result += "_id";\r
- return result;\r
- }\r
-}\r
+/**
+ *
+ */
+package eu.etaxonomy.cdm.database.update;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Creates an MN table
+ *
+ * @author a.mueller
+ */
+public class MnTableCreator extends TableCreator {
+
+ private String firstTableName;
+ private String firstTableAlias;
+ private String firstColumnName;
+ private String secondTableName;
+ private String secondTableAlias;
+ private String secondColumnName;
+ //is the MN table used for a list, if yes, a sortIndex column is needed
+ //and the sortindex column needs to be in the key instead of second table column.
+ private boolean isList;
+ private boolean is1toM;
+ private String sortIndexOrMapkeyColName;
+
+ public static MnTableCreator NewMnInstance(List<ISchemaUpdaterStep> stepList, String stepName, String firstTableName,
+ String secondTableName, boolean includeAudTable, boolean isList, boolean is1toM){
+ MnTableCreator result = new MnTableCreator(stepList, stepName, firstTableName, null, null,
+ secondTableName, null, null, new String[]{}, new String[]{}, null, null,
+ includeAudTable, isList, is1toM, false, false, false, null);
+ return result;
+ }
+
+ /**
+ *
+ * @param stepName The step name
+ * @param firstTableName The name of the first table
+ * @param firstTableAlias The alias for the first table as used in the MN table name
+ * @param secondTableName The name of the second table
+ * @param secondTableAlias The alias for the second table as used in the MN table name
+ * @param attributeName The name of the attribute pointing to the second table (this is used for the column name for the
+ * column pointing to the second table)
+ * @param includeAudTable <code>true</code> if also the Audit (_AUD) table should be created
+ * @param hasSortIndex by default <code>false</code> but true for {@link Map maps} (or maybe user defined MN-tables)
+ * @param secondTableInKey should the column that links to the second table also be in the key? This is by default
+ * <code>true</code> but for {@link List lists} should be <code>false</code>.
+ * @return
+ */
+ public static MnTableCreator NewMnInstance(List<ISchemaUpdaterStep> stepList, String stepName, String firstTableName, String firstTableAlias,
+ String secondTableName, String secondTableAlias, String attributeName,
+ boolean includeAudTable, boolean isList, boolean is1toM){
+ MnTableCreator result = new MnTableCreator(stepList, stepName, firstTableName, firstTableAlias, null, secondTableName, secondTableAlias, attributeName,
+ new String[]{}, new String[]{}, null, null,
+ includeAudTable, isList, is1toM, false, false, false, null);
+ return result;
+ }
+ public static MnTableCreator NewDescriptionInstance(List<ISchemaUpdaterStep> stepList, String stepName, String firstTableName, String firstTableAlias,
+ String attributeName, boolean includeAudTable){
+ MnTableCreator result = new MnTableCreator(stepList, stepName, firstTableName, firstTableAlias, null, "LanguageString", null, attributeName,
+ new String[]{}, new String[]{}, null, null,
+ includeAudTable, true, true, false, false, false, attributeName + "_mapkey_id");
+ return result;
+ }
+
+ /**
+ *
+ * @param stepName The step name
+ * @param firstTableName The name of the first table
+ * @param firstTableAlias The alias for the first table as used in the MN table name
+ * @param firstColumnName The name of the attribute pointing to the first table (this is used for the column name for the
+ * column pointing to the first table)
+ * @param secondTableName The name of the second table
+ * @param secondTableAlias The alias for the second table as used in the MN table name
+ * @param secondColumnName The name of the attribute pointing to the second table (this is used for the column name for the
+ * column pointing to the second table)
+ * @param includeAudTable <code>true</code> if also the Audit (_AUD) table should be created
+ * @param isList by default <code>false</code> but true for {@link Map maps} (or maybe user defined MN-tables)
+ * @param is1ToM should the column that links to the second table also be in the key? This is by default
+ * <code>true</code> but for {@link List lists} should be <code>false</code>.
+ * @return
+ */
+ public static MnTableCreator NewMnInstance(List<ISchemaUpdaterStep> stepList, String stepName, String firstTableName, String firstTableAlias, String firstColumnName, String secondTableName, String secondTableAlias, String secondColumnName,
+ boolean includeAudTable, boolean isList, boolean is1toM){
+ MnTableCreator result = new MnTableCreator(stepList, stepName, firstTableName, firstTableAlias, firstColumnName, secondTableName, secondTableAlias, secondColumnName,
+ new String[]{}, new String[]{}, null, null,
+ includeAudTable, isList, is1toM, false, false, false, null);
+ return result;
+ }
+
+// ****************************** CONSTRUCTOR *********************************/
+
+ protected MnTableCreator(List<ISchemaUpdaterStep> stepList, String stepName, String firstTableName, String firstTableAlias,
+ String firstColumnName, String secondTableName, String secondTableAlias, String secondColumnName,
+ String[] columnNames, String[] columnTypes, List<Object> defaultValues, List<Boolean> isNull,
+ boolean includeAudTable, boolean isList, boolean is1toM,
+ boolean includeCdmBaseAttributes, boolean includeAnnotatableEntity, boolean includeIdentifiableEntity,
+ String description_mapkey_id) {
+ super(stepList, stepName, makeAlias(firstTableName, firstTableAlias) + "_" + makeAlias(secondTableName, secondTableAlias),
+ Arrays.asList(columnNames), Arrays.asList(columnTypes), defaultValues,
+ isNull, new ArrayList<>(), includeAudTable,
+ includeCdmBaseAttributes, includeAnnotatableEntity, includeIdentifiableEntity, false, false);
+ this.firstTableName = firstTableName;
+ this.secondTableName = secondTableName;
+ this.firstTableAlias = makeAlias(firstTableName, firstTableAlias) ;
+ this.secondTableAlias = makeAlias(secondTableName, secondTableAlias) ;
+ this.firstColumnName = (firstColumnName != null) ? firstColumnName : this.firstTableAlias;
+ this.secondColumnName = (secondColumnName != null) ? secondColumnName : this.secondTableAlias;
+ this.isList = isList;
+ this.is1toM = is1toM;
+ this.sortIndexOrMapkeyColName = (description_mapkey_id == null) ? "sortIndex" : description_mapkey_id;
+ addMyColumns();
+ }
+
+ /**
+ * @param secondTableName
+ * @param secondTableAlias
+ * @return
+ */
+ private static String makeAlias(String tableName, String alias) {
+ return (alias != null) ? alias : tableName;
+ }
+
+
+ protected void addMyColumns(){
+ ColumnAdder.NewIntegerInstance(columnAdders, stepName, tableName, getFirstIdColumn(), false, true, this.firstTableName);
+ ColumnAdder.NewIntegerInstance(columnAdders, stepName, tableName, getSecondIdColumn(), false, true, this.secondTableName);
+// secondColAdder.addIndex(tableName+"_"+getSecondIdColumn(), null);
+ if (this.isList){
+ ColumnAdder.NewIntegerInstance(columnAdders, stepName, tableName, this.sortIndexOrMapkeyColName, false, true, null);
+ }
+ }
+
+ @Override
+ protected String primaryKey(boolean isAudit){
+ String result = "";
+ if (! isAudit){
+ result = getFirstIdColumn() + ",";
+ result += (isList ? sortIndexOrMapkeyColName : getSecondIdColumn());
+ }else{
+ result = "REV, " + primaryKey(false);
+ //for AUDIT also the second table column is in PK
+ result += (isList) ? ","+getSecondIdColumn() : "";
+ }
+ return result;
+ }
+
+ @Override
+ protected String unique(boolean isAudit){
+ if (! isAudit && is1toM){
+ return getSecondIdColumn();
+ }else{
+ return null;
+ }
+ }
+
+ private String getFirstIdColumn(){
+ return this.firstColumnName + "_id";
+ }
+
+ private String getSecondIdColumn(){
+ String result = this.secondColumnName.toLowerCase();
+
+ if (this.secondColumnName.equalsIgnoreCase(this.secondTableName) ){
+ if (! result.endsWith("s")){
+ result += "s";
+ }
+ }
+ result += "_id";
+ return result;
+ }
+}