001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.commonimpl.schema;
017:
018: import org.outerj.daisy.repository.schema.QuerySelectionList;
019: import org.outerj.daisy.repository.schema.ListItem;
020: import org.outerj.daisy.repository.query.SortOrder;
021: import org.outerj.daisy.repository.*;
022: import org.outerx.daisy.x10.*;
023:
024: import java.util.Locale;
025: import java.util.List;
026: import java.util.ArrayList;
027: import java.util.Collections;
028:
029: public class QuerySelectionListImpl implements QuerySelectionList {
030: private String query;
031: private boolean filterVariants;
032: private SortOrder sortOrder;
033: private FieldTypeImpl owner;
034:
035: public QuerySelectionListImpl(String query, boolean filterVariants,
036: SortOrder sortOrder, FieldTypeImpl owner) {
037: checkQueryParam(query);
038: this .query = query;
039: this .filterVariants = filterVariants;
040: this .sortOrder = sortOrder;
041: if (this .sortOrder == null)
042: this .sortOrder = SortOrder.ASCENDING;
043: this .owner = owner;
044: }
045:
046: public String getQuery() {
047: return query;
048: }
049:
050: public SortOrder getSortOrder() {
051: return sortOrder;
052: }
053:
054: public void setQuery(String query) {
055: if (owner.isReadOnly())
056: throw new RuntimeException(FieldTypeImpl.READ_ONLY_MESSAGE);
057:
058: checkQueryParam(query);
059: this .query = query;
060: }
061:
062: public void setSortOrder(SortOrder sortOrder) {
063: if (owner.isReadOnly())
064: throw new RuntimeException(FieldTypeImpl.READ_ONLY_MESSAGE);
065:
066: if (sortOrder == null)
067: throw new IllegalArgumentException(
068: "sortOrder arg cannot be null");
069: this .sortOrder = sortOrder;
070: }
071:
072: public boolean getFilterVariants() {
073: return filterVariants;
074: }
075:
076: public void setFilterVariants(boolean filterVariants) {
077: if (owner.isReadOnly())
078: throw new RuntimeException(FieldTypeImpl.READ_ONLY_MESSAGE);
079: this .filterVariants = filterVariants;
080: }
081:
082: private void checkQueryParam(String query) {
083: if (query == null || query.trim().length() == 0)
084: throw new IllegalArgumentException(
085: "query argument cannot be null, empty or whitespace");
086: }
087:
088: public QuerySelectionListDocument getXml() {
089: QuerySelectionListDocument listDocument = QuerySelectionListDocument.Factory
090: .newInstance();
091: QuerySelectionListDocument.QuerySelectionList listXml = listDocument
092: .addNewQuerySelectionList();
093: listXml.setQuery(query);
094: listXml.setFilterVariants(filterVariants);
095: listXml.setSortOrder(sortOrder.toString());
096: return listDocument;
097: }
098:
099: public List<? extends ListItem> getItems() {
100: throw new RuntimeException(
101: "This method should only be called through QuerySelectionListWrapper");
102: }
103:
104: public List<? extends ListItem> getItems(Repository repository) {
105: if (filterVariants)
106: throw new RepositoryRuntimeException(
107: "This selection list needs to filter on variants, so use getItems(branchId, languageId) instead.");
108: return getItems(-1, -1, Locale.US, repository);
109: }
110:
111: public List<? extends ListItem> getItems(long branchId,
112: long languageId, Locale locale) {
113: throw new RuntimeException(
114: "This method should only be called through QuerySelectionListWrapper");
115: }
116:
117: public List<? extends ListItem> getItems(long branchId,
118: long languageId, Locale locale, Repository repository) {
119: DistinctSearchResultDocument searchResultDocument;
120: try {
121: if (filterVariants) {
122: searchResultDocument = repository
123: .getQueryManager()
124: .performDistinctQuery(
125: query,
126: "branchId = " + branchId
127: + " and languageId = "
128: + languageId, sortOrder, locale);
129: } else {
130: searchResultDocument = repository.getQueryManager()
131: .performDistinctQuery(query, sortOrder, locale);
132: }
133: } catch (RepositoryException e) {
134: throw new RuntimeException(
135: "Error executing query selection list query: "
136: + query, e);
137: }
138: String resultValueTypeString = searchResultDocument
139: .getDistinctSearchResult().getValues().getValueType();
140: ValueType resultValueType = ValueType
141: .fromString(resultValueTypeString);
142:
143: if (resultValueType != owner.getValueType())
144: throw new RuntimeException(
145: "Values returned from selection list query are of a different type ("
146: + resultValueType.toString()
147: + ") then the field type ("
148: + owner.getValueType().toString() + ").");
149:
150: List<DistinctSearchResultDocument.DistinctSearchResult.Values.Value> valuesXml = searchResultDocument
151: .getDistinctSearchResult().getValues().getValueList();
152: List<QueryListItem> items = new ArrayList<QueryListItem>(
153: valuesXml.size());
154: FieldHelper.XmlFieldValueGetter valueGetter = FieldHelper
155: .getXmlFieldValueGetter(resultValueType);
156: for (DistinctSearchResultDocument.DistinctSearchResult.Values.Value valueXml : valuesXml) {
157: items.add(new QueryListItem(valueGetter.getValue(valueXml,
158: false), valueXml.getLabel()));
159: }
160: return Collections.unmodifiableList(items);
161: }
162:
163: public String getLabel(Object value, Locale locale) {
164: return getItemLabel(value, locale);
165: }
166:
167: public String getItemLabel(Object value, Locale locale) {
168: return null;
169: }
170:
171: public void addToFieldTypeXml(
172: SelectionListDocument.SelectionList selectionListXml) {
173: selectionListXml.setQuerySelectionList(getXml()
174: .getQuerySelectionList());
175: }
176:
177: public boolean equals(Object obj) {
178: if (!(obj instanceof QuerySelectionListImpl))
179: return false;
180:
181: QuerySelectionListImpl other = (QuerySelectionListImpl) obj;
182:
183: if (filterVariants != other.filterVariants)
184: return false;
185:
186: if (sortOrder != other.sortOrder)
187: return false;
188:
189: return query.equals(other.query);
190: }
191: }
|