1 package com.genimen.djeneric.web.renderers; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 6 import javax.xml.parsers.FactoryConfigurationError ; 7 import javax.xml.parsers.ParserConfigurationException ; 8 9 import org.w3c.dom.Document ; 10 import org.w3c.dom.Element ; 11 12 import com.genimen.djeneric.language.Messages; 13 import com.genimen.djeneric.repository.DjDomain; 14 import com.genimen.djeneric.repository.DjDomainValue; 15 import com.genimen.djeneric.repository.DjExtent; 16 import com.genimen.djeneric.repository.DjList; 17 import com.genimen.djeneric.repository.DjObject; 18 import com.genimen.djeneric.repository.DjProperty; 19 import com.genimen.djeneric.repository.DjSession; 20 import com.genimen.djeneric.repository.exceptions.DjenericException; 21 22 public class FilterRenderer extends AbstractRenderer 23 { 24 public static int _uidcounter = 1; 25 26 public FilterRenderer() 27 { 28 } 29 30 public Document asXml(DjExtent extent, DjSession session, String excludeProperty) throws FactoryConfigurationError , 31 ParserConfigurationException , DjenericException 32 { 33 Document doc = createDocument(); 34 Element filter = doc.createElement("filter"); 35 filter.setAttribute("id", extent.getName() + String.valueOf(_uidcounter++)); 36 filter.setAttribute("extent", extent.getName()); 37 filter.setAttribute("singlename", extent.getNameSingular()); 38 filter.setAttribute("pluralname", extent.getNamePlural()); 39 filter.setAttribute("title", Messages.getString("FilterDialog.Find", extent.getNamePlural())); 40 doc.appendChild(filter); 41 42 HashMap domains = new HashMap (); 43 HashMap extents = new HashMap (); 44 DjProperty[] props = extent.getProperties(); 45 46 for (int i = 0; i < props.length; i++) 47 { 48 if (!props[i].getName().equals(excludeProperty) && props[i].isQueryable()) 49 { 50 addProperty(doc, filter, props[i], domains, extents); 51 } 52 } 53 54 addDomainLists(doc, filter, domains); 55 addExtentLists(doc, filter, extents, session); 56 57 return doc; 58 } 59 60 protected void addProperty(Document doc, Element filter, DjProperty property, HashMap domains, HashMap extents) 61 { 62 Element prop = doc.createElement("property"); 63 filter.appendChild(prop); 64 prop.setAttribute("name", property.getName()); 65 prop.setAttribute("nativetype", property.getNativeType()); 66 int width = determineDisplayWidth(property); 67 prop.setAttribute("size", String.valueOf(width)); 68 69 prop.setAttribute("maxlength", String.valueOf(property.getLength() + 10)); 71 72 String componentType = "textfield"; 73 if (property.getType() instanceof DjDomain) 74 { 75 DjDomain domain = (DjDomain) property.getType(); 76 if (domain.hasValidValues()) 77 { 78 componentType = "combobox"; 79 80 String domainId = "dom_" + domain.getName(); 81 prop.setAttribute("valuelist", domainId); 82 domains.put(domainId, domain); 83 } 84 } 85 else 86 { 87 if (property.getType() instanceof DjExtent) 88 { 89 DjExtent theExtent = (DjExtent) property.getType(); 90 91 if (theExtent.isLarge()) 92 { 93 componentType = "chooser"; 94 prop.setAttribute("extent", theExtent.getName()); 95 } 96 else 97 { 98 componentType = "combobox"; 99 String extentId = "ext_" + theExtent.getName(); 100 prop.setAttribute("valuelist", extentId); 101 extents.put(extentId, theExtent); 102 } 103 } 104 } 105 prop.setAttribute("prompt", property.getPrompt()); 106 prop.setAttribute("component", componentType); 107 } 108 109 private void addExtentLists(Document doc, Element filter, HashMap extents, DjSession session) 110 throws DjenericException 111 { 112 Iterator it = extents.keySet().iterator(); 113 while (it.hasNext()) 114 { 115 String key = it.next().toString(); 116 Element list = doc.createElement("valuelist"); 117 filter.appendChild(list); 118 list.setAttribute("name", key); 119 DjExtent ext = (DjExtent) extents.get(key); 120 DjList objects = session.getObjects(ext); 121 objects.sort(); 122 Iterator ito = objects.iterator(); 123 while (ito.hasNext()) 124 { 125 DjObject obj = (DjObject) ito.next(); 126 Element pair = doc.createElement("item"); 127 list.appendChild(pair); 128 pair.setAttribute("value", String.valueOf(obj.getObjectId())); 129 pair.setAttribute("descr", obj.toString()); 130 } 131 } 132 } 133 134 private void addDomainLists(Document doc, Element filter, HashMap domains) 135 { 136 Iterator it = domains.keySet().iterator(); 137 while (it.hasNext()) 138 { 139 String key = it.next().toString(); 140 Element list = doc.createElement("valuelist"); 141 filter.appendChild(list); 142 list.setAttribute("name", key); 143 Object o = domains.get(key); 144 DjDomain dom = (DjDomain) domains.get(key); 145 DjDomainValue[] values = dom.getValidValues(); 146 147 for (int i = 0; i < values.length; i++) 148 { 149 Element pair = doc.createElement("item"); 150 list.appendChild(pair); 151 pair.setAttribute("value", values[i].getValue().toString()); 152 pair.setAttribute("descr", values[i].getDescription()); 153 } 154 } 155 } 156 157 private int determineDisplayWidth(DjProperty property) 158 { 159 if (property.getType() instanceof DjDomain) 160 { 161 DjDomain domain = (DjDomain) property.getType(); 162 int width = domain.getDisplayWidth(); 163 if (width == 0) width = property.getLength(); 164 if (width == 0 && property.getTypeCode() == DjDomain.DATE_TYPE) width = 10; 165 if (width > 50) width = 50; 166 return width; 167 } 168 else return 50; 169 } 170 } 171 | Popular Tags |