1 package org.enhydra.barracuda.examples.xmlc; 2 3 import java.io.*; 4 import java.util.*; 5 import javax.servlet.*; 6 import javax.servlet.http.*; 7 8 import org.w3c.dom.*; 9 import org.w3c.dom.html.*; 10 11 import org.apache.log4j.Logger; 12 13 import org.enhydra.xml.xmlc.*; 14 15 import org.enhydra.barracuda.core.comp.*; 16 import org.enhydra.barracuda.core.util.dom.*; 17 import org.enhydra.barracuda.core.comp.helper.ComponentGateway; 18 import org.enhydra.barracuda.core.view.*; 19 import org.enhydra.barracuda.examples.xmlc.data.*; 20 21 25 public class TemplateTableServlet extends ComponentGateway { 26 27 protected static final Logger logger = Logger.getLogger(TemplateTableServlet.class.getName()); 29 30 33 37 public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException { 38 String pageName = ""+req.getParameter("page"); 40 Class pageCl = CompEx3HTML.class; 41 if (pageName.equals("CompEx3a")) pageCl = CompEx3aHTML.class; 42 else if (pageName.equals("CompEx3b")) pageCl = CompEx3bHTML.class; 43 44 48 Document page = DefaultDOMLoader.getGlobalInstance().getDOM(pageCl, vc.getViewCapabilities().getClientLocale()); 50 51 BTemplate wcTemplate = new BTemplate(); 55 wcTemplate.addView(new DefaultTemplateView(page.getDocumentElement())); wcTemplate.addModel(new UserReportModel("org.enhydra.barracuda.examples.xmlc.UserReport")); 58 wcTemplate.addModel(new UserDataModel()); 59 root.addChild(wcTemplate); 60 61 return page; 63 } 64 65 69 class UserReportModel extends DefaultPropertiesModel { 70 71 74 78 public UserReportModel(String propFileName) { 79 super(propFileName); 80 } 81 82 85 public Object getItem(String key) { 86 ViewContext vc = getViewContext(); 87 Object result = null; 88 if (key.equals("Footer")) { 89 Document footer = null; 90 try {footer = DefaultDOMLoader.getGlobalInstance().getDOM(CompEx3Footer2HTML.class, vc.getViewCapabilities().getClientLocale());} 92 catch (IOException ioe) {TemplateTableServlet.logger.fatal ("Fatal Error loading DOM template:", ioe);} 93 Node footerNode = ((CompEx3Footer2HTML)footer).getElementUserReportFooter(); 94 result = vc.getElementFactory().getDocument().importNode(footerNode, true); 95 } 96 else result = (String ) super.getItem(key); 97 if (result==null) result = " "; 98 return result; 99 } 100 101 } 102 103 107 class UserDataModel extends AbstractTemplateModel implements IterativeModel { 108 109 UsersList users = null; 111 UserData ud = null; 112 Iterator it = null; 113 114 boolean useSelectionImages = true; 116 Node selNode = null; 117 Node unselNode = null; 118 119 122 public String getName() {return "UserData";} 123 124 127 public Object getItem(String key) { 128 ViewContext vc = getViewContext(); 129 Object result = null; 130 if (key.equals("Selected")) { 131 if (useSelectionImages) { 132 ElementFactory ef = vc.getElementFactory(); 133 selNode = ef.getElement("SelectedImageCell"); 134 unselNode = ef.getElement("UnselectedImageCell"); 135 useSelectionImages = false; } 137 boolean selected = ((Boolean ) ud.get(UserData.SELECTED)).booleanValue(); 138 if (selNode!=null && selected) result = selNode.cloneNode(true); 139 else if (unselNode!=null && !selected) result = unselNode.cloneNode(true); 140 else result = (selected ? "Yes" : "No"); 141 } 142 else if (key.equals("FirstName")) result = (String ) ud.get(UserData.FIRST_NAME); 143 else if (key.equals("LastName")) result = (String ) ud.get(UserData.LAST_NAME); 144 else if (key.equals("Gender")) result = (((Boolean ) ud.get(UserData.GENDER)).booleanValue() ? "Male" : "Female"); 145 else if (key.equals("Age")) result = ""+ud.get(UserData.AGE); 146 else if (key.equals("Phone")) result = (String ) ud.get(UserData.PHONE); 147 else if (key.equals("Email")) { 148 String email = (String ) ud.get(UserData.EMAIL); 150 result = new BLink(email, "mailto:"+email, vc); 151 } 152 else if (key.equals("Notes")) result = (String ) ud.get(UserData.NOTES); 153 else result = (String ) super.getItem(key); 154 if (result==null) result = " "; 155 return result; 156 } 157 158 161 public boolean processDirective(TemplateDirective td) { 162 if (td.getCommand().equals("Veto_If_Null")) { 163 String data = td.getKeyName(); 164 if (data!=null) { 165 if (null!=ud) { 166 StringTokenizer st = new StringTokenizer(data,","); 167 while (st.hasMoreTokens()) { 168 String key = st.nextToken(); 169 Object value = ud.get(key); 170 if (value==null || value.toString().length()<1) return false; } 172 } else { 173 return false; } 175 } 176 } 177 return true; } 179 180 183 public void preIterate() { 184 if (users==null) users = UsersList.getSampleInstance(); 185 it = users.iterator(); 186 } 187 188 191 public boolean hasNext() { 192 return (it!=null && it.hasNext()); 193 } 194 195 198 public void loadNext() { 199 ud = (UserData) it.next(); 200 } 201 202 205 public void postIterate() { 206 it = null; 207 } 208 } 209 210 } 211 212 | Popular Tags |