Example usage for java.util Vector elementAt

List of usage examples for java.util Vector elementAt

Introduction

In this page you can find the example usage for java.util Vector elementAt.

Prototype

public synchronized E elementAt(int index) 

Source Link

Document

Returns the component at the specified index.

Usage

From source file:skoa.helpers.Graficos.java

private void compararFicheros(String freferencia, String fcomparar) {
    System.out.println("min=" + rangoMinutos + ".");
    File fr = new File(ruta + freferencia); //El primer fichero es el de referencia, y del que se sacan los incrementos.
    File fc = new File(ruta + fcomparar);
    FileReader fr1 = null, fr2 = null;
    BufferedReader linea1 = null, linea2 = null;
    String L1 = null, L2 = null;/* w  ww  .  j a  v a2 s .  co  m*/
    SimpleDateFormat formatoDelTexto = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    //Vamos a abrir los dos ficheros a la vez, leyendo lnea por lnea y comparando.
    try {
        File temp = new File(ruta + "temp.txt");
        if (!temp.exists())
            temp.createNewFile(); //Crea el fichero
        //LEEMOS EL PRIMER FICHERO
        fr1 = new FileReader(fr);
        linea1 = new BufferedReader(fr1);
        Vector<String> fechas = new Vector<String>(); //vector donde irn todas las fechas
        Date d1 = null, dini = null, dfin = null;
        boolean primeravez = true, nuevo = true;
        float media1 = 0;
        int nelem1 = 0, segd1 = 0;
        String fecha = "", aux = "", lant = "";
        while ((L1 = linea1.readLine()) != null) {
            aux = L1.substring(0, L1.indexOf("\t")); //Fecha de la lnea.
            d1 = formatoDelTexto.parse(aux);
            String Ssegd1 = aux.substring(aux.indexOf(" ") + 7, aux.indexOf(" ") + 9);
            segd1 = Integer.parseInt(Ssegd1);
            d1.setSeconds(segd1);
            if (nuevo) { //Si terminamos el intervalo, pasamos al siguiente.
                Calendar c = Calendar.getInstance();
                if (!primeravez) {//Si no es la primera iteracion, se guarda antes de inicializar.
                    //System.out.println(media1+"  "+nelem1);
                    media1 = media1 / nelem1;
                    BufferedWriter bw = new BufferedWriter(new FileWriter(ruta + "temp.txt", true)); //Para escribir.
                    String x1 = "" + media1;
                    if (!x1.contentEquals("NaN"))
                        bw.write("" + fecha + "\t" + media1 + " " + unidad + "\n");
                    bw.close();
                    media1 = nelem1 = 0;
                    String v = lant.substring(lant.indexOf("\t") + 1);
                    v = v.substring(0, v.indexOf(" "));
                    media1 = Float.parseFloat(v);//Se inicializan con los valores de la anterior linea.
                    nelem1 = 1;
                } else {
                    String u = L1.substring(L1.indexOf("\t") + 1);
                    unidad = u.substring(u.indexOf(" ") + 1);
                    lant = L1;
                    //Si es la 1 vez, tambin se inicializa la fecha inicial.
                }
                primeravez = false;
                fecha = lant.substring(0, lant.indexOf("\t"));
                fechas.add(fecha);
                //Inserta la fecha en el vector de fechas, para luego usarlo en el 2 fichero.
                Date diniaux = formatoDelTexto.parse(fecha);
                String Ssegd2 = fecha.substring(fecha.indexOf(" ") + 7, fecha.indexOf(" ") + 9);
                int segd2 = Integer.parseInt(Ssegd2);
                c.setTime(diniaux);
                dini = c.getTime();
                dini.setSeconds(segd2);
                //System.out.println("Ini="+dini);
                c.add(Calendar.MINUTE, Integer.parseInt(rangoMinutos));
                dfin = c.getTime();
                dfin.setSeconds(segd2);
                //System.out.println("Fin="+dfin);
                nuevo = false;//Esta variable se usa para definir otra vez un nuevo intervalo
            }
            if (d1.compareTo(dini) == 0) { //Fechas Iguales
                aux = L1.substring(L1.indexOf("\t") + 1);
                aux = aux.substring(0, aux.indexOf(" "));
                media1 = media1 + Float.parseFloat(aux);
                nelem1++;
            } else if (d1.compareTo(dini) > 0 && d1.compareTo(dfin) < 0) { //Est dentro del intervalo
                aux = L1.substring(L1.indexOf("\t") + 1);
                aux = aux.substring(0, aux.indexOf(" "));
                media1 = media1 + Float.parseFloat(aux);
                nelem1++;
            } else {//Si la fecha es menor que la fecha inicial o mayor que la final, se cambia de intervalo.
                nuevo = true;
            }
            lant = L1;
        }
        //guardo lo ultimo y penultimo si lo hay
        media1 = media1 / nelem1;
        BufferedWriter bw = new BufferedWriter(new FileWriter(ruta + "temp.txt", true)); //Para escribir.
        String x1 = "" + media1;
        String auxi = dini.toGMTString(); //d mon yyyy hh:mm:ss
        String d = auxi.substring(0, auxi.indexOf(" "));
        if (Integer.parseInt(d) < 10)
            d = "0" + d;
        auxi = auxi.substring(auxi.indexOf(" ") + 1);
        String m = auxi.substring(0, auxi.indexOf(" "));
        if (m.contentEquals("Jan"))
            m = "01";
        if (m.contentEquals("Feb"))
            m = "02";
        if (m.contentEquals("Mar"))
            m = "03";
        if (m.contentEquals("Apr"))
            m = "04";
        if (m.contentEquals("May"))
            m = "05";
        if (m.contentEquals("Jun"))
            m = "06";
        if (m.contentEquals("Jul"))
            m = "07";
        if (m.contentEquals("Aug"))
            m = "08";
        if (m.contentEquals("Sep"))
            m = "09";
        if (m.contentEquals("Oct"))
            m = "10";
        if (m.contentEquals("Nov"))
            m = "11";
        if (m.contentEquals("Dec"))
            m = "12";
        auxi = auxi.substring(auxi.indexOf(" ") + 1);
        String y = auxi.substring(0, auxi.indexOf(" "));
        auxi = auxi.substring(auxi.indexOf(" ") + 1);
        String h = auxi.substring(0, auxi.indexOf(" "));
        //System.out.println(y+"-"+m+"-"+d+" "+h);
        if (!x1.contentEquals("NaN"))
            bw.write("" + y + "-" + m + "-" + d + " " + h + "\t" + media1 + " " + unidad + "\n");
        bw.close();
        fechas.add(y + "-" + m + "-" + d + " " + h);
        fecha = lant.substring(0, lant.indexOf("\t"));
        if (!fecha.isEmpty()) {
            String auxr = lant.substring(lant.indexOf("\t") + 1);
            auxr = auxr.substring(0, auxr.indexOf(" "));
            media1 = Float.parseFloat(auxr);
            bw = new BufferedWriter(new FileWriter(ruta + "temp.txt", true)); //Para escribir.
            x1 = "" + media1;
            if (!x1.contentEquals("NaN"))
                bw.write("" + fecha + "\t" + media1 + " " + unidad + "\n");
            bw.close();
            fechas.add(fecha);
        }
        fr1.close();
        //for (int i=0;i<fechas.size();i++) System.out.println("*"+fechas.elementAt(i));

        //Leido el primer fichero, leo el segundo.
        File temp2 = new File(ruta + "temp2.txt");
        if (!temp2.exists())
            temp2.createNewFile(); //Crea el fichero
        fr2 = new FileReader(fc);
        linea2 = new BufferedReader(fr2);
        int pos = 0;
        String fechaf = "";
        media1 = nelem1 = 0;
        nuevo = true;
        primeravez = true;
        while ((L2 = linea2.readLine()) != null) {
            aux = L2.substring(0, L2.indexOf("\t")); //Fecha de la lnea.
            d1 = formatoDelTexto.parse(aux);
            String Ssegd1 = aux.substring(aux.indexOf(" ") + 7, aux.indexOf(" ") + 9);
            segd1 = Integer.parseInt(Ssegd1);
            d1.setSeconds(segd1);
            if (nuevo) { //Si terminamos el intervalo, pasamos al siguiente.            
                Calendar c = Calendar.getInstance();
                if (!primeravez) {//Si no es la primera iteracion, se guarda antes de inicializar.
                    media1 = media1 / nelem1;
                    BufferedWriter bw1 = new BufferedWriter(new FileWriter(ruta + "temp2.txt", true)); //Para escribir.
                    x1 = "" + media1;
                    if (!x1.contentEquals("NaN"))
                        bw1.write("" + /*fechas.elementAt(pos)+"\t"+*/media1 + " " + unidad + "\n");
                    bw1.close();
                    pos++;
                    //media1=nelem1=0;
                    String v = lant.substring(lant.indexOf("\t") + 1);
                    v = v.substring(0, v.indexOf(" "));
                    media1 = Float.parseFloat(v);//Se inicializan con los valores de la anterior linea.
                    nelem1 = 1;
                } else {
                    String u = L2.substring(L2.indexOf("\t") + 1);
                    unidad = u.substring(u.indexOf(" ") + 1);
                    lant = L1;
                    pos = 0;
                    //Si es la 1 vez, tambin se inicializa la fecha inicial.
                }
                //System.out.println(fechas.elementAt(pos));
                primeravez = false;
                fecha = fechas.elementAt(pos);
                Date diniaux = formatoDelTexto.parse(fecha);
                String Ssegd2 = fecha.substring(fecha.indexOf(" ") + 7, fecha.indexOf(" ") + 9);
                int segd2 = Integer.parseInt(Ssegd2);
                c.setTime(diniaux);
                dini = c.getTime(); //FECHA INICIAL.                    
                dini.setSeconds(segd2);

                if (pos + 1 >= fechas.size())
                    break;
                fechaf = fechas.elementAt(pos + 1);
                Date dfinaux = formatoDelTexto.parse(fechaf);
                Ssegd2 = fecha.substring(fechaf.indexOf(" ") + 7, fechaf.indexOf(" ") + 9);
                segd2 = Integer.parseInt(Ssegd2);
                c.setTime(dfinaux);
                dfin = c.getTime(); //FECHA FINAL
                dfin.setSeconds(segd2);
                //System.out.println("INI="+dini);
                //System.out.println("FIN="+dfin);
                nuevo = false;//Esta variable se usa para definir otra vez un nuevo intervalo
            }
            if (d1.compareTo(dini) == 0) { //Fechas Iguales
                aux = L2.substring(L2.indexOf("\t") + 1);
                aux = aux.substring(0, aux.indexOf(" "));
                media1 = media1 + Float.parseFloat(aux);
                nelem1++;
            } else if (d1.compareTo(dini) > 0 && d1.compareTo(dfin) < 0) { //Est dentro del intervalo
                aux = L2.substring(L2.indexOf("\t") + 1);
                aux = aux.substring(0, aux.indexOf(" "));
                media1 = media1 + Float.parseFloat(aux);
                nelem1++;
            } else {//Si la fecha es menor que la fecha inicial o mayor que la final, se cambia de intervalo.
                nuevo = true;
            }
            lant = L2;
        }
        //guardo lo ultimo si lo hay
        fecha = lant.substring(0, lant.indexOf("\t"));
        if (!fecha.isEmpty()) {
            String auxr = lant.substring(lant.indexOf("\t") + 1);
            auxr = auxr.substring(0, auxr.indexOf(" "));
            media1 = Float.parseFloat(auxr);
            BufferedWriter bw2 = new BufferedWriter(new FileWriter(ruta + "temp2.txt", true)); //Para escribir.
            x1 = "" + media1;
            if (!x1.contentEquals("NaN"))
                bw2.write("" + /*fechas.elementAt(pos+1)+"\t"+*/media1 + " " + unidad + "\n");
            bw2.close();
        }
        fr2.close();
        //CREAMOS EL UNIFICADO
        File unificado = new File(ruta + "unificado.txt");
        if (!unificado.exists())
            unificado.createNewFile(); //Crea el fichero
        fr1 = new FileReader(temp);
        linea1 = new BufferedReader(fr1);
        fr2 = new FileReader(temp2);
        linea2 = new BufferedReader(fr2);
        L1 = L2 = "";
        BufferedWriter bwf = new BufferedWriter(new FileWriter(ruta + "unificado.txt", true)); //Para escribir.
        while ((L1 = linea1.readLine()) != null && (L2 = linea2.readLine()) != null) {
            bwf.write(L1 + "\t" + L2 + "\n");
        }
        bwf.close();
        fechas.removeAllElements();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != fr1)
                fr1.close(); //cierra el 1 fichero.
            if (null != fr2)
                fr2.close(); //cierra el 2 fichero.
        } catch (Exception e2) { //Sino salta una excepcion.
            e2.printStackTrace();
        }
    }
    File temp1 = new File(ruta + "temp.txt");
    File temp2 = new File(ruta + "temp2.txt");
    temp1.delete();
    temp2.delete();
}

From source file:cs.man.ac.uk.stats.ComputeANOVAStats.java

/**
 * Performs an ANOVA analysis on the data read in.
 * /*from   ww w.j ava 2s. c  o  m*/
 * @param outputPath the path to output details of the ANOVA analysis to.
 */
private static void ANOVAAnalysis(String outputPath) {
    /**
     * OUTPUT FILE PREPARATION
     */

    // Clear up output path.
    Common.fileDelete(outputPath);

    String tukeyOutputPath = outputPath.replace(".csv", "_HSD.csv");
    Common.fileDelete(tukeyOutputPath);
    Writer.append(tukeyOutputPath,
            "Result 1,Result 2,Test,Metric,MSwg,DFwg,n,alpha,HSD,H0 (1=rejected & 0=accepted),Outcome\n");

    // Write header information to output path
    String[] headers = header.split(",");
    Writer.append(outputPath, headers[0] + ",");

    for (int i = 1; i < headers.length; i++)
        Writer.append(outputPath,
                headers[i] + ",F-ratio,P-value,Fb,Fw,H0 (1=rejected & 0=accepted), alpha=" + alpha + ",");

    Writer.append(outputPath, "\n");

    /**
     * PERFROM ANOVA
     */

    for (Map.Entry<String, Vector<ANOVA>> entry : anovaObjects.entrySet()) {
        String key = entry.getKey();
        Vector<ANOVA> vector = entry.getValue();

        /**
         * OK, its crucial to understand what is going on here. We have a number of files
         * containing results of algorithm tests. Each File will contain the results of
         * a number of different tests using different parameters. Note that each files contains
         * results for a specific algorithm only.
         * 
         * Now if we want to perform ANOVA analysis on results from multiple algorithms, we
         * must analyze the results in these files together rather than in isolation. So we have
         * the following situation: n files, containing results of m tests, occurring in the same order in each
         * file. These are directly comparable results, for instance:
         * 
         *    FILE 1               FILE 2               FILE N
         *  Test 1               Test 1               Test 1   -|
         *  Test 1               Test 1               Test 1   |
         *  Test 1               Test 1               Test 1   |---> Test Block 1 (i.e. multiple runs of same test)
         *  Test 1               Test 1               Test 1   |
         *  Test 1               Test 1               Test 1   -|
         *  
         *  Test 2               Test 2               Test 2
         *  Test 2               Test 2               Test 2
         *  Test 2               Test 2               Test 2
         *  Test 2               Test 2               Test 2
         *  Test 2               Test 2               Test 2
         *  
         *  Test n               Test n               Test n   -|
         *  Test n               Test n               Test n    |
         *  Test n               Test n               Test n    |---> Test Block n
         *  Test n               Test n               Test n    |
         *  Test n               Test n               Test n   -|
         *  
         *  ** Note each test result is made up of a number of recorded metrics. For instance Test Block 1 in file 1
         *  would look something like (with four metrics recorded during testing lets say TP,TN,FP,FN):
         *  
         *  120   ,   6   ,   5   ,   3   -|---> Run 1 --->
         *  118   ,   7   ,   6   ,   4    |            |
         *  122   ,   8   ,   7   ,   5    |            |---> Test Block 1.
         *  130   ,   12   ,   5   ,   13    |            |
         *  100   ,   2   ,   5   ,   7   -|---> Run 5 --->
         *  
         *  The results of each test are actually described in terms of k variables (typically k=16). These variables
         *  include the true positives (TP), false positives (FP), accuracy, f-score etc. Thus to compare the results
         *  we need to do ANOVA analysis using data occurring in each of the three files. However we can only compare
         *  like with like. So in the example above we can only perform ANOVA on comparable test blocks. In
         *  which case ANOVA would be performed on Test 1 data in files 1, 2 and 3, then Test 2 data and so on.
         *  At no point would Test 1 data in any of the files be compared with Test 2 data for example.
         *  
         *  The code below does this. Using arrays we perform ANOVA on each metric
         *  in the test blocks. Clearly this makes the code below somewhat complicated to understand!
         *  I'm genuinely sorry for that, the main reason is because I may have to perform this type of 
         *  analysis many thousands of times. But I'll try to explain how it works.
         *  
         *  For each Test block, in each file, an ANOVA object is generated in code above ( in the process() method).
         *  Each ANOVA object essentially contains a matrix of the data collected in a test block. These ANOVA objects
         *  have methods that enable them to calculate the mean and sum of the values in their matrix. For instance,
         *  Test 1 involves ten runs of the same test. For each test, lets say we collect 4 pieces of data, the number of 
         *  true positives, true negatives, false positives and false negatives. An ANOVA object for Test 1 for File 1
         *  will contain a matrix of this information, and calculate the means/sums of these four variables storing them in:
         *  
         *  private double sums[];
         *   private double means[];
         *
         *  So then,
         *  
         *  sums[0] contains the sum of true positives.
         *  sums[1] contains the sum of true negatives.
         *  sums[2] contains the sum of false positives.
         *  sums[3] contains the sum of false negatives.  
         *  
         *  And likewise for the means.
         *  
         *  When the process() method terminates we have a number of ANOVA objects stored in a TreeMap structure,
         *  which groups comparable ANOVA objects by storing them in the same vector. 
         *  
         *  Here then we begin iterating through this tree map, and calculate the F-ratio for comparable ANOVA objects.
         *  This way we can calculate all the ANOVA results automatically, for every variable we have.
         */

        /*
         * ANOVA WORKED EXAMPLE (credit Wikipedia!).
         * 
         * Consider an experiment to study the effect of three different levels of a factor
         * on a response (e.g. three levels of a fertilizer on plant growth). If we had 6 observations
         * for each level, we could write the outcome of the experiment in a table like this, 
         * where a1, a2, and a3 are the three levels of the factor being studied. 
         * 
         * a1   a2   a3
         * 6   8   13
         * 8   12   9
         * 4   9   11
         * 5   11   8
         * 3   6   7
         * 4   8   12
         * 
         * The null hypothesis, denoted H0, for the overall F-test for this experiment would be that
         * all three levels of the factor produce the same response, on average. To calculate the F-ratio:
         * 
         * Step 1: Calculate the mean within each group:
         * 
         *    Y1 = ( 6 + 8 + 4 + 5 + 3 + 4 ) / 6       = 5
         *  Y2 = ( 8 + 12 + 9 + 11 + 6 + 8 ) / 6   = 9
         *  Y3 = ( 13 + 9 + 11 + 8 + 7 + 12 ) / 6   = 10
         * 
         * Step 2: Calculate the overall mean, Y:
         * 
         *    Y = (Y1 + Y2 + Y3) / 3 = 8.
         * 
         * Step 3: Calculate the "between-group" sum of squares:
         * 
         *  "between-group" sum of squares = n(Y1-Y)^2 + n(Y2-Y)^2 + n(Y3-Y)^2
         *                          = 6(5-8)^2 + 6(9-8)^2 + 6(9-8)^2
         *                          = 84
         * 
         * Step 4: The between-group degrees of freedom is one less than the number of groups.
         * 
         *   between-group degrees of freedom = a - 1
         *                             = 3-1
         *                             = 2
         *   
         * Step 5: The between-group mean square value is
         * 
         *  between-group mean square value = "between-group" sum of squares / between-group degrees of freedom
         *                          = 84/2
         *                          = 42
         *  
         * Step 6: Calculate the "within-group" sum of squares. Begin by centering the data in each group
         * 
         *       a1         a2         a3
         * 6 - 5 = 1   8 - 9 = -1   13 - 10 = 3
         * 8 - 5 = 3   12 - 9 = 3   9 - 10 = -1
         * 4 - 5 = -1   9 - 9 = 0   11 - 10 = 1
         * 5 - 5 = 0   11 - 9 = 2   8 - 10 = -2
         * 3 - 5 = -2   6 - 9 = -3   7 - 10 = -3
         * 4 - 5 = -1   8 - 9 = -1   12 - 10 = 2
         * 
         *    within-group sum of squares = 1^2 + 3^2 + (-1)^2 + 0^2 + (-2)^2 + (-1)^2 +
         *                           (-1)^2 + 3^2 + 0^2 + 2^2 + (-3)^2 + (-1)^2 +
         *                           3^2 + (-1)^2 + 1^2 + (-2)^2 + (-3)^2 + 2^2
         * 
         *                         = 1 + 9 + 1 + 0 + 4 + 1 + 1 + 9 + 0 + 4 + 9 + 1 + 9 + 1 + 1 + 4 + 9 + 4
         *                         = 68
         * 
         * Step 7: The within-group degrees of freedom is 
         *  
         *  within-group degrees of freedom = a(n-1)
         *                          = 3(6-1)
         *                          = 15
         * 
         * Step 8: Thus the within-group mean square value is,
         * 
         *  within-group mean square value = within-group sum of squares / within-group degrees of freedom
         *                          = 68 / 15
         *                          = 4.5
         * Step 9: The F-ratio is
         * 
         *  F-ratio = between-group mean square value /  within-group mean square value
         *        = 42/4.5
         *        = 9.3
         *  
         *  The critical value is the number that the test statistic must exceed to reject the test.
         *  In this case, Fcrit(2,15) = 3.68 at alpha = 0.05. Since F = 9.3 > 3.68, the results are
         *  significant at the 5% significance level. One would reject the null hypothesis, concluding
         *  that there is strong evidence that the expected values in the three groups differ. 
         *  The p-value for this test is 0.002.
         */

        /**
         * ANOVA Variables:
         * 
         * a         =   Number of distinct test groups (corresponds to number of input files).
         * 
         * n         =   Number of data items per test group (corresponds to data items in a test block).
         * 
         * overallMeans   =   An array which stores the means for each metric recorded in a test block.
         * 
         * sumSquaresBetweenGroup   =   the "between-group" sum of squares.
         * 
         * freedomBetweenGroup      =   The between-group degrees of freedom is one less than the number of groups.
         * 
         * meanSquareBetweenGroup   =   Stores the between-group mean square values.
         * 
         * sumSquaresWithinGroup   =   The within-group sum of squares is the sum of squares.
         * 
         * freedomWithinGroup      =   The within-group degrees of freedom is.
         * 
         * meanSquareWithinGroup   =   Stores the within-group mean square values.
         * 
         * F_Ratios               =   The F-ratio's.
         */

        int a = vector.size();// Number of groups.
        int n = vector.elementAt(0).getRows();// Number of data values per group.

        // Number of recorded metrics per test (number of variables).
        int metrics = vector.elementAt(0).getColumns();

        double[] overallMeans = new double[metrics];
        double[] sumSquaresBetweenGroup = new double[metrics];
        double[] meanSquareBetweenGroup = new double[metrics];
        double[] sumSquaresWithinGroup = new double[metrics];
        double[] meanSquareWithinGroup = new double[metrics];
        double[] F_Ratios = new double[metrics];

        //STEP 1. Calculate the overall means.
        for (int i = 0; i < vector.size(); i++)
            for (int j = 0; j < vector.elementAt(0).getColumns(); j++)
                overallMeans[j] += vector.elementAt(i).getMean(j);

        //STEP 2. Divide the overall means by the number of groups.
        for (int j = 0; j < overallMeans.length; j++)
            overallMeans[j] = overallMeans[j] / (double) vector.size();

        //STEP 3.  Calculate the "between-group" sum of squares:
        for (int i = 0; i < vector.size(); i++)
            for (int j = 0; j < vector.elementAt(0).getColumns(); j++)
                sumSquaresBetweenGroup[j] += (double) n
                        * (Math.pow((vector.elementAt(i).getMean(j) - overallMeans[j]), 2));

        //STEP 4: The between-group degrees of freedom
        double freedomBetweenGroup = a - 1;

        //STEP 5. between-group mean square value
        for (int i = 0; i < meanSquareBetweenGroup.length; i++)
            meanSquareBetweenGroup[i] = sumSquaresBetweenGroup[i] / freedomBetweenGroup;

        //STEP 6. Sum of centered squares (partly already calculated by ANOVA objects.
        for (int i = 0; i < vector.size(); i++)
            for (int j = 0; j < vector.elementAt(0).getColumns(); j++)
                sumSquaresWithinGroup[j] += vector.elementAt(i).getSumCentredSquares(j);

        //STEP 7.
        double freedomWithinGroup = (double) a * (n - 1);

        //STEP 8. The within-group mean square value is...
        for (int i = 0; i < meanSquareWithinGroup.length; i++)
            meanSquareWithinGroup[i] = sumSquaresWithinGroup[i] / freedomWithinGroup;

        // STEP 9. The final F-ratios are...
        for (int i = 0; i < F_Ratios.length; i++)
            F_Ratios[i] = meanSquareBetweenGroup[i] / meanSquareWithinGroup[i];

        Writer.append(outputPath, key + ",");

        for (int i = 0; i < F_Ratios.length; i++) {
            // The p-value is the probability of obtaining a test statistic,
            // at least as extreme as the one that was actually observed, 
            // assuming that the null hypothesis is true.
            FDistribution fdist = new FDistribution(freedomBetweenGroup, freedomWithinGroup);

            double pValue = (1.0 - fdist.cumulativeProbability(F_Ratios[i]));

            // headers[i]+",F-ratio,P-value,Fb,Fw,H0 (1=rejected & 0=accepted), alpha="+alpha+","
            if (pValue < alpha)
                Writer.append(outputPath, "," + F_Ratios[i] + "," + pValue + "," + freedomBetweenGroup + ","
                        + freedomWithinGroup + "," + "1,,");
            else
                Writer.append(outputPath, "," + F_Ratios[i] + "," + pValue + "," + freedomBetweenGroup + ","
                        + freedomWithinGroup + "," + "0,,");
        }

        Writer.append(outputPath, "\n");

        /**
         * TUKEY TEST
         * 
         * Now we have established the ANOVA results, that is we know the significance of the variance
         * between the individual test results. But knowing that there is a significant difference is not
         * enough. We need to know which test results were better and which were worse in order to determine
         * which algorithm performed better. To do this we need to perform the Tukey test. It performs a pair
         * wise comparison of the results so that they can be ranked.
         * 
         * The Studentized range statistic can then be calculated for any particular pair as:
         * 
         *    Q = ( ML  MS ) / sqrt( meanSquareWithinGroup / values per sample)
         * 
         *  and ML is the largest mean for a group, and MS is the smallest mean for a group.
         */

        // PAIRWISE COMPARISON
        for (int i = 0; i < vector.size(); i++) {
            for (int j = i + 1; j < vector.size(); j++) {
                // Here the comparison is performed. Remember we must do the Tukey test
                // on each metric. So we will calculate the HSD (Honestly Significant Difference)
                // multiple times.

                // For each metric
                for (int k = 0; k < vector.elementAt(i).getColumns(); k++) {
                    double mean_one = vector.elementAt(i).getMean(k);
                    double mean_two = vector.elementAt(j).getMean(k);
                    double meanSquaredWithinGroup = meanSquareWithinGroup[k];
                    double valuesPerSample = vector.elementAt(i).getRows();// All objects have same number of rows here.

                    double Q = 0;

                    // This is a string used to summarize the outcome of the test.
                    String outcome = vector.elementAt(i).getFileName() + " - "
                            + vector.elementAt(j).getFileName() + " +";

                    if (Double.compare(mean_one, mean_two) < 0) // mean_one < mean_two
                    {
                        Q = (mean_two - mean_one) / Math.sqrt(meanSquaredWithinGroup / valuesPerSample);
                        outcome = outcome.replace("-", " < ");
                    } else if (Double.compare(mean_one, mean_two) > 0) // mean_one > mean_two
                    {
                        Q = (mean_one - mean_two) / Math.sqrt(meanSquaredWithinGroup / valuesPerSample);
                        outcome = outcome.replace("-", " > ");
                    }

                    String H0Result = ""; // 1=rejected & 0=accepted
                    double QDist = getQDist(freedomWithinGroup, a, alpha);

                    if (Double.compare(Q, QDist) < 0) {
                        H0Result = "0";
                        outcome = outcome.replace("+", "H0 Accepted");
                    } else if (Double.compare(Q, QDist) > 0) {
                        H0Result = "1";
                        outcome = outcome.replace("+", "H0 Rejected");
                    } else {
                        H0Result = "-1";
                        outcome = outcome.replace("+", "H0 Accepted");
                    }

                    Writer.append(tukeyOutputPath,
                            vector.elementAt(i).getFileName() + "," + vector.elementAt(j).getFileName() + ","
                                    + key + "," + headers[k + 1] + "," + meanSquaredWithinGroup + ","
                                    + freedomWithinGroup + "," + valuesPerSample + "," + alpha + "," + Q + ","
                                    + H0Result + "," + outcome + "\n");
                }

                Writer.append(tukeyOutputPath, ",,,,\n");
            }

            Writer.append(tukeyOutputPath, ",,,,\n");
        }

        //System.out.println("\n\n");
    }
}

