KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > schema > QuerySelectionListImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.commonimpl.schema;
17
18 import org.outerj.daisy.repository.schema.QuerySelectionList;
19 import org.outerj.daisy.repository.schema.ListItem;
20 import org.outerj.daisy.repository.query.QueryManager;
21 import org.outerj.daisy.repository.query.SortOrder;
22 import org.outerj.daisy.repository.RepositoryException;
23 import org.outerj.daisy.repository.VariantKey;
24 import org.outerj.daisy.repository.ValueType;
25 import org.outerj.daisy.repository.RepositoryRuntimeException;
26 import org.outerx.daisy.x10.*;
27
28 import java.util.Locale JavaDoc;
29
30 public class QuerySelectionListImpl implements QuerySelectionList {
31     private String JavaDoc query;
32     private boolean filterVariants;
33     private SortOrder sortOrder;
34     private QueryManager queryManager;
35     private FieldTypeImpl owner;
36
37     public QuerySelectionListImpl(String JavaDoc query, boolean filterVariants, SortOrder sortOrder, QueryManager queryManager, FieldTypeImpl owner) {
38         checkQueryParam(query);
39         this.query = query;
40         this.filterVariants = filterVariants;
41         this.sortOrder = sortOrder;
42         if (this.sortOrder == null)
43             this.sortOrder = SortOrder.ASCENDING;
44         this.queryManager = queryManager;
45         this.owner = owner;
46     }
47
48     public String JavaDoc getQuery() {
49         return query;
50     }
51
52     public SortOrder getSortOrder() {
53         return sortOrder;
54     }
55
56     public void setQuery(String JavaDoc query) {
57         if (owner.isReadOnly())
58             throw new RuntimeException JavaDoc(FieldTypeImpl.READ_ONLY_MESSAGE);
59
60         checkQueryParam(query);
61         this.query = query;
62     }
63
64     public void setSortOrder(SortOrder sortOrder) {
65         if (owner.isReadOnly())
66             throw new RuntimeException JavaDoc(FieldTypeImpl.READ_ONLY_MESSAGE);
67
68         if (sortOrder == null)
69             throw new IllegalArgumentException JavaDoc("sortOrder arg cannot be null");
70         this.sortOrder = sortOrder;
71     }
72
73     public boolean getFilterVariants() {
74         return filterVariants;
75     }
76
77     public void setFilterVariants(boolean filterVariants) {
78         if (owner.isReadOnly())
79             throw new RuntimeException JavaDoc(FieldTypeImpl.READ_ONLY_MESSAGE);
80         this.filterVariants = filterVariants;
81     }
82
83     private void checkQueryParam(String JavaDoc query) {
84         if (query == null || query.trim().length() == 0)
85             throw new IllegalArgumentException JavaDoc("query argument cannot be null, empty or whitespace");
86     }
87
88     public QuerySelectionListDocument getXml() {
89         QuerySelectionListDocument listDocument = QuerySelectionListDocument.Factory.newInstance();
90         QuerySelectionListDocument.QuerySelectionList listXml = listDocument.addNewQuerySelectionList();
91         listXml.setQuery(query);
92         listXml.setFilterVariants(filterVariants);
93         listXml.setSortOrder(sortOrder.toString());
94         return listDocument;
95     }
96
97     public ListItem[] getItems() {
98         if (filterVariants)
99             throw new RepositoryRuntimeException("This selection list needs to filter on variants, so use getItems(branchId, languageId) instead.");
100         return getItems(-1, -1, Locale.US);
101     }
102
103     public ListItem[] getItems(long branchId, long languageId, Locale JavaDoc locale) {
104         DistinctSearchResultDocument searchResultDocument;
105         try {
106             if (filterVariants) {
107                 searchResultDocument = queryManager.performDistinctQuery(query, "branchId = " + branchId + " and languageId = " + languageId, sortOrder, locale);
108             } else {
109                 searchResultDocument = queryManager.performDistinctQuery(query, sortOrder, locale);
110             }
111         } catch (RepositoryException e) {
112             throw new RuntimeException JavaDoc("Error executing query selection list query: " + query, e);
113         }
114         String JavaDoc resultValueTypeString = searchResultDocument.getDistinctSearchResult().getValues().getValueType();
115         ValueType resultValueType = ValueType.fromString(resultValueTypeString);
116
117         if (resultValueType != owner.getValueType())
118             throw new RuntimeException JavaDoc("Values returned from selection list query are of a different type (" + resultValueType.toString() + ") then the field type (" + owner.getValueType().toString() + ").");
119
120         DistinctSearchResultDocument.DistinctSearchResult.Values.Value valuesXml[] = searchResultDocument.getDistinctSearchResult().getValues().getValueArray();
121         QueryListItem[] items = new QueryListItem[valuesXml.length];
122         XmlValueGetter valueGetter = getXmlValueGetter(resultValueType);
123         for (int i = 0; i < valuesXml.length; i++) {
124             DistinctSearchResultDocument.DistinctSearchResult.Values.Value valueXml = valuesXml[i];
125             items[i] = new QueryListItem(valueGetter.getValue(valueXml), valueXml.getLabel());
126         }
127         return items;
128     }
129
130     public String JavaDoc getLabel(Object JavaDoc value, Locale JavaDoc locale) {
131         // value is here a VariantKey. The label is the document name, so this would require
132
// looking up the document. However, the VariantKey object might contain -1 for branch
133
// and language (meaning these are the same as the document they are embedded it), in
134
// which case it is impossible to know what document to retrieve.
135
throw new RuntimeException JavaDoc("getLabel method not supported on this selection list implementation.");
136     }
137
138     public void addToFieldTypeXml(SelectionListDocument.SelectionList selectionListXml) {
139         selectionListXml.setQuerySelectionList(getXml().getQuerySelectionList());
140     }
141
142     private XmlValueGetter getXmlValueGetter(ValueType valueType) {
143         if (valueType == ValueType.STRING)
144             return STRING_VALUE_GETTER;
145         else if (valueType == ValueType.BOOLEAN)
146             return BOOLEAN_VALUE_GETTER;
147         else if (valueType == ValueType.LONG)
148             return LONG_VALUE_GETTER;
149         else if (valueType == ValueType.DECIMAL)
150             return DECIMAL_VALUE_GETTER;
151         else if (valueType == ValueType.DOUBLE)
152             return DOUBLE_VALUE_GETTER;
153         else if (valueType == ValueType.DATE)
154             return DATE_VALUE_GETTER;
155         else if (valueType == ValueType.DATETIME)
156             return DATETIME_VALUE_GETTER;
157         else if (valueType == ValueType.LINK)
158             return LINK_VALUE_GETTER;
159         else
160             throw new RuntimeException JavaDoc("Unsupported value type in query selection list: " + valueType.toString());
161     }
162
163     private static interface XmlValueGetter {
164         public Object JavaDoc getValue(DistinctSearchResultDocument.DistinctSearchResult.Values.Value value);
165     }
166
167     private static XmlValueGetter STRING_VALUE_GETTER = new XmlValueGetter() {
168         public Object JavaDoc getValue(DistinctSearchResultDocument.DistinctSearchResult.Values.Value value) {
169             return value.getString();
170         }
171     };
172
173     private static XmlValueGetter BOOLEAN_VALUE_GETTER = new XmlValueGetter() {
174         public Object JavaDoc getValue(DistinctSearchResultDocument.DistinctSearchResult.Values.Value value) {
175             return new Long JavaDoc(value.getLong());
176         }
177     };
178
179     private static XmlValueGetter LONG_VALUE_GETTER = new XmlValueGetter() {
180         public Object JavaDoc getValue(DistinctSearchResultDocument.DistinctSearchResult.Values.Value value) {
181             return new Long JavaDoc(value.getLong());
182         }
183     };
184
185     private static XmlValueGetter DECIMAL_VALUE_GETTER = new XmlValueGetter() {
186         public Object JavaDoc getValue(DistinctSearchResultDocument.DistinctSearchResult.Values.Value value) {
187             return value.getDecimal();
188         }
189     };
190
191     private static XmlValueGetter DOUBLE_VALUE_GETTER = new XmlValueGetter() {
192         public Object JavaDoc getValue(DistinctSearchResultDocument.DistinctSearchResult.Values.Value value) {
193             return new Double JavaDoc(value.getDouble());
194         }
195     };
196
197     private static XmlValueGetter DATE_VALUE_GETTER = new XmlValueGetter() {
198         public Object JavaDoc getValue(DistinctSearchResultDocument.DistinctSearchResult.Values.Value value) {
199             return value.getDate().getTime();
200         }
201     };
202
203     private static XmlValueGetter DATETIME_VALUE_GETTER = new XmlValueGetter() {
204         public Object JavaDoc getValue(DistinctSearchResultDocument.DistinctSearchResult.Values.Value value) {
205             return value.getDateTime().getTime();
206         }
207     };
208
209     private static XmlValueGetter LINK_VALUE_GETTER = new XmlValueGetter() {
210         public Object JavaDoc getValue(DistinctSearchResultDocument.DistinctSearchResult.Values.Value value) {
211             DistinctSearchResultDocument.DistinctSearchResult.Values.Value.Link link = value.getLink();
212             return new VariantKey(link.getDocumentId(), link.getBranchId(), link.getLanguageId());
213         }
214     };
215 }
216
Popular Tags