Coverage Report - com.buckosoft.PicMan.web.PicBrowserAjaxController
 
Classes in this File Line Coverage Branch Coverage Complexity
PicBrowserAjaxController
0%
0/102
0%
0/50
6.6
PicBrowserAjaxController$PicNameWithLabel
0%
0/2
N/A
6.6
 
 1  
 /******************************************************************************
 2  
  * PicBrowserAjaxController.java - The Spring controller for the PicMan AJAX calls
 3  
  * 
 4  
  * PicMan - The BuckoSoft Picture Manager in Java
 5  
  * Copyright(c) 2009 - Dick Balaska
 6  
  * 
 7  
  */
 8  
 package com.buckosoft.PicMan.web;
 9  
 
 10  
 import java.io.IOException;
 11  
 import java.io.OutputStream;
 12  
 import java.io.UnsupportedEncodingException;
 13  
 import java.text.DecimalFormat;
 14  
 import java.util.ArrayList;
 15  
 import java.util.List;
 16  
 
 17  
 import javax.servlet.http.HttpServletRequest;
 18  
 import javax.servlet.http.HttpServletResponse;
 19  
 
 20  
 import org.apache.commons.logging.Log;
 21  
 import org.apache.commons.logging.LogFactory;
 22  
 import org.dom4j.Document;
 23  
 import org.dom4j.io.OutputFormat;
 24  
 import org.dom4j.io.XMLWriter;
 25  
 import org.springframework.web.servlet.ModelAndView;
 26  
 
 27  
 import com.buckosoft.BSAccount.web.BSAccountPageController;
 28  
 import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
 29  
 import com.buckosoft.PicMan.business.PicManFacade;
 30  
 import com.buckosoft.PicMan.dom.PicBrowserDom;
 31  
 import com.buckosoft.PicMan.domain.MetaSet;
 32  
 import com.buckosoft.PicMan.domain.MetaSetRule;
 33  
 import com.buckosoft.PicMan.domain.User;
 34  
 
 35  
 /** Spring controller that returns DOMs for the PicMan PicBrowser.
 36  
  * @author Dick Balaska
 37  
  * @since 2009/06/15
 38  
  * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/main/java/com/buckosoft/PicMan/web/PicBrowserAjaxController.java">PicBrowserAjaxController.java</a>
 39  
  */
 40  0
 public class PicBrowserAjaxController extends BSAccountPageController {
 41  0
         private static boolean DEBUG = true;
 42  0
         protected final Log logger = LogFactory.getLog(getClass());
 43  
 
 44  
         private PicManFacade pmf;
 45  
 
 46  
         /** Set the reference to the PicMan API.
 47  
          * @param pmf The PicManFacade
 48  
          */
 49  0
         public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
 50  
 
 51  
         /** Enable debug logger output on this module
 52  
          * @param debugFlag true == turn on debugging.
 53  
          */
 54  
         public void setDEBUG(boolean debugFlag) {
 55  0
                 DEBUG = debugFlag;
 56  0
         }
 57  
 
 58  0
         private        String                         cachedDeclaration = "";
 59  
         private        MetaSet                        cachedMetaSet;
 60  
         private        PicNameWithLabel[]                cachedPics;
 61  0
         private        int                                cachedOffset = 0;
 62  
 
 63  0
         private final DecimalFormat df = new DecimalFormat("0.##");
 64  
 
 65  0
         public class PicNameWithLabel implements Comparable<PicNameWithLabel> {
 66  
                 public String        name;
 67  
                 public String        label;
 68  
                 /* (non-Javadoc)
 69  
                  * @see java.lang.Comparable#compareTo(java.lang.Object)
 70  
                  */
 71  
                 @Override
 72  
                 public int compareTo(PicNameWithLabel o) {
 73  0
                         return(this.name.compareTo(o.name));
 74  
                 }
 75  
                 
 76  
         }
 77  
 
 78  
         
 79  
         /** Spring standard http request handler.
 80  
          * @param request The http request.
 81  
          * @param response The http response.
 82  
          * @return null to return just an http status because we pushed the DOM out the response.
 83  
          */ 
 84  
         public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
 85  0
                 String query = request.getQueryString();
 86  0
                 if (DEBUG) {
 87  
                         //String sid = request.getParameter("sid");
 88  0
                                 logger.info("Browser Request Q=" + query);
 89  
                 }
 90  
 
 91  0
                 User user = null;
 92  0
                 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 93  0
                 if (userWebSession != null) 
 94  0
                         user = (User)userWebSession.getUser();
 95  
 
 96  0
                 response.setContentType("text/xml");
 97  0
                 response.addHeader("Cache-Control", "max-age=0");
 98  0
                 response.addHeader("Cache-Control", "no-cache");
 99  0
                 response.addHeader("expires", "0");
 100  0
                 response.addHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");
 101  0
                 response.addHeader("Pragma", "no-cache");
 102  
 
 103  0
                 String[] ss = query.split("&", 2);
 104  0
                 if (ss[0].startsWith("z=")) {
 105  0
                         cachedOffset = Integer.parseInt(ss[0].substring(2));
 106  
                 }
 107  0
                 if (ss.length == 1) {
 108  0
                         cachedDeclaration = null;
 109  0
                         cachedMetaSet = null;
 110  0
                         cachedPics = null;
 111  
                         
 112  0
                 } else if (!ss[1].equals(cachedDeclaration)) {
 113  0
                         cachedDeclaration = ss[1];
 114  0
                         cachedMetaSet = buildMetaSetFromQuery(cachedDeclaration);
 115  0
                         List<String>l = pmf.getDB().getPicNamesBySet(cachedMetaSet, 75, MetaSet.NONE, 0);
 116  0
                         ArrayList<PicNameWithLabel>lpr = new ArrayList<PicNameWithLabel>();
 117  0
                         for (String pn : l) {
 118  0
                                 PicNameWithLabel pnwr = new PicNameWithLabel();
 119  0
                                 pnwr.name = pn;
 120  0
                                 lpr.add(pnwr);
 121  0
                         }
 122  
                         
 123  0
                         cachedPics = lpr.toArray(new PicNameWithLabel[0]);
 124  0
                         java.util.Arrays.sort(cachedPics);
 125  
 //                        for (int i=0; i<user.getPicBrowserPicsPerPage(); i++)
 126  
 //                                cachedPics[i].label = df.format(pmf.getDB().getPicRate(cachedPics[i].name, cachedMetaSet, 75));
 127  
                 }
 128  
                 
 129  0
                 Document dom = null;
 130  0
                 if (cachedPics != null && user.isPicBrowserShowRatings()) {
 131  0
                         int end = user.getPicBrowserPicsPerPage()+cachedOffset < cachedPics.length ? user.getPicBrowserPicsPerPage()+cachedOffset : cachedPics.length;
 132  0
                         for (int i=cachedOffset; i<end; i++)
 133  0
                                 if (cachedPics[i].label == null)
 134  0
                                         cachedPics[i].label = df.format(pmf.getDB().getPicRate(cachedPics[i].name, cachedMetaSet, 75));
 135  
                 }
 136  0
                 dom = PicBrowserDom.createDocument(pmf, user, cachedPics, cachedOffset);
 137  
                 try {
 138  0
                         OutputStream stream = response.getOutputStream();
 139  0
                         OutputFormat outformat = OutputFormat.createPrettyPrint();
 140  0
                         outformat.setEncoding("ISO-8859-1");
 141  0
                         XMLWriter writer = new XMLWriter(stream, outformat);
 142  0
                         writer.write(dom);
 143  0
                         writer.flush();
 144  0
                         writer.close();
 145  0
                         stream.close();
 146  0
                 } catch (UnsupportedEncodingException e) {
 147  0
                         logger.info(e);
 148  0
                         pmf.addError(e);
 149  0
                 } catch (IOException e) {
 150  0
                         logger.info(e);
 151  0
                         pmf.addError(e);
 152  0
                 }
 153  
 
 154  0
                 if (cachedPics != null && user.isPicBrowserShowRatings()) {
 155  0
                         int start = user.getPicBrowserPicsPerPage()+cachedOffset;
 156  0
                         int end = start+cachedOffset < cachedPics.length ? start+cachedOffset : cachedPics.length;
 157  0
                         for (int i=start; i<end; i++)
 158  0
                                 if (cachedPics[i].label == null)
 159  0
                                         cachedPics[i].label = df.format(pmf.getDB().getPicRate(cachedPics[i].name, cachedMetaSet, 75));
 160  
                 }
 161  0
                 return null;
 162  
         }
 163  
         
 164  
         private MetaSet buildMetaSetFromQuery(String query) {
 165  0
                 MetaSet ms = new MetaSet();
 166  0
                 if (DEBUG)
 167  0
                         logger.info("buildFromQuery '" + query + "'");
 168  0
                 String[] ss = query.split("&");
 169  0
                 for (int i=0; i<ss.length; i++) {
 170  
                         MetaSetRule rule;
 171  0
                         String[] pv = ss[i].split("=");
 172  0
                         char command = pv[0].charAt(0);
 173  0
                         int index = Integer.parseInt(pv[0].substring(1));
 174  0
                         int value = 0;
 175  
                         try {
 176  0
                                 value = Integer.parseInt(pv[1]);
 177  0
                         } catch (Exception e) {}
 178  
 
 179  0
                         if (index >= ms.getRuleCount()) {
 180  0
                                 rule = new MetaSetRule();
 181  0
                                 rule.setIndex(index);
 182  0
                                 ms.addRule(rule);
 183  
                         } else {
 184  0
                                 rule = ms.getRule(index);
 185  
                         }
 186  0
                         if (command == 's') {
 187  0
                                 if (value < 0) {
 188  0
                                         rule.setType(MetaSet.FUNCTION);
 189  0
                                         rule.setSid(value);
 190  0
                                         rule.setRateVal(value);
 191  
                                 } else {
 192  0
                                         rule.setType(MetaSet.NAME);
 193  0
                                         rule.setSid(value);
 194  0
                                         rule.setValue(pmf.getDB().getSet(value).getName());
 195  
                                 }
 196  
                         }
 197  0
                         if (command == 'r') {
 198  0
                                 rule.setType(MetaSet.OPERATOR);
 199  0
                                 rule.setOperator(value);
 200  
                         }
 201  0
                         if (command == 'q')
 202  0
                                 rule.setRateOp(value);
 203  0
                         if (command == 'v' && rule.getType() != MetaSet.FUNCTION)
 204  0
                                 rule.setRateVal(value);
 205  
                 }
 206  0
                 return(ms);
 207  
         }
 208  
 }