From source file:org.ecoinformatics.seek.datasource.eml.eml2.Eml200Parser.java

/**
 * process the attributes// w w w  .  j ava  2s .c o  m
 */
private void processAttributes(CachedXPathAPI xpathapi, NodeList atts, AttributeList attributeListObj)
        throws Exception {

    for (int i = 0; i < atts.getLength(); i++) { // go through each
        // attribute
        Node att = atts.item(i);
        NodeList attChildren = att.getChildNodes();
        NamedNodeMap attAttributes = att.getAttributes();

        String attName = "";
        String attLabel = "";
        String attDefinition = "";
        String attUnit = "";
        String attUnitType = "";
        String attStorageType = "";
        String attMeasurementScale = "";
        String attPrecision = "";
        Domain domain = null;
        Vector missingCodeVector = new Vector();

        elementId++;

        for (int j = 0; j < attChildren.getLength(); j++) {
            Node child = attChildren.item(j);
            String childName = child.getNodeName();
            if (childName.equals("attributeName")) {
                attName = child.getFirstChild().getNodeValue().trim().replace('.', '_');
            } else if (childName.equals("attributeLabel")) {
                attLabel = child.getFirstChild().getNodeValue().trim();
            } else if (childName.equals("attributeDefinition")) {
                attDefinition = child.getFirstChild().getNodeValue().trim();
            } else if (childName.equals("measurementScale")) {
                // unit is tricky because it can be custom or standard
                // Vector info = new Vector();
                // int domainType = Domain.DOM_NONE;
                NodeList msNodeList = child.getChildNodes();
                for (int k = 0; k < msNodeList.getLength(); k++) {
                    Node n = msNodeList.item(k);
                    String name = n.getNodeName();
                    if (name.equals("interval") || name.equals("ratio")) {
                        String numberType = null;
                        String min = "", max = "";
                        Node sUnit = xpathapi.selectSingleNode(n, "unit/standardUnit");
                        Node cUnit = xpathapi.selectSingleNode(n, "unit/customUnit");
                        if (sUnit != null) {
                            attUnit = sUnit.getFirstChild().getNodeValue();
                            attUnitType = Attribute.STANDARDUNIT;
                        } else if (cUnit != null) {
                            attUnit = cUnit.getFirstChild().getNodeValue();
                            attUnitType = Attribute.CUSTOMUNIT;
                        } else {
                            System.err.println("xpath didn't work");
                        }
                        Node precision = xpathapi.selectSingleNode(n, "precision");
                        if (precision != null) {
                            // precision is optional in EML201 so if it is
                            // not provided, the attPrecision will be the
                            // empty string
                            attPrecision = precision.getFirstChild().getNodeValue();
                        }
                        Node dNode = xpathapi.selectSingleNode(n, "numericDomain");
                        NodeList numberKids = dNode.getChildNodes();
                        for (int index = 0; index < numberKids.getLength(); index++) {

                            String dName = numberKids.item(index).getNodeName();
                            if (dName.equals("numberType")) // got number
                            // type
                            {
                                numberType = numberKids.item(index).getFirstChild().getNodeValue();
                                if (isDebugging) {
                                    log.debug("The number type is " + numberType);
                                }
                            } else if (dName.equals("boundsGroup"))
                            // got bounds group
                            {
                                NodeList boundsList = xpathapi.selectNodeList(dNode, "./bounds");
                                for (i = 0; i < boundsList.getLength(); i++) {
                                    NodeList nl;
                                    Node bound;

                                    String exclMin = null, exclMax = null;
                                    try {
                                        nl = xpathapi.selectNodeList(boundsList.item(i), "./minimum");
                                        bound = nl.item(0);
                                        min = bound.getFirstChild().getNodeValue();
                                        exclMin = bound.getAttributes().getNamedItem("exclusive")
                                                .getNodeValue();
                                        nl = xpathapi.selectNodeList(boundsList.item(0), "./maximum");
                                        bound = nl.item(0);
                                        max = bound.getFirstChild().getNodeValue();
                                        exclMax = bound.getAttributes().getNamedItem("exclusive")
                                                .getNodeValue();
                                    } catch (Exception e) {
                                        log.debug("Error in handle bound ", e);
                                    }
                                }

                            }

                        }
                        Double minNum = null;
                        Double maxNum = null;
                        if (!min.trim().equals("") && !max.trim().equals("")) {
                            minNum = new Double(min);
                            maxNum = new Double(max);
                        }
                        domain = new NumericDomain(numberType, minNum, maxNum);

                    } else if (name.equals("nominal") || name.equals("ordinal")) {
                        NodeList list = xpathapi.selectSingleNode(n, "nonNumericDomain").getChildNodes();
                        for (int m = 0; m < list.getLength(); m++) {
                            Node dNode = list.item(m);
                            String dName = dNode.getNodeName();
                            if (dName.equals("textDomain")) {
                                TextDomain textDomain = new TextDomain();
                                NodeList definitionL = xpathapi.selectNodeList(dNode, "./definition");
                                Node defintionNode = definitionL.item(0);
                                String definition = defintionNode.getFirstChild().getNodeValue();
                                if (isDebugging) {
                                    log.debug("The definition value is " + definition);
                                }
                                textDomain.setDefinition(definition);
                                NodeList nl = xpathapi.selectNodeList(dNode, "./pattern");
                                String[] patternList = new String[nl.getLength()];
                                for (int l = 0; l < nl.getLength(); l++) {
                                    patternList[l] = nl.item(l).getFirstChild().getNodeValue();
                                }
                                if (patternList.length > 0) {
                                    textDomain.setPattern(patternList);
                                }
                                domain = textDomain;

                            } else if (dName.equals("enumeratedDomain")) {
                                EnumeratedDomain enumerDomain = new EnumeratedDomain();
                                Vector info = new Vector();
                                NodeList nl = xpathapi.selectNodeList(dNode, "./codeDefinition");
                                for (int l = 0; l < nl.getLength(); l++) {
                                    info.add(nl.item(l).getFirstChild().getNodeValue());
                                }
                                enumerDomain.setInfo(info);
                                domain = enumerDomain;

                            }
                        }

                    } else if (name.equalsIgnoreCase("datetime")) {
                        DateTimeDomain date = new DateTimeDomain();
                        String formatString = (xpathapi.selectSingleNode(n, "./formatString")).getFirstChild()
                                .getNodeValue();
                        if (isDebugging) {
                            log.debug("The format string in date time is " + formatString);
                        }
                        date.setFormatString(formatString);
                        domain = date;

                    }
                }

            } else if (childName.equals("missingValueCode")) {
                log.debug("in missilng valueCode");
                NodeList missingNodeList = child.getChildNodes();
                for (int k = 0; k < missingNodeList.getLength(); k++) {
                    Node n = missingNodeList.item(k);
                    String name = n.getNodeName();
                    if (name.equals("code")) {

                        Node missingCodeTextNode = n.getFirstChild();
                        if (missingCodeTextNode != null) {
                            String missingCode = missingCodeTextNode.getNodeValue();
                            if (isDebugging) {
                                log.debug("the missing code is " + missingCode);
                            }
                            missingCodeVector.add(missingCode);
                            hasMissingValue = true;
                        }
                    }
                }

            }
        }

        String resolvedType;
        DataType dataType = domain.getDataType();
        resolvedType = dataType.getName();
        if (isDebugging) {
            log.debug("The final type is " + resolvedType);
        }

        //check for duplicates of this name
        if (attributeListObj.containsNamedAttribute(attName)) {
            int duplicateNameCounter = 1;
            while (attributeListObj.containsNamedAttribute(attName)) {
                attName += "_" + duplicateNameCounter;
                duplicateNameCounter++;
            }
        }

        Attribute attObj = new Attribute(Integer.toString(elementId), attName, attLabel, attDefinition, attUnit,
                attUnitType, resolvedType, attMeasurementScale, domain);

        // add missing code into attribute
        for (int k = 0; k < missingCodeVector.size(); k++) {

            String missingCodeValue = (String) missingCodeVector.elementAt(k);
            if (isDebugging) {
                log.debug("the mssing value code " + missingCodeValue + " was added to attribute");
            }
            attObj.addMissingValueCode(missingCodeValue);
        }

        attributeListObj.add(attObj);

    }
}

From source file:edu.lternet.pasta.dml.parser.eml.Eml200Parser.java

/**
 * Processes the attributes in an attribute list. Called by
 * processAttributeList()./*  w  ww .  j  a va  2  s . co m*/
 * 
 * @param  xpathapi           the XPath API
 * @param  attributesNodeList a node list
 * @param  attributeList      an AttributeList object
 */
private void processAttributes(CachedXPathAPI xpathapi, NodeList attributesNodeList,
        AttributeList attributeList) throws Exception {
    int attributesNodeListLength = attributesNodeList.getLength();

    // Process each attribute
    for (int i = 0; i < attributesNodeListLength; i++) {
        Node attributeNode = attributesNodeList.item(i);
        NodeList attributeNodeChildren = attributeNode.getChildNodes();
        //NamedNodeMap attAttributes = att.getAttributes();

        String attName = "";
        String attLabel = "";
        String attDefinition = "";
        String attUnit = "";
        String attUnitType = "";
        String attMeasurementScale = "";
        String attPrecision = "";
        Domain domain = null;
        String id = null;
        Vector missingValueCodeVector = new Vector();
        double numberPrecision = 0;
        ArrayList<StorageType> storageTypeArray = new ArrayList<StorageType>();

        // get attribute id
        NamedNodeMap attributeNodeAttributesMap = attributeNode.getAttributes();

        if (attributeNodeAttributesMap != null) {
            Node idNode = attributeNodeAttributesMap.getNamedItem(ID);

            if (idNode != null) {
                id = idNode.getNodeValue();
            }
        }

        elementId++;

        for (int j = 0; j < attributeNodeChildren.getLength(); j++) {
            Node childNode = attributeNodeChildren.item(j);
            String childNodeName = childNode.getNodeName();

            if (childNodeName.equals("attributeName")) {
                attName = childNode.getFirstChild().getNodeValue().trim().replace('.', '_');
            } else if (childNodeName.equals("attributeLabel")) {
                attLabel = childNode.getFirstChild().getNodeValue().trim();
            } else if (childNodeName.equals("attributeDefinition")) {
                attDefinition = childNode.getFirstChild().getNodeValue().trim();
            }
            // Process storageType elements
            else if (childNodeName.equals("storageType")) {
                String storageTypeTextValue = childNode.getFirstChild().getNodeValue().trim();
                NamedNodeMap storageTypeAttributesMap = childNode.getAttributes();
                StorageType storageType;
                String typeSystem = "";
                Node typeSystemNode = null;

                // Determine whether the typeSystem attribute was specified
                if (storageTypeAttributesMap != null) {
                    typeSystemNode = storageTypeAttributesMap.getNamedItem(TYPE_SYSTEM);

                    if (typeSystemNode != null) {
                        typeSystem = typeSystemNode.getNodeValue();
                    }
                }

                // Use the appropriate StorageType constructor depending on 
                // whether the 'typeSystem' attribute was specified
                if (!typeSystem.equals("")) {
                    storageType = new StorageType(storageTypeTextValue, typeSystem);
                } else {
                    storageType = new StorageType(storageTypeTextValue);
                }

                storageTypeArray.add(storageType);
            } else if (childNodeName.equals("measurementScale")) {
                //unit is tricky because it can be custom or standard
                //Vector info = new Vector();
                //int domainType = Domain.DOM_NONE;
                NodeList measurementScaleChildNodes = childNode.getChildNodes();

                for (int k = 0; k < measurementScaleChildNodes.getLength(); k++) {
                    Node measurementScaleChildNode = measurementScaleChildNodes.item(k);
                    String measurementScaleChildNodeName = measurementScaleChildNode.getNodeName();

                    if (measurementScaleChildNodeName.equals("interval")
                            || measurementScaleChildNodeName.equals("ratio")) {
                        String numberType = null;
                        String min = "", max = "";
                        Node standardUnitNode = xpathapi.selectSingleNode(measurementScaleChildNode,
                                "unit/standardUnit");
                        Node customUnitNode = xpathapi.selectSingleNode(measurementScaleChildNode,
                                "unit/customUnit");

                        if (standardUnitNode != null) {
                            attUnit = standardUnitNode.getFirstChild().getNodeValue();
                            attUnitType = Attribute.STANDARDUNIT;
                        } else if (customUnitNode != null) {
                            attUnit = customUnitNode.getFirstChild().getNodeValue();
                            attUnitType = Attribute.CUSTOMUNIT;
                        } else {
                            System.err.println("Unable to determine attribute unit.");
                        }

                        Node precisionNode = xpathapi.selectSingleNode(measurementScaleChildNode, "precision");

                        if (precisionNode != null) {
                            // precision is optional in EML201 so if it is
                            // not provided, the attPrecision will be the
                            // empty string
                            attPrecision = precisionNode.getFirstChild().getNodeValue();
                            numberPrecision = (new Double(attPrecision)).doubleValue();

                        }

                        Node numericDomainNode = xpathapi.selectSingleNode(measurementScaleChildNode,
                                "numericDomain");
                        NodeList numericDomainChildNodes = numericDomainNode.getChildNodes();

                        for (int index = 0; index < numericDomainChildNodes.getLength(); index++) {
                            String numericDomainChildNodeName = numericDomainChildNodes.item(index)
                                    .getNodeName();

                            if (numericDomainChildNodeName.equals("numberType")) {
                                // Got number type
                                numberType = numericDomainChildNodes.item(index).getFirstChild().getNodeValue();

                                if (isDebugging) {
                                    //log.debug("The number type is "+ numberType);
                                }
                            } else if (numericDomainChildNodeName.equals("boundsGroup")) {
                                // Got bounds group
                                NodeList boundsNodeList = xpathapi.selectNodeList(numericDomainNode,
                                        "./bounds");

                                for (i = 0; i < boundsNodeList.getLength(); i++) {
                                    NodeList aNodeList;
                                    Node boundsNode;

                                    //String exclMin = null, exclMax = null;
                                    try {
                                        aNodeList = xpathapi.selectNodeList(boundsNodeList.item(i),
                                                "./minimum");
                                        boundsNode = aNodeList.item(0);
                                        min = boundsNode.getFirstChild().getNodeValue();
                                        /*exclMin = bound.getAttributes()
                                            .getNamedItem("exclusive")
                                            .getNodeValue();*/
                                        aNodeList = xpathapi.selectNodeList(boundsNodeList.item(0),
                                                "./maximum");
                                        boundsNode = aNodeList.item(0);
                                        max = boundsNode.getFirstChild().getNodeValue();
                                        /*exclMax = bound.getAttributes()
                                            .getNamedItem("exclusive")
                                            .getNodeValue();*/
                                    } catch (Exception e) {
                                        //log.debug("Error in handle bound ", e);
                                    }
                                }
                            }
                        }

                        Double minNum = null;
                        Double maxNum = null;

                        if (!min.trim().equals("")) {
                            minNum = new Double(min);
                        }

                        if (!max.trim().equals("")) {
                            maxNum = new Double(max);
                        }

                        NumericDomain numericDomain = new NumericDomain(numberType, minNum, maxNum);
                        numericDomain.setPrecision(numberPrecision);
                        domain = numericDomain;

                    } else if (measurementScaleChildNodeName.equals("nominal")
                            || measurementScaleChildNodeName.equals("ordinal")) {
                        NodeList nonNumericDomainChildNodes = xpathapi
                                .selectSingleNode(measurementScaleChildNode, "nonNumericDomain")
                                .getChildNodes();

                        for (int m = 0; m < nonNumericDomainChildNodes.getLength(); m++) {
                            Node nonNumericDomainChildNode = nonNumericDomainChildNodes.item(m);
                            String nonNumericDomainChildNodeName = nonNumericDomainChildNode.getNodeName();

                            if (nonNumericDomainChildNodeName.equals("textDomain")) {
                                TextDomain textDomain = new TextDomain();
                                NodeList definitionNodeList = xpathapi.selectNodeList(nonNumericDomainChildNode,
                                        "./definition");
                                Node defintionNode = definitionNodeList.item(0);
                                String definition = defintionNode.getFirstChild().getNodeValue();

                                if (isDebugging) {
                                    //log.debug(
                                    // "The definition value is "+definition);
                                }

                                textDomain.setDefinition(definition);
                                NodeList patternNodeList = xpathapi.selectNodeList(nonNumericDomainChildNode,
                                        "./pattern");

                                String[] patternList = new String[patternNodeList.getLength()];

                                for (int l = 0; l < patternNodeList.getLength(); l++) {
                                    patternList[l] = patternNodeList.item(l).getFirstChild().getNodeValue();
                                }

                                if (patternList.length > 0) {
                                    textDomain.setPattern(patternList);
                                }

                                domain = textDomain;

                            } else if (nonNumericDomainChildNodeName.equals("enumeratedDomain")) {
                                EnumeratedDomain enumeratedDomain = new EnumeratedDomain();
                                Vector info = new Vector();

                                NodeList codeDefinitionNodeList = xpathapi
                                        .selectNodeList(nonNumericDomainChildNode, "./codeDefinition");

                                for (int l = 0; l < codeDefinitionNodeList.getLength(); l++) {
                                    info.add(codeDefinitionNodeList.item(l).getFirstChild().getNodeValue());
                                }

                                enumeratedDomain.setInfo(info);
                                domain = enumeratedDomain;
                            }
                        }
                    } else if (measurementScaleChildNodeName.equalsIgnoreCase("datetime")) {
                        DateTimeDomain date = new DateTimeDomain();
                        String formatString = (xpathapi.selectSingleNode(measurementScaleChildNode,
                                "./formatString")).getFirstChild().getNodeValue();

                        if (isDebugging) {
                            //log.debug(
                            //          "The format string in date time is " 
                            //          + formatString);
                        }
                        date.setFormatString(formatString);
                        domain = date;
                    }
                }
            } else if (childNodeName.equals("missingValueCode")) {
                //log.debug("in missingValueCode");
                NodeList missingValueCodeChildNodes = childNode.getChildNodes();

                for (int k = 0; k < missingValueCodeChildNodes.getLength(); k++) {
                    Node missingValueCodeChildNode = missingValueCodeChildNodes.item(k);
                    String missingValueCodeChildNodeName = missingValueCodeChildNode.getNodeName();

                    if (missingValueCodeChildNodeName.equals("code")) {
                        Node missingValueCodeTextNode = missingValueCodeChildNode.getFirstChild();

                        if (missingValueCodeTextNode != null) {
                            String missingValueCode = missingValueCodeTextNode.getNodeValue();

                            if (isDebugging) {
                                //log.debug("the missing code is "+missingCode);
                            }

                            missingValueCodeVector.add(missingValueCode);
                            //hasMissingValue = true;
                        }
                    }
                }
            }
        }

        /******************************************************
         * need to use domain type to replace data type
         ******************************************************/
        /*String resolvedType = null;
        //DataType dataType = domain.getDataType();
        //resolvedType = dataType.getName();
        if(isDebugging) {
          //log.debug("The final type is " + resolvedType);
        }*/

        Attribute attObj = new Attribute(id, attName, attLabel, attDefinition, attUnit, attUnitType,
                attMeasurementScale, domain);

        // Add storageType elements to the Attribute object 
        // if any were parsed in the EML
        for (StorageType storageType : storageTypeArray) {
            attObj.addStorageType(storageType);
        }

        // Add missing value code into attribute
        for (int k = 0; k < missingValueCodeVector.size(); k++) {
            String missingValueCode = (String) missingValueCodeVector.elementAt(k);
            if (isDebugging) {
                //log.debug("the mssing value code " + missingCodeValue + 
                //          " was added to attribute");
            }

            attObj.addMissingValueCode(missingValueCode);
        }

        attributeList.add(attObj);
    }
}

From source file:org.ecoinformatics.datamanager.parser.generic.GenericDataPackageParser.java

/**
 * Processes the attributes in an attribute list. Called by
 * processAttributeList().//from w  ww.j  a v a 2  s .com
 * 
 * @param  xpathapi           the XPath API
 * @param  attributesNodeList a node list
 * @param  attributeList      an AttributeList object
 */
