KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkQuerySelectionList;
19 import org.outerj.daisy.repository.schema.ListItem;
20 import org.outerj.daisy.repository.query.QueryManager;
21 import org.outerj.daisy.repository.VariantKey;
22 import org.outerj.daisy.repository.RepositoryException;
23 import org.outerj.daisy.repository.RepositoryRuntimeException;
24 import org.outerx.daisy.x10.LinkQuerySelectionListDocument;
25 import org.outerx.daisy.x10.SearchResultDocument;
26 import org.outerx.daisy.x10.SelectionListDocument;
27
28 import java.util.Locale JavaDoc;
29
30 public class LinkQuerySelectionListImpl implements LinkQuerySelectionList {
31     private String JavaDoc whereClause;
32     private boolean filterVariants;
33     private QueryManager queryManager;
34     private FieldTypeImpl owner;
35
36     public LinkQuerySelectionListImpl(String JavaDoc whereClause, boolean filterVariants, QueryManager queryManager, FieldTypeImpl owner) {
37         checkWhereClause(whereClause);
38         this.queryManager = queryManager;
39         this.whereClause = whereClause;
40         this.filterVariants = filterVariants;
41         this.owner = owner;
42     }
43
44     public String JavaDoc getWhereClause() {
45         return whereClause;
46     }
47
48     private void checkWhereClause(String JavaDoc whereClause) {
49         if (whereClause == null || whereClause.trim().length() == 0)
50             throw new IllegalArgumentException JavaDoc("whereClause argument cannot be null, empty or whitespace");
51     }
52
53     public void setWhereClause(String JavaDoc whereClause) {
54         if (owner.isReadOnly())
55             throw new RuntimeException JavaDoc(FieldTypeImpl.READ_ONLY_MESSAGE);
56
57         checkWhereClause(whereClause);
58         this.whereClause = whereClause;
59     }
60
61     public boolean getFilterVariants() {
62         return filterVariants;
63     }
64
65     public void setFilterVariants(boolean filterVariants) {
66         if (owner.isReadOnly())
67             throw new RuntimeException JavaDoc(FieldTypeImpl.READ_ONLY_MESSAGE);
68         this.filterVariants = filterVariants;
69     }
70
71     public LinkQuerySelectionListDocument getXml() {
72         LinkQuerySelectionListDocument listDocument = LinkQuerySelectionListDocument.Factory.newInstance();
73         LinkQuerySelectionListDocument.LinkQuerySelectionList selectionListXml = listDocument.addNewLinkQuerySelectionList();
74         selectionListXml.setWhereClause(whereClause);
75         selectionListXml.setFilterVariants(filterVariants);
76         return listDocument;
77     }
78
79     public ListItem[] getItems() {
80         if (filterVariants)
81             throw new RepositoryRuntimeException("This selection list needs to filter on variants, so use getItems(branchId, languageId) instead.");
82         return getItems(-1, -1, Locale.US);
83     }
84
85     public ListItem[] getItems(long branchId, long languageId, Locale JavaDoc locale) {
86         String JavaDoc query = "select name where " + whereClause;
87         SearchResultDocument searchResultDocument;
88         try {
89             if (filterVariants) {
90                 searchResultDocument = queryManager.performQuery(query, "branchId = " + branchId + " and languageId = " + languageId, locale);
91             } else {
92                 searchResultDocument = queryManager.performQuery(query, locale);
93             }
94         } catch (RepositoryException e) {
95             throw new RuntimeException JavaDoc("Error executing link-query selection list query: " + query, e);
96         }
97         SearchResultDocument.SearchResult.Rows.Row[] rows = searchResultDocument.getSearchResult().getRows().getRowArray();
98
99         LinkListItem[] items = new LinkListItem[rows.length];
100         for (int i = 0; i < rows.length; i++) {
101             SearchResultDocument.SearchResult.Rows.Row row = rows[i];
102             items[i] = new LinkListItem(new VariantKey(row.getDocumentId(), row.getBranchId(), row.getLanguageId()), row.getValueArray(0));
103         }
104         return items;
105     }
106
107     public String JavaDoc getLabel(Object JavaDoc value, Locale JavaDoc locale) {
108         // value is here a VariantKey. The label is the document name, so this would require
109
// looking up the document. However, the VariantKey object might contain -1 for branch
110
// and language (meaning these are the same as the document they are embedded it), in
111
// which case it is impossible to know what document to retrieve.
112
throw new RuntimeException JavaDoc("getLabel method not supported on this selection list implementation.");
113     }
114
115     public void addToFieldTypeXml(SelectionListDocument.SelectionList selectionListXml) {
116         selectionListXml.setLinkQuerySelectionList(getXml().getLinkQuerySelectionList());
117     }
118 }
119
Popular Tags