Source code: org/objectstyle/cayenne/access/util/SortHandler.java
1 package org.objectstyle.cayenne.access.util;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.objectstyle.cayenne.CayenneRuntimeException;
8 import org.objectstyle.cayenne.access.QueryEngine;
9 import org.objectstyle.cayenne.map.DataMap;
10
11 /**
12 * Defines a set of sorting methods based on object dependencies. The actual
13 * dependency tracking algorithm is pluggable via a sporter class.
14 *
15 * @author Andrei Adamchik
16 */
17 public class SortHandler {
18 protected static Class defaultSorterClass = DefaultSorter.class;
19
20 protected DependencySorter sorter;
21
22 public static Class getDefaultSorterClass() {
23 return defaultSorterClass;
24 }
25
26 /**
27 * Sets the class of the sorter that should be used as a source of
28 * dependency sorting algorithms. Class must be an instance of
29 * DependencySorter and must have a default no-argument constructor.
30 *
31 * @param sorterClass The sorterClass to set
32 */
33 public static void setDefaultSorterClass(Class defaultSorterClass) {
34 SortHandler.defaultSorterClass = defaultSorterClass;
35 }
36
37 /**
38 * Constructor for SortHandler.
39 */
40 public SortHandler(QueryEngine queryEngine, DataMap[] dataMaps) {
41 this(defaultSorterClass, queryEngine, dataMaps);
42 }
43
44 /**
45 * Creates SortHandler with a specified sorter class.
46 */
47 public SortHandler(
48 Class sorterClass,
49 QueryEngine queryEngine,
50 DataMap[] dataMaps) {
51
52 // sanity check
53 if (sorterClass == null) {
54 throw new IllegalArgumentException("DependencySorter class can not be null.");
55 }
56
57 if (!DependencySorter.class.isAssignableFrom(sorterClass)) {
58 throw new IllegalArgumentException(
59 "Sorter class "
60 + sorterClass.getName()
61 + " must implement DependencySorter interface.");
62 }
63
64 try {
65 sorter = (DependencySorter) sorterClass.newInstance();
66 } catch (Exception ex) {
67 throw new CayenneRuntimeException(
68 "Error instantiating sorter from " + sorterClass.getName(),
69 ex);
70 }
71
72 sorter.initSorter(queryEngine, dataMaps);
73 }
74
75 /**
76 * Creates and returns an array of queries in the right sorting order from
77 * an unsorted array.
78 */
79 public List sortedQueries(List unsortedQueries) {
80 Object[] array = unsortedQueries.toArray();
81 Arrays.sort(array, sorter.getQueryComparator());
82 return Arrays.asList(array);
83 }
84
85 /**
86 * Returns a new list containing all the DbEntities in
87 * <code>entities</code>, in the correct order for inserting objects int,o
88 * or creating the tables of, those entities.
89 */
90 public List sortedDbEntitiesInInsertOrder(List dbEntities) {
91 Object[] array = dbEntities.toArray();
92 Arrays.sort(array, sorter.getDbEntityComparator(true));
93 return Arrays.asList(array);
94 }
95
96 /**
97 * Returns a new list containing all the DbEntities in <code>entities</code>,
98 * in the correct order for deleting objects from or removing the tables of, those entities.
99 */
100 public List sortedDbEntitiesInDeleteOrder(List dbEntities) {
101 Object[] array = dbEntities.toArray();
102 Arrays.sort(array, sorter.getDbEntityComparator(false));
103 return Arrays.asList(array);
104 }
105
106 /**
107 * Sorts an unsorted array of DataObjects in the right
108 * insert order for database constraints not to be violated,
109 * and so that master pk's for dependent relationships
110 * are in place prior to being needed for the dependent
111 * object, and reflexive relationships are correctly handled
112 */
113 public void sortObjectsInInsertOrder(List objects) {
114 Collections.sort(objects, sorter.getDataObjectComparator(true));
115 }
116
117 /**
118 * Sorts an unsorted array of DataObjects in the right
119 * delete order for database constraints not to be violated,
120 * and so that master pk's for dependent relationships
121 * are in place prior to being needed for the dependent
122 * object and reflexive relationships are correctly handled
123 */
124 public void sortObjectsInDeleteOrder(List objects) {
125 Collections.sort(objects, sorter.getDataObjectComparator(false));
126 }
127 }