KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > web > DistinctTagHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.exceptions.web;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Vector JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
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 JavaDoc;
33 import javax.servlet.jsp.JspException JavaDoc;
34 import javax.servlet.jsp.JspWriter JavaDoc;
35 import javax.servlet.jsp.PageContext JavaDoc;
36 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
37 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
38
39 /**
40  * @author Jan Horvath
41  * @version
42  */

43
44 public class DistinctTagHandler extends BodyTagSupport JavaDoc {
45     
46     EntityManagerFactory emf = Persistence.createEntityManagerFactory("StrutsExceptionsPU",new java.util.HashMap JavaDoc());
47     
48     /** Creates new instance of tag handler */
49     private String JavaDoc entity;
50     private String JavaDoc field;
51     private String JavaDoc var;
52     private Iterator JavaDoc it;
53     /** Creates new instance of tag handler */
54     public DistinctTagHandler() {
55         super();
56     }
57     
58     public int doStartTag() throws JspException JavaDoc, JspException JavaDoc {
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 JavaDoc, JspException JavaDoc {
69         if (shouldEvaluateRestOfPageAfterEndTag()) {
70             return EVAL_PAGE;
71         } else {
72             return SKIP_PAGE;
73         }
74     }
75     
76     public int doAfterBody() throws JspException JavaDoc {
77         try {
78             
79             BodyContent JavaDoc bodyContent = getBodyContent();
80             JspWriter JavaDoc out = bodyContent.getEnclosingWriter();
81             bodyContent.writeOut(out);
82             bodyContent.clearBody();
83         } catch (Exception JavaDoc 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 JavaDoc vec = (Vector JavaDoc) it.next();
97             String JavaDoc val = "x";
98             if (vec.size() > 0) val = (String JavaDoc) 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 JavaDoc ex) throws JspException JavaDoc {
107         // Since the doAfterBody method is guarded, place exception handing code here.
108
throw new JspException JavaDoc("error in NewTag: " + ex);
109     }
110     
111     /**
112      * Fill in this method to determine if the rest of the JSP page
113      * should be generated after this tag is finished.
114      * Called from doEndTag().
115      */

116     private boolean shouldEvaluateRestOfPageAfterEndTag() {
117         return true;
118     }
119     
120     public void setEntity(String JavaDoc entity) {
121         this.entity = entity;
122     }
123     
124     public void setField(String JavaDoc field) {
125         this.field = field;
126     }
127     
128     public void setVar(String JavaDoc var) {
129         this.var = var;
130     }
131     
132     protected Collection JavaDoc getDistinct(String JavaDoc entity, String JavaDoc column) {
133         List JavaDoc 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 JavaDoc e) {
139             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e);
140             throw new RuntimeException JavaDoc(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