1 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 ; 29 30 public class LinkQuerySelectionListImpl implements LinkQuerySelectionList { 31 private String whereClause; 32 private boolean filterVariants; 33 private QueryManager queryManager; 34 private FieldTypeImpl owner; 35 36 public LinkQuerySelectionListImpl(String 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 getWhereClause() { 45 return whereClause; 46 } 47 48 private void checkWhereClause(String whereClause) { 49 if (whereClause == null || whereClause.trim().length() == 0) 50 throw new IllegalArgumentException ("whereClause argument cannot be null, empty or whitespace"); 51 } 52 53 public void setWhereClause(String whereClause) { 54 if (owner.isReadOnly()) 55 throw new RuntimeException (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 (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 locale) { 86 String 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 ("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 getLabel(Object value, Locale locale) { 108 throw new RuntimeException ("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 |