1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.dataaccess.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.ojb.broker.PersistenceBroker;
20 import org.apache.ojb.broker.metadata.ClassDescriptor;
21 import org.apache.ojb.broker.metadata.CollectionDescriptor;
22 import org.apache.ojb.broker.metadata.FieldDescriptor;
23 import org.apache.ojb.broker.platforms.PlatformMySQLImpl;
24 import org.apache.ojb.broker.query.Query;
25 import org.apache.ojb.broker.query.QueryByCriteria;
26
27 import java.util.Map;
28
29
30
31
32
33
34
35
36
37 public class PurapItemQueryCustomizer extends KualiQueryCustomizerDefaultImpl {
38 protected static final String MYSQL_NEGATION = "-";
39 public final static String ORDER_BY_FIELD = "orderByField.";
40 public final static String ASCENDING = "ASC";
41 public final static String DESCENDING = "DESC";
42
43
44
45
46
47
48
49
50 @Override
51 public Query customizeQuery(Object anObject, PersistenceBroker broker, CollectionDescriptor cod, QueryByCriteria query) {
52 boolean platformMySQL = broker.serviceSqlGenerator().getPlatform() instanceof PlatformMySQLImpl;
53
54 Map<String, String> attributes = getAttributes();
55 for (String attributeName : attributes.keySet()) {
56 if (!attributeName.startsWith(ORDER_BY_FIELD)) {
57 continue;
58 }
59
60 String fieldName = attributeName.substring(ORDER_BY_FIELD.length());
61 ClassDescriptor itemClassDescriptor = broker.getClassDescriptor(cod.getItemClass());
62 FieldDescriptor orderByFieldDescriptior = itemClassDescriptor.getFieldDescriptorByName(fieldName);
63
64
65 String orderByColumnName = orderByFieldDescriptior.getColumnName();
66
67
68 String fieldValue = attributes.get(attributeName);
69 boolean ascending = (StringUtils.equals(fieldValue, ASCENDING));
70
71 if (!ascending && StringUtils.equals(fieldValue, DESCENDING)) {
72 throw new RuntimeException("neither ASC nor DESC was specified in ojb file for " + fieldName);
73 }
74
75 if (platformMySQL) {
76
77 String mysqlPrefix = (ascending) ? MYSQL_NEGATION : "";
78 query.addOrderBy(mysqlPrefix + orderByColumnName, false);
79 } else {
80 query.addOrderBy(orderByColumnName, ascending);
81 }
82 }
83 return query;
84 }
85 }