Project

General

Profile

Download (5.5 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
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.
9
*/
10
package eu.etaxonomy.cdm.database.update;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Map;
17
import java.util.Set;
18

    
19
import org.apache.log4j.Logger;
20

    
21
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
22
import eu.etaxonomy.cdm.database.ICdmDataSource;
23

    
24
/**
25
 * @author a.mueller
26
 * @date 16.09.2010
27
 *
28
 */
29
public class SortIndexUpdater extends SchemaUpdaterStepBase<SortIndexUpdater> implements ISchemaUpdaterStep {
30
	private static final Logger logger = Logger.getLogger(SortIndexUpdater.class);
31

    
32
	private final String tableName;
33
	private final String sortIndexColumn;
34
	private final String parentColumn;
35
	private String idColumn = "id";
36
	private final boolean includeAudTable;
37
	private Integer baseValue = 0;
38

    
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);
41
	}
42

    
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);
45
	}
46

    
47

    
48
	protected SortIndexUpdater(String stepName, String tableName, String parentColumn, String sortIndexColumn, String idColumn, boolean includeAudTable, Integer baseValue) {
49
		super(stepName);
50
		this.tableName = tableName;
51
		this.parentColumn = parentColumn;
52
		this.sortIndexColumn = sortIndexColumn;
53
		this.idColumn = idColumn;
54
		this.includeAudTable = includeAudTable;
55
		this.baseValue = baseValue;
56
	}
57

    
58
	@Override
59
	public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
60
		boolean result = true;
61
		result &= addColumn(caseType.transformTo(tableName), datasource, monitor, caseType);
62
		if (includeAudTable){
63
			String aud = "_AUD";
64
			result &= addColumn(caseType.transformTo(tableName + aud), datasource, monitor, caseType);
65
		}
66
		return (result == true )? 0 : null;
67
	}
68

    
69
	private boolean addColumn(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
70
		//Note: caseType not required here
71
		Map<Integer, Set<Integer>> indexMap = makeIndexMap(tableName, datasource);
72

    
73
		updateIndices(tableName, datasource, indexMap);
74

    
75
		return true;
76
	}
77

    
78
	/**
79
	 * @param tableName
80
	 * @param datasource
81
	 * @param rs
82
	 * @param index
83
	 * @param oldParentId
84
	 * @return
85
	 * @throws SQLException
86
	 */
87
	private Map<Integer, Set<Integer>> makeIndexMap(String tableName, ICdmDataSource datasource) throws SQLException {
88
		String resulsetQuery = "SELECT @id as id, @parentColumn " +
89
				" FROM @tableName " +
90
				" WHERE @parentColumn IS NOT NULL " +
91
				" ORDER BY @parentColumn,    @id";
92
		resulsetQuery = resulsetQuery.replace("@tableName", tableName);
93
		resulsetQuery = resulsetQuery.replace("@parentColumn", parentColumn);
94
		resulsetQuery = resulsetQuery.replace("@id", idColumn);
95

    
96
		ResultSet rs = datasource.executeQuery(resulsetQuery);
97
		Integer index = baseValue;
98
		int oldParentId = -1;
99

    
100

    
101
		//increase index with each row, set to 0 if parent is not the same as the previous one
102
		Map<Integer, Set<Integer>> indexMap = new HashMap<Integer, Set<Integer>>();
103
		while (rs.next() ){
104
			int id = rs.getInt("id");
105
			Object oParentId = rs.getObject(parentColumn);
106
			if (oParentId != null){
107
				int parentId = Integer.valueOf(oParentId.toString());
108
				if (oldParentId != parentId){
109
					index = baseValue;
110
					oldParentId = parentId;
111
				}else{
112
					index++;
113
				}
114
				putIndex(id, index, indexMap);
115
			}else{
116
				logger.warn("This should not happen");
117
				index = baseValue;
118
			}
119
//			System.out.println(oParentId + "," + id+","+ index+";");
120
		}
121
		return indexMap;
122
	}
123

    
124
	private void updateIndices(String tableName, ICdmDataSource datasource, Map<Integer, Set<Integer>> indexMap)
125
			throws SQLException {
126
		for (Integer index :  indexMap.keySet()){
127
			Set<Integer> set = indexMap.get(index);
128
			String idSetString = makeIdSetString(set);
129

    
130
			String updateQuery = "UPDATE @tableName SET @sortIndexColumn = @index WHERE @id IN (@idList) ";
131
			updateQuery = updateQuery.replace("@tableName", tableName);
132
			updateQuery = updateQuery.replace("@sortIndexColumn", sortIndexColumn);
133
			updateQuery = updateQuery.replace("@index", index.toString());
134
			updateQuery = updateQuery.replace("@idList", idSetString);
135
			updateQuery = updateQuery.replace("@id", idColumn);
136
			datasource.executeUpdate(updateQuery);
137
		}
138
	}
139

    
140
	private static String makeIdSetString(Set<Integer> set) {
141
		StringBuffer result = new StringBuffer(set.size() * 5);
142
		for (Integer id:set){
143
			result.append(id + ",");
144
		}
145
		return result.substring(0, result.length() - 1);
146
	}
147

    
148
	/**
149
	 * Adds the id to the index (each id is attached to an (sort)index)
150
	 */
151
	private void putIndex(Integer id, Integer index, Map<Integer, Set<Integer>> indexMap) {
152
		Set<Integer> set = indexMap.get(index);
153
		if (set == null){
154
			set = new HashSet<Integer>();
155
			indexMap.put(index, set);
156
		}
157
		set.add(id);
158
	}
159

    
160
}
(24-24/34)