View Javadoc

1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
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   * JdbcCpoOrderBy is an interface for specifying the sort order in which objects are returned from the Datasource.
30   *
31   * @author david berry
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     * Gets the boolean that determines if the objects will be returned from from the CpoAdapter in Ascending order or
72     * Descending order
73     *
74     * @return boolean true if it is to sort in Ascensing Order false if it is to be sorted in Descending Order
75     */
76    @Override
77    public boolean getAscending() {
78      return this.ascending;
79    }
80  
81    /**
82     * Gets the name of the attribute that is to be used to sort the results from the CpoAdapter.
83     *
84     * @return String The name of the attribute
85     */
86    @Override
87    public String getAttribute() {
88      return this.attribute;
89    }
90  
91    /**
92     * Gets a string representing a datasource specific function call that must be applied to the attribute that will be
93     * used for sorting.
94     *
95     * i.e. - "upper(attribute_name)"
96     *
97     * @return String The name of the function
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 }