private void processAttributes(CachedXPathAPI xpathapi, NodeList attributesNodeList,
        AttributeList attributeList) throws Exception {
    int attributesNodeListLength = attributesNodeList.getLength();

    // Process each attribute
    for (int i = 0; i < attributesNodeListLength; i++) {
        Node attributeNode = attributesNodeList.item(i);
        NodeList attributeNodeChildren = attributeNode.getChildNodes();
        //NamedNodeMap attAttributes = att.getAttributes();

        String attName = "";
        String attLabel = "";
        String attDefinition = "";
        String attUnit = "";
        String attUnitType = "";
        String attMeasurementScale = "";
        String attPrecision = "";
        Domain domain = null;
        String id = null;
        Vector missingValueCodeVector = new Vector();
        double numberPrecision = 0;
        ArrayList<StorageType> storageTypeArray = new ArrayList<StorageType>();

        // get attribute id
        NamedNodeMap attributeNodeAttributesMap = attributeNode.getAttributes();

        if (attributeNodeAttributesMap != null) {
            Node idNode = attributeNodeAttributesMap.getNamedItem(ID);

            if (idNode != null) {
                id = idNode.getNodeValue();
            }
        }

        elementId++;

        for (int j = 0; j < attributeNodeChildren.getLength(); j++) {
            Node childNode = attributeNodeChildren.item(j);
            String childNodeName = childNode.getNodeName();
            String childNodeValue = childNode.getFirstChild() == null ? null
                    : childNode.getFirstChild().getNodeValue();
            childNodeValue = childNodeValue == null ? childNodeValue : childNodeValue.trim();
            if (childNodeName.equals("attributeName")) {
                if (childNodeValue != null) {
                    attName = childNodeValue.replace('.', '_');
                }
            } else if (childNodeName.equals("attributeLabel")) {
                attLabel = childNodeValue;
            } else if (childNodeName.equals("attributeDefinition")) {
                attDefinition = childNodeValue;
            }
            // Process storageType elements
            else if (childNodeName.equals("storageType")) {
                String storageTypeTextValue = childNodeValue;
                NamedNodeMap storageTypeAttributesMap = childNode.getAttributes();
                StorageType storageType;
                String typeSystem = "";
                Node typeSystemNode = null;

                // Determine whether the typeSystem attribute was specified
                if (storageTypeAttributesMap != null) {
                    typeSystemNode = storageTypeAttributesMap.getNamedItem(typeSystem);

                    if (typeSystemNode != null) {
                        typeSystem = typeSystemNode.getNodeValue();
                    }
                }

                // Use the appropriate StorageType constructor depending on 
                // whether the 'typeSystem' attribute was specified
                if (!typeSystem.equals("")) {
                    storageType = new StorageType(storageTypeTextValue, typeSystem);
                } else {
                    storageType = new StorageType(storageTypeTextValue);
                }

                storageTypeArray.add(storageType);
            } else if (childNodeName.equals("measurementScale")) {
                //unit is tricky because it can be custom or standard
                //Vector info = new Vector();
                //int domainType = Domain.DOM_NONE;
                NodeList measurementScaleChildNodes = childNode.getChildNodes();

                for (int k = 0; k < measurementScaleChildNodes.getLength(); k++) {
                    Node measurementScaleChildNode = measurementScaleChildNodes.item(k);
                    String measurementScaleChildNodeName = measurementScaleChildNode.getNodeName();

                    if (measurementScaleChildNodeName.equals("interval")
                            || measurementScaleChildNodeName.equals("ratio")) {
                        String numberType = null;
                        String min = "", max = "";
                        Node standardUnitNode = xpathapi.selectSingleNode(measurementScaleChildNode,
                                "unit/standardUnit");
                        Node customUnitNode = xpathapi.selectSingleNode(measurementScaleChildNode,
                                "unit/customUnit");

                        if (standardUnitNode != null) {
                            attUnit = standardUnitNode.getFirstChild().getNodeValue();
                            attUnitType = Attribute.STANDARDUNIT;
                        } else if (customUnitNode != null) {
                            attUnit = customUnitNode.getFirstChild().getNodeValue();
                            attUnitType = Attribute.CUSTOMUNIT;
                        } else {
                            System.err.println("Unable to determine attribute unit.");
                        }

                        Node precisionNode = xpathapi.selectSingleNode(measurementScaleChildNode, "precision");

                        if (precisionNode != null) {
                            // precision is optional in EML201 so if it is
                            // not provided, the attPrecision will be the
                            // empty string
                            attPrecision = precisionNode.getFirstChild().getNodeValue();
                            numberPrecision = (new Double(attPrecision)).doubleValue();

                        }

                        Node numericDomainNode = xpathapi.selectSingleNode(measurementScaleChildNode,
                                "numericDomain");
                        NodeList numericDomainChildNodes = numericDomainNode.getChildNodes();

                        for (int index = 0; index < numericDomainChildNodes.getLength(); index++) {
                            String numericDomainChildNodeName = numericDomainChildNodes.item(index)
                                    .getNodeName();

                            if (numericDomainChildNodeName.equals("numberType")) {
                                // Got number type
                                numberType = numericDomainChildNodes.item(index).getFirstChild().getNodeValue();

                                if (isDebugging) {
                                    //log.debug("The number type is "+ numberType);
                                }
                            } else if (numericDomainChildNodeName.equals("boundsGroup")) {
                                // Got bounds group
                                NodeList boundsNodeList = xpathapi.selectNodeList(numericDomainNode,
                                        "./bounds");

                                for (i = 0; i < boundsNodeList.getLength(); i++) {
                                    NodeList aNodeList;
                                    Node boundsNode;

                                    //String exclMin = null, exclMax = null;
                                    try {
                                        aNodeList = xpathapi.selectNodeList(boundsNodeList.item(i),
                                                "./minimum");
                                        boundsNode = aNodeList.item(0);
                                        min = boundsNode.getFirstChild().getNodeValue();
                                        /*exclMin = bound.getAttributes()
                                            .getNamedItem("exclusive")
                                            .getNodeValue();*/
                                        aNodeList = xpathapi.selectNodeList(boundsNodeList.item(0),
                                                "./maximum");
                                        boundsNode = aNodeList.item(0);
                                        max = boundsNode.getFirstChild().getNodeValue();
                                        /*exclMax = bound.getAttributes()
                                            .getNamedItem("exclusive")
                                            .getNodeValue();*/
                                    } catch (Exception e) {
                                        //log.debug("Error in handle bound ", e);
                                    }
                                }
                            }
                        }

                        Double minNum = null;
                        Double maxNum = null;

                        if (!min.trim().equals("")) {
                            minNum = new Double(min);
                        }

                        if (!max.trim().equals("")) {
                            maxNum = new Double(max);
                        }

                        NumericDomain numericDomain = new NumericDomain(numberType, minNum, maxNum);
                        numericDomain.setPrecision(numberPrecision);
                        domain = numericDomain;

                    } else if (measurementScaleChildNodeName.equals("nominal")
                            || measurementScaleChildNodeName.equals("ordinal")) {
                        NodeList nonNumericDomainChildNodes = xpathapi
                                .selectSingleNode(measurementScaleChildNode, "nonNumericDomain")
                                .getChildNodes();

                        for (int m = 0; m < nonNumericDomainChildNodes.getLength(); m++) {
                            Node nonNumericDomainChildNode = nonNumericDomainChildNodes.item(m);
                            String nonNumericDomainChildNodeName = nonNumericDomainChildNode.getNodeName();

                            if (nonNumericDomainChildNodeName.equals("textDomain")) {
                                TextDomain textDomain = new TextDomain();
                                NodeList definitionNodeList = xpathapi.selectNodeList(nonNumericDomainChildNode,
                                        "./definition");
                                Node defintionNode = definitionNodeList.item(0);
                                String definition = defintionNode.getFirstChild() == null ? null
                                        : defintionNode.getFirstChild().getNodeValue();

                                if (isDebugging) {
                                    //log.debug(
                                    // "The definition value is "+definition);
                                }

                                textDomain.setDefinition(definition);
                                NodeList patternNodeList = xpathapi.selectNodeList(nonNumericDomainChildNode,
                                        "./pattern");

                                String[] patternList = new String[patternNodeList.getLength()];

                                for (int l = 0; l < patternNodeList.getLength(); l++) {
                                    patternList[l] = patternNodeList.item(l).getFirstChild().getNodeValue();
                                }

                                if (patternList.length > 0) {
                                    textDomain.setPattern(patternList);
                                }

                                domain = textDomain;

                            } else if (nonNumericDomainChildNodeName.equals("enumeratedDomain")) {
                                EnumeratedDomain enumeratedDomain = new EnumeratedDomain();
                                Vector info = new Vector();

                                NodeList codeDefinitionNodeList = xpathapi
                                        .selectNodeList(nonNumericDomainChildNode, "./codeDefinition");

                                for (int l = 0; l < codeDefinitionNodeList.getLength(); l++) {
                                    info.add(codeDefinitionNodeList.item(l).getFirstChild().getNodeValue());
                                }

                                enumeratedDomain.setInfo(info);
                                domain = enumeratedDomain;
                            }
                        }
                    } else if (measurementScaleChildNodeName.equalsIgnoreCase("datetime")) {
                        DateTimeDomain date = new DateTimeDomain();
                        String formatString = (xpathapi.selectSingleNode(measurementScaleChildNode,
                                "./formatString")).getFirstChild().getNodeValue();

                        if (isDebugging) {
                            //log.debug(
                            //          "The format string in date time is " 
                            //          + formatString);
                        }
                        date.setFormatString(formatString);
                        domain = date;
                    }
                }
            } else if (childNodeName.equals("missingValueCode")) {
                //log.debug("in missingValueCode");
                NodeList missingValueCodeChildNodes = childNode.getChildNodes();

                for (int k = 0; k < missingValueCodeChildNodes.getLength(); k++) {
                    Node missingValueCodeChildNode = missingValueCodeChildNodes.item(k);
                    String missingValueCodeChildNodeName = missingValueCodeChildNode.getNodeName();

                    if (missingValueCodeChildNodeName.equals("code")) {
                        Node missingValueCodeTextNode = missingValueCodeChildNode.getFirstChild();

                        if (missingValueCodeTextNode != null) {
                            String missingValueCode = missingValueCodeTextNode.getNodeValue();

                            if (isDebugging) {
                                //log.debug("the missing code is "+missingCode);
                            }

                            missingValueCodeVector.add(missingValueCode);
                            //hasMissingValue = true;
                        }
                    }
                }
            }
        }

        /******************************************************
         * need to use domain type to replace data type
         ******************************************************/
        /*String resolvedType = null;
        //DataType dataType = domain.getDataType();
        //resolvedType = dataType.getName();
        if(isDebugging) {
          //log.debug("The final type is " + resolvedType);
        }*/

        Attribute attObj = new Attribute(id, attName, attLabel, attDefinition, attUnit, attUnitType,
                attMeasurementScale, domain);

        // Add storageType elements to the Attribute object 
        // if any were parsed in the EML
        for (StorageType storageType : storageTypeArray) {
            attObj.addStorageType(storageType);
        }

        // Add missing value code into attribute
        for (int k = 0; k < missingValueCodeVector.size(); k++) {
            String missingValueCode = (String) missingValueCodeVector.elementAt(k);
            if (isDebugging) {
                //log.debug("the mssing value code " + missingCodeValue + 
                //          " was added to attribute");
            }

            attObj.addMissingValueCode(missingValueCode);
        }

        attributeList.add(attObj);
    }
}

From source file:alter.vitro.vgw.service.query.SimpleQueryHandler.java

