|
| 1 | +package edu.uiowa.slis.graphtaglib; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.PreparedStatement; |
| 5 | +import java.sql.ResultSet; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.util.Hashtable; |
| 8 | +import java.util.Iterator; |
| 9 | +import java.util.SortedSet; |
| 10 | +import java.util.TreeSet; |
| 11 | + |
| 12 | +import org.apache.commons.logging.Log; |
| 13 | +import org.apache.commons.logging.LogFactory; |
| 14 | + |
| 15 | +import javax.naming.InitialContext; |
| 16 | +import javax.naming.NamingException; |
| 17 | +import javax.servlet.jsp.JspException; |
| 18 | +import javax.servlet.jsp.JspTagException; |
| 19 | +import javax.servlet.jsp.tagext.BodyTagSupport; |
| 20 | +import javax.sql.DataSource; |
| 21 | + |
| 22 | +public class SiteIDIterator extends BodyTagSupport { |
| 23 | + private static final long serialVersionUID = 1L; |
| 24 | + |
| 25 | + private static final Log logger = LogFactory.getLog(SiteIDIterator.class); |
| 26 | + static Hashtable<Integer, String> siteHash = null; |
| 27 | + |
| 28 | + SortedSet<Integer> idSet = null; |
| 29 | + Iterator<Integer> idIterator = null; |
| 30 | + int currentID = 0; |
| 31 | + String currentSite = null; |
| 32 | + |
| 33 | + boolean pruneOrphans = false; |
| 34 | + |
| 35 | + synchronized void init() throws NamingException, SQLException { |
| 36 | + if (siteHash != null) |
| 37 | + return; |
| 38 | + siteHash = new Hashtable<Integer, String>(); |
| 39 | + DataSource theDataSource = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/VIVOTagLib"); |
| 40 | + Connection conn = theDataSource.getConnection(); |
| 41 | + PreparedStatement stmt = conn.prepareStatement("select id,site from vivo_aggregated.site order by id"); |
| 42 | + ResultSet rs = stmt.executeQuery(); |
| 43 | + while (rs.next()) { |
| 44 | + siteHash.put(rs.getInt(1), rs.getString(2)); |
| 45 | + } |
| 46 | + stmt.close(); |
| 47 | + conn.close(); |
| 48 | + } |
| 49 | + |
| 50 | + public int doStartTag() throws JspException { |
| 51 | + logger.debug("doStartTag"); |
| 52 | + try { |
| 53 | + init(); |
| 54 | + } catch (Exception e) { |
| 55 | + logger.error("error initializing SiteIDIterator:", e); |
| 56 | + } |
| 57 | + idSet = new TreeSet<Integer>(); |
| 58 | + Graph theGraph = (Graph) findAncestorWithClass(this, Graph.class); |
| 59 | + for (GraphNode node: theGraph.nodes) { |
| 60 | + idSet.add(node.getGroup("site")); |
| 61 | + } |
| 62 | + |
| 63 | + idIterator = idSet.iterator(); |
| 64 | + |
| 65 | + if (idIterator.hasNext()) { |
| 66 | + currentID = idIterator.next(); |
| 67 | + currentSite = siteHash.get(currentID); |
| 68 | + pageContext.setAttribute("isLastID", !idIterator.hasNext()); |
| 69 | + return EVAL_BODY_INCLUDE; |
| 70 | + } |
| 71 | + |
| 72 | + return SKIP_BODY; |
| 73 | + } |
| 74 | + |
| 75 | + public int doAfterBody() throws JspTagException { |
| 76 | + if (idIterator.hasNext()) { |
| 77 | + currentID = idIterator.next(); |
| 78 | + currentSite = siteHash.get(currentID); |
| 79 | + pageContext.setAttribute("isLastID", !idIterator.hasNext()); |
| 80 | + return EVAL_BODY_AGAIN; |
| 81 | + } |
| 82 | + |
| 83 | + return SKIP_BODY; |
| 84 | + } |
| 85 | + |
| 86 | + public int doEndTag() throws JspTagException, JspException { |
| 87 | + logger.debug("doEndTag"); |
| 88 | + clearServiceState(); |
| 89 | + return super.doEndTag(); |
| 90 | + } |
| 91 | + |
| 92 | + private void clearServiceState() { |
| 93 | + idSet = null; |
| 94 | + idIterator = null; |
| 95 | + currentID = 0; |
| 96 | + currentSite = null; |
| 97 | + |
| 98 | + pageContext.removeAttribute("isLastNode"); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments