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 java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
19 import org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
22 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
29 public class SortIndexUpdater
extends SchemaUpdaterStepBase
<SortIndexUpdater
> implements ISchemaUpdaterStep
{
30 private static final Logger logger
= Logger
.getLogger(SortIndexUpdater
.class);
32 private String tableName
;
33 private String sortIndexColumn
;
34 private String parentColumn
;
35 private String idColumn
= "id";
36 private boolean includeAudTable
;
37 private Integer baseValue
= 0;
39 public static final SortIndexUpdater
NewInstance(String stepName
, String tableName
, String parentColumn
, String sortIndexColumn
, boolean includeAudTable
){
40 return new SortIndexUpdater(stepName
, tableName
, parentColumn
, sortIndexColumn
, "id", includeAudTable
, 0);
43 public static final SortIndexUpdater
NewInstance(String stepName
, String tableName
, String parentColumn
, String sortIndexColumn
, String idColumn
, boolean includeAudTable
){
44 return new SortIndexUpdater(stepName
, tableName
, parentColumn
,sortIndexColumn
, idColumn
, includeAudTable
, 0);
48 protected SortIndexUpdater(String stepName
, String tableName
, String parentColumn
, String sortIndexColumn
, String idColumn
, boolean includeAudTable
, Integer baseValue
) {
50 this.tableName
= tableName
;
51 this.parentColumn
= parentColumn
;
52 this.sortIndexColumn
= sortIndexColumn
;
53 this.idColumn
= idColumn
;
54 this.includeAudTable
= includeAudTable
;
55 this.baseValue
= baseValue
;
59 public Integer
invoke(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
60 boolean result
= true;
61 result
&= addColumn(tableName
, datasource
, monitor
);
64 result
&= addColumn(tableName
+ aud
, datasource
, monitor
);
66 return (result
== true )?
0 : null;
69 private boolean addColumn(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
71 Map
<Integer
, Set
<Integer
>> indexMap
= makeIndexMap(datasource
);
73 updateIndices(tableName
, datasource
, indexMap
);
84 * @throws SQLException
86 private Map
<Integer
, Set
<Integer
>> makeIndexMap(ICdmDataSource datasource
) throws SQLException
{
87 String resulsetQuery
= "SELECT @id as id, @parentColumn " +
89 " WHERE @parentColumn IS NOT NULL " +
90 " ORDER BY @parentColumn, @id";
91 resulsetQuery
= resulsetQuery
.replace("@tableName", tableName
);
92 resulsetQuery
= resulsetQuery
.replace("@parentColumn", parentColumn
);
93 resulsetQuery
= resulsetQuery
.replace("@id", idColumn
);
95 ResultSet rs
= datasource
.executeQuery(resulsetQuery
);
96 Integer index
= baseValue
;
100 //increase index with each row, set to 0 if parent is not the same as the previous one
101 Map
<Integer
, Set
<Integer
>> indexMap
= new HashMap
<Integer
, Set
<Integer
>>();
103 int id
= rs
.getInt("id");
104 Object oParentId
= rs
.getObject(parentColumn
);
105 if (oParentId
!= null){
106 int parentId
= Integer
.valueOf(oParentId
.toString());
107 if (oldParentId
!= parentId
){
109 oldParentId
= parentId
;
113 putIndex(id
, index
, indexMap
);
115 logger
.warn("This should not happen");
118 // System.out.println(oParentId + "," + id+","+ index+";");
123 private void updateIndices(String tableName
, ICdmDataSource datasource
, Map
<Integer
, Set
<Integer
>> indexMap
)
124 throws SQLException
{
125 for (Integer index
: indexMap
.keySet()){
126 Set
<Integer
> set
= indexMap
.get(index
);
127 String idSetString
= makeIdSetString(set
);
129 String updateQuery
= "UPDATE @tableName SET @sortIndexColumn = @index WHERE @id IN (@idList) ";
130 updateQuery
= updateQuery
.replace("@tableName", tableName
);
131 updateQuery
= updateQuery
.replace("@sortIndexColumn", sortIndexColumn
);
132 updateQuery
= updateQuery
.replace("@index", index
.toString());
133 updateQuery
= updateQuery
.replace("@idList", idSetString
);
134 updateQuery
= updateQuery
.replace("@id", idColumn
);
135 datasource
.executeUpdate(updateQuery
);
139 private static String
makeIdSetString(Set
<Integer
> set
) {
140 StringBuffer result
= new StringBuffer(set
.size() * 5);
141 for (Integer id
:set
){
142 result
.append(id
+ ",");
144 return result
.substring(0, result
.length() - 1);
148 * Adds the id to the index (each id is attached to an (sort)index)
150 private void putIndex(Integer id
, Integer index
, Map
<Integer
, Set
<Integer
>> indexMap
) {
151 Set
<Integer
> set
= indexMap
.get(index
);
153 set
= new HashSet
<Integer
>();
154 indexMap
.put(index
, set
);