private Vector<ReqResultOverData> findAggrAttrValue(String pQueryDefId,
        Vector<QueriedMoteAndSensors> pMotesAndTheirSensorAndFunctsVec,
        Vector<ReqFunctionOverData> reqFunctionVec, List<String> serviceDeployStatusStr,
        List<String[]> localReplacedResources) {

    ////ww w  . ja  va 2 s.c  o  m
    // ADDED CODE -- OPTIMIZATION PENDING +++++
    //
    // --------------- SERVICE CONTINUATION PREP
    // TODO: SERVICE CONTINUATION PREP
    //service Continuation Additions:
    //String serviceDeployStatusStr = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_UNKNOWN;
    serviceDeployStatusStr.add(ResponseAggrMsg.DEPLOY_STATUS_SERVICE_UNKNOWN);
    // deploy status flags
    boolean serviceDeployAllNodesAvailable = true;
    boolean serviceDeployContinuationEmployed = false;
    boolean serviceDeployPartiallyPossible = false;
    boolean serviceDeployImpossible = false;
    // [0] is the original nodeId, [1] the replacing node id and [2] the capability
    //List<String[]> localReplacedResources = new ArrayList<String[]>();

    //
    //
    // TODO: 1.Use the motesAndTheirSensorAndFunctVec to get the requested motes and the requested capabilities.
    // TODO: 2.Check wth Continuation Service and Resource Availability Service.
    //      TODO.   2a. If all nodes are available then Deploy_Status = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_POSSIBLE.
    //              2b. If a node in the requested motes is unavailable (or future: a requested resource is unavailable)
    //                Check the equivalent nodes for matches for this capability.
    //                If a match is found, replace the node in the motesAndTheirSensorAndFunctsVec with the replacement node
    //                  and keep this replacing tracked/stored locally (as well as the cache of the continuationService)
    //                  when the results are found, replace the original mote back, but also send the extra xml that says that the values from that node for that capability are from the replacement node
    //                  TODO: Careful! a node could be replaced by more than one nodes, based on the capabilities requested! TEST THIS CASE!
    //                  TODO: Careful! a node could be replaced for one capability, but not for another!
    //                  Also set the flag serviceContinuationEmployed to true.
    //                      if at the end only this flag is set then update the Deploy_Status to  ResponseAggrMsg.DEPLOY_STATUS_SERVICE_CONTINUATION
    //               If a match is not found then remove this node from the results.
    //                  Also set the flag servicePartiallyPossible to true.
    //                  if at the end only this flag is set then update the Deploy_Status ResponseAggrMsg.DEPLOY_STATUS_SERVICE_PARTIAL
    //              If a the end both flags  serviceContinuationEmployed and servicePartiallyPossible are true
    //                  and not the serviceImpossible flag then update the Deploy_Status to ResponseAggrMsg.DEPLOY_STATUS_SERVICE_PARTIAL_CONT_COMBO
    //
    //              Finally if NO nodes are available for the service set the serviceImpossible flag to true and
    //                  update the deploy_status to  ResponseAggrMsg.DEPLOY_STATUS_SERVICE_IMPOSSIBLE
    // END: SERVICE CONTINUATION PREP
    Vector<QueriedMoteAndSensors> originalMotesAndTheirSensorAndFunctsVec = pMotesAndTheirSensorAndFunctsVec;
    Vector<QueriedMoteAndSensors> newMotesAndTheirSensorAndFunctsVec = new Vector<QueriedMoteAndSensors>();

    List<String> allInvolvedMoteIdsList = new ArrayList<String>();
    for (QueriedMoteAndSensors aMoteAndSensors : originalMotesAndTheirSensorAndFunctsVec) {
        allInvolvedMoteIdsList.add(aMoteAndSensors.getMoteid());
    }
    logger.debug("Queried motes and sensors:");
    for (QueriedMoteAndSensors aMoteAndSensors : originalMotesAndTheirSensorAndFunctsVec) {
        logger.debug("Mote Id: " + aMoteAndSensors.getMoteid());
        if (aMoteAndSensors.getQueriedSensorIdsAndFuncVec() != null
                && !aMoteAndSensors.getQueriedSensorIdsAndFuncVec().isEmpty()) {
            HashMap<String, Vector<Integer>> functionsForCapabilityOfThisMoteHM = new HashMap<String, Vector<Integer>>();
            for (ReqSensorAndFunctions sensAndFuncts : aMoteAndSensors.getQueriedSensorIdsAndFuncVec()) {
                logger.debug("     Capabilities: " + sensAndFuncts.getSensorModelid()); // TODO: we could probably acquire the friendly name too from some map
                //TODO: this isNodeResourceAvailable could be also done ideally within the ContinuationOfProvisionService within the findNextEquivalaneNode funciton (also could be synchronized)
                //logger.debug("DDDDD Size of functs:"+ Integer.toString(sensAndFuncts.getFunctionsOverSensorModelVec().size()));
                //{
                //    int smid = sensAndFuncts.getSensorModelIdInt();
                //    //logger.debug("For mote "+fullMoteId +" and sensor "+Integer.toString(smid) + " function vector size is "+reqFunctionVec.size());
                //    for (Integer inFunctVec : sensAndFuncts.getFunctionsOverSensorModelVec()) {
                //        logger.debug("Fid: " + inFunctVec);
                //    }
                // }

                functionsForCapabilityOfThisMoteHM.put(sensAndFuncts.getSensorModelid(),
                        sensAndFuncts.getFunctionsOverSensorModelVec());
                if (!ResourceAvailabilityService.getInstance().isNodeResourceAvailable(pQueryDefId,
                        aMoteAndSensors.getMoteid(), sensAndFuncts.getSensorModelid())) {
                    logger.debug("Node id: " + aMoteAndSensors.getMoteid() + " unavailable for: "
                            + sensAndFuncts.getSensorModelid());
                    String[] replacementInfo = ContinuationOfProvisionService.getInstance()
                            .findNextEquivalentNode(pQueryDefId, allInvolvedMoteIdsList,
                                    aMoteAndSensors.getMoteid(), sensAndFuncts.getSensorModelid());
                    if (replacementInfo == null) {
                        //
                        logger.debug("Could not find replacement node for " + sensAndFuncts.getSensorModelid()
                                + " vsn id: " + pQueryDefId);
                        serviceDeployPartiallyPossible = true;
                    } else {
                        logger.debug("Found replacement node " + replacementInfo[1] + " for node "
                                + replacementInfo[0] + " for " + replacementInfo[2] + " vsn id: "
                                + pQueryDefId);
                        serviceDeployContinuationEmployed = true;
                        // to prevent duplicates (though there really should not be such case)
                        addToLocalReplacementInfoList(localReplacedResources, replacementInfo);

                    }

                } //end if: node capability is not available
                else { //capability is available
                       // add self as a replacement (locally)
                       // a node could be available for some capabilities but not for others
                    String[] replacementInfo = { aMoteAndSensors.getMoteid(), aMoteAndSensors.getMoteid(),
                            sensAndFuncts.getSensorModelid() };
                    logger.debug("Adding self to local cache");
                    addToLocalReplacementInfoList(localReplacedResources, replacementInfo);
                }
            } //end for loop for this node's capability

            //loop through the localReplacedResources for this node and update the newMotesAndTheirSensorAndFunctsVec
            List<String> consideredReplacementNodes = new ArrayList<String>();
            for (String[] entryLocal : localReplacedResources) {
                //logger.debug("Checking  localReplacedResources for: " + entryLocal[0]);
                if (entryLocal[0].compareToIgnoreCase(aMoteAndSensors.getMoteid()) == 0) {
                    String idOfOneReplacingNode = entryLocal[1];
                    if (!consideredReplacementNodes.contains(idOfOneReplacingNode)) {
                        //logger.debug("INNER Checking  localReplacedResources for: " + idOfOneReplacingNode);
                        consideredReplacementNodes.add(idOfOneReplacingNode);

                        Vector<ReqSensorAndFunctions> replacementNodeSensorAndFuncts = new Vector<ReqSensorAndFunctions>();
                        QueriedMoteAndSensors replacementMoteAndSensors = new QueriedMoteAndSensors(
                                idOfOneReplacingNode, replacementNodeSensorAndFuncts);
                        // inner loop again to find all capabilities that this node (idOfOneReplacingNode) is a replacement for
                        for (String[] entryLocalInner : localReplacedResources) {
                            if (entryLocalInner[0].compareToIgnoreCase(aMoteAndSensors.getMoteid()) == 0
                                    && entryLocalInner[1].compareToIgnoreCase(idOfOneReplacingNode) == 0) {
                                //logger.debug("INNER MATCh FOUND for: " +  entryLocalInner[1] + " capability: " + entryLocalInner[2] );
                                String capabilityToAdd = entryLocalInner[2];
                                int capabilityToAddInt = ReqSensorAndFunctions.invalidSensModelId;
                                try {
                                    capabilityToAddInt = Integer.valueOf(capabilityToAdd);
                                } catch (Exception ex33) {
                                    logger.error(
                                            "Could not convert capability id to int for replacement capability: "
                                                    + capabilityToAdd);
                                }
                                //logger.error("CAP TO ADD" + capabilityToAdd);
                                if (functionsForCapabilityOfThisMoteHM.containsKey(capabilityToAdd)
                                        && functionsForCapabilityOfThisMoteHM.get(capabilityToAdd) != null
                                        && !functionsForCapabilityOfThisMoteHM.get(capabilityToAdd).isEmpty()) {
                                    //logger.error("FOUND IN HASHMAP!!!");
                                    Vector<Integer> funcsOverThisCapability = functionsForCapabilityOfThisMoteHM
                                            .get(capabilityToAdd);
                                    //int smid = capabilityToAddInt;
                                    //logger.debug("DEB DEB For mote "+aMoteAndSensors.getMoteid() +" and sensor "+Integer.toString(smid) + " function vector size is "+reqFunctionVec.size());
                                    //for (Integer inFunctVec : funcsOverThisCapability) {
                                    //    logger.debug("DEB DEB Fid: " + inFunctVec);
                                    //}
                                    ReqSensorAndFunctions thisSensorAndFuncts = new ReqSensorAndFunctions(
                                            capabilityToAddInt, funcsOverThisCapability);
                                    //thisSensorAndFuncts.getSensorModelid();
                                    //thisSensorAndFuncts.getFunctionsOverSensorModelVec().size();
                                    //logger.debug("DEB DEB 333 For  sensor "+ thisSensorAndFuncts.getSensorModelid()+ " function vector size is "+ thisSensorAndFuncts.getFunctionsOverSensorModelVec().size());
                                    //for (Integer inFunctVec : funcsOverThisCapability) {
                                    //    logger.debug("DEB DEB 333 Fid: " + inFunctVec);
                                    //}
                                    replacementNodeSensorAndFuncts.addElement(thisSensorAndFuncts);
                                }
                            }
                        }
                        if (!replacementNodeSensorAndFuncts.isEmpty()) {
                            //logger.error("ADDING ELEMENT TO NEW MOTES LIST!!!" + replacementMoteAndSensors.getMoteid() + ":: " + Integer.toString(replacementMoteAndSensors.getQueriedSensorIdsAndFuncVec().size()));
                            replacementMoteAndSensors
                                    .setQueriedSensorIdsAndFuncVec(replacementNodeSensorAndFuncts);
                            newMotesAndTheirSensorAndFunctsVec.addElement(replacementMoteAndSensors);
                        }
                    }
                }
            }
            //functionsForCapabilityOfThisMoteHM.clear();
        }
    } //end for loop for this node of queried motes
    if (newMotesAndTheirSensorAndFunctsVec == null || newMotesAndTheirSensorAndFunctsVec.isEmpty()) {
        serviceDeployImpossible = true;
        logger.debug("Service Deploy is impossible for vsn id: " + pQueryDefId);
    }

    // decide status
    String statusDecidedStr = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_UNKNOWN;
    if (serviceDeployImpossible) {
        statusDecidedStr = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_IMPOSSIBLE;
    } else if (serviceDeployContinuationEmployed && serviceDeployPartiallyPossible) {
        statusDecidedStr = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_PARTIAL_CONT_COMBO;
    } else if (serviceDeployContinuationEmployed) {
        statusDecidedStr = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_CONTINUATION;
    } else if (serviceDeployPartiallyPossible) {
        statusDecidedStr = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_PARTIAL;
    } else if (serviceDeployAllNodesAvailable && !serviceDeployImpossible && !serviceDeployContinuationEmployed
            && !serviceDeployPartiallyPossible) {
        statusDecidedStr = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_POSSIBLE;
    }
    serviceDeployStatusStr.set(0, statusDecidedStr);
    logger.debug("Decided DEPLOY STATUS WAS: " + serviceDeployStatusStr.get(0));
    // We proceed here because even if service deploy is not possible, a reply will be sent with the status and empty lists (TODO consider)
    // However we also send (near the end of this method, alert messages for the deploy status if <> OK
    //
    //
    // TODO: To skip redundant queries in network
    // TODO: Count the reqFunction in reqFunction Vec (Debug print them) (also check that they are executed even if gateway level for each node-which should not happen)
    // TODO: Verify that if a function is gateway level and its removed(?) from the reqFunctionVec then it's not executed by the wsi adapter!
    //
    //
    // TODO: handle conditions for aggregate (gateway level functions).
    //
    //clone the reqFunctionsVec   . TODO. this is not cloning though, we pass references to the added elements
    Vector<ReqFunctionOverData> onlyNodeReqFunctVec = new Vector<ReqFunctionOverData>();
    Vector<ReqFunctionOverData> onlyGwLevelReqFunctVec = new Vector<ReqFunctionOverData>();
    for (int i = 0; i < reqFunctionVec.size(); i++) {
        if (ReqFunctionOverData.isValidGatewayReqFunct(reqFunctionVec.elementAt(i).getfuncName()))
            onlyGwLevelReqFunctVec.addElement(reqFunctionVec.elementAt(i));
        else {
            onlyNodeReqFunctVec.addElement(reqFunctionVec.elementAt(i));
        }
    }
    //
    // get the involved capabilities per gatewaylevel function, and then remove the function id from those sensorModels!
    //
    //  Produce a hashmap of gwLevel function name to Vector of capabilities (sensorModelId from the query/request)
    HashMap<String, Vector<String>> gwLevelFunctToCapsList = new HashMap<String, Vector<String>>(); // todo: IMPORTANT later we should group sensormodelIds per capability they belong to, but for now sensormodelid == capability!
    Iterator<ReqFunctionOverData> gwLevelFunctsIter = onlyGwLevelReqFunctVec.iterator();
    while (gwLevelFunctsIter.hasNext()) {
        Vector<String> myInvolvedCaps = new Vector<String>();
        ReqFunctionOverData tmpGwLevelFunct = gwLevelFunctsIter.next();
        // new change to new Vector of motes (19/04)
        Iterator<QueriedMoteAndSensors> onMotesSensFunctsVecIter = newMotesAndTheirSensorAndFunctsVec
                .iterator();
        while (onMotesSensFunctsVecIter.hasNext()) {
            QueriedMoteAndSensors tmpMoteAndSenAndFuncts = onMotesSensFunctsVecIter.next();
            Iterator<ReqSensorAndFunctions> sensAndFunctsIter = tmpMoteAndSenAndFuncts
                    .getQueriedSensorIdsAndFuncVec().iterator();

            while (sensAndFunctsIter.hasNext()) {
                ReqSensorAndFunctions sensAndFuncts = sensAndFunctsIter.next();
                //Vector<Integer> sensfunctsVector = sensAndFuncts.getFunctionsOverSensorModelVec();
                int initSize = sensAndFuncts.getFid().size();

                for (int k = initSize - 1; k >= 0; k--) {
                    int sensfid = sensAndFuncts.getFid().get(k).intValue();
                    if (sensfid == tmpGwLevelFunct.getfuncId()) {
                        if (!myInvolvedCaps.contains(sensAndFuncts.getSensorModelid())) {
                            myInvolvedCaps.addElement(sensAndFuncts.getSensorModelid());
                        }

                        // TODO: WHY??? (NOT NEEDED ANYMORE because we use the onlyNodeReqFunctVec to query the sensor and that filters out the functions in the adapter) ::here we should also delete the fid from the sensor model (but the simple way does not work for some reason, so it is left for future)

                        //List tmpList =  removeElementAt(sensAndFuncts.getFid(),k);
                        //sensAndFuncts.getFid().clear();
                        //sensAndFuncts.getFid().addAll(tmpList);
                        //sensAndFuncts.getFunctionsOverSensorModelVec().clear();

                    }
                }
            }
        }
        gwLevelFunctToCapsList.put(tmpGwLevelFunct.getfuncName(), myInvolvedCaps);
    }
    //
    //
    //
    Vector<ReqResultOverData> allResultsRead = new Vector<ReqResultOverData>();
    //WsiAdapterCon myDCon = WsiAdapterConFactory.createMiddleWCon("uberdust", DbConInfoFactory.createConInfo("restHttp"));
    // DONE: The translateAggrQuery should not be executed for gateway level functions (skip them here or in the adapter con class.(?)
    // new changed to the new vector of motes : 19/04
    logger.debug("Submitting query to the network");
    // ASK ONLY FOR NODE LEVEL FUNCTIONS (TODO: Essentially for now, only last value is a node level function sent from the VSP, although other node level functions are supported)
    allResultsRead = myDCon.translateAggrQuery(newMotesAndTheirSensorAndFunctsVec, onlyNodeReqFunctVec);
    logger.debug("After Submitting query to the network");
    //
    //
    // TODO: All gateway level functions reference a node level function at some point (either directly eg max or two hops eg "IF MAX "
    //
    //
    // Handle gateway level functions
    // first order of business, delete everything within them (some connectors could put latest values of all nodes, but we want to do it the more proper way)
    // then get the values of the referenced function(s)
    // aggregate the values and produce a single result. TODO: here UOMs of different sensor models could come into play. Handle this in the future!
    //
    //
    // 1. we create a new derived structure with unique fid keyed entries for required Result over data.
    Vector<ReqResultOverData> allUniqueFunctionsWithResults = new Vector<ReqResultOverData>();
    Iterator<ReqResultOverData> messyResultsIter = allResultsRead.iterator();
    // Loop over all resultOverData. They are keyed by fid, but there can be multiple of the same fid!
    // So here we merge those of same fid.
    while (messyResultsIter.hasNext()) //OUTER loop
    {
        ReqResultOverData tmpResStructFromMessyVec = messyResultsIter.next();

        //ReqResultOverData tmpResStructMatched = null;
        boolean foundTheFid = false;
        Iterator<ReqResultOverData> uniqueFuncResultsIter = allUniqueFunctionsWithResults.iterator();
        while (uniqueFuncResultsIter.hasNext()) //for the first pass of the OUTER loop the allUniqueFunctionsWithResults is empty
        {
            ReqResultOverData uniqueFunctResult = uniqueFuncResultsIter.next();
            if (uniqueFunctResult.getFidInt() == tmpResStructFromMessyVec.getFidInt()) {
                foundTheFid = true;
                uniqueFunctResult.getOut().addAll(tmpResStructFromMessyVec.getAllResultsforFunct());
                break;
            }
        }
        if (!foundTheFid) {
            allUniqueFunctionsWithResults.addElement(new ReqResultOverData(tmpResStructFromMessyVec.getFidInt(),
                    tmpResStructFromMessyVec.getAllResultsforFunct()));
        }

    }
    //
    // Repeat this process slightly altered to add the unique Gw level functions
    //
    Iterator<ReqFunctionOverData> gwfunctIter = onlyGwLevelReqFunctVec.iterator();
    while (gwfunctIter.hasNext()) //OUTER loop
    {
        ReqFunctionOverData tmpReqGwFunct = gwfunctIter.next();
        //ReqResultOverData tmpResStructMatched = null;
        boolean foundTheFid = false;
        Iterator<ReqResultOverData> uniqueFuncResultsIter = allUniqueFunctionsWithResults.iterator();
        while (uniqueFuncResultsIter.hasNext()) //for the first pass of the OUTER loop the allUniqueFunctionsWithResults is empty
        {
            ReqResultOverData uniqueFunctResult = uniqueFuncResultsIter.next();
            if (uniqueFunctResult.getFidInt() == tmpReqGwFunct.getfuncId()) {
                foundTheFid = true;
                break;
            }
        }
        if (!foundTheFid) {
            allUniqueFunctionsWithResults.addElement(
                    new ReqResultOverData(tmpReqGwFunct.getfuncId(), new Vector<ResultAggrStruct>()));
        }

    }
    // end of 1.
    //
    // 2. Go through all the gateway level functions (all of which are missing values right now).
    //    For each gateway level function, go through all the results for this function.
    //
    gwfunctIter = onlyGwLevelReqFunctVec.iterator();
    while (gwfunctIter.hasNext()) {
        ReqFunctionOverData tmpGwFunct = gwfunctIter.next();

        Iterator<ReqResultOverData> resultsIter = allUniqueFunctionsWithResults.iterator();
        // loop over all resultOverData for this specific function (matching is made in the next two lines)
        while (resultsIter.hasNext()) {
            ReqResultOverData tmpResForGWFunct = resultsIter.next();
            if (tmpResForGWFunct.getFidInt() == tmpGwFunct.getfuncId()) {

                // descriptionTokens[0] : GW LEVEL PREFIX
                // descriptionTokens[1] : FUNCTION NAME
                // descriptionTokens[2] : REFERENCED FUNCTION ID
                String[] descriptionTokens = tmpGwFunct.getfuncName()
                        .split(ReqFunctionOverData.GW_LEVEL_SEPARATOR);
                //
                // 3. Handle min, max and avg gateway level functions. (IF THEN FUNCTIONS ARE HANDLED AS ANOTHER CASE - THEY ARE ONE HOP HIGHER)
                //    MIN, MAX, and AVG are all one hop (reference) away from a node level function (last value)
                if (descriptionTokens != null && descriptionTokens.length > 2
                        && (descriptionTokens[1].equalsIgnoreCase(ReqFunctionOverData.maxFunc)
                                || descriptionTokens[1].equalsIgnoreCase(ReqFunctionOverData.minFunc)
                                || descriptionTokens[1].equalsIgnoreCase(ReqFunctionOverData.avgFunc))) {
                    logger.debug("Clearing up values for gw funct name: " + tmpGwFunct.getfuncName());
                    // cleanup of output list  (it should however be already empty now that we rightfully only poll the WSI for node level functions)
                    tmpResForGWFunct.getOut().clear();
                    tmpResForGWFunct.getAllResultsforFunct().clear();

                    //after cleanup of output list
                    logger.debug("Filling up values for gw funct name: " + tmpGwFunct.getfuncName());
                    if (descriptionTokens[1].equalsIgnoreCase(ReqFunctionOverData.maxFunc)) {
                        // MAX FUNCTION   =======================================
                        int aggregatedValues = 0;

                        int refFunct = ReqFunctionOverData.unknownFuncId;
                        try {
                            refFunct = Integer.valueOf(descriptionTokens[2]);
                        } catch (Exception exfrtm) {
                            logger.error("Reference function id was set as unknown!");
                        }
                        HashMap<String, Long> capToTsFromMinLong = new HashMap<String, Long>();
                        HashMap<String, Long> capToTsToMaxLong = new HashMap<String, Long>();
                        HashMap<String, Long> capToMaxValueLong = new HashMap<String, Long>();
                        //
                        Iterator<ReqResultOverData> resultsIter002 = allUniqueFunctionsWithResults.iterator();
                        // INNER LOOP THROUGH FUNCTIONS with results, searching for the referenced NODE level function
                        while (resultsIter002.hasNext()) {
                            ReqResultOverData tmpRes = resultsIter002.next();
                            if (tmpRes.getFidInt() == refFunct) {
                                // for every GENERIC capability requested( the generic capability is coded as hashcode() )
                                for (String currCapSidStr : gwLevelFunctToCapsList
                                        .get(tmpGwFunct.getfuncName())) {
                                    if (!capToMaxValueLong.containsKey(currCapSidStr)) {
                                        capToMaxValueLong.put(currCapSidStr, Long.valueOf(Long.MIN_VALUE));
                                        capToTsFromMinLong.put(currCapSidStr, Long.valueOf(Long.MAX_VALUE));
                                        capToTsToMaxLong.put(currCapSidStr, Long.valueOf(Long.MIN_VALUE));
                                    }

                                    Iterator<OutType> tmpOutItemIter = tmpRes.getOut().iterator();
                                    while (tmpOutItemIter.hasNext()) {
                                        ResultAggrStruct tmpOutItem = new ResultAggrStruct(
                                                tmpOutItemIter.next());
                                        if (currCapSidStr.trim().equalsIgnoreCase(tmpOutItem.getSid().trim())) {
                                            try {
                                                long longValToCompare = Long.parseLong(tmpOutItem.getVal());
                                                if (longValToCompare > capToMaxValueLong.get(currCapSidStr)
                                                        .longValue()) {
                                                    capToMaxValueLong.put(currCapSidStr,
                                                            Long.valueOf(longValToCompare));
                                                }
                                                if (capToTsFromMinLong.get(currCapSidStr)
                                                        .longValue() > tmpOutItem.getTis().getFromTimestamp()
                                                                .getTime()) {
                                                    capToTsFromMinLong.put(currCapSidStr, Long.valueOf(
                                                            tmpOutItem.getTis().getFromTimestamp().getTime()));
                                                }
                                                if (capToTsToMaxLong.get(currCapSidStr).longValue() < tmpOutItem
                                                        .getTis().getToTimestamp().getTime()) {
                                                    capToTsToMaxLong.put(currCapSidStr, Long.valueOf(
                                                            tmpOutItem.getTis().getToTimestamp().getTime()));
                                                }
                                                aggregatedValues += 1;

                                            } catch (Exception e) {
                                                logger.error("Invalid format to aggregate");
                                            }
                                        }
                                    }
                                    ResultAggrStruct thisAggrResult = new ResultAggrStruct(
                                            ResultAggrStruct.MidSpecialForAggregateMultipleValues,
                                            Integer.valueOf(currCapSidStr),
                                            Long.toString(capToMaxValueLong.get(currCapSidStr)),
                                            aggregatedValues,
                                            new TimeIntervalStructure(
                                                    new Timestamp(capToTsFromMinLong.get(currCapSidStr)),
                                                    new Timestamp(capToTsToMaxLong.get(currCapSidStr))));
                                    tmpResForGWFunct.getOut().add(thisAggrResult);
                                }
                            }
                        }
                    } else if (descriptionTokens[1].equalsIgnoreCase(ReqFunctionOverData.minFunc)) {
                        // MIN FUNCTION   =======================================
                        int aggregatedValues = 0;

                        int refFunct = ReqFunctionOverData.unknownFuncId;
                        try {
                            refFunct = Integer.valueOf(descriptionTokens[2]);
                        } catch (Exception exfrtm) {
                            logger.error("Reference function id was set as unknown!");
                        }
                        HashMap<String, Long> capToTsFromMinLong = new HashMap<String, Long>();
                        HashMap<String, Long> capToTsToMaxLong = new HashMap<String, Long>();
                        HashMap<String, Long> capToMinValueLong = new HashMap<String, Long>();
                        //
                        Iterator<ReqResultOverData> resultsIter002 = allUniqueFunctionsWithResults.iterator();
                        while (resultsIter002.hasNext()) {

                            ReqResultOverData tmpRes = resultsIter002.next();
                            if (tmpRes.getFidInt() == refFunct) {
                                // for every GENERIC capability requested( the genereic capability is coded as hashcode() )
                                for (String currCapSidStr : gwLevelFunctToCapsList
                                        .get(tmpGwFunct.getfuncName())) {
                                    if (!capToMinValueLong.containsKey(currCapSidStr)) {
                                        capToMinValueLong.put(currCapSidStr, Long.valueOf(Long.MAX_VALUE));
                                        capToTsFromMinLong.put(currCapSidStr, Long.valueOf(Long.MAX_VALUE));
                                        capToTsToMaxLong.put(currCapSidStr, Long.valueOf(Long.MIN_VALUE));
                                    }

                                    Iterator<OutType> tmpOutItemIter = tmpRes.getOut().iterator();
                                    while (tmpOutItemIter.hasNext()) {
                                        ResultAggrStruct tmpOutItem = new ResultAggrStruct(
                                                tmpOutItemIter.next());
                                        if (currCapSidStr.trim().equalsIgnoreCase(tmpOutItem.getSid().trim())) {
                                            try {
                                                long longValToCompare = Long.parseLong(tmpOutItem.getVal());
                                                if (longValToCompare < capToMinValueLong.get(currCapSidStr)
                                                        .longValue()) {
                                                    capToMinValueLong.put(currCapSidStr,
                                                            Long.valueOf(longValToCompare));
                                                }
                                                if (capToTsFromMinLong.get(currCapSidStr)
                                                        .longValue() > tmpOutItem.getTis().getFromTimestamp()
                                                                .getTime()) {
                                                    capToTsFromMinLong.put(currCapSidStr, Long.valueOf(
                                                            tmpOutItem.getTis().getFromTimestamp().getTime()));
                                                }
                                                if (capToTsToMaxLong.get(currCapSidStr).longValue() < tmpOutItem
                                                        .getTis().getToTimestamp().getTime()) {
                                                    capToTsToMaxLong.put(currCapSidStr, Long.valueOf(
                                                            tmpOutItem.getTis().getToTimestamp().getTime()));
                                                }
                                                aggregatedValues += 1;

                                            } catch (Exception e) {
                                                logger.error("Invalid format to aggregate");
                                            }
                                        }
                                    }
                                    ResultAggrStruct thisAggrResult = new ResultAggrStruct(
                                            ResultAggrStruct.MidSpecialForAggregateMultipleValues,
                                            Integer.valueOf(currCapSidStr),
                                            Long.toString(capToMinValueLong.get(currCapSidStr)),
                                            aggregatedValues,
                                            new TimeIntervalStructure(
                                                    new Timestamp(capToTsFromMinLong.get(currCapSidStr)),
                                                    new Timestamp(capToTsToMaxLong.get(currCapSidStr))));
                                    logger.debug("Adding a result");
                                    tmpResForGWFunct.getOut().add(thisAggrResult);
                                    logger.debug("Added a result");

                                }
                            }
                        }
                    } else if (descriptionTokens[1].equalsIgnoreCase(ReqFunctionOverData.avgFunc)) {
                        // AVG FUNCTION   =======================================
                        int aggregatedValues = 0;
                        int refFunct = ReqFunctionOverData.unknownFuncId;
                        try {
                            refFunct = Integer.valueOf(descriptionTokens[2]);
                        } catch (Exception exfrtm) {
                            logger.error("Reference function id was set as unknown!");
                        }
                        HashMap<String, Long> capToTsFromMinLong = new HashMap<String, Long>();
                        HashMap<String, Long> capToTsToMaxLong = new HashMap<String, Long>();
                        HashMap<String, Long> capToAvgValueLong = new HashMap<String, Long>();
                        //
                        Iterator<ReqResultOverData> resultsIter002 = allUniqueFunctionsWithResults.iterator();

                        while (resultsIter002.hasNext()) {

                            ReqResultOverData tmpRes = resultsIter002.next();
                            /*System.out.println("LLLLLLLL TEST 3");
                            StringBuilder tmpRsOD = new StringBuilder();
                            tmpRsOD.append("resf fid:");
                            tmpRsOD.append(tmpRes.getFidInt());
                            tmpRsOD.append(" AND ref funct:");
                            tmpRsOD.append(refFunct);
                            System.out.println("OOOOOOOOOOOOOO TEST 3B" + tmpRsOD.toString());*/
                            if (tmpRes.getFidInt() == refFunct) {
                                // for every GENERIC capability requested( the genereic capability is coded as hashcode() )
                                for (String currCapSidStr : gwLevelFunctToCapsList
                                        .get(tmpGwFunct.getfuncName())) {
                                    if (!capToAvgValueLong.containsKey(currCapSidStr)) {
                                        capToAvgValueLong.put(currCapSidStr, Long.valueOf(0));
                                        capToTsFromMinLong.put(currCapSidStr, Long.valueOf(Long.MAX_VALUE));
                                        capToTsToMaxLong.put(currCapSidStr, Long.valueOf(Long.MIN_VALUE));
                                    }

                                    Iterator<OutType> tmpOutItemIter = tmpRes.getOut().iterator();
                                    while (tmpOutItemIter.hasNext()) {
                                        ResultAggrStruct tmpOutItem = new ResultAggrStruct(
                                                tmpOutItemIter.next());
                                        if (currCapSidStr.trim().equalsIgnoreCase(tmpOutItem.getSid().trim())) {
                                            try {
                                                long longValOfSensor = Long.parseLong(tmpOutItem.getVal());
                                                long valPrevious = capToAvgValueLong.get(currCapSidStr)
                                                        .longValue();
                                                long newVal = valPrevious + longValOfSensor;
                                                capToAvgValueLong.put(currCapSidStr, Long.valueOf(newVal));

                                                //
                                                if (capToTsFromMinLong.get(currCapSidStr)
                                                        .longValue() > tmpOutItem.getTis().getFromTimestamp()
                                                                .getTime()) {
                                                    capToTsFromMinLong.put(currCapSidStr, Long.valueOf(
                                                            tmpOutItem.getTis().getFromTimestamp().getTime()));
                                                }
                                                if (capToTsToMaxLong.get(currCapSidStr).longValue() < tmpOutItem
                                                        .getTis().getToTimestamp().getTime()) {
                                                    capToTsToMaxLong.put(currCapSidStr, Long.valueOf(
                                                            tmpOutItem.getTis().getToTimestamp().getTime()));
                                                }
                                                aggregatedValues += 1;

                                            } catch (Exception e) {
                                                logger.error("Invalid format to aggregate");
                                            }

                                        }
                                    }
                                    Double avgVal = Double
                                            .valueOf(capToAvgValueLong.get(currCapSidStr).longValue())
                                            / Double.valueOf(aggregatedValues);
                                    /*StringBuilder tmpRs = new StringBuilder();
                                    tmpRs.append("Result:");
                                    tmpRs.append(avgVal);
                                    tmpRs.append(" aggr vals:");
                                    tmpRs.append(aggregatedValues);
                                    System.out.println("OOOOOOOOOOOOOO TEST 3C" + tmpRs.toString());*/
                                    ResultAggrStruct thisAggrResult = new ResultAggrStruct(
                                            ResultAggrStruct.MidSpecialForAggregateMultipleValues,
                                            Integer.valueOf(currCapSidStr), Double.toString(avgVal),
                                            aggregatedValues,
                                            new TimeIntervalStructure(
                                                    new Timestamp(capToTsFromMinLong.get(currCapSidStr)),
                                                    new Timestamp(capToTsToMaxLong.get(currCapSidStr))));

                                    tmpResForGWFunct.getOut().add(thisAggrResult);
                                    //System.out.println("OOOOOOOOOOOOOO TEST 3D" + tmpRs.toString());
                                }
                            }
                        }

                    }
                }
            }
        }
    } // end of while loop on ONE HOP REFERENCE GW FUNCTIONs (MIN, MAX, AVG

    // Start of while loop on 2nd HOP reference GW function (need the one hops already filled in)
    // TODO: we don't handle/anticipate the case where the IF_THEN function references another IF_THEN function (even repeatedly). More flexibility could be implemented!!
    gwfunctIter = onlyGwLevelReqFunctVec.iterator(); // gets a NEW iterator
    while (gwfunctIter.hasNext()) {
        ReqFunctionOverData tmpGwFunct = gwfunctIter.next();

        Iterator<ReqResultOverData> resultsIter = allUniqueFunctionsWithResults.iterator();
        // loop over all resultOverData for this specific function (matching is made in the next two lines)
        while (resultsIter.hasNext()) {
            ReqResultOverData tmpResForGWFunct = resultsIter.next();

            if (tmpResForGWFunct.getFidInt() == tmpGwFunct.getfuncId()) {

                // descriptionTokens[0] : GW LEVEL PREFIX
                // descriptionTokens[1] : FUNCTION NAME
                // descriptionTokens[2] : REFERENCED FUNCTION ID
                String[] descriptionTokens = tmpGwFunct.getfuncName()
                        .split(ReqFunctionOverData.GW_LEVEL_SEPARATOR);

                if (descriptionTokens != null && descriptionTokens.length > 2
                        && (descriptionTokens[1].equalsIgnoreCase(ReqFunctionOverData.ruleRuleBinaryAndFunc)
                                || descriptionTokens[1]
                                        .equalsIgnoreCase(ReqFunctionOverData.ruleRuleIfThenFunc))) {
                    logger.debug("Clearing up values for gw funct name: " + tmpGwFunct.getfuncName());
                    // cleanup of output list  (it should however be already empty now that we rightfully only poll the WSI for node level functions)
                    tmpResForGWFunct.getOut().clear();
                    tmpResForGWFunct.getAllResultsforFunct().clear();
                    //after cleanup of output list
                    logger.debug("Filling values for funct name: " + tmpGwFunct.getfuncName());
                    if (descriptionTokens[1].equalsIgnoreCase(ReqFunctionOverData.ruleRuleBinaryAndFunc)) {

                        //TODO: handle a binary rule (condition1 and condition2)
                    } else if (descriptionTokens[1].equalsIgnoreCase(ReqFunctionOverData.ruleRuleIfThenFunc)) {

                        logger.debug("Filling values for funct name: " + tmpGwFunct.getfuncName());
                        //handle a binary rule (condition1 then do 3)
                        // 1: check if the referenced function has results that meet the conditions in its threshold
                        int consideredValues = 0;
                        int refFunct = ReqFunctionOverData.unknownFuncId;
                        try {
                            refFunct = Integer.valueOf(descriptionTokens[2]);
                        } catch (Exception exfrtm) {
                            logger.error("Reference function id was set as unknown!");
                        }
                        HashMap<String, Long> capToTsFromMinLong = new HashMap<String, Long>();
                        HashMap<String, Long> capToTsToMaxLong = new HashMap<String, Long>();
                        HashMap<String, Long> capToConditionValueLong = new HashMap<String, Long>();
                        //
                        Iterator<ReqResultOverData> resultsIter002 = allUniqueFunctionsWithResults.iterator();
                        while (resultsIter002.hasNext()) {
                            ReqResultOverData tmpRes = resultsIter002.next();
                            if (tmpRes.getFidInt() == refFunct) {
                                // for every GENERIC capability requested( the genereic capability is coded as hashcode() )
                                for (String currCapSidStr : gwLevelFunctToCapsList
                                        .get(tmpGwFunct.getfuncName())) {
                                    if (!capToConditionValueLong.containsKey(currCapSidStr)) {
                                        capToTsFromMinLong.put(currCapSidStr, Long.valueOf(Long.MAX_VALUE));
                                        capToTsToMaxLong.put(currCapSidStr, Long.valueOf(Long.MIN_VALUE));
                                        capToConditionValueLong.put(currCapSidStr, Long.valueOf(0));
                                    }

                                    Iterator<OutType> tmpOutItemIter = tmpRes.getOut().iterator();
                                    while (tmpOutItemIter.hasNext()) {
                                        ResultAggrStruct tmpOutItem = new ResultAggrStruct(
                                                tmpOutItemIter.next());

                                        if (currCapSidStr.trim().equalsIgnoreCase(tmpOutItem.getSid().trim())) {
                                            try {
                                                // TODO: Actually here we need to find in the original ReqFunctVec (that contains the full function definitions, not just the function id)
                                                //      the thresholds set. Before we search for the thresholds in the referenced function but now (better) we get them from this function (If_then)

                                                boolean foundTheCurrentFunctionInTheOriginalReqFunctionVec = false;
                                                long longValOfSensor = Long.parseLong(tmpOutItem.getVal());
                                                ReqFunctionOverData currentFunctionInCondition = null;
                                                for (int kx1 = 0; kx1 < reqFunctionVec.size(); kx1++) {
                                                    if (reqFunctionVec.elementAt(kx1)
                                                            .getfuncId() == tmpResForGWFunct.getFidInt()) {
                                                        currentFunctionInCondition = reqFunctionVec
                                                                .elementAt(kx1);
                                                        foundTheCurrentFunctionInTheOriginalReqFunctionVec = true;
                                                        break;
                                                    }
                                                }
                                                // but also find the reference function in the condition to include details in the notification
                                                boolean foundTheReferencedFunctionInTheOriginalReqFunctionVec = false;
                                                ReqFunctionOverData referencedFunctionInCondition = null;
                                                for (int kx1 = 0; kx1 < reqFunctionVec.size(); kx1++) {
                                                    if (reqFunctionVec.elementAt(kx1)
                                                            .getfuncId() == tmpResForGWFunct.getFidInt()) {
                                                        referencedFunctionInCondition = reqFunctionVec
                                                                .elementAt(kx1);
                                                        foundTheReferencedFunctionInTheOriginalReqFunctionVec = true;
                                                        break;
                                                    }
                                                }
                                                if (foundTheCurrentFunctionInTheOriginalReqFunctionVec) // the referred function here must have a threshold field because it's an evaluation of a condition
                                                {
                                                    if (currentFunctionInCondition != null
                                                            && currentFunctionInCondition
                                                                    .getThresholdField() != null
                                                            && !currentFunctionInCondition.getThresholdField()
                                                                    .isEmpty()) {
                                                        logger.debug(
                                                                "-------- INTO EVALUATING CONDITION NOW! ");
                                                        ThresholdStructure requiredThresholds = new ThresholdStructure(
                                                                currentFunctionInCondition.getThresholdField());
                                                        if (requiredThresholds.getLowerBound() != null
                                                                && !requiredThresholds.getLowerBound()
                                                                        .isEmpty()) {
                                                            logger.debug("Condition low parameter: "
                                                                    + requiredThresholds.getLowerBound()
                                                                            .trim());
                                                            //    TODO: handle other conditions for services (lower than, equals, between)
                                                            long lowbound = Long.parseLong(
                                                                    requiredThresholds.getLowerBound());
                                                            if (longValOfSensor >= lowbound) {
                                                                logger.debug("Sensor: " + tmpOutItem.getMid()
                                                                        + ". Condition is met: "
                                                                        + Long.toString(longValOfSensor)
                                                                        + " >= " + requiredThresholds
                                                                                .getLowerBound().trim());
                                                                consideredValues = 1;
                                                                ResultAggrStruct thisAggrResult = new ResultAggrStruct(
                                                                        tmpOutItem.getMid(),
                                                                        Integer.valueOf(currCapSidStr),
                                                                        Long.toString(longValOfSensor),
                                                                        consideredValues,
                                                                        new TimeIntervalStructure(new Timestamp(
                                                                                Long.valueOf(tmpOutItem.getTis()
                                                                                        .getFromTimestamp()
                                                                                        .getTime())),
                                                                                new Timestamp(Long.valueOf(
                                                                                        tmpOutItem.getTis()
                                                                                                .getToTimestamp()
                                                                                                .getTime()))));
                                                                tmpResForGWFunct.getOut().add(thisAggrResult);
                                                                // DONE: Send an alert notification
                                                                NotificationsFromVSNs newNotify = new NotificationsFromVSNs();
                                                                newNotify.setQueryDefId(pQueryDefId);
                                                                newNotify.setVgwID(myPeerId);
                                                                // get continuation info. Careful, we have not yet replaced the replacemntIDs with the original nodes in the measurements here (it's done later)
                                                                // but we have to set the MoteId to the Original Id and the replacementId to the replacement node
                                                                String[] replaceItem = getLocalReplacemntInfoListItem(
                                                                        localReplacedResources,
                                                                        tmpOutItem.getMid(),
                                                                        tmpOutItem.getSid());
                                                                if (replaceItem != null
                                                                        && replaceItem[0] != null
                                                                        && !replaceItem[0].isEmpty()
                                                                        && replaceItem[0].compareToIgnoreCase(
                                                                                replaceItem[1]) != 0) {
                                                                    newNotify.setMoteID(replaceItem[0]);
                                                                    newNotify.setReplacmntID(
                                                                            tmpOutItem.getMid());
                                                                } else {
                                                                    newNotify.setMoteID(tmpOutItem.getMid());
                                                                    newNotify.setReplacmntID("");
                                                                }
                                                                newNotify.setValue(longValOfSensor);
                                                                if (tmpOutItem.getTis() != null && tmpOutItem
                                                                        .getTis().isTimestampFromDefined())
                                                                    newNotify.setValueTimestamp(
                                                                            Long.toString(tmpOutItem.getTis()
                                                                                    .getFromTimestamp()
                                                                                    .getTime()));
                                                                newNotify.setBoundValue(lowbound);
                                                                newNotify.setRefFunctName(
                                                                        referencedFunctionInCondition
                                                                                .getfuncName());
                                                                newNotify.setRefFunctTriggerSign("gt"); //default for lower bound conditions
                                                                newNotify.setCapabilityCode(
                                                                        tmpOutItem.getSid().trim());
                                                                newNotify.setTimestamp(Long
                                                                        .toString(System.currentTimeMillis()));
                                                                newNotify.setType(
                                                                        NotificationsFromVSNs.CRITICAL_TYPE);
                                                                newNotify.setLevel(
                                                                        NotificationsFromVSNs.GATEWAY_LEVEL);
                                                                newNotify.setRefFunctId(
                                                                        referencedFunctionInCondition
                                                                                .getfuncId());
                                                                newNotify.setMessage(
                                                                        "Condition was met for node id: "
                                                                                + newNotify.getMoteID()
                                                                                + " value: " + longValOfSensor
                                                                                + " capability code:__"
                                                                                + tmpOutItem.getSid().trim());
                                                                // Send the response to the requesting end user
                                                                //System.out.println("Sending Notification!");
                                                                String notifMsgToSend = NotificationsFromVSNs
                                                                        .getAlertDelimitedString(newNotify);
                                                                this.sendResponse(notifMsgToSend);

                                                            } else {
                                                                logger.debug("Sensor: " + tmpOutItem.getMid()
                                                                        + " with value: "
                                                                        + Long.toString(longValOfSensor)
                                                                        + " does not meet Condition!");
                                                            }
                                                        }
                                                    }

                                                }

                                            } catch (Exception e) {
                                                logger.error("Invalid format to aggregate");
                                            }

                                        }
                                    }
                                    //
                                    //
                                }
                            }
                        }
                    }
                }

            }
        }

    }
    // Add trailing section for service deployability and replacements list
    // Careful! for the replacements list, skip the entries where the node replaces itself
    // DONE: RECONSTRUCT the Vector<ReqResultOverData> allUniqueFunctionsWithResults for the original nodes!
    //
    //
    logger.debug("BEFORE RECONSTRUCTION");
    if (allUniqueFunctionsWithResults != null) {
        logger.debug("IN RECONSTRUCTION");
        for (ReqResultOverData aResultOverData : allUniqueFunctionsWithResults) {
            String functionId = aResultOverData.getFid();
            // replacing is needed only for node level functions and possibly for if then functions referring to last values of sensors (not for aggregate GW level or if_then over aggregates)
            // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&+++++++++++++++++++++++++++++++++++==
            /*
            boolean isGwLevel = false;
            Iterator<ReqFunctionOverData> gwfunctIterLocal =  onlyGwLevelReqFunctVec.iterator();
            while(gwfunctIterLocal.hasNext())     //OUTER loop
            {
            ReqFunctionOverData tmpReqGwFunct = gwfunctIterLocal.next();
            if(Integer.toString(tmpReqGwFunct.getfuncId()).equalsIgnoreCase(functionId)){
                isGwLevel = true;
                break;
            }
                    
            }
            if(!isGwLevel) {
            */
            logger.debug("FID:: " + functionId);
            if (aResultOverData.getAllResultsforFunct() != null) {
                if (aResultOverData.getAllResultsforFunct().isEmpty()) {
                    logger.debug("has no results!!");
                } else {
                    logger.debug("found results!!");
                }

                Vector<ResultAggrStruct> newReconstructedResultVec = null;
                boolean foundAtLeastOneResultForSpecificMoteId = false;
                for (ResultAggrStruct thisResult : aResultOverData.getAllResultsforFunct()) {
                    if (thisResult.getMid()
                            .compareToIgnoreCase(ResultAggrStruct.MidSpecialForAggregateMultipleValues) != 0) {
                        if (!foundAtLeastOneResultForSpecificMoteId) {
                            foundAtLeastOneResultForSpecificMoteId = true;
                            newReconstructedResultVec = new Vector<ResultAggrStruct>();
                        }
                        String[] replaceItem = getLocalReplacemntInfoListItem(localReplacedResources,
                                thisResult.getMid(), thisResult.getSid());
                        if (replaceItem != null && replaceItem[0] != null && !replaceItem[0].isEmpty()) {
                            logger.debug("Back to replacing node :" + thisResult.getMid()
                                    + " with original node: " + replaceItem[0]);
                            thisResult.setMid(replaceItem[0]);
                            newReconstructedResultVec.addElement(thisResult);
                        }
                    }
                }
                if (foundAtLeastOneResultForSpecificMoteId) {
                    aResultOverData.setAllResultsforFunct(newReconstructedResultVec);
                }
            }
            /* } */
        }
    }

    //

    // DEBUG:
    logger.debug("The gateway has collected results and is ready to send them!");
    //return allResultsRead;    // Support for various data types is added by the DataTypeAdapter class
    //    ********************** COAP MESSAGES BACK TO GATEWAY *******************************
    //   ALSO SEND ANY SECURITY MESSAGES
    //   TODO: we could clean the cache after sending these messages (?)
    if (!VitroGatewayService.getVitroGatewayService().isWsiTrustCoapMessagingSupport()) {
        logger.debug("No SUPPORT FOR SENDING TRUST SECURITY INFO back to VSP!");
    }
    if (!VitroGatewayService.getVitroGatewayService().isTrustRoutingCoapMessagingActive()) {
        logger.debug("No ACTIVATION FOR SENDING TRUST SECURITY INFO back to VSP!");
    }

    if (VitroGatewayService.getVitroGatewayService().isWsiTrustCoapMessagingSupport()
            && VitroGatewayService.getVitroGatewayService().isTrustRoutingCoapMessagingActive()) {
        logger.debug("Attempting to send TRUST SECURITY INFO back to VSP!");
        HashMap<String, InfoOnTrustRouting> cacheTrustCoapCopy = new HashMap<String, InfoOnTrustRouting>(
                TrustRoutingQueryService.getInstance().getCachedDirectoryOfTrustRoutingInfo());
        String aRefCapCode = "";
        int aRefFunctId = 1;// last value is always in the request
        if (originalMotesAndTheirSensorAndFunctsVec != null) {
            try {
                aRefCapCode = originalMotesAndTheirSensorAndFunctsVec.firstElement()
                        .getQueriedSensorIdsAndFuncVec().get(0).getSensorModelid();
            } catch (Exception e339) {
                logger.error("Could not acquire sample capability id for security TRUST alert ");
            }
            try {
                aRefFunctId = originalMotesAndTheirSensorAndFunctsVec.firstElement()
                        .getQueriedSensorIdsAndFuncVec().get(0).getFunctionsOverSensorModelVec().firstElement();
            } catch (Exception e339) {
                logger.error("Could not acquire sample function id for security TRUST alert ");
            }
        }
        if (cacheTrustCoapCopy != null) {

            for (String sourceNodeId : cacheTrustCoapCopy.keySet()) {
                InfoOnTrustRouting tmpInfoOnTrust = cacheTrustCoapCopy.get(sourceNodeId);
                HashMap<String, Integer> tmpParentIdToPFiHM = tmpInfoOnTrust.getParentIdsToPFI();
                for (String parentNodeId : tmpParentIdToPFiHM.keySet()) {
                    // TODO: Send a SECURITY notification
                    NotificationsFromVSNs newNotify = new NotificationsFromVSNs();
                    newNotify.setQueryDefId(pQueryDefId);
                    newNotify.setVgwID(myPeerId);
                    newNotify.setMoteID(sourceNodeId);
                    newNotify.setValue(tmpParentIdToPFiHM.get(parentNodeId));
                    // TODO: Demo: change to current timestamp which is more reliable
                    newNotify.setValueTimestamp(Long.toString(System.currentTimeMillis())); // the time stamp for the PFI value
                    newNotify.setTimestamp(Long.toString(System.currentTimeMillis())); //the time stamp of the notification

                    //newNotify.setTimestamp(tmpInfoOnTrust.getTimestamp() );
                    //newNotify.setValueTimestamp(tmpInfoOnTrust.getTimestamp());
                    newNotify.setType(NotificationsFromVSNs.SECURITY_TYPE);
                    newNotify.setLevel(NotificationsFromVSNs.GATEWAY_LEVEL);
                    // we need sample valid funct ids and capability codes related to this VSN , to associate it at the VSP level with a partial service!
                    newNotify.setRefFunctId(aRefFunctId);
                    newNotify.setCapabilityCode(aRefCapCode);
                    // the message field is here used to store the parent ID.
                    newNotify.setMessage(parentNodeId);
                    // Send the response to the requesting end user
                    //System.out.println("Sending Notification!");
                    String notifMsgToSend = NotificationsFromVSNs.getAlertDelimitedString(newNotify);
                    try {
                        this.sendResponse(notifMsgToSend);
                        logger.debug("Sent one TRUST SECURITY INFO back to VSP!");
                    } catch (Exception securSendExc) {
                        logger.error("Could not send Security Type notification", securSendExc);
                    }
                }
            }
        }
        //
        /*
        logger.debug("Sending a dummy message security for TRUST-DEBUG");
        {   //---------------------------------------------------------------------
        // TODO: Send a SECURITY notification
        NotificationsFromVSNs newNotify = new NotificationsFromVSNs();
        newNotify.setQueryDefId(pQueryDefId);
        newNotify.setVgwID(myPeerId);
        newNotify.setMoteID("urn:wisebed:ctitestbed:0xca2");
        newNotify.setValue(400);
        newNotify.setValueTimestamp(Long.toString(new Date().getTime()));
        newNotify.setTimestamp(Long.toString(new Date().getTime()));
        newNotify.setType(NotificationsFromVSNs.SECURITY_TYPE);
        newNotify.setLevel(NotificationsFromVSNs.GATEWAY_LEVEL);
                
        newNotify.setRefFunctId(aRefFunctId);
        newNotify.setCapabilityCode(aRefCapCode);
        // the message field is here used to store the parent ID.
        newNotify.setMessage("urn:wisebed:ctitestbed:0xCC");
        // Send the response to the requesting end user
        //System.out.println("Sending Notification!");
        String notifMsgToSend = NotificationsFromVSNs.getAlertDelimitedString(newNotify);
        try{
            this.sendResponse(notifMsgToSend);
            logger.debug("Sent one TRUST SECURITY INFO back to VSP!");
        }catch(Exception securSendExc){
            logger.error("Could not send Security Type notification" , securSendExc);
        }
                
        //---------------------------------------------------------------------
        }
        */

    } //end of if we have to send the security Coap Routing Trust Messages

    // %%%%%%%%%% DIRECTLY INFORM THE GATEWAY OF PROBLEMATIC DEPLOY STATUS:
    if (serviceDeployImpossible || serviceDeployContinuationEmployed || serviceDeployPartiallyPossible) {
        String aRefMote = "";
        String aRefCapCode = "";
        int aRefFunctId = 1;// last value is always in the request
        if (originalMotesAndTheirSensorAndFunctsVec != null) {
            try {
                aRefMote = originalMotesAndTheirSensorAndFunctsVec.firstElement().getMoteid();
            } catch (Exception e339) {
                logger.error("Could not acquire sample ref node it for DEPLOY ABILITY STATUS alert ");
            }

            try {
                aRefCapCode = originalMotesAndTheirSensorAndFunctsVec.firstElement()
                        .getQueriedSensorIdsAndFuncVec().get(0).getSensorModelid();
            } catch (Exception e339) {
                logger.error("Could not acquire sample capability for DEPLOY ABILITY STATUS alert ");
            }
            try {
                aRefFunctId = originalMotesAndTheirSensorAndFunctsVec.firstElement()
                        .getQueriedSensorIdsAndFuncVec().get(0).getFunctionsOverSensorModelVec().firstElement();
            } catch (Exception e339) {
                logger.error("Could not acquire sample function id for DEPLOY ABILITY STATUS alert ");
            }
        }
        String strMessage = "";
        long deployValue = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_POSSIBLE_INT;
        if (serviceDeployImpossible) {
            strMessage = "The requested VSN cannot be supported by this island: " + myPeerId;
            // case ResponseAggrMsg.DEPLOY_STATUS_SERVICE_IMPOSSIBLE;
            deployValue = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_IMPOSSIBLE_INT;
        } else if (serviceDeployContinuationEmployed && serviceDeployPartiallyPossible) {
            // case ResponseAggrMsg.DEPLOY_STATUS_SERVICE_PARTIAL_CONT_COMBO;
            strMessage = "The requested VSN is partially supported using service continuation on this island: "
                    + myPeerId;

            deployValue = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_PARTIAL_CONT_COMBO_INT;
        } else if (serviceDeployContinuationEmployed) {
            // case ResponseAggrMsg.DEPLOY_STATUS_SERVICE_CONTINUATION;
            strMessage = "The requested VSN is supported using service continuation on this island: "
                    + myPeerId;
            deployValue = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_CONTINUATION_INT;
        } else if (serviceDeployPartiallyPossible) {
            // case ResponseAggrMsg.DEPLOY_STATUS_SERVICE_PARTIAL;
            strMessage = "The requested VSN is partially supported on this island: " + myPeerId;
            deployValue = ResponseAggrMsg.DEPLOY_STATUS_SERVICE_PARTIAL_INT;
        }
        // SEND THE NOTIFICATION::
        // TODO: Send a DEPLOY_STATUS_TYPE notification
        NotificationsFromVSNs newNotify = new NotificationsFromVSNs();
        newNotify.setQueryDefId(pQueryDefId);
        newNotify.setVgwID(myPeerId);
        newNotify.setMoteID(aRefMote);
        newNotify.setValue(deployValue);
        // TODO: Demo: change to current timestamp which is more reliable
        newNotify.setValueTimestamp(Long.toString(System.currentTimeMillis())); // the time stamp for the PFI value
        newNotify.setTimestamp(Long.toString(System.currentTimeMillis())); //the time stamp of the notification

        //newNotify.setTimestamp(tmpInfoOnTrust.getTimestamp() );
        //newNotify.setValueTimestamp(tmpInfoOnTrust.getTimestamp());
        newNotify.setType(NotificationsFromVSNs.DEPLOY_STATUS_TYPE);
        newNotify.setLevel(NotificationsFromVSNs.GATEWAY_LEVEL);
        // we need sample valid funct ids and capability codes related to this VSN , to associate it at the VSP level with a partial service!
        newNotify.setRefFunctId(aRefFunctId);
        newNotify.setCapabilityCode(aRefCapCode);
        // the message field is here used to store the parent ID.
        newNotify.setMessage(strMessage);
        // Send the response to the requesting end user
        //System.out.println("Sending Notification!");
        String notifMsgToSend = NotificationsFromVSNs.getAlertDelimitedString(newNotify);
        try {
            this.sendResponse(notifMsgToSend);
            logger.debug("Sent one DEPLOY STATUS info back to VSP!");
        } catch (Exception securSendExc) {
            logger.error("Could not send DEPLOY STATUS notification", securSendExc);
        }
    }
    return allUniqueFunctionsWithResults;
}

