/**\r
* Removes a given term if it is not in use.\r
* TODO does not yet check all DefinedTermBase_XXX tables except for representations.\r
- * Does also not handle AUD tables\r
+ * Does also not handle AUD tables => should probably be handled as DeleteEvent, not simply delete the record;\r
+ * There is now a factory method for handling AUDs.\r
*\r
* @author a.mueller\r
* @since 06.09.2013\r
\r
private String uuidTerm ;\r
private List<String> checkUsedQueries = new ArrayList<>();\r
+ private boolean isAudit = false;\r
\r
@SuppressWarnings("unused")\r
private static final Logger logger = Logger.getLogger(SingleTermRemover.class);\r
\r
public static final SingleTermRemover NewInstance(List<ISchemaUpdaterStep> stepList, String stepName,\r
String uuidTerm, List<String> checkUsedQueries, int adapt){\r
- return new SingleTermRemover(stepList, stepName, uuidTerm, checkUsedQueries);\r
+ return new SingleTermRemover(stepList, stepName, uuidTerm, checkUsedQueries, false);\r
}\r
\r
/**\r
String uuidTerm, String firstCheckUsedQuery, int adapt){\r
List<String> checkUsedQueries = new ArrayList<>();\r
checkUsedQueries.add(firstCheckUsedQuery);\r
- return new SingleTermRemover(stepList, stepName, uuidTerm, checkUsedQueries);\r
+ return new SingleTermRemover(stepList, stepName, uuidTerm, checkUsedQueries, false);\r
}\r
\r
- private SingleTermRemover(List<ISchemaUpdaterStep> stepList, String stepName, String uuidTerm, List<String> checkUsedQueries) {\r
+ /**\r
+ * @param firstCheckUsedQuery The first query to check if this term is used. Must return a single int value > 0\r
+ * if this term is used at the given place.\r
+ * @return\r
+ */\r
+ public static final SingleTermRemover NewAudInstance(List<ISchemaUpdaterStep> stepList, String stepName,\r
+ String uuidTerm, String firstCheckUsedQuery, int adapt){\r
+ List<String> checkUsedQueries = new ArrayList<>();\r
+ checkUsedQueries.add(firstCheckUsedQuery);\r
+ return new SingleTermRemover(stepList, stepName, uuidTerm, checkUsedQueries, true);\r
+ }\r
+\r
+ private SingleTermRemover(List<ISchemaUpdaterStep> stepList, String stepName, String uuidTerm,\r
+ List<String> checkUsedQueries, boolean isAudit) {\r
super(stepList, stepName);\r
this.uuidTerm = uuidTerm;\r
this.checkUsedQueries = checkUsedQueries;\r
+ this.isAudit = isAudit;\r
}\r
\r
@Override\r
public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,\r
CaseType caseType, SchemaUpdateResult result) throws SQLException {\r
+\r
//get term id\r
String sql = " SELECT id FROM %s WHERE uuid = '%s'";\r
+ String tableName = isAudit ? "DefinedTermBase_AUD" : "DefinedTermBase";\r
Integer id = (Integer)datasource.getSingleValue(String.format(sql,\r
- caseType.transformTo("DefinedTermBase"), this.uuidTerm));\r
+ caseType.transformTo(tableName), this.uuidTerm));\r
if (id == null || id == 0){\r
return;\r
}\r
try {\r
//get representation ids\r
List<Integer> repIDs = new ArrayList<>();\r
- getRepIds(datasource, id, repIDs, "representations_id", "DefinedTermBase_Representation", caseType );\r
- getRepIds(datasource, id, repIDs, "inverserepresentations_id", "DefinedTermBase_InverseRepresentation", caseType);\r
+ String tableName = isAudit ? "DefinedTermBase_Representation_AUD" : "DefinedTermBase_Representation";\r
+ String inverseTableName = isAudit ? "DefinedTermBase_InverseRepresentation_AUD" : "DefinedTermBase_InverseRepresentation";\r
+ getRepIds(datasource, id, repIDs, "representations_id", tableName, caseType );\r
+ getRepIds(datasource, id, repIDs, "inverserepresentations_id", inverseTableName, caseType);\r
\r
//remove from MN table\r
String sql = " DELETE FROM %s WHERE DefinedTermBase_id = %d";\r
- sql = String.format(sql, caseType.transformTo("DefinedTermBase_Representation"), id);\r
+ sql = String.format(sql, caseType.transformTo(tableName), id);\r
datasource.executeUpdate(sql);\r
sql = " DELETE FROM %s WHERE DefinedTermBase_id = %d";\r
- sql = String.format(sql, caseType.transformTo("DefinedTermBase_InverseRepresentation"), id);\r
+ sql = String.format(sql, caseType.transformTo(tableName), id);\r
datasource.executeUpdate(sql);\r
\r
//remove representations\r
+ tableName = isAudit ? "Representation_AUD" : "Representation";\r
for (Integer repId : repIDs){\r
sql = " DELETE FROM %s WHERE id = %d ";\r
sql = String.format(sql,\r
- caseType.transformTo("Representation"),\r
+ caseType.transformTo(tableName),\r
repId);\r
datasource.executeUpdate(sql);\r
}\r
\r
//remove term\r
+ tableName = isAudit ? "DefinedTermBase_AUD" : "DefinedTermBase";\r
sql = " DELETE FROM %s WHERE id = %d";\r
sql = String.format(sql,\r
- caseType.transformTo("DefinedTermBase"),\r
+ caseType.transformTo(tableName),\r
id);\r
datasource.executeUpdate(sql);\r
} catch (SQLException e) {\r