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
.v24_25
;
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
;
23 import eu
.etaxonomy
.cdm
.database
.update
.ISchemaUpdaterStep
;
24 import eu
.etaxonomy
.cdm
.database
.update
.SchemaUpdaterStepBase
;
31 public class SortIndexUpdater
extends SchemaUpdaterStepBase
<SortIndexUpdater
> implements ISchemaUpdaterStep
{
32 @SuppressWarnings("unused")
33 private static final Logger logger
= Logger
.getLogger(SortIndexUpdater
.class);
35 private String tableName
;
36 private String sortIndexColumn
;
37 private String parentColumn
;
38 private boolean includeAudTable
;
39 private Integer baseValue
= 0;
41 public static final SortIndexUpdater
NewInstance(String stepName
, String tableName
, String parentColumn
, String sortIndexColumn
, boolean includeAudTable
){
42 return new SortIndexUpdater(stepName
, tableName
, parentColumn
, sortIndexColumn
, includeAudTable
, 0);
45 protected SortIndexUpdater(String stepName
, String tableName
, String parentColumn
, String sortIndexColumn
, boolean includeAudTable
, Integer baseValue
) {
47 this.tableName
= tableName
;
48 this.parentColumn
= parentColumn
;
49 this.sortIndexColumn
= sortIndexColumn
;
50 this.includeAudTable
= includeAudTable
;
51 this.baseValue
= baseValue
;
55 * @see eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase#invoke(eu.etaxonomy.cdm.database.ICdmDataSource, eu.etaxonomy.cdm.common.IProgressMonitor)
58 public Integer
invoke(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
59 boolean result
= true;
60 result
&= addColumn(tableName
, datasource
, monitor
);
63 result
&= addColumn(tableName
+ aud
, datasource
, monitor
);
65 return (result
== true )?
0 : null;
68 private boolean addColumn(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
70 Map
<Integer
, Set
<Integer
>> indexMap
= makeIndexMap(datasource
);
72 updateIndices(tableName
, datasource
, indexMap
);
83 * @throws SQLException
85 private Map
<Integer
, Set
<Integer
>> makeIndexMap(ICdmDataSource datasource
) throws SQLException
{
86 String resulsetQuery
= "SELECT id, @parentColumn " +
88 " WHERE @parentColumn IS NOT NULL " +
89 " ORDER BY @parentColumn, id";
90 resulsetQuery
= resulsetQuery
.replace("@tableName", tableName
);
91 resulsetQuery
= resulsetQuery
.replace("@parentColumn", parentColumn
);
93 ResultSet rs
= datasource
.executeQuery(resulsetQuery
);
94 Integer index
= baseValue
;
98 Map
<Integer
, Set
<Integer
>> indexMap
= new HashMap
<Integer
, Set
<Integer
>>();
102 int id
= rs
.getInt("id");
103 Object oParentId
= rs
.getObject(parentColumn
);
104 if (oParentId
!= null){
105 int parentId
= Integer
.valueOf(oParentId
.toString());
106 if (oldParentId
!= parentId
){
108 oldParentId
= parentId
;
112 putIndex(id
, index
, indexMap
);
114 logger
.warn("This should not happen");
117 // System.out.println(oParentId + "," + id+","+ index+";");
122 private void updateIndices(String tableName
, ICdmDataSource datasource
, Map
<Integer
, Set
<Integer
>> indexMap
)
123 throws SQLException
{
124 for (Integer index
: indexMap
.keySet()){
125 Set
<Integer
> set
= indexMap
.get(index
);
126 String idSetString
= makeIdSetString(set
);
128 String updateQuery
= "UPDATE @tableName SET @sortIndexColumn = @index WHERE id IN (@idList) ";
129 updateQuery
= updateQuery
.replace("@tableName", tableName
);
130 updateQuery
= updateQuery
.replace("@sortIndexColumn", sortIndexColumn
);
131 updateQuery
= updateQuery
.replace("@index", index
.toString());
132 updateQuery
= updateQuery
.replace("@idList", idSetString
);
133 datasource
.executeUpdate(updateQuery
);
137 private static String
makeIdSetString(Set
<Integer
> set
) {
138 StringBuffer result
= new StringBuffer(set
.size() * 5);
139 for (Integer id
:set
){
140 result
.append(id
+ ",");
142 return result
.substring(0, result
.length() - 1);
145 private void putIndex(Integer id
, Integer index
, Map
<Integer
, Set
<Integer
>> indexMap
) {
146 Set
<Integer
> set
= indexMap
.get(index
);
148 set
= new HashSet
<Integer
>();
149 indexMap
.put(index
, set
);