KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querymodel > OrderByNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.db.sql.visualeditor.querymodel;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 /**
26  * Represents a SQL ORDER BY clause
27  */

28 public class OrderByNode implements OrderBy {
29
30     // Fields
31

32     // A vector of generalized column objects (JoinTables)
33

34     ArrayList JavaDoc _sortSpecificationList;
35
36
37     // Constructors
38

39     public OrderByNode() {
40     }
41
42     public OrderByNode(ArrayList JavaDoc sortSpecificationList) {
43         _sortSpecificationList = sortSpecificationList;
44     }
45
46
47     // Methods
48

49     // Return the SQL string that corresponds to this From clause
50
public String JavaDoc genText() {
51         String JavaDoc res = ""; // NOI18N
52
if (_sortSpecificationList != null && _sortSpecificationList.size() > 0) {
53
54             res = "\nORDER BY " + ((SortSpecification)_sortSpecificationList.get(0)).genText(); // NOI18N
55

56             for (int i=1; i<_sortSpecificationList.size(); i++) {
57                 res += ", " + "\n " + // NOI18N
58
((SortSpecification)_sortSpecificationList.get(i)).genText();
59             }
60         }
61
62         return res;
63     }
64
65
66
67     // Methods
68

69     // Accessors/Mutators
70

71     void renameTableSpec(String JavaDoc oldTableSpec, String JavaDoc corrName) {
72         if (_sortSpecificationList != null) {
73             for (int i=0; i<_sortSpecificationList.size(); i++)
74                 ((SortSpecification)_sortSpecificationList.get(i)).renameTableSpec(oldTableSpec, corrName);
75         }
76     }
77
78     public void removeSortSpecification(String JavaDoc tableSpec) {
79         if (_sortSpecificationList != null) {
80             for (int i=0; i<_sortSpecificationList.size(); i++) {
81                 ColumnNode col = (ColumnNode)((SortSpecification)_sortSpecificationList.get(i)).getColumn();
82                 if (col.getTableSpec().equals(tableSpec))
83                 {
84                     _sortSpecificationList.remove(i);
85                     // item from arraylist is removed, reset index value
86
// as remove shifts any subsequent elements to the left
87
// (subtracts one from their indices).
88
i=i-1;
89                 }
90             }
91         }
92     }
93
94     public void removeSortSpecification(String JavaDoc tableSpec, String JavaDoc columnName) {
95         if (_sortSpecificationList != null) {
96             for (int i=0; i<_sortSpecificationList.size(); i++) {
97                 ColumnNode col = (ColumnNode)((SortSpecification)_sortSpecificationList.get(i)).getColumn();
98                 if (col.matches(tableSpec, columnName))
99                 {
100                     _sortSpecificationList.remove(i);
101                     // item from arraylist is removed, reset index value
102
// as remove shifts any subsequent elements to the left
103
// (subtracts one from their indices).
104
i=i-1;
105                 }
106             }
107         }
108     }
109
110     public void addSortSpecification(String JavaDoc tableSpec, String JavaDoc columnName, String JavaDoc direction, int order) {
111         SortSpecification sortSpec = new SortSpecification(new ColumnNode(tableSpec, columnName), direction);
112         // Insert the new one in an appropriate place
113
if (_sortSpecificationList == null)
114             _sortSpecificationList = new ArrayList JavaDoc();
115         _sortSpecificationList.add(order-1, sortSpec);
116     }
117
118     public int getSortSpecificationCount() {
119         return (_sortSpecificationList != null) ? _sortSpecificationList.size() : 0;
120     }
121
122     public SortSpecification getSortSpecification(int i) {
123         return (_sortSpecificationList != null) ? ((SortSpecification)_sortSpecificationList.get(i)) : null;
124     }
125
126     public void getReferencedColumns (Collection JavaDoc columns) {
127         if (_sortSpecificationList != null) {
128             for (int i = 0; i < _sortSpecificationList.size(); i++)
129                 ((SortSpecification)_sortSpecificationList.get(i)).getReferencedColumns(columns);
130         }
131     }
132
133     public void getQueryItems(Collection JavaDoc items) {
134         if (_sortSpecificationList != null)
135             items.addAll(_sortSpecificationList);
136     }
137 }
138
Popular Tags