001 package echopoint.internal;
002
003 import java.io.IOException;
004 import java.util.HashMap;
005 import java.util.List;
006 import java.util.Properties;
007
008 import javax.servlet.http.HttpServletRequest;
009 import javax.xml.transform.OutputKeys;
010
011 import nextapp.echo.app.util.DomUtil;
012 import nextapp.echo.webcontainer.Connection;
013 import nextapp.echo.webcontainer.ContentType;
014 import nextapp.echo.webcontainer.Service;
015 import nextapp.echo.webcontainer.SynchronizationException;
016 import nextapp.echo.webcontainer.WebContainerServlet;
017 import nextapp.echo.webcontainer.service.WindowHtmlService;
018
019 import org.w3c.dom.Document;
020 import org.w3c.dom.Element;
021 import org.xml.sax.SAXException;
022
023 import echopoint.AutoLookupSelectField;
024 import echopoint.model.AutoLookupSelectModel;
025 import echopoint.model.AutoLookupSelectModel.EntrySelect;
026
027 public class AutoLookupSelectService implements Service {
028
029 private static final AutoLookupSelectService INSTANCE;
030 private java.util.Map interestedParties = new HashMap();
031 public static final String SERVICE_ID = "echopoint.AutoLookupSelectService";
032
033 public static final Properties OUTPUT_PROPERTIES = new Properties();
034
035 static {
036 // The XML declaration is omitted as Internet Explorer 6 will operate in quirks mode if it is present.
037 OUTPUT_PROPERTIES.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
038 OUTPUT_PROPERTIES.putAll(DomUtil.OUTPUT_PROPERTIES_INDENT);
039 OUTPUT_PROPERTIES.setProperty(OutputKeys.DOCTYPE_PUBLIC, WindowHtmlService.XHTML_1_0_TRANSITIONAL_PUBLIC_ID);
040 OUTPUT_PROPERTIES.setProperty(OutputKeys.DOCTYPE_SYSTEM, WindowHtmlService.XHTML_1_0_TRANSITIONAL_SYSTEM_ID);
041 }
042
043 static {
044 INSTANCE = new AutoLookupSelectService();
045 }
046
047 public static AutoLookupSelectService getInstance(){
048 return INSTANCE;
049 }
050
051 public static void install() {
052 WebContainerServlet.getServiceRegistry().add(INSTANCE);
053 }
054
055 private AutoLookupSelectService(){
056 }
057
058 @Override
059 public String getId() {
060 return SERVICE_ID;
061 }
062
063 @Override
064 public int getVersion() {
065 return DO_NOT_CACHE;
066 }
067
068 @Override
069 public void service(Connection conn) throws IOException {
070 HttpServletRequest request = conn.getRequest();
071 request.setCharacterEncoding("utf-8");
072
073 String elementId = request.getParameter("elementId");
074 String searchValue = request.getParameter("searchValue");
075
076 AutoLookupSelectField textFieldEx = (AutoLookupSelectField) interestedParties.get(elementId);
077 if (textFieldEx == null) {
078 System.out.println(interestedParties);
079 throw new IllegalStateException("El AutolookupSelectField " + elementId + " no pudo ser encontrado.");
080 }
081 AutoLookupSelectModel autoLookupModel = textFieldEx.getAutoLookupModel();
082 if (autoLookupModel == null) {
083 return; // nothing to do
084 }
085
086 Document doc = DomUtil.createDocument("xml", null, null, null);
087 Element dataElement = doc.getDocumentElement();
088 Element autoLookupModelE = doc.createElement("autoLookupModel");
089
090 List<EntrySelect> entries = autoLookupModel.searchEntries(searchValue);
091 if (entries != null) {
092 for (EntrySelect entry : entries) {
093 Element entryE = doc.createElement("entry");
094 autoLookupModelE.appendChild(entryE);
095
096 Element valueE = doc.createElement("value");
097 valueE.setTextContent(entry.getValue());
098 entryE.appendChild(valueE);
099
100 Element keyE = doc.createElement("key");
101 keyE.setTextContent(entry.getKey());
102 entryE.appendChild(keyE);
103
104 Element searchE = doc.createElement("searchVal");
105 searchE.setTextContent(entry.getSearchVal());
106 entryE.appendChild(searchE);
107
108 Element xhtmlE = doc.createElement("xhtml");
109 xhtmlE.setTextContent(entry.getValue());
110 entryE.appendChild(xhtmlE);
111 }
112 }
113 dataElement.appendChild(autoLookupModelE);
114
115 conn.setContentType(ContentType.TEXT_XML);
116 try {
117 DomUtil.save(doc, conn.getWriter(), OUTPUT_PROPERTIES);
118 } catch (SAXException ex) {
119 throw new SynchronizationException("Failed to write HTML document.", ex);
120 }
121 }
122
123 public synchronized void register(AutoLookupSelectField selectFieldEx) {
124 interestedParties.put("C." + selectFieldEx.getRenderId(), selectFieldEx);
125 }
126
127 public synchronized void deregister(AutoLookupSelectField selectFieldEx) {
128 interestedParties.remove(selectFieldEx);
129 }
130 }