« LOM OAI-PMH Harvester in Python | Main | OS X on VirtualBox »

February 28, 2012

Jython servlet and JSP for AJAX slide presentations

Servlet Code:

from javax.servlet.http import HttpServlet

from java.util import ArrayList

from java.util import HashMap

import re

import string

from just import just

class presentationXML(HttpServlet):

    def doGet(self,request,response):

        self.doPost (request,response)

    def doPost(self,request,response):

        response.setContentType ("text/plain")
        
        presentation_id = request.getParameter('presentation_id')
        
        presentation = self.get_presentation(presentation_id)
        
        prezs = presentation.get('rows')
        
        top_slide = prezs.get(0)
        
        request.setAttribute("top_title", top_slide['title'])
        
        request.setAttribute("top_author", top_slide['author'])
        
        request.setAttribute("top_desc", top_slide['description'])

        slides = self.get_slides(presentation_id)
        
        rows = slides.get('rows')
        
        columns = slides.get('columns')
        
        request.setAttribute("columns", columns)
        
        request.setAttribute("rows", rows)
        
        disp = request.getRequestDispatcher('presentation.xml.jsp')
        
        disp.forward(request, response)
        
    def build_slide(self, slide):
        
        title = slide['title']
        
        body = slide['body']

        slide_list = ""   

        for i in body.split("\n"):
            
            (tag, line) = i.split(':')
            
            slide_list = slide_list + "\n<" + tag + '><span class="red">»</span> ' + line + "</" + tag + ">"
        
        return {'title' : title, 'slide_list' : slide_list, 'example_url' : slide['example_url'] }

    def get_slides(self, pres_id):

        juice = just()    
        
        reply = juice.sql("select * from slides.slide where presentation_id = " + str(pres_id) + " order by sort_order asc")

        slides = []

        for row in reply['rows']:
            
            slide = self.build_slide(row)

            slides.append(slide)

        return( self.jhash(slides, ['title', 'slide_list', 'example_url' ]) )
        
    def get_presentation(self, pres_id):

        juice = just()    
        
        reply = juice.sql("select * from slides.presentation where presentation_id = " + str(pres_id) + "limit 1")

        return( self.jhash(reply['rows'], reply['columns']) )
        
    def jhash(self, rows, columns):
        
        results = HashMap()   
        
        jrows = ArrayList()
        
        jcols = ArrayList()
        
        for row in rows:
            
            res = HashMap()   
            
            for col in columns:
                
                jcols.add(col)   
                
                res.put(col, row[col])
                
            jrows.add(res)
        
        results.put('columns', jcols)
        
        results.put('rows', jrows)
            
        return results

JSP:

<?xml version="1.0" encoding="UTF-8"?>
<%@ page contentType="text/xml" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<?xml-stylesheet type="text/xsl" href="xslt/ajax-s-html.xml"?>
<ajax-s>
<pages>

<page>
<br/>
<br/>
<h2>${top_title}</h2>
    <h3>${top_desc}</h3>
    <h4><span class="red">&#187;</span> ${top_author}</h4>
</page>

<c:forEach items="${rows}" var="row">
 <page>
    <h2>${row['title']}</h2>
    <dl>
    <c:out value="${row['slide_list']}" escapeXml="false"/>
    </dl>
    <c:if test="${row['example_url'] != ''}">
    <p style="text-align: right"><a target="__newwin" href="${row['example_url']}">e.g. <span class="red">&#187;</span></a></p>
    </c:if>
  </page>
</c:forEach>
</pages>
</ajax-s>

DB Query Bean

import re

import string

import com.ziclix.python.sql as sql

class just:
        
    def sql(self, skewl):
        
        description, results = self.query(skewl)

        cols = []
        
        final = {}
        
        final['rows'] = []
        
        final['columns'] = []
        
        for i in description:
            
            cols.append(i[0])
        
        for i in results:
        
            row = list(i)
            
            r = {}  
            
            count = 0            
            
            for c in row:
                
                r[cols[count]] = c
                
                count = count + 1
            
            final['rows'].append(r)
        
        final['columns'] = cols
        
        return final
        
    def query(self, q):
        
        dburl, user, pw, drv = ("jdbc:postgresql://localhost:5432/roo","*****","*****","org.postgresql.Driver")
        
        db = sql.zxJDBC.connect(dburl, user, pw, drv)
        
        cursor = db.cursor()
        
        cursor.datahandler = sql.handler.PostgresqlDataHandler(cursor.datahandler)
        
        cursor.execute(q)

        columns = cursor.description

        results = cursor.fetchall()
        
        return (columns, results)

Tags: Jython

Posted by pj at February 28, 2012 03:05 PM

Comments