1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.synchronoss.cpo.jdbc;
22
23 import org.synchronoss.cpo.CpoException;
24 import org.synchronoss.cpo.CpoOrderBy;
25 import org.synchronoss.cpo.meta.domain.CpoAttribute;
26 import org.synchronoss.cpo.meta.domain.CpoClass;
27
28
29
30
31
32
33 public class JdbcCpoOrderBy implements CpoOrderBy {
34
35 private boolean ascending;
36 private String attribute;
37 private String function;
38 private String marker = DEFAULT_MARKER;
39
40 @SuppressWarnings("unused")
41 private JdbcCpoOrderBy() {
42 }
43
44 public JdbcCpoOrderBy(String attr, boolean asc) {
45 ascending = asc;
46 attribute = attr;
47 function = null;
48 }
49
50 public JdbcCpoOrderBy(String marker, String attr, boolean asc) {
51 this.marker = marker;
52 ascending = asc;
53 attribute = attr;
54 function = null;
55 }
56
57 public JdbcCpoOrderBy(String attr, boolean asc, String func) {
58 ascending = asc;
59 attribute = attr;
60 function = func;
61 }
62
63 public JdbcCpoOrderBy(String marker, String attr, boolean asc, String func) {
64 this.marker = marker;
65 ascending = asc;
66 attribute = attr;
67 function = func;
68 }
69
70
71
72
73
74
75
76 @Override
77 public boolean getAscending() {
78 return this.ascending;
79 }
80
81
82
83
84
85
86 @Override
87 public String getAttribute() {
88 return this.attribute;
89 }
90
91
92
93
94
95
96
97
98
99 @Override
100 public String getFunction() {
101 return this.function;
102 }
103
104 @Override
105 public String toString(CpoClass cpoClass) throws CpoException {
106 StringBuilder sb = new StringBuilder();
107 String column;
108 int attrOffset;
109 int fromIndex = 0;
110
111 if (attribute != null && attribute.length() > 0) {
112 CpoAttribute jdbcAttribute = cpoClass.getAttributeJava(attribute);
113 if (jdbcAttribute == null) {
114 throw new CpoException(attribute);
115 }
116 sb.append(" ");
117
118 column = jdbcAttribute.getDataName();
119 if (column != null && column.length() > 0) {
120 if (function != null && function.length() > 0) {
121 while ((attrOffset = function.indexOf(attribute, fromIndex)) != -1) {
122 sb.append(function.substring(0, attrOffset));
123 sb.append(column);
124 fromIndex += attrOffset + attribute.length();
125 }
126 sb.append(function.substring(fromIndex));
127 } else {
128 sb.append(column);
129 }
130 }
131
132 if (this.getAscending()) {
133 sb.append(" ASC");
134 } else {
135 sb.append(" DESC");
136 }
137 }
138
139 return sb.toString();
140 }
141
142 @Override
143 public String getMarker() {
144 return marker;
145 }
146
147 }