3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.database
.update
;
12 import org
.apache
.log4j
.Logger
;
14 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
15 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
16 import eu
.etaxonomy
.cdm
.model
.common
.ITreeNode
;
23 public class TreeIndexUpdater
extends AuditedSchemaUpdaterStepBase
<TreeIndexUpdater
> implements ISchemaUpdaterStep
{
24 private static final Logger logger
= Logger
.getLogger(TreeIndexUpdater
.class);
26 private String indexColumnName
= "treeIndex";
27 private String treeIdColumnName
;
28 private String parentIdColumnName
= "parent_id";
30 public static final TreeIndexUpdater
NewInstance(String stepName
, String tableName
, String treeIdColumnName
, boolean includeAudTable
){
31 return new TreeIndexUpdater(stepName
, tableName
, treeIdColumnName
, null, includeAudTable
);
34 public static final TreeIndexUpdater
NewInstance(String stepName
, String tableName
, String treeIdColumnName
, String indexColumnName
, boolean includeAudTable
){
35 return new TreeIndexUpdater(stepName
, tableName
, treeIdColumnName
, indexColumnName
, includeAudTable
);
39 protected TreeIndexUpdater(String stepName
, String tableName
, String treeIdColumnName
, String indexColumnName
, boolean includeAudTable
) {
41 this.tableName
= tableName
;
42 this.treeIdColumnName
= treeIdColumnName
;
43 this.indexColumnName
= indexColumnName
== null ?
this.indexColumnName
: indexColumnName
;
44 this.includeAudTable
= includeAudTable
;
48 protected boolean invokeOnTable(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) {
50 boolean result
= true;
52 // String charType = "CHAR"; //TODO may depend on database type
54 //clean up nodes without classification //this should not happen with correct "delete" implementation
55 String sql
= String
.format(" DELETE FROM %s WHERE %s IS NULL ", tableName
, treeIdColumnName
);
56 datasource
.executeUpdate(sql
);
58 //... set all index entries to NULL
59 sql
= String
.format(" UPDATE %s SET %s = NULL", tableName
, indexColumnName
);
60 datasource
.executeUpdate(sql
);
63 String separator
= ITreeNode
.separator
;
64 String treePrefix
= ITreeNode
.treePrefix
;
65 sql
= String
.format(" UPDATE %s tn " +
66 " SET tn.%s = CONCAT('%s%s', tn.%s, '%s', tn.id, '%s') " +
67 " WHERE tn.%s IS NULL AND tn.%s IS NOT NULL ",
69 indexColumnName
, separator
, treePrefix
, treeIdColumnName
, separator
, separator
,
70 parentIdColumnName
, treeIdColumnName
);
71 datasource
.executeUpdate(sql
);
73 //width search index creation
74 String sqlCount
= String
.format(" SELECT count(*) as n " +
75 " FROM %s child INNER JOIN %s parent ON child.%s = parent.id " +
76 " WHERE parent.%s IS NOT NULL AND child.%s IS NULL ",
77 tableName
, tableName
, parentIdColumnName
, indexColumnName
, indexColumnName
);
82 sql
= String
.format(" UPDATE %s child INNER JOIN %s parent ON child.%s = parent.id " +
83 " SET child.%s = CONCAT( parent.%s, child.id, '%s') " +
84 " WHERE parent.%s IS NOT NULL AND child.%s IS NULL ",
85 tableName
, tableName
, parentIdColumnName
,
86 indexColumnName
, indexColumnName
, separator
,
87 indexColumnName
, indexColumnName
);
88 datasource
.executeUpdate(sql
);
90 n
= (Long
)datasource
.getSingleValue(sqlCount
);
93 sqlCount
= String
.format(" SELECT count(*) as n " +
95 " WHERE %s IS NULL ", tableName
, indexColumnName
);
96 n
= (Long
)datasource
.getSingleValue(sqlCount
);
98 String message
= "There are tree nodes with no tree index in %s. This indicates that there is a problem in the tree structure of 1 or more classifications.";
99 logger
.error(String
.format(message
, tableName
));
104 monitor
.warning(e
.getMessage(), e
);
105 logger
.error(e
.getMessage());