001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entity.condition;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Comparator;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.ofbiz.entity.GenericEntity;
027: import org.ofbiz.entity.GenericModelException;
028: import org.ofbiz.entity.config.DatasourceInfo;
029: import org.ofbiz.entity.model.ModelEntity;
030:
031: public class OrderByList implements Comparator {
032: protected List orderByList = new ArrayList();
033:
034: public OrderByList() {
035: }
036:
037: public OrderByList(Collection orderByList) {
038: addOrderBy(orderByList);
039: }
040:
041: public void addOrderBy(Collection orderByList) {
042: Iterator it = orderByList.iterator();
043: while (it.hasNext()) {
044: addOrderBy(OrderByItem.parse(it.next()));
045: }
046: }
047:
048: public void addOrderBy(String text) {
049: addOrderBy(OrderByItem.parse(text));
050: }
051:
052: public void addOrderBy(EntityConditionValue value) {
053: addOrderBy(value, false);
054: }
055:
056: public void addOrderBy(EntityConditionValue value,
057: boolean descending) {
058: addOrderBy(new OrderByItem(value, descending));
059: }
060:
061: public void addOrderBy(OrderByItem orderByItem) {
062: orderByList.add(orderByItem);
063: }
064:
065: public void checkOrderBy(ModelEntity modelEntity)
066: throws GenericModelException {
067: for (int i = 0; i < orderByList.size(); i++) {
068: OrderByItem orderByItem = (OrderByItem) orderByList.get(i);
069: orderByItem.checkOrderBy(modelEntity);
070: }
071: }
072:
073: public String makeOrderByString(ModelEntity modelEntity,
074: boolean includeTablenamePrefix,
075: DatasourceInfo datasourceInfo) {
076: StringBuffer sb = new StringBuffer();
077: makeOrderByString(sb, modelEntity, includeTablenamePrefix,
078: datasourceInfo);
079: return sb.toString();
080: }
081:
082: public void makeOrderByString(StringBuffer sb,
083: ModelEntity modelEntity, boolean includeTablenamePrefix,
084: DatasourceInfo datasourceInfo) {
085: if (!orderByList.isEmpty()) {
086: sb.append(" ORDER BY ");
087: }
088: for (int i = 0; i < orderByList.size(); i++) {
089: if (i != 0)
090: sb.append(", ");
091: OrderByItem orderByItem = (OrderByItem) orderByList.get(i);
092: orderByItem.makeOrderByString(sb, modelEntity,
093: includeTablenamePrefix, datasourceInfo);
094: }
095: }
096:
097: public int compare(Object obj1, Object obj2) {
098: return compare((GenericEntity) obj1, (GenericEntity) obj2);
099: }
100:
101: public int compare(GenericEntity entity1, GenericEntity entity2) {
102: int result = 0;
103: for (int i = 0; i < orderByList.size() && result == 0; i++) {
104: OrderByItem orderByItem = (OrderByItem) orderByList.get(i);
105: result = orderByItem.compare(entity1, entity2);
106: }
107: return result;
108: }
109:
110: public boolean equals(java.lang.Object obj) {
111: if (!(obj instanceof OrderByList))
112: return false;
113: OrderByList that = (OrderByList) obj;
114: return orderByList.equals(that.orderByList);
115: }
116:
117: public String toString() {
118: return makeOrderByString(null, false, null);
119: }
120: }
|