From source file:com.google.gsa.Kerberos.java

/**
 * Servlet's doPost: processes a POST request. Controls the overall 
 * kerberos silent authentication process. It supports both the Security 
 * Framework's SAML and Forms Based interface.
 * <p>/*from   w  w w.  j  av  a2  s  . c  om*/
 * You can find more information on the Security Framework's Kerberos guide 
 * about the scenarios implemented here
 * 
 * @param request HTTP request
 * @param response HTTP response
 * 
 * @throws ServletException
 * @throws IOException
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    logger.debug("Kerberos servlet");

    if (gsaValveConfigPath == null) {
        if (request.getAttribute("gsaValveConfigPath") == null) {
            //Read parameter from config file: SAML
            gsaValveConfigPath = readValveConfigPath();
        } else {
            gsaValveConfigPath = request.getAttribute("gsaValveConfigPath").toString();
        }
    }

    logger.debug("Valve Config Path is: " + gsaValveConfigPath);

    // Initialize status code
    int statusCode = HttpServletResponse.SC_UNAUTHORIZED;

    //Authentication Processes 
    AuthenticationProcessImpl authenticationProcessCls = null;
    KerberosAuthenticationProcess krbAuthN = new KerberosAuthenticationProcess();

    //Initialize cookies vars
    Cookie gsaRefererCookie = null;
    Cookie gsaAuthCookie = null;

    //Session Cookie arrays
    Vector<Cookie> krbCookies = new Vector<Cookie>();
    Vector<Cookie> nonKrbCookies = new Vector<Cookie>();

    //user agent
    String userAgent = null;

    //user credentials
    Credentials creds = null;

    //User Session and Session ID vars definition
    UserSession userSession = null;
    String sessionID = null;
    String encodedSessionID = null;

    //Create the credentials store
    try {
        this.valveConf = ValveConfigurationInstance.getValveConfig(gsaValveConfigPath);
    } catch (ValveConfigurationException e) {
        logger.error("Valve Config instantiation error: " + e);
    }

    logger.debug("Creating the credentials store");
    creds = new Credentials();
    String username = null;

    //Setting Valve parameters
    logger.debug("Setting Valve params");
    setValveParams(request);

    //Protection
    if ((!isKerberos) || (!isNegotiate)) {
        logger.error(
                "Configuration error: if you want to use Kerberos silent AuthN, isKerberos and isNegotiate config vars have to be set to true");
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "Configuration error - Kerberos is not set properly");
        return;
    }

    Cookie cookies[] = null;

    // Retrieve cookies
    cookies = request.getCookies();

    // Protection: look for auth and referer cookies
    if (cookies != null) {

        // Look for the referer cookie
        for (int i = 0; i < cookies.length; i++) {

            // Look for the referer cookie
            if ((cookies[i].getName()).equals(refererCookieName)) {

                // Cache cookie
                gsaRefererCookie = cookies[i];

                logger.debug("Referer cookie already exists: " + gsaRefererCookie.getValue());

            } else {
                // Look for the auth cookie
                if ((cookies[i].getName()).equals(authCookieName)) {

                    // Cache cookie
                    gsaAuthCookie = cookies[i];

                    logger.debug("Auth cookie already exists: " + gsaAuthCookie.getValue());

                }
            }

            if ((gsaRefererCookie != null) && (gsaAuthCookie != null)) {
                // Exit
                break;
            }

        }

    }

    // Protection
    if (!isSAML) {
        if (gsaRefererCookie == null) {

            // Raise error
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                    "The GSA authentication servlet couldn't read the referer cookie");

            // Log error
            logger.error(
                    "The GSA authentication servlet couldn't read the referer cookie, pls. check the cookie domain value");

            // Return
            return;

        }
    } else {
        //SAML

        //Get SAML Params
        relayState = request.getParameter("RelayState");
        samlRequest = request.getParameter("SAMLRequest");
        //String relayStateCookie = valveConf.getSAMLConfig().getRelayStateCookie();
        boolean noParams = false;
        boolean cookieExist = true;

        //Protection
        if ((relayState == null) || (relayState.equals(""))) {
            noParams = true;
        } else {
            if ((samlRequest == null) || (samlRequest.equals(""))) {
                noParams = true;
            }
        }

        createRefererCookie(gsaRefererCookie);

        //if ((noParams)&&(!cookieExist)) {
        if (noParams) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid request");
            return;
        }
    }

    logger.debug("Let's validate if gsaAuthCookie is present");

    if (gsaAuthCookie != null) {

        if (!isSAML) {
            //redirect
            String redirect = gsaRefererCookie.getValue();

            logger.debug("redirect is " + redirect);
            //redirect only if the URL is different than the login one                    
            if (!redirect.equals(loginUrl)) {

                //user properly authenticated
                logger.debug("The user was properly authenticated. Lets redirect to..." + redirect);

                // Redirect
                response.sendRedirect(redirect);

            } else {
                logger.debug("It's the login URL. No redirect");
            }
        } else {
            logger.debug("As this is SAML. Let's obviate the previous authentication cookie");
            gsaAuthCookie = null;
        }
    }

    userSession = new UserSession();

    Sessions sessions = Sessions.getInstance();
    sessions.setMaxSessionAgeMinutes(maxSessionAge);
    sessions.setSessionTimeoutMinutes(sessionTimeout);

    if (gsaAuthCookie == null) {

        logger.debug("gsaAuthCookie does not exist");

        isNegotiate = true;

        // Read User-Agent header
        userAgent = request.getHeader("User-Agent");

        logger.debug("userAgent is... " + userAgent);

        //check if user is gsa-crawler
        if (userAgent.startsWith(GSA_CRAWLER_USER)) {

            logger.debug("User is " + GSA_CRAWLER_USER);

            //check if user is gsa-crawler and have to authenticate it thru a form                                  
            if (KrbUsrPwdCrawler) {

                logger.debug("gsa-crawler has to access thru username and password");

                //check if crawler already provided credentials

                if (request.getParameter("UserIDKrb") == null) {

                    //the login page have to be filled in by the admin user before reaching here. Return error
                    logger.error("The login page [" + KrbUsrPwdCrawlerUrl
                            + "] has to be invoked and its credentials fields filled in before reaching here");
                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                            "It means the GSA Valve Kerberos configuration is not done properly or you just forgot to fill in the Kerberos credentials in the login page");
                    return;

                } else {

                    //user already submits credentials
                    logger.debug("Crawler has already sent credentials");
                    //set isNegotiate equal false (it authenticates the user thru username and pwd credentials)                                                                                    
                    isNegotiate = false;

                    //set Crawler credentials
                    setCrawlerCredentials(request, creds, KrbAdditionalAuthN);

                    //authenticate user
                    statusCode = krbAuthentication(request, response, krbAuthN, krbCookies,
                            gsaRefererCookie.getValue(), creds, isNegotiate);

                    // Protection: check status code
                    if (statusCode != HttpServletResponse.SC_OK) {

                        // Raise error
                        response.sendError(statusCode, "Authentication process failed!");

                        // Debug
                        if (logger.isDebugEnabled())
                            logger.debug("Krb Authentication process failed with code: " + statusCode);

                        if (statusCode == HttpServletResponse.SC_UNAUTHORIZED) {
                            logger.debug(
                                    "Note: this 401 could not be an error as sending 401 could be part of the Negotiation process");
                        }

                        // Return
                        return;

                    }

                    //check if the additional authN method is available. If so, start authN with these creds as well
                    //N: modification for always lanching the root authN process. Comment out the following line
                    //if (KrbAdditionalAuthN) {

                    statusCode = nonKrbAuthentication(request, response, authenticationProcessCls,
                            nonKrbCookies, gsaRefererCookie.getValue(), creds);

                    //check if the status code is indeterminate
                    if (statusCode == -1) {
                        //the process could not determinate the authorization
                        //as there is no pattern that matches with any repository
                        statusCode = HttpServletResponse.SC_UNAUTHORIZED;
                    }

                    // Protection: check status code
                    if (statusCode != HttpServletResponse.SC_OK) {

                        // Raise error
                        response.sendError(statusCode, "Authentication process failed!");

                        // Debug
                        if (logger.isDebugEnabled())
                            logger.debug("Non Krb Authentication process failed with code: " + statusCode);

                        // Return
                        return;

                    }

                    //}

                }
            } else { // end KrbUsrPwdCrawler is set. 
                //If KrbUsrPwdCrawler is not set to true, then do nothing (assume content is feeded)
                //just send back the error as a configuration one (we shouldn't configure Froms-based crawling)
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                        "Configuration error. Review your configuration as you can not define this rule if it's not set properly (see doc on how to set it up using Kerberos config attributes)");
                return;
            }

        } else { //User is not Crawler

            logger.debug("User is NOT crawler");

            //check if we have double AuthN or not
            if (!KrbAdditionalAuthN) {

                logger.debug("Krb silent authN only");

                //set isNegotiate equal true (it authenticates the user thru kerberos ticket)                                                                                    
                isNegotiate = true;

                String refererCookieValue = null;
                if (gsaRefererCookie != null) {
                    refererCookieValue = new String(gsaRefererCookie.getValue());
                }

                //authenticate user
                statusCode = krbAuthentication(request, response, krbAuthN, krbCookies, refererCookieValue,
                        creds, isNegotiate);

                // Protection: check status code
                if (statusCode != HttpServletResponse.SC_OK) {

                    // Raise error
                    response.sendError(statusCode, "Authentication process failed!");

                    // Debug
                    if (logger.isDebugEnabled())
                        logger.debug("Krb Authentication process failed with code: " + statusCode);

                    if (statusCode == HttpServletResponse.SC_UNAUTHORIZED) {
                        logger.debug(
                                "Note: this 401 could not be an error as sending 401 could be part of the Negotiation process");
                    }

                    // Return
                    return;

                } else {

                    boolean doesKrbSubjectExist = lookForKrbCreds(creds);

                    if (!doesKrbSubjectExist) {
                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                "Credentials not valid. Try to close your browser and try it again");

                        // Log error
                        logger.error("Kerberos Subject is not present when authenticating");

                        // Return
                        return;
                    }

                    //N: call rootAuthN once we have the Kerberos creds
                    //N: Begin update
                    if (!KrbAdditionalAuthN) {
                        statusCode = nonKrbAuthentication(request, response, authenticationProcessCls,
                                nonKrbCookies, refererCookieValue, creds);

                        //check if the status code is indeterminate
                        if (statusCode == -1) {
                            //the process could not determinate the authorization
                            //as there is no pattern that matches with any repository
                            statusCode = HttpServletResponse.SC_UNAUTHORIZED;
                        }

                        // Protection: check status code
                        if (statusCode != HttpServletResponse.SC_OK) {

                            // Raise error
                            response.sendError(statusCode, "Authentication process failed!");

                            // Debug
                            if (logger.isDebugEnabled())
                                logger.debug("Non Krb Authentication process failed with code: " + statusCode);

                            // Return
                            return;
                        }

                    }
                    //N:End update

                }

            } else { //Double AuthN required. So that apart from the Krb silent authN, we authN the user as well thru username and pwd

                logger.debug("Krb and Forms based AuthN mechanisms");

                //check if Krb credentials are already set
                Cookie gsaKrbCookie = getCookie(request, KRB_COOKIE_NAME);

                //if (gsaKrbCookie != null) { //Kerberos cookie set
                if (!isKrbProcess(gsaKrbCookie)) { //Kerberos cookie set    

                    logger.debug("Krb cookie is set. Krb AuthN already in place");

                    Subject krbSubj = getKrbSubject(gsaKrbCookie.getValue());

                    //Protection
                    if (krbSubj == null) { // couldn't localize the subject. 

                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                "Credentials not valid. Try to close your browser and try it again");

                        // Log error
                        logger.error("Kerberos Subject is not present when authenticating");

                        // Return
                        return;
                    } else {

                        logger.debug("The Krb subject exists. This is the Forms based AuthN part");

                        //check if parameters are present
                        if (request.getParameter("UserIDKrb") == null) {

                            logger.debug("Login page has not been already invoked");

                            String redirectUrl = contructKrbLoginURL();

                            logger.debug("Redirecting to...." + redirectUrl);

                            //redirect to the login page
                            response.sendRedirect(response.encodeRedirectURL(redirectUrl));

                            // Return
                            return;

                        } else {

                            //user already submits credentials
                            logger.debug("User has already sent credentials");

                            createCredsDoubleAuthN(request, creds, krbSubj);

                            logger.debug("User Credentials created. Let's authenticate the user without Krb");

                            statusCode = nonKrbAuthentication(request, response, authenticationProcessCls,
                                    nonKrbCookies, gsaRefererCookie.getValue(), creds);

                            //check if the status code is indeterminate
                            if (statusCode == -1) {
                                //the process could not determinate the authorization
                                //as there is no pattern that matches with any repository
                                statusCode = HttpServletResponse.SC_UNAUTHORIZED;
                            }

                            // Protection: check status code
                            if (statusCode != HttpServletResponse.SC_OK) {

                                // Raise error
                                response.sendError(statusCode, "Authentication process failed!");

                                // Debug
                                if (logger.isDebugEnabled())
                                    logger.debug(
                                            "Non Krb Authentication process failed with code: " + statusCode);

                                // Return
                                return;

                            }
                            boolean resultDelete = deleteKrbSubject(gsaKrbCookie.getValue());
                            if (!resultDelete) {
                                logger.error("Not KrbSubj found when deleting it");
                            }

                        }
                    }

                } else { //Krb cookie does not exist
                    logger.debug(
                            "Krb cookie does not exist. Let's silently authenticate the user thru Krb firstly");
                    logger.debug("Krb silent authN only");

                    //set isNegotiate equal true (it authenticates the user thru kerberos ticket)                                                                                    
                    isNegotiate = true;

                    //authenticate user
                    statusCode = krbAuthentication(request, response, krbAuthN, krbCookies,
                            gsaRefererCookie.getValue(), creds, isNegotiate);

                    // Protection: check status code
                    if (statusCode != HttpServletResponse.SC_OK) {

                        // Raise error
                        response.sendError(statusCode, "Authentication process failed!");

                        // Debug
                        if (logger.isDebugEnabled())
                            logger.debug("Krb Authentication process failed with code: " + statusCode);

                        if (statusCode == HttpServletResponse.SC_UNAUTHORIZED) {
                            logger.debug(
                                    "Note: this 401 could not be an error as sending 401 could be part of the Negotiation process");
                        }

                        // Return
                        return;

                    } else {
                        Cookie krbCookie = krbCookies.elementAt(0);
                        String krbAuthCookieValue = krbCookie.getValue();

                        logger.debug("Krb cookie value: " + krbAuthCookieValue);
                        if (krbAuthCookieValue == null) {
                            logger.error("Krb cookie not present");
                            // Raise error
                            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                    "Kerberos cookie not present");
                            // Return
                            return;
                        } else {
                            addKrbCookie(response, krbCookie);
                            addKrbSubject(krbAuthCookieValue, krbAuthN.getUserSubject());
                            logger.debug(
                                    "The User Krb identity is already present. Let's authenticate the user thru username/password");
                            //redirect to Login page
                            String redirectUrl = contructKrbLoginURL();
                            response.sendRedirect(response.encodeRedirectURL(redirectUrl));
                            logger.debug("Redirect to.... " + redirectUrl);
                            return;
                        }

                    }

                }

            }
        }

        logger.debug("Krb and/or Forms based AuthN OK. Let's create the session");

        //set username and cookies
        username = creds.getCredential(KRB5_ID).getUsername();

        //creation time var
        long creationTime = System.currentTimeMillis();

        //Setting session values
        sessionID = UserIDEncoder.getID(username, creationTime);
        encodedSessionID = URLEncoder.encode(sessionID, encoder);

        logger.debug("Krb Username is... " + username);

        // setSession                                               
        boolean sessionOk = settingSession(userSession, gsaAuthCookie, creds, username, krbAuthN, creationTime,
                encodedSessionID, krbCookies, nonKrbCookies);

        logger.debug("Session is .... " + sessionOk);

        if (!sessionOk) {
            //SAML
            statusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
            response.setStatus(statusCode);

            // Log error
            logger.error("Kerberos Subject has not been created properly");

            // Return
            return;
        } else {
            //Store Session in the Session Map
            sessions.addSession(sessionID, userSession);

            sessions.setMaxSessionAgeMinutes(maxSessionAge);

            if (isSessionEnabled) {
                sessions.setSessionTimeoutMinutes(sessionTimeout);
            } else {
                sessions.setSessionTimeoutMinutes(-1);
            }

            logger.debug("User Session created");

            // Add internal authentication cookie
            response.addCookie(gsaAuthCookie);

            logger.debug("Auth cookie added");

            // Debug
            if (logger.isDebugEnabled())
                logger.debug("Authentication process successful");

            if (!isSAML) {
                // Debug
                if (logger.isDebugEnabled())
                    logger.debug("Redirecting user to: " + gsaRefererCookie.getValue());

                // Redirect
                response.sendRedirect(gsaRefererCookie.getValue());
            } else {
                try {
                    redirectingSAML(response, cookies, sessionID);
                } catch (ValveConfigurationException e) {
                    logger.error("Configuration error: " + e.getMessage(), e);
                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                }
            }

        }

    } //end of AuthN cases

}

