<commit_before>/*
 * InfoServlet.java
 * 
 * Copyright (c) 2011 Rhythm & Hues Studios. All rights reserved.
 */

package com.rhythm.louie.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rhythm.louie.ExternalProperties;
import com.rhythm.louie.ServiceManager;
import com.rhythm.louie.ServiceProperties;

import com.rhythm.pb.command.ArgType;
import com.rhythm.pb.command.PBCommand;
import com.rhythm.pb.command.PBParamType;
import com.rhythm.pb.command.Service;

/**
 *
 * @author cjohnson
 */
public class InfoServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String service = request.getParameter("service");
        if (service==null || service.isEmpty()) {
            listAllServices(request,response);
        } else if (service.equals("all")) {
            showAllServiceCalls(request,response);
        } else {
            showServiceCalls(service,request,response);
        }
    }
    
    private void showServiceCalls(String serviceName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>LoUIE Services</title>");  
            out.println("<link rel=\"shortcut icon\" href=\"./favicon.ico\" />"); 
            out.println("</head>");
            out.println("<body>");
            
            out.println("<h1>" + serviceName+ "&nbsp;&nbsp;&nbsp;&nbsp;</h1>");
            out.println("<h3>"+getPropertyString(serviceName)+"</h3>");
            
            out.println("<a href=\"./info\">Service List</a><br><br>");
            
            try {
                Service service = ServiceManager.getService(serviceName);
                if (service == null) {
                    out.println("No Such Service: " + serviceName);
                } else {
                    writeGroupedServiceCalls(out, service,false);
                }
            } catch (Exception ex) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,"Error Looking up Service: " + serviceName);
                out.println("Error Looking up Service: " + serviceName+"\n"+ex.toString());
            }
            
            out.println("</body>");
            out.println("</html>");
        } finally { 
            out.close();
        }
    }
    
    private String getPropertyString(String serviceName) {
        StringBuilder sb = new StringBuilder();
        ServiceProperties props = ServiceProperties.getServiceProperties(serviceName);
        boolean hasProps = false;

        if (props.isCentralized()) {
            sb.append("Centralized");
            hasProps = true;
        }

        if (props.isReadOnly()) {
            if (hasProps) {
                sb.append(" / ");
            }
            sb.append("Read Only");
            hasProps = true;
        }

        if (!props.isCachingOn()) {
            if (hasProps) {
                sb.append(" / ");
            }
            sb.append("Caching OFF");
            hasProps = true;
        }

        return sb.toString();
    }
    
    private void writeGroupedServiceCalls(PrintWriter out, Service service) throws Exception {
        writeGroupedServiceCalls(out,service,true);
    }
    
    
    private void writeGroupedServiceCalls(PrintWriter out, Service service, boolean showServiceName) throws Exception {
        String serviceName = service.getServiceName();
        ServiceProperties props = ServiceProperties.getServiceProperties(serviceName);
        
        if (showServiceName) {
            out.println("<h3>" + serviceName+ "&nbsp;&nbsp;&nbsp;&nbsp;</h3>");
            out.println(getPropertyString(serviceName)+"<br>");
        }
        Map<String, List<String>> groupedMethods = new TreeMap<String, List<String>>();
        
        List<PBCommand<?,?>> pbcommands = new ArrayList<PBCommand<?,?>>(service.getCommands());
        Collections.sort(pbcommands, new Comparator<PBCommand<?,?>>() {

            @Override
            public int compare(PBCommand<?, ?> o1, PBCommand<?, ?> o2) {
                String group1 = o1.getGroup();
                String group2 = o2.getGroup();
                boolean dep1 = o1.isDeprecated();
                boolean dep2 = o2.isDeprecated();
                if (dep1 && !dep2) return 1;
                if (dep2 && !dep1) return -1;
                if (!group1.equals("") && !group2.equals("")) {
                    int order1 = o1.getGroupOrder();
                    int order2 = o2.getGroupOrder();

                    if (order1 == order2) { return 0; }
                    if (order1 == -1) { return 1; }
                    else if (order2 == -1) { return -1; }
                    return ((Integer)order1).compareTo(order2);
                }
                return o1.getCommandName().compareTo(o2.getCommandName());
            }
            
        });
        for (PBCommand<?, ?> command : pbcommands) {<commit_msg>Info - bug fix command sort vialated comparable contract<commit_after>/*
 * InfoServlet.java
 * 
 * Copyright (c) 2011 Rhythm & Hues Studios. All rights reserved.
 */

package com.rhythm.louie.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rhythm.louie.ExternalProperties;
import com.rhythm.louie.ServiceManager;
import com.rhythm.louie.ServiceProperties;

import com.rhythm.pb.command.ArgType;
import com.rhythm.pb.command.PBCommand;
import com.rhythm.pb.command.PBParamType;
import com.rhythm.pb.command.Service;

/**
 *
 * @author cjohnson
 */
public class InfoServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String service = request.getParameter("service");
        if (service==null || service.isEmpty()) {
            listAllServices(request,response);
        } else if (service.equals("all")) {
            showAllServiceCalls(request,response);
        } else {
            showServiceCalls(service,request,response);
        }
    }
    
    private void showServiceCalls(String serviceName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>LoUIE Services</title>");  
            out.println("<link rel=\"shortcut icon\" href=\"./favicon.ico\" />"); 
            out.println("</head>");
            out.println("<body>");
            
            out.println("<h1>" + serviceName+ "&nbsp;&nbsp;&nbsp;&nbsp;</h1>");
            out.println("<h3>"+getPropertyString(serviceName)+"</h3>");
            
            out.println("<a href=\"./info\">Service List</a><br><br>");
            
            try {
                Service service = ServiceManager.getService(serviceName);
                if (service == null) {
                    out.println("No Such Service: " + serviceName);
                } else {
                    writeGroupedServiceCalls(out, service,false);
                }
            } catch (Exception ex) {
                String error = "Error Looking up Service: "+serviceName+" "+ex.toString();
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, error);
                out.println(error);
            }
            
            out.println("</body>");
            out.println("</html>");
        } finally { 
            out.close();
        }
    }
    
    private String getPropertyString(String serviceName) {
        StringBuilder sb = new StringBuilder();
        ServiceProperties props = ServiceProperties.getServiceProperties(serviceName);
        boolean hasProps = false;

        if (props.isCentralized()) {
            sb.append("Centralized");
            hasProps = true;
        }

        if (props.isReadOnly()) {
            if (hasProps) {
                sb.append(" / ");
            }
            sb.append("Read Only");
            hasProps = true;
        }

        if (!props.isCachingOn()) {
            if (hasProps) {
                sb.append(" / ");
            }
            sb.append("Caching OFF");
            hasProps = true;
        }

        return sb.toString();
    }
    
    private void writeGroupedServiceCalls(PrintWriter out, Service service) throws Exception {
        writeGroupedServiceCalls(out,service,true);
    }
    
    
    private void writeGroupedServiceCalls(PrintWriter out, Service service, boolean showServiceName) throws Exception {
        String serviceName = service.getServiceName();
        ServiceProperties props = ServiceProperties.getServiceProperties(serviceName);
        
        if (showServiceName) {
            out.println("<h3>" + serviceName+ "&nbsp;&nbsp;&nbsp;&nbsp;</h3>");
            out.println(getPropertyString(serviceName)+"<br>");
        }
        Map<String, List<String>> groupedMethods = new TreeMap<String, List<String>>();
        
        List<PBCommand<?,?>> pbcommands = new ArrayList<PBCommand<?,?>>(service.getCommands());
        Collections.sort(pbcommands, new Comparator<PBCommand<?,?>>() {

            @Override
            public int compare(PBCommand<?, ?> o1, PBCommand<?, ?> o2) {
                int cmp = Boolean.compare(o1.isDeprecated(), o2.isDeprecated());
                if (cmp == 0) {
                    cmp = String.CASE_INSENSITIVE_ORDER.compare(o1.getGroup(), o2.getGroup());
                }
                if (cmp == 0) {
                    cmp = Integer.compare(o1.getGroupOrder(), o2.getGroupOrder());
                }
                if (cmp == 0) {
                    cmp = o1.getCommandName().compareTo(o2.getCommandName());
                }
                return cmp;
            }
            
        });
        for (PBCommand<?, ?> command : pbcommands) {
{'fraction_non_alphanumeric': 0.12621011115095015, 'fraction_numerical': 0.00627465041233417, 'mean_word_length': 2.111266034578918, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4739658', 'n_tokens_mistral': 3012, 'n_tokens_neox': 2936, 'n_words': 598}