001    package org.wdssii.webindex.servlet;
002    
003    import java.io.IOException;
004    import java.io.PrintWriter;
005    
006    import javax.servlet.http.HttpServletRequest;
007    import javax.servlet.http.HttpServletResponse;
008    
009    import org.apache.commons.logging.Log;
010    import org.apache.commons.logging.LogFactory;
011    import org.springframework.validation.BindException;
012    import org.springframework.web.servlet.ModelAndView;
013    import org.springframework.web.servlet.mvc.AbstractCommandController;
014    
015    public class WebIndexController extends AbstractCommandController {
016            private WebIndexFactory indexFactory;
017            private SourcesListingDAO sourcesDAO;
018            private final Log log = LogFactory.getLog(getClass());
019                    
020            public static class Command {
021                    private String source = null;
022                    private long lastRead = IndexRecordList.RETURN_ALL_RECORDS; // default value if lastRead not set
023                    public String getSource() {
024                            return source;
025                    }
026                    public void setSource(String sourceName) {
027                            this.source = sourceName;
028                    }
029                    public long getLastRead() {
030                            return lastRead;
031                    }
032                    public void setLastRead(long lastRead) {
033                            this.lastRead = lastRead;
034                    }
035            }
036    
037            
038            public WebIndexController(WebIndexFactory factory, SourcesListingDAO sourcesDAO){
039                    this.indexFactory = factory;
040                    this.sourcesDAO = sourcesDAO;
041                    setCommandClass(WebIndexController.Command.class);
042            }
043            
044            @Override
045            protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command_, BindException errors) throws Exception {
046                    if (errors.hasErrors()){
047                            return new ModelAndView("showErrors", "errors", errors.getAllErrors());
048                    }
049                    
050                    Command command = (Command) command_;
051                    String source = command.getSource();
052                    long lastRead = command.getLastRead();
053                    
054                    if ( log.isDebugEnabled() ){
055                            log.debug("looking for next records from " + source + " lastRead=" + lastRead);
056                    }
057                    if ( source == null ){
058                            String[] sources = sourcesDAO.getAllSources();
059                            if ( log.isDebugEnabled() ){
060                                    log.debug("Sending along " + sources.length + " sources");
061                            }
062                            return new ModelAndView("showSources", "sources", sources );
063                    }
064                    
065                    WebIndexDAO indexDAO = indexFactory.getDAO(source);
066                    IndexRecordList to = indexDAO.getCurrentRecordList(lastRead);
067                    if ( log.isDebugEnabled() ){
068                            log.debug("Returning " + to.getRecords().length + " records in response to request");
069                    }
070                    return generateResponse(response, to);
071            }
072    
073            /** this method is in the controller because it is too inefficient to perform in the JSP */
074            private ModelAndView generateResponse(HttpServletResponse response, final IndexRecordList to) throws IOException
075            {
076                    long startTime = System.currentTimeMillis();
077                    PrintWriter out = response.getWriter();
078                    
079                    final IndexRecordBean[] records = to.getRecords();
080                    final long lastRead = to.getLastNumber();
081                    final String source = to.getSource();
082                    
083                    response.setContentType("text/xml");
084                    out.print("<records source=\"");
085                    out.print(source);
086                    out.print("\" lastRead=\"");
087                    out.print(lastRead);
088                    out.print("\" listsize=\"");
089                    out.print(records.length);
090                    out.println("\">");
091                    for (IndexRecordBean record : records){
092                            out.println(record.getXML());
093                    }
094                    out.println("</records>");                
095                    long endTime = System.currentTimeMillis();
096                    if ( log.isDebugEnabled() ){
097                            log.debug("Took " + (endTime-startTime) + " milliseconds to render response XML");
098                    }
099                    return null; // for ModelAndView, to indicate nothing further needs to be done
100            }
101    
102    }