From source file:alter.vitro.vgw.wsiadapter.WsiUberDustCon.java

Vector<ReqResultOverData> DEBUG_offline_translateAggrQuery(
        Vector<QueriedMoteAndSensors> motesAndTheirSensorAndFunctsVec,
        Vector<ReqFunctionOverData> reqFunctionVec) {
    //logger.debug("IN UBERDUST QUERY");
    /*for(QueriedMoteAndSensors qms: motesAndTheirSensorAndFunctsVec) {
    logger.debug("Mote: " + qms.getMoteid());
       for(ReqSensorAndFunctions rsf : qms.getQueriedSensorIdsAndFuncVec()){
       logger.debug("Capability: " +rsf.getSensorModelid());
       for( Integer fidd: rsf.getFunctionsOverSensorModelVec()) {
           logger.debug("Function: " +fidd);
       }/*from   ww  w . jav  a 2 s .c  om*/
            
       }
            
    }*/
    String responseBodyFromHttpNodesGetStr = WsiUberDustCon.DEBUG_OFFLINE_STR_NODE_GETRESTSTATUS;
    String responseBodyFromHttpNodes_STATUS_Get = WsiUberDustCon.DEBUG_OFFLINE_STR_GETRESTSTATUS_RAW;
    String responseBodyFromHttpNodes_ADMINSTATUS_Get = WsiUberDustCon.DEBUG_OFFLINE_STR_BODY_ADMINSTATUS;

    boolean useTheStandardWayToGetStateForActuatorReading = false; // false allows to get explicitly the latest reading instead of the standard one (in the general status page) updated every few minutes
    // TODO: tmp structure to be replaced when this is tested and works
    Vector<VerySimpleSensorEntity> allSensorsWithCapsAndMeasures = new Vector<VerySimpleSensorEntity>();
    // Maps Smart Device ids to Room names, where the room names are available.
    HashMap<String, String> smartDevIdsToRooms = new HashMap<String, String>();
    //
    // ##########################################################################################################################################
    //
    /*
     * TODO: optimize the status/resource retrieval process for uberdust!
     * TODO: Take into account the mote status before ADDING it to the gateway description list (++++ LATER)
     *         For now we assume that the queries to the actual WSN are handled by the middleware.
     *         We search the uberdust "database" for data. (but we can still perform actions to affect the WSI!)
     *
     *         The plan is for a future service
     *         where a peer could submit queries for submission in the actual WSNs, and subsequently gather the data
     *         of the results. (e.g. administration service>reprogramming service)
     */
    try {

        //logger.debug("--------OK Response: "+ httpUberdustNodesGetResponseStatusCode+"------------------------------");
        //
        String[] nodeUrnsInUberdust = responseBodyFromHttpNodesGetStr.split("\\r?\\n");
        int totalNodeUrnsInUberdust = nodeUrnsInUberdust.length;

        String[] nodeAndLastCapReadingsUrnsInUberdust = responseBodyFromHttpNodes_STATUS_Get.split("\\r?\\n");
        int totalNodeWithCapsInUberdust = nodeAndLastCapReadingsUrnsInUberdust.length;

        // LOOP OVER EVERY NODE (smart device), and for each node, get its capabilities from the second response (responseBody_STATUS_Str)
        Vector<String> allFaultyNodesUrns = getFaultyNodes();

        for (String aNodeUrnsInUberdust : nodeUrnsInUberdust) {
            if (allFaultyNodesUrns.contains(aNodeUrnsInUberdust)) {
                logger.debug("Skiipping node: " + aNodeUrnsInUberdust);
                continue;
            }
            Vector<VerySimpleObservationCapabilities> sensObsCapsVector = new Vector<VerySimpleObservationCapabilities>();
            Vector<VerySimpleSensorMeasurement> sensObsMeasurementVector = new Vector<VerySimpleSensorMeasurement>();

            if (VitroGatewayService.getVitroGatewayService().getAssignedGatewayUniqueIdFromReg()
                    .equalsIgnoreCase("vitrogw_hai")) {
                aNodeUrnsInUberdust = dictionaryUberdustUrnToHaiUrnName.get(aNodeUrnsInUberdust);
                if (aNodeUrnsInUberdust == null)
                    continue;
            }

            //                                logger.debug("Iteration " + String.valueOf(k+1) + " of " + String.valueOf(totalNodeUrnsInUberdust));
            //                                logger.debug(nodeUrnsInUberdust[k]);

            Vector<Integer> sensorModels_IDs_OfSmartDevVector = new Vector<Integer>();
            CSmartDevice tmpSmartDev = new CSmartDevice(aNodeUrnsInUberdust, "", /* smart device type name */
                    "", /* location description e.g. room1*/
                    new GeodesicPoint(), /*  */
                    sensorModels_IDs_OfSmartDevVector);

            // TODO: Add an extra early for loop to update the fields for the attributes of the SmartDevice such as:
            //      Eventually if the SmartDev has NO other valid sensors (e.g. observation sensors or actuators) then it won't be added !
            String tmp_longitude = "";
            String tmp_latitude = "";
            String tmp_altitude = "";

            for (String aNodeAndLastCapReadingsUrnsInUberdust1 : nodeAndLastCapReadingsUrnsInUberdust) {
                //to update the device attributes!
                String[] nodeCapAndReadingRowItems = aNodeAndLastCapReadingsUrnsInUberdust1.split("\\t");
                if (VitroGatewayService.getVitroGatewayService().getAssignedGatewayUniqueIdFromReg()
                        .equalsIgnoreCase("vitrogw_hai")) {
                    nodeCapAndReadingRowItems[0] = dictionaryUberdustUrnToHaiUrnName
                            .get(nodeCapAndReadingRowItems[0]);
                    if (nodeCapAndReadingRowItems[0] == null)
                        continue;
                }

                if (nodeCapAndReadingRowItems.length > 3
                        && nodeCapAndReadingRowItems[0].compareToIgnoreCase(aNodeUrnsInUberdust) == 0) //we are at the capabilities of the current smartdevice
                {
                    // [0] is mote (smart device) id
                    // [1] is capability
                    // [2] is timestamp
                    // [3] is measurement value
                    if ((nodeCapAndReadingRowItems[1] != null)
                            && !(nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase(""))) {
                        if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("room")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            tmpSmartDev.setLocationDesc(nodeCapAndReadingRowItems[3].trim());
                            smartDevIdsToRooms.put(tmpSmartDev.getId(), tmpSmartDev.getLocationDesc());
                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("nodetype")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            tmpSmartDev.setName(nodeCapAndReadingRowItems[3].trim());
                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("description")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //TODO: do we need this?

                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("x")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //TODO: we need the function to derive a valid longitude from the uberdust value (pending)
                            tmp_longitude = nodeCapAndReadingRowItems[3].trim();
                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("y")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //TODO: we need the function to derive a valid latitude)
                            tmp_latitude = nodeCapAndReadingRowItems[3].trim();
                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("z")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //altitude is in meters (assumption)
                            tmp_altitude = nodeCapAndReadingRowItems[3].trim();

                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("phi")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //TODO: do we need this?
                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("theta")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //TODO: do we need this?
                        }
                    }
                }
            } // end of first round of for loop for attributes
            if (!tmp_latitude.equalsIgnoreCase("") && !tmp_longitude.equalsIgnoreCase("")
                    && !tmp_altitude.equalsIgnoreCase("")) {
                tmpSmartDev.setGplocation(new GeodesicPoint(tmp_latitude, tmp_longitude, tmp_altitude));
            }

            //
            // Again same loop for measurement and actuation capabilities!
            //
            for (String aNodeAndLastCapReadingsUrnsInUberdust : nodeAndLastCapReadingsUrnsInUberdust) {
                String[] nodeCapAndReadingRowItems = aNodeAndLastCapReadingsUrnsInUberdust.split("\\t");
                if (VitroGatewayService.getVitroGatewayService().getAssignedGatewayUniqueIdFromReg()
                        .equalsIgnoreCase("vitrogw_hai")) {
                    nodeCapAndReadingRowItems[0] = dictionaryUberdustUrnToHaiUrnName
                            .get(nodeCapAndReadingRowItems[0]);
                    if (nodeCapAndReadingRowItems[0] == null)
                        continue;
                }
                if (nodeCapAndReadingRowItems.length > 3
                        && nodeCapAndReadingRowItems[0].compareToIgnoreCase(aNodeUrnsInUberdust) == 0) //we are at the capabilities of the current smartdevice
                {
                    // [0] is mote (smart device) id
                    // [1] is capability
                    // [2] is measurement value
                    // [3] is timestamp
                    //                                        logger.debug(nodeCapAndReadingRowItems[1]);
                    // TODO: FILTER OUT UNSUPPORTED OR COMPLEX CAPABILITIES!!!!
                    // Since uberdust does not distinguish currenlty between sensing/actuating capabilities and properties, we need to filter out manually
                    // everything we don't consider a sensing/actuating capability.
                    // Another filtering out is done at a later stage with the SensorMLMessageAdapter, which will filter out the capabilities not supported by IDAS
                    // TODO: it could be nice to have this filtering unified.
                    if ((nodeCapAndReadingRowItems[1] != null)
                            && (nodeCapAndReadingRowItems[1].trim().compareTo("") != 0)
                            && !getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim())
                                    .equalsIgnoreCase("UnknownPhenomenon")) {
                        // The model id is set as the hashcode of the capability name appended with the model type of the device.
                        // Perhaps this should be changed to something MORE specific
                        // TODO: the units should be set here as we know them for Uberdust. Create a small dictionary to set them!
                        // TODO: the non-observation sensors/ non-actuation should be filtered here!! the Name for the others should be "UnknownPhenomenon"

                        String tmpGenericCapabilityForSensor = getSimpleCapForUberdustUrn(
                                nodeCapAndReadingRowItems[1].trim());
                        Integer thedigestInt = (tmpGenericCapabilityForSensor + "-" + tmpSmartDev.getName())
                                .hashCode();
                        if (thedigestInt < 0)
                            thedigestInt = thedigestInt * (-1);

                        //                                            /*
                        //                                            CSensorModel tmpSensorModel = new CSensorModel(givGatewayInfo.getId(), /*Gateway Id*/
                        //                                                    thedigestInt, /*Sensor Model Id */
                        //                                                    (tmpGenericCapabilityForSensor + "-" + tmpSmartDev.getName()), /* Sensor Model name */
                        //                                                    CSensorModel.numericDataType, /* Data type*/  // TODO: later on this should be adjustable!!!
                        //                                                    CSensorModel.defaultAccuracy, /* Accuracy */
                        //                                                    CSensorModel.defaultUnits) /* Units */;  // TODO: this should be set when it is known!!!

                        // GET THE OBSERVATION
                        VerySimpleObservationCapabilities tmpObsCap = new VerySimpleObservationCapabilities(
                                nodeCapAndReadingRowItems[1], true);
                        if ((tmpObsCap.getSimpleName() != null)
                                && !(tmpObsCap.getSimpleName().equalsIgnoreCase("UnknownPhenomenon"))) {
                            sensObsCapsVector.add(tmpObsCap);

                            // ts of measurement in place [2]
                            // value of measurement in place [3]
                            // logger.debug(nodeCapAndReadingRowItems[2]+'\t'+nodeCapAndReadingRowItems[3]);
                            long theTimeStamp = Long.parseLong(nodeCapAndReadingRowItems[2]);
                            String theValue = nodeCapAndReadingRowItems[3];
                            if (theValue.contains(" "))
                                theValue = theValue.split(" ")[0]; // if it contains the UOM as a suffix,then just keep the first part
                            String observPropertyDef = tmpObsCap.getPhenomenonIDASUrn();
                            String observOutputUOMCode = tmpObsCap.getUomIDASUrn();// tmpObsCap.getUomIDASCode();
                            // just one (last) value
                            String[] observOutputMeasurementData = new String[1];
                            // Dummy measurement value
                            if (tmpObsCap.getSimpleName().equalsIgnoreCase("temperature")) {
                                //since we assume kelvin to be the default UOM, until fixed, wew set oiur ceslious to Kelvin here:
                                //K = C+273 . TODO. Later on this normalization should be done at the VSP!
                                double d = Double.parseDouble(theValue);
                                double convertedKelvinValue = d + 273.0;
                                String convertedKelvinValueStr = Long.toString((long) convertedKelvinValue);

                                observOutputMeasurementData[0] = convertedKelvinValueStr; //to kelvin
                            } else {
                                observOutputMeasurementData[0] = theValue;
                            }
                            // TODO: Check if timezone is correct!
                            // FOR UBERDUST: override sensors timestamp with reply from uberdust timestamp (now)
                            Date dateNow = new Date();
                            theTimeStamp = dateNow.getTime();
                            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
                            String observPropertyTSStr = df.format(new Date(theTimeStamp));
                            //logger.debug("---- " + observPropertyTSStr + " ---- " + theValue + " ----------------------------");
                            sensObsMeasurementVector
                                    .add(new VerySimpleSensorMeasurement(observPropertyDef, observOutputUOMCode,
                                            observOutputMeasurementData, observPropertyTSStr, theTimeStamp));
                        }

                        sensorModels_IDs_OfSmartDevVector.add(thedigestInt);

                    }
                }
            }
            if (!sensorModels_IDs_OfSmartDevVector.isEmpty()) {
                // TODO: FILTER OUT UNSUPPORTED OR COMPLEX NODES!!!!
                VerySimpleSensorEntity sens01 = new VerySimpleSensorEntity(aNodeUrnsInUberdust,
                        sensObsCapsVector, sensObsMeasurementVector);
                allSensorsWithCapsAndMeasures.add(sens01);

                // TODO: MAYBE HERE WE CAN CHECK IF THESE MEASUREMENTS are for the results??
                //if (!sensObsMeasurementVector.isEmpty())
                //{
                //    Iterator<VerySimpleSensorMeasurement> it1 = sensObsMeasurementVector.iterator();
                //    while( it1.hasNext())
                //    {
                //        VerySimpleSensorMeasurement sensorMeasurement = (VerySimpleSensorMeasurement)it1.next();
                //
                //    }
                //}
                //#####################################
            }
        } // ends for loop over all smartdevices discovered!
    } catch (Exception e) {
        logger.error("error::" + e.getMessage());

    }

    //
    // TILL HERE WE HAVE A VECTOR WITH ALL Devices and Capabilities and Measurements: allSensorsWithCapsAndMeasures
    //
    //

    Vector<ResultAggrStruct> vOfSensorValues;

    Vector<ReqResultOverData> retVecofResults;
    retVecofResults = new Vector<ReqResultOverData>();

    //logger.debug("Size of motesAndTheirSensorAndFunctsVec::" + Integer.toString(motesAndTheirSensorAndFunctsVec.size())  );

    for (int i = 0; i < motesAndTheirSensorAndFunctsVec.size(); i++) {

        String fullMoteId = motesAndTheirSensorAndFunctsVec.elementAt(i).getMoteid();

        // for each entry, get the vector of queried sensor types and the functions to be applied to the measurements.
        List<ReqSensorAndFunctions> tmpVecSmAndFuncList = motesAndTheirSensorAndFunctsVec.elementAt(i)
                .getQueriedSensorIdsAndFuncVec();
        //            Vector<Integer> tmpVecSmIds =  motesAndTheirSensorHM.get(fullMoteId);
        /**
         *
         *  TODO: So far we assume all of the data types in measurements to be Long! This should be fixed!!!
         *
         */
        try {

            //
            // We have the readings from all sensors.
            // we must select only the readings from the specific sensors of interest (those inside the  tmpVecSmAndFuncList vector) .
            //

            //logger.debug("Size of tmpVecSmAndFuncList::" + Integer.toString(tmpVecSmAndFuncList.size())  );
            for (ReqSensorAndFunctions aTmpVecSmAndFuncList : tmpVecSmAndFuncList) {
                int smid = aTmpVecSmAndFuncList.getSensorModelIdInt();
                int countValuesOfthisSensorModel = 0;

                // TODO : fix to other connectors ->moved vOfSensorValues in the for loop!

                //logger.debug("For mote "+fullMoteId +" and sensor "+Integer.toString(smid) + " function vector size is "+reqFunctionVec.size());
                for (ReqFunctionOverData currentRequestedFunction : reqFunctionVec) {
                    vOfSensorValues = new Vector<ResultAggrStruct>();

                    if (currentRequestedFunction.getfuncId() == ReqFunctionOverData.unknownFuncId) {
                        vOfSensorValues
                                .addElement(new ResultAggrStruct(fullMoteId, smid, "No Result", 1, null));
                        countValuesOfthisSensorModel += 1;
                    } else if (aTmpVecSmAndFuncList.getFunctionsOverSensorModelVec()
                            .contains(currentRequestedFunction.getfuncId())) { // this loop (and this condition) allows to retrieve the valid "functions" to be performed on values of this sensor
                        Vector<VerySimpleSensorMeasurement> mySensorReadingsRelatedToCurrentFunction = new Vector<VerySimpleSensorMeasurement>(); // bugfix: this is now moved inside the functions loop
                        // for each different "function" on the sensor values, we may need to gather completely different values. (e.g. a function could request a history of measurements, or only measurements that are above a threshold)
                        // TODO: Separate cases for binary values (e.g. image or webcam stream) and numeric values  (and string values?)

                        // TODO: for uberdust, loop through all nodes in (http get status vector): allSensorsWithCapsAndMeasures
                        //          and keep the readings, apply functions (FOR NOW WE ALWAYS APPLY LAST READING NO MATTER WHAT)
                        // TODO: Fix -> for now we always apply last reading no matter what the actual function was (since we have no history).
                        // TODO: fix serial numbers for sensor models. They should not just be the hash on the capability simple name...
                        for (VerySimpleSensorEntity tmpSmartDev : allSensorsWithCapsAndMeasures) {
                            if (tmpSmartDev.getSerialID().equalsIgnoreCase(fullMoteId)) // first match based on the requested smart device ID
                            {
                                for (VerySimpleSensorMeasurement tmpMeasurement : tmpSmartDev
                                        .getMeasurementsVector()) {
                                    String obsPropertyIDASUrn = tmpMeasurement.getObservPropertyDef();
                                    String obsPropertySimple = "lalala";
                                    Iterator<String> itDict = dictionaryNameToIDASPhenomenon.keySet()
                                            .iterator();
                                    String tmpSimpleName;
                                    // initial loop to get the "real" simple name for the search key capability (at this poing this is not yet a valid requested sensor)
                                    // we need the simple name because we used it to calculate the sensor model id (with the hashCode() )
                                    // so we get simple names, then calc their hashCodes (turn it into a positive number if it was negative) and then compare it with the requested hashcode (smid)  (assumed positive, because the DVNS will make sure of that)
                                    // logger.debug("^^^^^^^^^^OV: "+ obsPropertyIDASUrn);
                                    while (itDict.hasNext()) {
                                        tmpSimpleName = itDict.next();
                                        //logger.debug("^^^^^^^^^^VS: "+ (dictionaryNameToIDASPhenomenon.get(tmpSimpleName)).toString());

                                        if ((staticprefixPhenomenonIDAS
                                                + (dictionaryNameToIDASPhenomenon.get(tmpSimpleName)))
                                                        .equalsIgnoreCase(obsPropertyIDASUrn)) {
                                            //logger.debug("*** *** *** Found matching capability in dictionary:" + tmpSimpleName);
                                            obsPropertySimple = tmpSimpleName;
                                            break;
                                        }
                                    }

                                    int projectedSmIdforPropertyDef = obsPropertySimple.hashCode();
                                    if (projectedSmIdforPropertyDef < 0) {
                                        projectedSmIdforPropertyDef = projectedSmIdforPropertyDef * (-1);
                                    }

                                    if (smid == projectedSmIdforPropertyDef) {
                                        // debug:
                                        //                                            if((tmpSimpleName.equalsIgnoreCase("switchlight1")
                                        //                                                    ||tmpSimpleName.equalsIgnoreCase("switchlight2")
                                        //                                                    ||tmpSimpleName.equalsIgnoreCase("switchlight3")
                                        //                                                    ||tmpSimpleName.equalsIgnoreCase("switchlight4") )
                                        //                                                &&
                                        //                                            ((smartDevIdsToRooms.get(fullMoteId)!=null) && smartDevIdsToRooms.get(fullMoteId).equalsIgnoreCase("0.I.3")));
                                        //                                                    {
                                        //                                                logger.debug("*** *** *** ADDING A MEASUREMENT FOR: "+ tmpSimpleName + " Mote:" +fullMoteId + "Room: " + smartDevIdsToRooms.get(fullMoteId));
                                        //                                            }
                                        mySensorReadingsRelatedToCurrentFunction.add(tmpMeasurement);

                                        break; // TODO: break since a smartdevice will not have two of the same sensor models. Can it though? in general?
                                    }
                                    //else
                                    //{
                                    //    logger.debug("*** *** *** BUT DOES NOT MATCH A requested sensor: "+ tmpSimpleName);
                                    //}
                                }
                                break; //since we processed  the sensor dev that we wanted.
                            }
                        }

                        //logger.debug("READINGS LENGTH:" + Integer.toString(mySensorReadingsRelatedToCurrentFunction.length) );

                        for (int o = 0; o < mySensorReadingsRelatedToCurrentFunction.size(); o++) {
                            /* TODO: (++++) this could be optimized further (not write the entire data in the vector) / first process it
                             * according to the function.
                             * TODO: Somewhere around here we should handle the History function (not likely for uberdust)
                             */
                            //SensorTypes tmpSensor = jWebTypesManager.selectSensorType(smid);
                            long valueToAdd = -1;
                            //if(tmpSensor.getIsComplex() == false)
                            //{
                            // TODO: here we handle the actuation capabilities for lights as well, if a set value function was requested on them
                            // TODO: if a last value reading was requested we can handle that too by sending their state (as reported)
                            if (mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservPropertyDef()
                                    .equalsIgnoreCase(staticprefixPhenomenonIDAS
                                            + (dictionaryNameToIDASPhenomenon.get("switchlight1")))
                                    || mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                            .getObservPropertyDef()
                                            .equalsIgnoreCase(staticprefixPhenomenonIDAS
                                                    + (dictionaryNameToIDASPhenomenon.get("switchlight2")))
                                    || mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                            .getObservPropertyDef()
                                            .equalsIgnoreCase(staticprefixPhenomenonIDAS
                                                    + (dictionaryNameToIDASPhenomenon.get("switchlight3")))
                                    || mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                            .getObservPropertyDef().equalsIgnoreCase(staticprefixPhenomenonIDAS
                                                    + (dictionaryNameToIDASPhenomenon.get("switchlight4")))) {
                                logger.debug("Function: " + currentRequestedFunction.getfuncName());
                                // TODO: for now we use the threshold field to set the actuation value! Later this could be a separate parameter field
                                if (currentRequestedFunction.getfuncName()
                                        .equalsIgnoreCase(ReqFunctionOverData.setValFunc)
                                        && currentRequestedFunction.getThresholdField() != null
                                        && !currentRequestedFunction.getThresholdField().isEmpty()) {
                                    logger.debug("-------- HANDLING ACTUATION NOW! "
                                            + mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                                    .getObservPropertyDef()
                                            + " room: " + smartDevIdsToRooms.get(fullMoteId) + " mote: "
                                            + fullMoteId + " val: " + mySensorReadingsRelatedToCurrentFunction
                                                    .elementAt(o).getObservOutputMeasurementData()[0]);
                                    ThresholdStructure requiredThresholds = new ThresholdStructure(
                                            currentRequestedFunction.getThresholdField());
                                    if (requiredThresholds.getLowerBound() != null
                                            && !requiredThresholds.getLowerBound().isEmpty()) {
                                        logger.debug("Actuation parameter: "
                                                + requiredThresholds.getLowerBound().trim());
                                        // attempt to set the light to the desired value!
                                        // TODO: check if a valid value (0 or 1)
                                        try {
                                            String valStr = actuateSmartDevToValue(fullMoteId,
                                                    smartDevIdsToRooms.get(fullMoteId),
                                                    getUberdustUrnForIDASCapName(
                                                            mySensorReadingsRelatedToCurrentFunction
                                                                    .elementAt(o).getObservPropertyDef()),
                                                    requiredThresholds.getLowerBound().trim());
                                            double d = Double.parseDouble(valStr);
                                            valueToAdd = (long) d;
                                        } catch (Exception e) {
                                            //logger.debug("*** *** *** OOOOO it's an exception for  ************ "+ mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservOutputMeasurementData()[0]);
                                            valueToAdd = -1;
                                        }

                                    }
                                } else {

                                    if (useTheStandardWayToGetStateForActuatorReading) {
                                        try {
                                            String valStr = mySensorReadingsRelatedToCurrentFunction
                                                    .elementAt(o).getObservOutputMeasurementData()[0];
                                            double d = Double.parseDouble(valStr);
                                            valueToAdd = (long) d;
                                        } catch (Exception e) {
                                            //logger.debug("*** *** *** OOOOO it's an exception for  ************ "+ mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservOutputMeasurementData()[0]);
                                            valueToAdd = -1;
                                        }
                                    } else {
                                        String UberdustUrnForCap = getUberdustUrnForIDASCapName(
                                                mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                                        .getObservPropertyDef());
                                        String justtheCapName = UberdustUrnForCap
                                                .substring(staticprefixUberdustCapability.length());
                                        //TODO: this masking is just for the demo!
                                        //mask light4 capability as light5 in order to show it in the demo: (light4 is not visible from the camera's viewpoint)
                                        // Changed light4 to lz4 to reflect naming change in uberdust
                                        if (justtheCapName.equalsIgnoreCase("lz4"))
                                            justtheCapName = "lz5";
                                        String lineOfStateReading = getLatestReadingTabSepLineForVirtualNode(
                                                fullMoteId, justtheCapName);
                                        String[] lineTokens = lineOfStateReading.split("\\t");
                                        // [0] has the timestamp
                                        // [1] has the value
                                        long valueOfReturnedState;
                                        String observPropertyTSStr;
                                        long theTimeStamp = 0;
                                        try {
                                            double d = Double.parseDouble(lineTokens[1]);
                                            valueOfReturnedState = (long) d;
                                            theTimeStamp = Long.parseLong(lineTokens[0]);
                                            DateFormat df = new SimpleDateFormat(
                                                    "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
                                            observPropertyTSStr = df.format(new Date(theTimeStamp));
                                            logger.debug("Actuator state was: " + lineTokens[1] + " at: "
                                                    + observPropertyTSStr);
                                        } catch (Exception e) {
                                            //logger.debug("*** *** *** OOOOO it's an exception for  ************ "+ mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservOutputMeasurementData()[0]);
                                            DateFormat df = new SimpleDateFormat(
                                                    "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
                                            observPropertyTSStr = df.format(new Date(theTimeStamp));
                                            valueOfReturnedState = -1;

                                        }
                                        mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                                .setObservPropertyTSLong(theTimeStamp);
                                        mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                                .setObservPropertyTSStr(observPropertyTSStr);
                                        mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                                .getObservOutputMeasurementData()[0] = Long
                                                        .toString(valueOfReturnedState);
                                        // todo: move code repetition
                                        try {
                                            String valStr = mySensorReadingsRelatedToCurrentFunction
                                                    .elementAt(o).getObservOutputMeasurementData()[0];
                                            double d = Double.parseDouble(valStr);
                                            valueToAdd = (long) d;
                                        } catch (Exception e) {
                                            //logger.debug("*** *** *** OOOOO it's an exception for  ************ "+ mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservOutputMeasurementData()[0]);
                                            valueToAdd = -1;
                                        }
                                    }
                                }

                            } else {
                                try {
                                    String valStr = mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                            .getObservOutputMeasurementData()[0];
                                    double d = Double.parseDouble(valStr);
                                    valueToAdd = (long) d;
                                } catch (Exception e) {
                                    //logger.debug("*** *** *** OOOOO it's an exception for  ************ "+ mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservOutputMeasurementData()[0]);
                                    valueToAdd = -1;
                                }
                            }

                            long timestampOfReading = mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                    .getObservPropertyTSLong();
                            Timestamp timestampOfReadingSql = new Timestamp(timestampOfReading);
                            vOfSensorValues.addElement(new ResultAggrStruct(fullMoteId, smid,
                                    Long.toString(valueToAdd), 1,
                                    new TimeIntervalStructure(timestampOfReadingSql, timestampOfReadingSql)));
                            //}
                            //                                else// put blob value as a String (FOR NOW this is just a URL to the binary file so this is ok) (++++)
                            //                                    // TODO: later handling of binary data will change and we should send the real binary files over pipes to the client
                            //                                {
                            //                                    vOfSensorValues.addElement(new ResultAggrStruct(fullMoteId,
                            //                                        smid,
                            //                                        new String(myMotesSensorsReadings[o].getComplexRawData()),
                            //                                        1,
                            //                                        new TimeIntervalStructure(myMotesSensorsReadings[o].getDate(),
                            //                                                myMotesSensorsReadings[o].getDate()))
                            //                                    );
                            //                                }

                            countValuesOfthisSensorModel += 1;
                        }
                        if (countValuesOfthisSensorModel == 0) {
                            vOfSensorValues.addElement(new ResultAggrStruct(fullMoteId, smid,
                                    ReqResultOverData.specialValueNoReading, 1, null));
                            countValuesOfthisSensorModel += 1;
                        } else
                            logger.debug("Counted Values of this sensor: " + fullMoteId + " "
                                    + Integer.toString(countValuesOfthisSensorModel));
                    }
                    // this condition checks that at least one value was retrieved from the sensor and used in the function (even if that value was "no result")
                    if (countValuesOfthisSensorModel > 0) // we should make sure that this is always true.
                    {
                        retVecofResults.addElement(
                                new ReqResultOverData(currentRequestedFunction.getfuncId(), vOfSensorValues));
                    }
                } // ends the block where we gather values of a sensor for a specific function
            } // ends the loop over the requested sensor Models (capabilities) of the current requested Smart Device
        } catch (Exception e) {
            e.printStackTrace();
        }
    } // end of for loop over all requested Smart Devices in the request vector

    // here we have a Vector filled with ResultAggrStruct

    // END OF LEGACY driver code

    return retVecofResults;
}

From source file:alter.vitro.vgw.wsiadapter.WsiUberDustCon.java

/**
 * Should translate the aggregated query to the appropriate type according to the middleware underneath
 * and return appropriate readings/values. The type of values should be stored elsewhere (at the VITRO Service Provider (VSP) !
 * TODO: NOTICE: for now the SensorModelId here is a hashcode of the simple name of the capability (resource name) .
 * TODO: ^^^^^^^ It is also always positive so multiply by *(-1) if it is not.
 * TODO: ^^^^^^^ This is inconsistent with what we sent for uberdust in the registration
 * TODO: ^^^^^^^ because for now the framework will ignore this setting on the RegisterSensor messages, and will just calculate it on its own from the hashcode of the resource names.
 *
 *
 *
 * @param motesAndTheirSensorAndFunctsVec Vector of moteIds mapped to their sensors (those of them that are involved in the query) and the requested function ids
 * @param reqFunctionVec        Vector with Functions to be applied to query data
 * @return a Vector of the Results as ReqResultOverData structures (XML)
 *//*from w ww  .  ja v a  2  s.co m*/
// TODO: Important
public synchronized Vector<ReqResultOverData> translateAggrQuery(
        Vector<QueriedMoteAndSensors> motesAndTheirSensorAndFunctsVec,
        Vector<ReqFunctionOverData> reqFunctionVec) {

    if (DEBUG_OFFLINE_MODE) {
        return DEBUG_offline_translateAggrQuery(motesAndTheirSensorAndFunctsVec, reqFunctionVec);
    }

    boolean useTheStandardWayToGetStateForActuatorReading = false; // false allows to get explicitly the latest reading instead of the standard one (in the general status page) updated every few minutes
    // TODO: tmp structure to be replaced when this is tested and works
    Vector<VerySimpleSensorEntity> allSensorsWithCapsAndMeasures = new Vector<VerySimpleSensorEntity>();
    // Maps Smart Device ids to Room names, where the room names are available.
    HashMap<String, String> smartDevIdsToRooms = new HashMap<String, String>();
    //
    // ##########################################################################################################################################
    //
    /*
     * TODO: optimize the status/resource retrieval process for uberdust!
     * TODO: Take into account the mote status before ADDING it to the gateway description list (++++ LATER)
     *         For now we assume that the queries to the actual WSN are handled by the middleware.
     *         We search the uberdust "database" for data. (but we can still perform actions to affect the WSI!)
     *
     *         The plan is for a future service
     *         where a peer could submit queries for submission in the actual WSNs, and subsequently gather the data
     *         of the results. (e.g. administration service>reprogramming service)
     */
    HttpClient httpclient = new DefaultHttpClient();
    try {
        //
        //
        // TODO: x, y, z can be used with wisedb Coordinate.java (look code) to produce GoogleEarth Coordinates (what ISO is that? Can it be advertised in SensorML for IDAS ?)
        // TODO: make use of Description and Type and Room Fields when available ?
        // TODO: Make a summary, how many valid from those found in uberdust? How many were registered successfully? How many measurements were registered successfully?
        //
        //
        HttpGet httpUberdustNodesGet = new HttpGet(uberdustNodesGetRestUri);
        HttpResponse httpUberdustNodesGetResponse = httpclient.execute(httpUberdustNodesGet);

        int httpUberdustNodesGetResponseStatusCode = httpUberdustNodesGetResponse.getStatusLine()
                .getStatusCode();
        HttpEntity httpUberdustNodesGetResponseEntity = httpUberdustNodesGetResponse.getEntity();
        if (httpUberdustNodesGetResponseEntity != null) {

            String responseBodyStr = EntityUtils.toString(httpUberdustNodesGetResponseEntity);
            if (httpUberdustNodesGetResponseStatusCode != 200) {
                // responseBody will have the error response
                logger.debug("--------ERROR Response: " + httpUberdustNodesGetResponseStatusCode
                        + "------------------------------");
                logger.debug(responseBodyStr);
                logger.debug("----------------------------------------");
            } else {
                //logger.debug("--------OK Response: "+ httpUberdustNodesGetResponseStatusCode+"------------------------------");
                //
                String[] nodeUrnsInUberdust = responseBodyStr.split("\\r?\\n");
                int totalNodeUrnsInUberdust = nodeUrnsInUberdust.length;

                HttpGet httpUberdustNodes_STATUS_Get = new HttpGet(uberdustNodes_Status_GetRestUri);
                HttpResponse httpUberdustNodes_STATUS_GetResponse = httpclient
                        .execute(httpUberdustNodes_STATUS_Get);

                int httpUberdustNodes_STATUS_GetResponseStatusCode = httpUberdustNodes_STATUS_GetResponse
                        .getStatusLine().getStatusCode();
                HttpEntity httpUberdustNodes_STATUS_GetResponseEntity = httpUberdustNodes_STATUS_GetResponse
                        .getEntity();
                if (httpUberdustNodes_STATUS_GetResponseEntity != null) {
                    String responseBody_STATUS_Str = EntityUtils
                            .toString(httpUberdustNodes_STATUS_GetResponseEntity);
                    if (httpUberdustNodes_STATUS_GetResponseStatusCode != 200) {
                        // responseBody_STATUS_Str will have the error response
                        logger.debug("--------ERROR Response: " + httpUberdustNodes_STATUS_GetResponseStatusCode
                                + "------------------------------");
                        logger.debug(responseBody_STATUS_Str);
                        logger.debug("----------------------------------------");
                    } else {
                        //logger.debug("--------OK Response: "+ httpUberdustNodes_STATUS_GetResponseStatusCode+"------------------------------");

                        String[] nodeAndLastCapReadingsUrnsInUberdust = responseBody_STATUS_Str
                                .split("\\r?\\n");
                        int totalNodeWithCapsInUberdust = nodeAndLastCapReadingsUrnsInUberdust.length;

                        // LOOP OVER EVERY NODE (smart device), and for each node, get its capabilities from the second response (responseBody_STATUS_Str)
                        Vector<String> allFaultyNodesUrns = getFaultyNodes();

                        for (String aNodeUrnsInUberdust : nodeUrnsInUberdust) {
                            if (allFaultyNodesUrns.contains(aNodeUrnsInUberdust)) {
                                logger.debug("Skiipping node: " + aNodeUrnsInUberdust);
                                continue;
                            }
                            Vector<VerySimpleObservationCapabilities> sensObsCapsVector = new Vector<VerySimpleObservationCapabilities>();
                            Vector<VerySimpleSensorMeasurement> sensObsMeasurementVector = new Vector<VerySimpleSensorMeasurement>();

                            if (VitroGatewayService.getVitroGatewayService().getAssignedGatewayUniqueIdFromReg()
                                    .equalsIgnoreCase("vitrogw_hai")) {
                                aNodeUrnsInUberdust = dictionaryUberdustUrnToHaiUrnName
                                        .get(aNodeUrnsInUberdust);
                                if (aNodeUrnsInUberdust == null)
                                    continue;
                            }

                            //                                logger.debug("Iteration " + String.valueOf(k+1) + " of " + String.valueOf(totalNodeUrnsInUberdust));
                            //                                logger.debug(nodeUrnsInUberdust[k]);

                            Vector<Integer> sensorModels_IDs_OfSmartDevVector = new Vector<Integer>();
                            CSmartDevice tmpSmartDev = new CSmartDevice(aNodeUrnsInUberdust,
                                    "", /* smart device type name */
                                    "", /* location description e.g. room1*/
                                    new GeodesicPoint(), /*  */
                                    sensorModels_IDs_OfSmartDevVector);

                            // TODO: Add an extra early for loop to update the fields for the attributes of the SmartDevice such as:
                            //      Eventually if the SmartDev has NO other valid sensors (e.g. observation sensors or actuators) then it won't be added !
                            String tmp_longitude = "";
                            String tmp_latitude = "";
                            String tmp_altitude = "";

                            for (String aNodeAndLastCapReadingsUrnsInUberdust1 : nodeAndLastCapReadingsUrnsInUberdust) {
                                //to update the device attributes!
                                String[] nodeCapAndReadingRowItems = aNodeAndLastCapReadingsUrnsInUberdust1
                                        .split("\\t");
                                if (VitroGatewayService.getVitroGatewayService()
                                        .getAssignedGatewayUniqueIdFromReg().equalsIgnoreCase("vitrogw_hai")) {
                                    nodeCapAndReadingRowItems[0] = dictionaryUberdustUrnToHaiUrnName
                                            .get(nodeCapAndReadingRowItems[0]);
                                    if (nodeCapAndReadingRowItems[0] == null)
                                        continue;
                                }

                                if (nodeCapAndReadingRowItems.length > 3 && nodeCapAndReadingRowItems[0]
                                        .compareToIgnoreCase(aNodeUrnsInUberdust) == 0) //we are at the capabilities of the current smartdevice
                                {
                                    // [0] is mote (smart device) id
                                    // [1] is capability
                                    // [2] is timestamp
                                    // [3] is measurement value
                                    if ((nodeCapAndReadingRowItems[1] != null)
                                            && !(nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase(""))) {
                                        if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("room")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            tmpSmartDev.setLocationDesc(nodeCapAndReadingRowItems[3].trim());
                                            smartDevIdsToRooms.put(tmpSmartDev.getId(),
                                                    tmpSmartDev.getLocationDesc());
                                        } else if (nodeCapAndReadingRowItems[1].trim()
                                                .equalsIgnoreCase("nodetype")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            tmpSmartDev.setName(nodeCapAndReadingRowItems[3].trim());
                                        } else if (nodeCapAndReadingRowItems[1].trim()
                                                .equalsIgnoreCase("description")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //TODO: do we need this?

                                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("x")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //TODO: we need the function to derive a valid longitude from the uberdust value (pending)
                                            tmp_longitude = nodeCapAndReadingRowItems[3].trim();
                                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("y")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //TODO: we need the function to derive a valid latitude)
                                            tmp_latitude = nodeCapAndReadingRowItems[3].trim();
                                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("z")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //altitude is in meters (assumption)
                                            tmp_altitude = nodeCapAndReadingRowItems[3].trim();

                                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("phi")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //TODO: do we need this?
                                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("theta")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //TODO: do we need this?
                                        }
                                    }
                                }
                            } // end of first round of for loop for attributes
                            if (!tmp_latitude.equalsIgnoreCase("") && !tmp_longitude.equalsIgnoreCase("")
                                    && !tmp_altitude.equalsIgnoreCase("")) {
                                tmpSmartDev.setGplocation(
                                        new GeodesicPoint(tmp_latitude, tmp_longitude, tmp_altitude));
                            }

                            //
                            // Again same loop for measurement and actuation capabilities!
                            //
                            for (String aNodeAndLastCapReadingsUrnsInUberdust : nodeAndLastCapReadingsUrnsInUberdust) {
                                String[] nodeCapAndReadingRowItems = aNodeAndLastCapReadingsUrnsInUberdust
                                        .split("\\t");
                                if (VitroGatewayService.getVitroGatewayService()
                                        .getAssignedGatewayUniqueIdFromReg().equalsIgnoreCase("vitrogw_hai")) {
                                    nodeCapAndReadingRowItems[0] = dictionaryUberdustUrnToHaiUrnName
                                            .get(nodeCapAndReadingRowItems[0]);
                                    if (nodeCapAndReadingRowItems[0] == null)
                                        continue;
                                }
                                if (nodeCapAndReadingRowItems.length > 3 && nodeCapAndReadingRowItems[0]
                                        .compareToIgnoreCase(aNodeUrnsInUberdust) == 0) //we are at the capabilities of the current smartdevice
                                {
                                    // [0] is mote (smart device) id
                                    // [1] is capability
                                    // [2] is measurement value
                                    // [3] is timestamp
                                    //                                        logger.debug(nodeCapAndReadingRowItems[1]);
                                    // TODO: FILTER OUT UNSUPPORTED OR COMPLEX CAPABILITIES!!!!
                                    // Since uberdust does not distinguish currenlty between sensing/actuating capabilities and properties, we need to filter out manually
                                    // everything we don't consider a sensing/actuating capability.
                                    // Another filtering out is done at a later stage with the SensorMLMessageAdapter, which will filter out the capabilities not supported by IDAS
                                    // TODO: it could be nice to have this filtering unified.
                                    if ((nodeCapAndReadingRowItems[1] != null)
                                            && (nodeCapAndReadingRowItems[1].trim().compareTo("") != 0)
                                            && !getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim())
                                                    .equalsIgnoreCase("UnknownPhenomenon")) {
                                        // The model id is set as the hashcode of the capability name appended with the model type of the device.
                                        // Perhaps this should be changed to something MORE specific
                                        // TODO: the units should be set here as we know them for Uberdust. Create a small dictionary to set them!
                                        // TODO: the non-observation sensors/ non-actuation should be filtered here!! the Name for the others should be "UnknownPhenomenon"

                                        String tmpGenericCapabilityForSensor = getSimpleCapForUberdustUrn(
                                                nodeCapAndReadingRowItems[1].trim());
                                        Integer thedigestInt = (tmpGenericCapabilityForSensor + "-"
                                                + tmpSmartDev.getName()).hashCode();
                                        if (thedigestInt < 0)
                                            thedigestInt = thedigestInt * (-1);

                                        //                                            /*
                                        //                                            CSensorModel tmpSensorModel = new CSensorModel(givGatewayInfo.getId(), /*Gateway Id*/
                                        //                                                    thedigestInt, /*Sensor Model Id */
                                        //                                                    (tmpGenericCapabilityForSensor + "-" + tmpSmartDev.getName()), /* Sensor Model name */
                                        //                                                    CSensorModel.numericDataType, /* Data type*/  // TODO: later on this should be adjustable!!!
                                        //                                                    CSensorModel.defaultAccuracy, /* Accuracy */
                                        //                                                    CSensorModel.defaultUnits) /* Units */;  // TODO: this should be set when it is known!!!

                                        // GET THE OBSERVATION
                                        VerySimpleObservationCapabilities tmpObsCap = new VerySimpleObservationCapabilities(
                                                nodeCapAndReadingRowItems[1], true);
                                        if ((tmpObsCap.getSimpleName() != null) && !(tmpObsCap.getSimpleName()
                                                .equalsIgnoreCase("UnknownPhenomenon"))) {
                                            sensObsCapsVector.add(tmpObsCap);

                                            // ts of measurement in place [2]
                                            // value of measurement in place [3]
                                            // logger.debug(nodeCapAndReadingRowItems[2]+'\t'+nodeCapAndReadingRowItems[3]);
                                            long theTimeStamp = Long.parseLong(nodeCapAndReadingRowItems[2]);
                                            String theValue = nodeCapAndReadingRowItems[3];
                                            if (theValue.contains(" "))
                                                theValue = theValue.split(" ")[0]; // if it contains the UOM as a suffix,then just keep the first part
                                            String observPropertyDef = tmpObsCap.getPhenomenonIDASUrn();
                                            String observOutputUOMCode = tmpObsCap.getUomIDASUrn();// tmpObsCap.getUomIDASCode();
                                            // just one (last) value
                                            String[] observOutputMeasurementData = new String[1];
                                            // Dummy measurement value
                                            if (tmpObsCap.getSimpleName().equalsIgnoreCase("temperature")) {
                                                //since we assume kelvin to be the default UOM, until fixed, wew set oiur ceslious to Kelvin here:
                                                //K = C+273 . TODO. Later on this normalization should be done at the VSP!
                                                double d = Double.parseDouble(theValue);
                                                double convertedKelvinValue = d + 273.0;
                                                String convertedKelvinValueStr = Long
                                                        .toString((long) convertedKelvinValue);

                                                observOutputMeasurementData[0] = convertedKelvinValueStr; //to kelvin
                                            } else {
                                                observOutputMeasurementData[0] = theValue;
                                            }
                                            // TODO: Check if timezone is correct!
                                            // FOR UBERDUST: override sensors timestamp with reply from uberdust timestamp (now)
                                            Date dateNow = new Date();
                                            theTimeStamp = dateNow.getTime();
                                            DateFormat df = new SimpleDateFormat(
                                                    "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
                                            String observPropertyTSStr = df.format(new Date(theTimeStamp));
                                            //logger.debug("---- " + observPropertyTSStr + " ---- " + theValue + " ----------------------------");
                                            sensObsMeasurementVector
                                                    .add(new VerySimpleSensorMeasurement(observPropertyDef,
                                                            observOutputUOMCode, observOutputMeasurementData,
                                                            observPropertyTSStr, theTimeStamp));
                                        }

                                        sensorModels_IDs_OfSmartDevVector.add(thedigestInt);

                                    }
                                }
                            }
                            if (!sensorModels_IDs_OfSmartDevVector.isEmpty()) {
                                // TODO: FILTER OUT UNSUPPORTED OR COMPLEX NODES!!!!
                                VerySimpleSensorEntity sens01 = new VerySimpleSensorEntity(aNodeUrnsInUberdust,
                                        sensObsCapsVector, sensObsMeasurementVector);
                                allSensorsWithCapsAndMeasures.add(sens01);

                                // TODO: MAYBE HERE WE CAN CHECK IF THESE MEASUREMENTS are for the results??
                                //if (!sensObsMeasurementVector.isEmpty())
                                //{
                                //    Iterator<VerySimpleSensorMeasurement> it1 = sensObsMeasurementVector.iterator();
                                //    while( it1.hasNext())
                                //    {
                                //        VerySimpleSensorMeasurement sensorMeasurement = (VerySimpleSensorMeasurement)it1.next();
                                //
                                //    }
                                //}
                                //#####################################
                            }
                        } // ends for loop over all smartdevices discovered!
                    } //if GET STATUS response code is OK!
                } // if GET STATUS response entity is NOT null
            } //if get list of nodes replied validly
        } //if get list of nodes response entity is NOT null
    } catch (Exception e) {
        logger.debug("error::" + e.getMessage());

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate de-allocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }

    //
    // TILL HERE WE HAVE A VECTOR WITH ALL Devices and Capabilities and Measurements: allSensorsWithCapsAndMeasures
    //
    //
    // LEGACY CODE
    //
    //
    //  TODO: This vector works now, but if we integrate components for sensors, we will need the real sensormodelIds to be registered by IDAS (DVNS),
    // (for now, only the hashcode of simple capabilities are considered to be the sensormodelids,(and this "hashcode" is set to always be a positive value, so if it is negative multiply it by (-1))
    //          thus forcing all sensor models that measure the same phenomenon to be considered identical .
    //
    // Q: a MAX function means maximum among readings from a sensor of a specific mote (aggregation)
    // or among readings from ALL motes (aggregation in a higher level)
    // A: We should actually interpret it as both. Since we have a timeperiod tag in the Function XML
    //    we can assume that we want the MAX over the values we took within that specified timeperiod (from all desired motes and all readings of their corresponding sensor)
    //    e.g.  In the period (Mon to Wed) mote-1 can have 20 light values from its light sensor, mote-2 can have 34 values etc. We calculate the max over all these.
    //    However if no timeperiod tag is set, we should assume that we get the MAX over only the Latest Value per mote, and probably in relatively current timewindow (e.g. from now() until a few hours ago)
    //    e.g. mote-1's latest light value is x1, mote-2 latest light value is x2 etc., and we calculate the max over just these values.
    //
    // We have a Vectors of functions to be applied and
    // a HashMap of SmartDevices (motes) mapped to those of their sensors that should be "queries"
    //
    // The steps are::
    // 1. For each mote ID:
    //        get each sensor ID and get all readings of mote.
    // 2. Fill in a Vector of ResultAggrStucts and send them with the Vector of Functions to executeFuctOverData for each function.
    //     This array is used because we want a generic format to send data to the function
    //     and that is the XML structure of the ResultAggrStruct.
    //     Each entry in the array has a mid, sid, (datatype), value AND timeperiod (from=to=timestamp) from database).
    // 3. Return a Vector of ReqResultOverData
    //
    // Note: Levels of aggregations should be:
    //       1. Values of motes specific sensor model (from light sensor 3006) (within a period of time, or all readings from the beginning of time!!)
    //       2. Values of all motes with the same sensor model   (aggragate over the results of step 1, over the involved motes)
    //       3. Values of all motes with sensor model that belongs to the same generic capability (e.g. light sensor 2000 and light sensor 3006 ( aggregate over the results of step 2)
    //       4. Values among gateways (this can be done only at the user peer).
    //
    // !! Note: No Longer Needed: --> Parse the moteID and extract WSN_ID and SMARTDEVICE_ID info (the format is WSN_ID::SMARTDEVICE_ID)
    //
    Vector<ResultAggrStruct> vOfSensorValues;

    Vector<ReqResultOverData> retVecofResults;
    retVecofResults = new Vector<ReqResultOverData>();

    //logger.debug("Size of motesAndTheirSensorAndFunctsVec::" + Integer.toString(motesAndTheirSensorAndFunctsVec.size())  );

    for (int i = 0; i < motesAndTheirSensorAndFunctsVec.size(); i++) {

        String fullMoteId = motesAndTheirSensorAndFunctsVec.elementAt(i).getMoteid();

        // for each entry, get the vector of queried sensor types and the functions to be applied to the measurements.
        List<ReqSensorAndFunctions> tmpVecSmAndFuncList = motesAndTheirSensorAndFunctsVec.elementAt(i)
                .getQueriedSensorIdsAndFuncVec();
        //            Vector<Integer> tmpVecSmIds =  motesAndTheirSensorHM.get(fullMoteId);
        /**
         *
         *  TODO: So far we assume all of the data types in measurements to be Long! This should be fixed!!!
         *
         */
        try {

            //
            // We have the readings from all sensors.
            // we must select only the readings from the specific sensors of interest (those inside the  tmpVecSmAndFuncList vector) .
            //

            //logger.debug("Size of tmpVecSmAndFuncList::" + Integer.toString(tmpVecSmAndFuncList.size())  );
            for (ReqSensorAndFunctions currentSensorModel : tmpVecSmAndFuncList) {
                int smid = currentSensorModel.getSensorModelIdInt();
                int countValuesOfthisSensorModel = 0;

                // TODO : fix to other connectors ->moved vOfSensorValues in the for loop!

                //logger.debug("For mote "+fullMoteId +" and sensor "+Integer.toString(smid) + " function vector size is "+reqFunctionVec.size());
                for (ReqFunctionOverData currentRequestedFunction : reqFunctionVec) {
                    vOfSensorValues = new Vector<ResultAggrStruct>();

                    if (currentRequestedFunction.getfuncId() == ReqFunctionOverData.unknownFuncId) {
                        vOfSensorValues
                                .addElement(new ResultAggrStruct(fullMoteId, smid, "No Result", 1, null));
                        countValuesOfthisSensorModel += 1;
                    } else if (currentSensorModel.getFunctionsOverSensorModelVec()
                            .contains(currentRequestedFunction.getfuncId())) { // this loop (and this condition) allows to retrieve the valid "functions" to be performed on values of this sensor
                        Vector<VerySimpleSensorMeasurement> mySensorReadingsRelatedToCurrentFunction = new Vector<VerySimpleSensorMeasurement>(); // bugfix: this is now moved inside the functions loop
                        // for each different "function" on the sensor values, we may need to gather completely different values. (e.g. a function could request a history of measurements, or only measurements that are above a threshold)
                        // TODO: Separate cases for binary values (e.g. image or webcam stream) and numeric values  (and string values?)

                        // TODO: for uberdust, loop through all nodes in (http get status vector): allSensorsWithCapsAndMeasures
                        //          and keep the readings, apply functions (FOR NOW WE ALWAYS APPLY LAST READING NO MATTER WHAT)
                        // TODO: Fix -> for now we always apply last reading no matter what the actual function was (since we have no history).
                        // TODO: fix serial numbers for sensor models. They should not just be the hash on the capability simple name...
                        for (VerySimpleSensorEntity tmpSmartDev : allSensorsWithCapsAndMeasures) {
                            if (tmpSmartDev.getSerialID().equalsIgnoreCase(fullMoteId)) // first match based on the requested smart device ID
                            {
                                for (VerySimpleSensorMeasurement tmpMeasurement : tmpSmartDev
                                        .getMeasurementsVector()) {
                                    String obsPropertyIDASUrn = tmpMeasurement.getObservPropertyDef();
                                    String obsPropertySimple = "lalala";
                                    Iterator<String> itDict = dictionaryNameToIDASPhenomenon.keySet()
                                            .iterator();
                                    String tmpSimpleName;
                                    // initial loop to get the "real" simple name for the search key capability (at this poing this is not yet a valid requested sensor)
                                    // we need the simple name because we used it to calculate the sensor model id (with the hashCode() )
                                    // so we get simple names, then calc their hashCodes (turn it into a positive number if it was negative) and then compare it with the requested hashcode (smid)  (assumed positive, because the DVNS will make sure of that)
                                    // logger.debug("^^^^^^^^^^OV: "+ obsPropertyIDASUrn);
                                    while (itDict.hasNext()) {
                                        tmpSimpleName = itDict.next();
                                        //logger.debug("^^^^^^^^^^VS: "+ (dictionaryNameToIDASPhenomenon.get(tmpSimpleName)).toString());

                                        if ((staticprefixPhenomenonIDAS
                                                + (dictionaryNameToIDASPhenomenon.get(tmpSimpleName)))
                                                        .equalsIgnoreCase(obsPropertyIDASUrn)) {
                                            //logger.debug("*** *** *** Found matching capability in dictionary:" + tmpSimpleName);
                                            obsPropertySimple = tmpSimpleName;
                                            break;
                                        }
                                    }

                                    int projectedSmIdforPropertyDef = obsPropertySimple.hashCode();
                                    if (projectedSmIdforPropertyDef < 0) {
                                        projectedSmIdforPropertyDef = projectedSmIdforPropertyDef * (-1);
                                    }

                                    if (smid == projectedSmIdforPropertyDef) {
                                        // debug:
                                        //                                            if((tmpSimpleName.equalsIgnoreCase("switchlight1")
                                        //                                                    ||tmpSimpleName.equalsIgnoreCase("switchlight2")
                                        //                                                    ||tmpSimpleName.equalsIgnoreCase("switchlight3")
                                        //                                                    ||tmpSimpleName.equalsIgnoreCase("switchlight4") )
                                        //                                                &&
                                        //                                            ((smartDevIdsToRooms.get(fullMoteId)!=null) && smartDevIdsToRooms.get(fullMoteId).equalsIgnoreCase("0.I.3")));
                                        //                                                    {
                                        //                                                logger.debug("*** *** *** ADDING A MEASUREMENT FOR: "+ tmpSimpleName + " Mote:" +fullMoteId + "Room: " + smartDevIdsToRooms.get(fullMoteId));
                                        //                                            }
                                        mySensorReadingsRelatedToCurrentFunction.add(tmpMeasurement);

                                        break; // TODO: break since a smartdevice will not have two of the same sensor models. Can it though? in general?
                                    }
                                    //else
                                    //{
                                    //    logger.debug("*** *** *** BUT DOES NOT MATCH A requested sensor: "+ tmpSimpleName);
                                    //}
                                }
                                break; //since we processed  the sensor dev that we wanted.
                            }
                        }

                        //logger.debug("READINGS LENGTH:" + Integer.toString(mySensorReadingsRelatedToCurrentFunction.length) );

                        for (int o = 0; o < mySensorReadingsRelatedToCurrentFunction.size(); o++) {
                            /* TODO: (++++) this could be optimized further (not write the entire data in the vector) / first process it
                             * according to the function.
                             * TODO: Somewhere around here we should handle the History function (not likely for uberdust)
                             */
                            //SensorTypes tmpSensor = jWebTypesManager.selectSensorType(smid);
                            long valueToAdd = -1;
                            //if(tmpSensor.getIsComplex() == false)
                            //{
                            // TODO: here we handle the actuation capabilities for lights as well, if a set value function was requested on them
                            // TODO: if a last value reading was requested we can handle that too by sending their state (as reported)
                            if (mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservPropertyDef()
                                    .equalsIgnoreCase(staticprefixPhenomenonIDAS
                                            + (dictionaryNameToIDASPhenomenon.get("switchlight1")))
                                    || mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                            .getObservPropertyDef()
                                            .equalsIgnoreCase(staticprefixPhenomenonIDAS
                                                    + (dictionaryNameToIDASPhenomenon.get("switchlight2")))
                                    || mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                            .getObservPropertyDef()
                                            .equalsIgnoreCase(staticprefixPhenomenonIDAS
                                                    + (dictionaryNameToIDASPhenomenon.get("switchlight3")))
                                    || mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                            .getObservPropertyDef().equalsIgnoreCase(staticprefixPhenomenonIDAS
                                                    + (dictionaryNameToIDASPhenomenon.get("switchlight4")))) {
                                logger.debug("Function: " + currentRequestedFunction.getfuncName());
                                // TODO: for now we use the threshold field to set the actuation value! Later this could be a separate parameter field
                                if (currentRequestedFunction.getfuncName()
                                        .equalsIgnoreCase(ReqFunctionOverData.setValFunc)
                                        && currentRequestedFunction.getThresholdField() != null
                                        && !currentRequestedFunction.getThresholdField().isEmpty()) {
                                    logger.debug("-------- HANDLING ACTUATION NOW! "
                                            + mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                                    .getObservPropertyDef()
                                            + " room: " + smartDevIdsToRooms.get(fullMoteId) + " mote: "
                                            + fullMoteId + " val: " + mySensorReadingsRelatedToCurrentFunction
                                                    .elementAt(o).getObservOutputMeasurementData()[0]);
                                    ThresholdStructure requiredThresholds = new ThresholdStructure(
                                            currentRequestedFunction.getThresholdField());
                                    if (requiredThresholds.getLowerBound() != null
                                            && !requiredThresholds.getLowerBound().isEmpty()) {
                                        logger.debug("Actuation parameter: "
                                                + requiredThresholds.getLowerBound().trim());
                                        // attempt to set the light to the desired value!
                                        // TODO: check if a valid value (0 or 1)
                                        try {
                                            String valStr = actuateSmartDevToValue(fullMoteId,
                                                    smartDevIdsToRooms.get(fullMoteId),
                                                    getUberdustUrnForIDASCapName(
                                                            mySensorReadingsRelatedToCurrentFunction
                                                                    .elementAt(o).getObservPropertyDef()),
                                                    requiredThresholds.getLowerBound().trim());
                                            double d = Double.parseDouble(valStr);
                                            valueToAdd = (long) d;
                                        } catch (Exception e) {
                                            //logger.debug("*** *** *** OOOOO it's an exception for  ************ "+ mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservOutputMeasurementData()[0]);
                                            valueToAdd = -1;
                                        }

                                    }
                                } else {

                                    if (useTheStandardWayToGetStateForActuatorReading) {
                                        try {
                                            String valStr = mySensorReadingsRelatedToCurrentFunction
                                                    .elementAt(o).getObservOutputMeasurementData()[0];
                                            double d = Double.parseDouble(valStr);
                                            valueToAdd = (long) d;
                                        } catch (Exception e) {
                                            //logger.debug("*** *** *** OOOOO it's an exception for  ************ "+ mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservOutputMeasurementData()[0]);
                                            valueToAdd = -1;
                                        }
                                    } else {
                                        String UberdustUrnForCap = getUberdustUrnForIDASCapName(
                                                mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                                        .getObservPropertyDef());
                                        String justtheCapName = UberdustUrnForCap
                                                .substring(staticprefixUberdustCapability.length());
                                        //TODO: this masking is just for the demo!
                                        //mask light4 capability as light5 in order to show it in the demo: (light4 is not visible from the camera's viewpoint)
                                        // Changed light4 to lz4 to reflect naming change in uberdust
                                        if (justtheCapName.equalsIgnoreCase("lz4"))
                                            justtheCapName = "lz5";
                                        String lineOfStateReading = getLatestReadingTabSepLineForVirtualNode(
                                                fullMoteId, justtheCapName);
                                        String[] lineTokens = lineOfStateReading.split("\\t");
                                        // [0] has the timestamp
                                        // [1] has the value
                                        long valueOfReturnedState;
                                        String observPropertyTSStr;
                                        long theTimeStamp = 0;
                                        try {
                                            double d = Double.parseDouble(lineTokens[1]);
                                            valueOfReturnedState = (long) d;
                                            theTimeStamp = Long.parseLong(lineTokens[0]);
                                            DateFormat df = new SimpleDateFormat(
                                                    "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
                                            observPropertyTSStr = df.format(new Date(theTimeStamp));
                                            logger.debug("Actuator state was: " + lineTokens[1] + " at: "
                                                    + observPropertyTSStr);
                                        } catch (Exception e) {
                                            //logger.debug("*** *** *** OOOOO it's an exception for  ************ "+ mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservOutputMeasurementData()[0]);
                                            DateFormat df = new SimpleDateFormat(
                                                    "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
                                            observPropertyTSStr = df.format(new Date(theTimeStamp));
                                            valueOfReturnedState = -1;

                                        }
                                        mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                                .setObservPropertyTSLong(theTimeStamp);
                                        mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                                .setObservPropertyTSStr(observPropertyTSStr);
                                        mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                                .getObservOutputMeasurementData()[0] = Long
                                                        .toString(valueOfReturnedState);
                                        // todo: move code repetition
                                        try {
                                            String valStr = mySensorReadingsRelatedToCurrentFunction
                                                    .elementAt(o).getObservOutputMeasurementData()[0];
                                            double d = Double.parseDouble(valStr);
                                            valueToAdd = (long) d;
                                        } catch (Exception e) {
                                            //logger.debug("*** *** *** OOOOO it's an exception for  ************ "+ mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservOutputMeasurementData()[0]);
                                            valueToAdd = -1;
                                        }
                                    }
                                }

                            } else {
                                try {
                                    String valStr = mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                            .getObservOutputMeasurementData()[0];
                                    double d = Double.parseDouble(valStr);
                                    valueToAdd = (long) d;
                                } catch (Exception e) {
                                    //logger.debug("*** *** *** OOOOO it's an exception for  ************ "+ mySensorReadingsRelatedToCurrentFunction.elementAt(o).getObservOutputMeasurementData()[0]);
                                    valueToAdd = -1;
                                }
                            }

                            long timestampOfReading = mySensorReadingsRelatedToCurrentFunction.elementAt(o)
                                    .getObservPropertyTSLong();
                            Timestamp timestampOfReadingSql = new Timestamp(timestampOfReading);
                            vOfSensorValues.addElement(new ResultAggrStruct(fullMoteId, smid,
                                    Long.toString(valueToAdd), 1,
                                    new TimeIntervalStructure(timestampOfReadingSql, timestampOfReadingSql)));
                            //}
                            //                                else// put blob value as a String (FOR NOW this is just a URL to the binary file so this is ok) (++++)
                            //                                    // TODO: later handling of binary data will change and we should send the real binary files over pipes to the client
                            //                                {
                            //                                    vOfSensorValues.addElement(new ResultAggrStruct(fullMoteId,
                            //                                        smid,
                            //                                        new String(myMotesSensorsReadings[o].getComplexRawData()),
                            //                                        1,
                            //                                        new TimeIntervalStructure(myMotesSensorsReadings[o].getDate(),
                            //                                                myMotesSensorsReadings[o].getDate()))
                            //                                    );
                            //                                }

                            countValuesOfthisSensorModel += 1;
                        }
                        if (countValuesOfthisSensorModel == 0) {
                            vOfSensorValues.addElement(new ResultAggrStruct(fullMoteId, smid,
                                    ReqResultOverData.specialValueNoReading, 1, null));
                            countValuesOfthisSensorModel += 1;
                        } else
                            logger.debug("Counted Values of this sensor: " + fullMoteId + " "
                                    + Integer.toString(countValuesOfthisSensorModel));
                    }
                    // this condition checks that at least one value was retrieved from the sensor and used in the function (even if that value was "no result")
                    if (countValuesOfthisSensorModel > 0) // we should make sure that this is always true.
                    {
                        retVecofResults.addElement(
                                new ReqResultOverData(currentRequestedFunction.getfuncId(), vOfSensorValues));
                    }
                } // ends the block where we gather values of a sensor for a specific function
            } // ends the loop over the requested sensor Models (capabilities) of the current requested Smart Device
        } catch (Exception e) {
            e.printStackTrace();
        }
    } // end of for loop over all requested Smart Devices in the request vector

    // here we have a Vector filled with ResultAggrStruct

    // END OF LEGACY driver code

    return retVecofResults;
}