1 19 package org.netbeans.modules.exceptions.web; 20 21 import java.io.IOException ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Vector ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 import javax.persistence.EntityManager; 29 import javax.persistence.EntityManagerFactory; 30 import javax.persistence.Persistence; 31 import javax.persistence.Query; 32 import javax.servlet.jsp.JspContext ; 33 import javax.servlet.jsp.JspException ; 34 import javax.servlet.jsp.JspWriter ; 35 import javax.servlet.jsp.PageContext ; 36 import javax.servlet.jsp.tagext.BodyContent ; 37 import javax.servlet.jsp.tagext.BodyTagSupport ; 38 39 43 44 public class DistinctTagHandler extends BodyTagSupport { 45 46 EntityManagerFactory emf = Persistence.createEntityManagerFactory("StrutsExceptionsPU",new java.util.HashMap ()); 47 48 49 private String entity; 50 private String field; 51 private String var; 52 private Iterator it; 53 54 public DistinctTagHandler() { 55 super(); 56 } 57 58 public int doStartTag() throws JspException , JspException { 59 it = getDistinct(entity, field).iterator(); 60 61 if (setVariable()) { 62 return EVAL_BODY_BUFFERED; 63 } else { 64 return SKIP_BODY; 65 } 66 } 67 68 public int doEndTag() throws JspException , JspException { 69 if (shouldEvaluateRestOfPageAfterEndTag()) { 70 return EVAL_PAGE; 71 } else { 72 return SKIP_PAGE; 73 } 74 } 75 76 public int doAfterBody() throws JspException { 77 try { 78 79 BodyContent bodyContent = getBodyContent(); 80 JspWriter out = bodyContent.getEnclosingWriter(); 81 bodyContent.writeOut(out); 82 bodyContent.clearBody(); 83 } catch (Exception ex) { 84 handleBodyContentException(ex); 85 } 86 87 if (setVariable()) { 88 return EVAL_BODY_AGAIN; 89 } else { 90 return SKIP_BODY; 91 } 92 } 93 94 private boolean setVariable() { 95 if (it.hasNext()) { 96 Vector vec = (Vector ) it.next(); 97 String val = "x"; 98 if (vec.size() > 0) val = (String ) vec.elementAt(0); 99 pageContext.setAttribute(var, val, PageContext.PAGE_SCOPE); 100 return true; 101 } else { 102 return false; 103 } 104 } 105 106 private void handleBodyContentException(Exception ex) throws JspException { 107 throw new JspException ("error in NewTag: " + ex); 109 } 110 111 116 private boolean shouldEvaluateRestOfPageAfterEndTag() { 117 return true; 118 } 119 120 public void setEntity(String entity) { 121 this.entity = entity; 122 } 123 124 public void setField(String field) { 125 this.field = field; 126 } 127 128 public void setVar(String var) { 129 this.var = var; 130 } 131 132 protected Collection getDistinct(String entity, String column) { 133 List result = null; 134 EntityManager em = getEM(); 135 try { 136 Query q = em.createNativeQuery("SELECT DISTINCT x." + column + " FROM " + entity + " x ORDER BY " + column); 137 result = q.getResultList(); 138 } catch(Exception e) { 139 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e); 140 throw new RuntimeException (e); 141 } finally { 142 em.close(); 143 } 144 return result; 145 } 146 147 private EntityManager getEM() { 148 EntityManager em = emf.createEntityManager(); 149 return em; 150 } 151 152 } 153 | Popular Tags |