Example usage for java.util Vector iterator

List of usage examples for java.util Vector iterator

Introduction

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

Prototype

public synchronized Iterator<E> iterator() 

Source Link

Document

Returns an iterator over the elements in this list in proper sequence.

Usage

From source file:com.flexoodb.common.FlexUtils.java

/**
* obtains the XML equivalent of the elements and its values found in the passed object. 
* 
* @param  classname name of the class that will be used as the root element for this object, if null it will use the objects natural class name.
* @param  obj the object containing the elements that will be used to populate the XML.
* @param  includeflextag true if the final XML will include the tag or not.
* @param  usefilename set to true if the object you're passing (ie is only relevant) has a binary (ie byte array) field and you want to overried the field content to contain the filename instead of the actual byte array string.
* @return XML string./* www. j  a v  a2  s .c o  m*/
* @see Hashtable
 *@see Element
*/
static public String getXML(String classname, Object obj, boolean includeflextag, boolean usefilename)
        throws Exception {
    Class c = obj.getClass();
    //Method[] m = c.getMethods();
    Vector ve = retrieveMethods(obj.getClass(), null);
    Method[] m = new Method[ve.size()];
    ve.toArray(m);

    String name, retval;
    StringBuilder main = new StringBuilder();
    String id = null;
    String parentid = null;

    if (c.getSimpleName().endsWith("Class")) {
        return "";
    }

    if (classname == null) {
        classname = c.getSimpleName();
    }

    classname = classname.toLowerCase();

    StringBuffer xml = new StringBuffer();

    for (int i = 0; i < m.length; i++) {
        name = m[i].getName();
        retval = m[i].getReturnType().getName();

        if (retval.equalsIgnoreCase("void") && name.startsWith("set")) {
            // we get the method name
            String field = name.substring(3);
            String getter = (m[i].getParameterTypes()[0].getSimpleName().equals("boolean") ? "is" : "get")
                    + field;

            try {

                Method met = c.getMethod(getter, (Class[]) null);
                Object o = null;
                try {
                    o = met.invoke(obj, (Object[]) null);
                } catch (Exception g) {
                    // no content                        
                }

                field = field.toLowerCase();

                //System.out.println(i+">>>> name "+name+" field:"+field+" type:"+t+" "+o);
                if (o != null) {
                    String t = met.getReturnType().getSimpleName();
                    if (t.endsWith("Date")) {
                        if ((o + "").trim().startsWith("0003-11-30T00:00:00")) {
                            xml.append("<" + field + " type=\"" + t + "\"><![CDATA[ ]]></" + field + ">");
                        } else {
                            String dt = ((new SimpleDateFormat(_dateformat)).format((Date) o));
                            dt = dt.replaceFirst("00:00:00", "");
                            xml.append(
                                    "<" + field + " type=\"" + t + "\"><![CDATA[" + dt + "]]></" + field + ">");
                        }

                    } else if (t.endsWith("XMLGregorianCalendar")) {
                        // 2009-01-01T10:00:00.000+08:00
                        if ((o + "").trim().startsWith("0003-11-30T00:00:00")) {
                            xml.append("<" + field + " type=\"" + t + "\"><![CDATA[ ]]></" + field + ">");
                        } else {

                            Date d = (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S")).parse(o + "");

                            String dt = (new SimpleDateFormat(_dateformat)).format(d);
                            dt = dt.replaceFirst("00:00:00", "");
                            xml.append(
                                    "<" + field + " type=\"" + t + "\"><![CDATA[" + dt + "]]></" + field + ">");

                        }

                    } else if (t.endsWith("byte[]")) {

                        if (usefilename) {
                            FileContainer fc = extractFileFromMimeMessage((byte[]) o);

                            xml.append("<" + field + " type=\"" + t + "\"><![CDATA["
                                    + (fc == null || fc.getFileName() == null ? "" : fc.getFileName()) + "]]></"
                                    + field + ">");
                        } else {
                            xml.append("<" + field + " type=\"" + t + "\"><![CDATA[" + new String((byte[]) o)
                                    + "]]></" + field + ">");
                            //xml.append("<"+field+" type=\""+t+"\"><![CDATA["+new String(fc.getByteArray())+"]]></"+field+">");
                        }
                    } else if (t.toLowerCase().endsWith("boolean")) {
                        xml.append("<" + field + " type=\"" + t + "\"><![CDATA[" + o + "]]></" + field + ">");
                    } else {
                        if (indexable(t)) {
                            if (field.equals("id")) {
                                id = o.toString();
                            } else if (field.equals("parentid")) {
                                parentid = o.toString();
                            } else {
                                // 2013-01-15 yujb reverted to not replacing '%' with &amp;#037;
                                xml.append("<" + field + " type=\"" + t + "\"><![CDATA[" + o.toString()
                                        + "]]></" + field + ">");
                                //xml.append("<"+field+" type=\""+t+"\"><![CDATA["+replaceString(o.toString(),"%","&amp;#037;")+"]]></"+field+">");                                  
                            }
                        } else {
                            if (c.getSimpleName().endsWith("FlexContainer") && field.equals("object")) {
                                xml.append(getXML(null, o, false, usefilename));
                            }
                        }
                    }
                } else {
                    xml.append("<" + field + " type=\"" + met.getReturnType().getSimpleName() + "\"/>");
                }
            } catch (Exception f) {
                //throw f;
                f.printStackTrace();
                // attempt a get.
            }
        } else {

            // what if this is a list?
            if (retval.endsWith("List")) {
                String getter = m[i].getName();
                Method met = c.getMethod(getter, (Class[]) null);
                List o = null;
                try {
                    o = (List) met.invoke(obj, (Object[]) null);
                } catch (Exception g) {
                    // no content
                    g.printStackTrace();
                }

                if (o != null) {
                    Iterator it = o.iterator();
                    while (it.hasNext()) {
                        Object lo = it.next();
                        xml.append(getXML(null, lo, false, usefilename));
                    }
                }
            } else if (retval.endsWith("ConcurrentHashMap")) {
                String getter = m[i].getName();
                Method met = c.getMethod(getter, (Class[]) null);
                ConcurrentHashMap o = null;
                try {
                    o = (ConcurrentHashMap) met.invoke(obj, (Object[]) null);
                } catch (Exception g) {
                    // no content
                    g.printStackTrace();
                }

                if (o != null) {
                    Enumeration en = o.keys();
                    while (en.hasMoreElements()) {
                        String el = (String) en.nextElement();
                        Vector<FlexContainer> children = (Vector<FlexContainer>) o.get(el);
                        Iterator it = children.iterator();
                        xml.append("<" + el + "typerecords total=\"" + children.size() + "\" from=\"0\" to=\""
                                + children.size() + "\" returned=\"" + children.size() + "\"/>");
                        xml.append("<" + el + "typelist>");

                        while (it.hasNext()) {
                            FlexContainer lo = (FlexContainer) it.next();
                            xml.append("<flexcontainer id=\"" + lo.getId() + "\" parentid=\"" + lo.getParentId()
                                    + "\">" + getXML(null, lo, false, usefilename) + "</flexcontainer>");
                        }
                        xml.append("</" + el + "typelist>");
                    }
                }
            } else {
                if (!name.startsWith("get") && !retval.startsWith("java") && !retval.endsWith("byte[]")
                        && !retval.equals("int") && !retval.equals("boolean") && !retval.equals("void")) // if complex
                {
                    String getter = m[i].getName();
                    Method met = c.getMethod(getter, (Class[]) null);
                    Object o = null;
                    try {
                        o = met.invoke(obj, (Object[]) null);
                    } catch (Exception g) {
                        // no content
                        g.printStackTrace();
                    }
                    //System.out.println(i+">>>> name "+name+" field:"+getter.substring(3)+" retval:"+retval+" "+o);
                    if (o != null) {
                        xml.append(getXML(getter.substring(3), o, false, usefilename));
                    }
                }
            }
        }
    }

    // then we get the complex types!
    /*Field[] f = c.getDeclaredFields();
    for (int i=0;i<f.length;i++)
    {
    String pname = f[i].getType().getSimpleName();
    if (pname.equals("List"))
    {
        Type type = f[i].getGenericType();
        ParameterizedType t = (ParameterizedType)type;
        Class c1 = (Class) t.getActualTypeArguments()[0];
        String getter = f[i].getName();
        String n = (getter.substring(0,1).toUpperCase())+getter.substring(1);
        getter = "get"+n;
            
        Method met = c.getMethod(getter, (Class[])null);
        List o = null;
        try
        {
            o = (List) met.invoke(obj,(Object[])null);
        }
        catch (Exception g)
        {
            // no content
            g.printStackTrace();
        }
            
        if (o!=null)
        {
            Iterator it = o.iterator();
            while (it.hasNext())
            {
                Object lo = it.next();
                xml.append(getXML(n,lo,false));
            }
        }
            
    }
    else
    {
    }
    }*/

    // then we check for parent classes
    // check for superclass
    /*Class sc = c.getSuperclass();
    if (!sc.getSimpleName().equals("Object"))
    {
    xml.append(getXML(sc.getName(),sc.newInstance(),false));
    }*/

    if (!c.getSimpleName().endsWith("FlexContainer")) {
        //main.append("<"+classname+" id=\""+id+"\">");
        main.append("<" + classname + ">");
        xml.append("</" + classname + ">");
    }

    if (includeflextag) {
        if (c.getSimpleName().equals("FlexContainer")) {
            main.append("<" + classname + " id=\"" + id + "\" parentid=\"" + parentid + "\">");
        } else {
            main.append("<" + classname + ">");
        }
        main.append(xml);
        main.append("</flexcontainer>");
    } else {
        main.append(xml);
    }
    //System.out.println("\n>>>>\n"+main+"\n<<<<<");
    return main.toString();

}

From source file:org.chiba.tools.schemabuilder.AbstractSchemaFormBuilder.java

protected void addChoicesForSelectSwitchControl(Document xForm, Element choicesElement, Vector choiceValues,
        HashMap case_types) {

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("addChoicesForSelectSwitchControl, values=");
        Iterator it = choiceValues.iterator();
        while (it.hasNext()) {
            //String name=(String) it.next();
            XSTypeDefinition type = (XSTypeDefinition) it.next();
            String name = type.getName();
            LOGGER.debug("  - " + name);
        }//  w w  w . j  a va 2  s.  c  om
    }

    // sort the enums values and then add them as choices
    //
    // TODO: Should really put the default value (if any) at the top of the list.
    //
    /*List sortedList = choiceValues.subList(0, choiceValues.size());
    Collections.sort(sortedList);
    Iterator iterator = sortedList.iterator();*/
    // -> no, already sorted
    Iterator iterator = choiceValues.iterator();
    while (iterator.hasNext()) {
        XSTypeDefinition type = (XSTypeDefinition) iterator.next();
        String textValue = type.getName();
        //String textValue = (String) iterator.next();

        if (LOGGER.isDebugEnabled())
            LOGGER.debug("addChoicesForSelectSwitchControl, processing " + textValue);

        Element item = xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "item");
        this.setXFormsId(item);
        choicesElement.appendChild(item);

        Element captionElement = xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "label");
        this.setXFormsId(captionElement);
        item.appendChild(captionElement);
        captionElement.appendChild(xForm.createTextNode(createCaption(textValue)));

        Element value = xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "value");
        this.setXFormsId(value);
        item.appendChild(value);
        value.appendChild(xForm.createTextNode(textValue));

        /// action in the case

        Element action = xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "action");
        this.setXFormsId(action);
        item.appendChild(action);

        action.setAttributeNS(XMLEVENTS_NS, xmleventsNSPrefix + "event", "xforms-select");

        Element toggle = xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "toggle");
        this.setXFormsId(toggle);

        //build the case element
        Element caseElement = xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "case");
        String case_id = this.setXFormsId(caseElement);
        case_types.put(textValue, caseElement);

        toggle.setAttributeNS(XFORMS_NS, getXFormsNSPrefix() + "case", case_id);

        //toggle.setAttributeNS(XFORMS_NS,getXFormsNSPrefix() + "case",bindIdPrefix + "_" + textValue +"_case");
        action.appendChild(toggle);
    }
}

From source file:edu.cornell.med.icb.geo.tools.CalculateRanks.java

private void postProcess(final FileReader inputReader, final PrintWriter outputWriter,
        final PrintWriter talliesWriter) throws IOException {
    final BufferedReader br = new BufferedReader(inputReader);
    String line;/*www .ja va  2  s  .com*/
    final Vector<ClassificationResults> allResults = new Vector<ClassificationResults>();
    final Map<String, List<ClassificationResults>> organizer = new HashMap<String, List<ClassificationResults>>();

    final Comparator<ClassificationResults> looAccuracyComparator = new Comparator<ClassificationResults>() {
        public int compare(final ClassificationResults o1, final ClassificationResults o2) {
            return (int) Math
                    .round(o2.getLooPerformance().getAccuracy() - o1.getLooPerformance().getAccuracy());
        }
    };

    // read stats from input file:
    while ((line = br.readLine()) != null) {
        if (line.startsWith("Dataset\t")) {

            continue; // skip headers.
        }

        final ClassificationResults result = ClassificationResults.create(line);
        if (filterWithShuffleTest) {
            if (result.getShuffleGEP() < 5) {
                allResults.add(result);
            }
        } else {
            allResults.add(result);
        }

        geneListNames.add(result.getGeneListName());
        final String key = result.getClassificationUnique();

        List<ClassificationResults> resultForClassification = organizer.get(key);
        if (resultForClassification == null) {
            resultForClassification = new Vector<ClassificationResults>();
        }
        resultForClassification.add(result);
        organizer.put(key, resultForClassification);
    }

    final NumberFormat formatter = new DecimalFormat();
    formatter.setMaximumFractionDigits(2);

    // calculate ranks and output:
    {
        ClassificationResults.writeHeader(outputWriter, filterWithShuffleTest);
        final Iterator<List<ClassificationResults>> iterator = organizer.values().iterator();
        while (iterator.hasNext()) {
            final List<ClassificationResults> resultsForClassification = iterator.next();
            // first sort by accuracy:
            Collections.sort(resultsForClassification, looAccuracyComparator);
            // then, iterate the list and assign ranks:
            int rank = 0;
            double previousPerformanceValue = -1;
            for (final ClassificationResults singleResult : resultsForClassification) {
                final double currentPerformanceValue = singleResult.getLooPerformance().getAccuracy();
                if (currentPerformanceValue != previousPerformanceValue) {
                    rank++;
                    previousPerformanceValue = currentPerformanceValue;
                }
                singleResult.setRank(rank);
                singleResult.write(outputWriter, formatter);
            }
        }
    }
    // tally ranks per gene list:
    if (talliesWriter != null) {
        talliesWriter.write("Gene List\tRank\tTally\n");
        for (final String geneListName : geneListNames) {
            System.out.println(geneListName + " gene list:");
            final Int2IntMap rankTallies = new Int2IntOpenHashMap();

            final Iterator<ClassificationResults> iterator = allResults.iterator();
            while (iterator.hasNext()) {
                final ClassificationResults classificationResults = iterator.next();
                if (!classificationResults.getGeneListName().equals(geneListName)) {
                    continue;
                }

                int tally = rankTallies.get(classificationResults.getRank());
                tally++;
                rankTallies.put(classificationResults.getRank(), tally);

            }

            final Set<Map.Entry<Integer, Integer>> entries = rankTallies.entrySet();
            for (final Map.Entry<Integer, Integer> entry : entries) {
                final Object rank = entry.getKey();
                final Object tally = entry.getValue();
                System.out.println("rank: " + rank + " tally: " + tally);
                if (talliesWriter != null) {
                    talliesWriter.write(geneListName);
                    talliesWriter.write("\t");
                    talliesWriter.write(rank.toString());
                    talliesWriter.write("\t");
                    talliesWriter.write(tally.toString());
                    talliesWriter.write("\n");
                }
            }

        }
    }
}

From source file:org.mbs3.juniuploader.gui.pnlSettings.java

private void logLevelBtnAction(ActionEvent evt) {
    JRadioButton jbr = (JRadioButton) evt.getSource();
    DefaultButtonModel bm = (DefaultButtonModel) jbr.getModel();
    ButtonGroup bg = bm.getGroup();

    String pkg = "org.mbs.juniuploader";
    String logClass = pkg + ".util.GUILog";

    java.util.Vector toMark = new java.util.Vector();
    String level = jbr.getActionCommand();

    log.info("Changing logging level to " + level + " for selected components");
    if (bg == grpMain) {
        log.debug("grpMain setting up level " + level);
        String prefix = pkg + "";
        toMark.add(new String(prefix + ".ClassRunner"));
        toMark.add(new String(prefix + ".ShutdownThread"));
        toMark.add(new String(prefix + ".StatusThread"));
        toMark.add(new String(prefix + ".Uploader"));

        toMark.add(new String(prefix + ".util.Prefs"));
        toMark.add(new String(prefix + ".util.Util"));

    } else if (bg == grpGUI) {
        log.debug("grpGUI setting up level " + level);
        String prefix = pkg + ".gui";
        toMark.add(new String(prefix + ".frmMain"));
        toMark.add(new String(prefix + ".pnlDebug"));
        toMark.add(new String(prefix + ".pnlFormVariables"));
        toMark.add(new String(prefix + ".pnlMainMenu"));
        toMark.add(new String(prefix + ".pnlRemoteInterface"));
        toMark.add(new String(prefix + ".pnlSettings"));
        toMark.add(new String(prefix + ".pnlUploadRules"));
        toMark.add(new String(prefix + ".pnlUploadSites"));
        toMark.add(new String(prefix + ".pnlWoWDirectories"));

    } else if (bg == grpObjects) {
        log.debug("grpObjects setting up level " + level);
        String prefix = pkg + ".objects";
        toMark.add(new String(prefix + ".Addon"));
        toMark.add(new String(prefix + ".AddonFile"));
        toMark.add(new String(prefix + ".LocalSystem"));
        toMark.add(new String(prefix + ".RemoteSystem"));

        prefix = pkg + ".objects.localobjects";
        toMark.add(new String(prefix + ".LUAFile"));
        toMark.add(new String(prefix + ".WAccount"));
        toMark.add(new String(prefix + ".WDirectory"));

        prefix = pkg + ".objects.remoteobjects";
        toMark.add(new String(prefix + ".FormPair"));
        toMark.add(new String(prefix + ".FormPairGroup"));
        toMark.add(new String(prefix + ".UploadRule"));
        toMark.add(new String(prefix + ".UploadSite"));

        prefix = pkg + ".objects.util";
        toMark.add(new String(prefix + ".CheckSummer"));
        toMark.add(new String(prefix + ".MyVector"));
        toMark.add(new String(prefix + ".UUIDGen"));
    } else if (bg == grpHTTP) {
        log.debug("grpHTTP setting up level " + level);
        toMark.add(new String("httpclient.wire.header"));
        toMark.add(new String("org.apache.commons.httpclient"));
    } else {/*from   www . j  a  va 2  s.  c  o  m*/
        log.info("Unknown button group!!");
    }

    // now that we know what properties to set, let's set them
    Iterator i = toMark.iterator();
    while (i.hasNext()) {
        String name = (String) i.next();
        System.setProperty(logClass + "." + name, level);
        log.trace("Configuring " + logClass + "." + name + " to " + level);
    }
}

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) {

    //// w  w  w . ja va2s  . co  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:org.apache.ws.scout.registry.BusinessLifeCycleManagerV3Impl.java

private void clearPublisherAssertions(String authinfo, IRegistryV3 ireg) {
    Vector<PublisherAssertion> pasvect = null;
    PublisherAssertion[] pasarr = null;//w  w w .  j a  v a  2 s . co m
    try {
        AssertionStatusReport report = ireg.getAssertionStatusReport(authinfo, "status:complete");
        List<AssertionStatusItem> assertionStatusItemList = report.getAssertionStatusItem();
        AssertionStatusItem[] assertionStatusItemArr = new AssertionStatusItem[assertionStatusItemList.size()];

        int len = assertionStatusItemArr != null ? assertionStatusItemArr.length : 0;
        for (int i = 0; i < len; i++) {
            AssertionStatusItem asi = assertionStatusItemArr[i];
            /* String sourceKey = asi.getFromKey();
             String targetKey = asi.getToKey();
             PublisherAssertion pa = new PublisherAssertion();
             pa.setFromKey(sourceKey);
             pa.setToKey(targetKey);
             KeyedReference keyr = asi.getKeyedReference();
             pa.setKeyedReference(keyr);
             pa.setTModelKey(keyr.getTModelKey());
             pa.setKeyName(keyr.getKeyName());
             pa.setKeyValue(keyr.getKeyValue());
             if(pasvect == null) pasvect = new Vector(len);
             pasvect.add(pa);*/
            if (pasvect == null)
                pasvect = new Vector<PublisherAssertion>(len);
            pasvect.add(this.getPublisherAssertion(asi));
        }
        report = ireg.getAssertionStatusReport(authinfo, "status:toKey_incomplete");
        assertionStatusItemArr = report.getAssertionStatusItem().toArray(assertionStatusItemArr);

        len = assertionStatusItemArr != null ? assertionStatusItemArr.length : 0;
        for (int i = 0; i < len; i++) {
            AssertionStatusItem asi = (AssertionStatusItem) assertionStatusItemArr[i];
            if (pasvect == null)
                pasvect = new Vector<PublisherAssertion>(len);
            pasvect.add(this.getPublisherAssertion(asi));
        }

        report = ireg.getAssertionStatusReport(authinfo, "status:fromKey_incomplete");
        assertionStatusItemArr = report.getAssertionStatusItem().toArray(assertionStatusItemArr);

        len = assertionStatusItemArr != null ? assertionStatusItemArr.length : 0;
        for (int i = 0; i < len; i++) {
            AssertionStatusItem asi = (AssertionStatusItem) assertionStatusItemArr[i];
            if (pasvect == null)
                pasvect = new Vector<PublisherAssertion>(len);
            pasvect.add(this.getPublisherAssertion(asi));
        }

        if (pasvect != null) {
            pasarr = new PublisherAssertion[pasvect.size()];
            Iterator iter = pasvect.iterator();
            int pasarrPos = 0;
            while (iter.hasNext()) {
                pasarr[pasarrPos] = ((PublisherAssertion) iter.next());
                pasarrPos++;
            }
        }
    } catch (RegistryV3Exception e) {
        throw new RuntimeException(e);
    }

    if (pasarr != null && pasarr.length > 0)
        try {
            ireg.deletePublisherAssertions(authinfo, pasarr);
        } catch (RegistryV3Exception e) {
            log.debug("Ignoring exception " + e.getMessage(), e);
        }
}

From source file:org.apache.rampart.util.RampartUtil.java

public static Vector getPartsAndElements(boolean sign, SOAPEnvelope envelope, boolean includeBody, Vector parts,
        Vector elements, Set namespaces) {

    Vector found = new Vector();
    Vector result = new Vector();

    // check body
    if (includeBody) {
        if (sign) {
            result.add(new WSEncryptionPart(addWsuIdToElement(envelope.getBody()), null,
                    WSConstants.PART_TYPE_BODY));
        } else {/*from w w  w. j  a v a 2s. c  om*/
            result.add(new WSEncryptionPart(addWsuIdToElement(envelope.getBody()), "Content",
                    WSConstants.PART_TYPE_BODY));
        }
        found.add(envelope.getBody());
    }

    // Search envelope header for 'parts' from Policy (SignedParts/EncryptedParts)

    SOAPHeader header = envelope.getHeader();

    for (int i = 0; i < parts.size(); i++) {
        WSEncryptionPart wsep = (WSEncryptionPart) parts.get(i);
        if (wsep.getName() == null) {
            // NO name - search by namespace
            ArrayList headerList = header.getHeaderBlocksWithNSURI(wsep.getNamespace());

            for (int j = 0; j < headerList.size(); j++) {
                SOAPHeaderBlock shb = (SOAPHeaderBlock) headerList.get(j);

                // find reference in envelope
                OMElement e = header.getFirstChildWithName(shb.getQName());

                if (!found.contains(e)) {
                    // found new
                    found.add(e);

                    if (sign) {
                        result.add(new WSEncryptionPart(e.getLocalName(), wsep.getNamespace(), "Content",
                                WSConstants.PART_TYPE_HEADER));
                    } else {

                        WSEncryptionPart encryptedHeader = new WSEncryptionPart(e.getLocalName(),
                                wsep.getNamespace(), "Element", WSConstants.PART_TYPE_HEADER);
                        OMAttribute wsuId = e.getAttribute(new QName(WSConstants.WSU_NS, "Id"));

                        if (wsuId != null) {
                            encryptedHeader.setEncId(wsuId.getAttributeValue());
                        }

                        result.add(encryptedHeader);
                    }
                }
            }
        } else {
            // try to find
            OMElement e = header.getFirstChildWithName(new QName(wsep.getNamespace(), wsep.getName()));
            if (e != null) {
                if (!found.contains(e)) {
                    // found new (reuse wsep)
                    found.add(e);
                    wsep.setType(WSConstants.PART_TYPE_HEADER);
                    OMAttribute wsuId = e.getAttribute(new QName(WSConstants.WSU_NS, "Id"));

                    if (wsuId != null) {
                        wsep.setEncId(wsuId.getAttributeValue());
                    }

                    result.add(wsep);
                }
            }
        }
    }

    // ?? Search for 'Elements' here

    // decide what exactly is going to be used - only the default namespaces, or the list of all declared namespaces in the message !

    Iterator elementsIter = elements.iterator();
    while (elementsIter.hasNext()) {
        String expression = (String) elementsIter.next();
        try {
            XPath xp = new AXIOMXPath(expression);
            Iterator nsIter = namespaces.iterator();

            while (nsIter.hasNext()) {
                OMNamespace tmpNs = (OMNamespace) nsIter.next();
                xp.addNamespace(tmpNs.getPrefix(), tmpNs.getNamespaceURI());
            }

            List selectedNodes = xp.selectNodes(envelope);

            Iterator nodesIter = selectedNodes.iterator();
            while (nodesIter.hasNext()) {
                OMElement e = (OMElement) nodesIter.next();
                String localName = e.getLocalName();
                String namespace = e.getNamespace() != null ? e.getNamespace().getNamespaceURI() : null;

                if (sign) {
                    WSEncryptionPart encryptedElem = new WSEncryptionPart(localName, namespace, "Content",
                            WSConstants.PART_TYPE_ELEMENT);
                    encryptedElem.setXpath(expression);
                    result.add(encryptedElem);

                } else {

                    WSEncryptionPart encryptedElem = new WSEncryptionPart(localName, namespace, "Element",
                            WSConstants.PART_TYPE_ELEMENT);
                    encryptedElem.setXpath(expression);

                    OMAttribute wsuId = e.getAttribute(new QName(WSConstants.WSU_NS, "Id"));

                    if (wsuId != null) {
                        encryptedElem.setEncId(wsuId.getAttributeValue());
                    }

                    result.add(encryptedElem);
                }
            }

        } catch (JaxenException e) {
            // This has to be changed to propagate an instance of a RampartException up
            throw new RuntimeException(e);
        }
    }

    return result;
}

From source file:org.dspace.discovery.SolrServiceImpl.java

/**
 * Build a Lucene document for a DSpace Item and write the index
 *
 * @param context Users Context/*from  w w w . j  ava 2 s .  c o m*/
 * @param item    The DSpace Item to be indexed
 * @throws SQLException
 * @throws IOException
 */
protected void buildDocument(Context context, Item item) throws SQLException, IOException {
    String handle = item.getHandle();

    if (handle == null) {
        handle = HandleManager.findHandle(context, item);
    }

    // get the location string (for searching by collection & community)
    List<String> locations = getItemLocations(item);

    SolrInputDocument doc = buildDocument(Constants.ITEM, item.getID(), handle, locations);

    log.debug("Building Item: " + handle);

    doc.addField("withdrawn", item.isWithdrawn());
    doc.addField("discoverable", item.isDiscoverable());

    //Keep a list of our sort values which we added, sort values can only be added once
    List<String> sortFieldsAdded = new ArrayList<String>();
    Set<String> hitHighlightingFields = new HashSet<String>();
    try {
        List<DiscoveryConfiguration> discoveryConfigurations = SearchUtils.getAllDiscoveryConfigurations(item);

        //A map used to save each sidebarFacet config by the metadata fields
        Map<String, List<DiscoverySearchFilter>> searchFilters = new HashMap<String, List<DiscoverySearchFilter>>();
        Map<String, DiscoverySortFieldConfiguration> sortFields = new HashMap<String, DiscoverySortFieldConfiguration>();
        Map<String, DiscoveryRecentSubmissionsConfiguration> recentSubmissionsConfigurationMap = new HashMap<String, DiscoveryRecentSubmissionsConfiguration>();
        Set<String> moreLikeThisFields = new HashSet<String>();
        for (DiscoveryConfiguration discoveryConfiguration : discoveryConfigurations) {
            for (int i = 0; i < discoveryConfiguration.getSearchFilters().size(); i++) {
                DiscoverySearchFilter discoverySearchFilter = discoveryConfiguration.getSearchFilters().get(i);
                for (int j = 0; j < discoverySearchFilter.getMetadataFields().size(); j++) {
                    String metadataField = discoverySearchFilter.getMetadataFields().get(j);
                    List<DiscoverySearchFilter> resultingList;
                    if (searchFilters.get(metadataField) != null) {
                        resultingList = searchFilters.get(metadataField);
                    } else {
                        //New metadata field, create a new list for it
                        resultingList = new ArrayList<DiscoverySearchFilter>();
                    }
                    resultingList.add(discoverySearchFilter);

                    searchFilters.put(metadataField, resultingList);
                }
            }

            DiscoverySortConfiguration sortConfiguration = discoveryConfiguration.getSearchSortConfiguration();
            if (sortConfiguration != null) {
                for (DiscoverySortFieldConfiguration discoverySortConfiguration : sortConfiguration
                        .getSortFields()) {
                    sortFields.put(discoverySortConfiguration.getMetadataField(), discoverySortConfiguration);
                }
            }

            DiscoveryRecentSubmissionsConfiguration recentSubmissionConfiguration = discoveryConfiguration
                    .getRecentSubmissionConfiguration();
            if (recentSubmissionConfiguration != null) {
                recentSubmissionsConfigurationMap.put(recentSubmissionConfiguration.getMetadataSortField(),
                        recentSubmissionConfiguration);
            }

            DiscoveryHitHighlightingConfiguration hitHighlightingConfiguration = discoveryConfiguration
                    .getHitHighlightingConfiguration();
            if (hitHighlightingConfiguration != null) {
                List<DiscoveryHitHighlightFieldConfiguration> fieldConfigurations = hitHighlightingConfiguration
                        .getMetadataFields();
                for (DiscoveryHitHighlightFieldConfiguration fieldConfiguration : fieldConfigurations) {
                    hitHighlightingFields.add(fieldConfiguration.getField());
                }
            }
            DiscoveryMoreLikeThisConfiguration moreLikeThisConfiguration = discoveryConfiguration
                    .getMoreLikeThisConfiguration();
            if (moreLikeThisConfiguration != null) {
                for (String metadataField : moreLikeThisConfiguration.getSimilarityMetadataFields()) {
                    moreLikeThisFields.add(metadataField);
                }
            }
        }

        List<String> toProjectionFields = new ArrayList<String>();
        String projectionFieldsString = new DSpace().getConfigurationService()
                .getProperty("discovery.index.projection");
        if (projectionFieldsString != null) {
            if (projectionFieldsString.indexOf(",") != -1) {
                for (int i = 0; i < projectionFieldsString.split(",").length; i++) {
                    toProjectionFields.add(projectionFieldsString.split(",")[i].trim());
                }
            } else {
                toProjectionFields.add(projectionFieldsString);
            }
        }

        DCValue[] mydc = item.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY);
        for (DCValue meta : mydc) {
            String field = meta.schema + "." + meta.element;
            String unqualifiedField = field;

            String value = meta.value;

            if (value == null) {
                continue;
            }

            if (meta.qualifier != null && !meta.qualifier.trim().equals("")) {
                field += "." + meta.qualifier;
            }

            List<String> toIgnoreMetadataFields = SearchUtils.getIgnoredMetadataFields(item.getType());
            //We are not indexing provenance, this is useless
            if (toIgnoreMetadataFields != null && (toIgnoreMetadataFields.contains(field)
                    || toIgnoreMetadataFields.contains(unqualifiedField + "." + Item.ANY))) {
                continue;
            }

            String authority = null;
            String preferedLabel = null;
            List<String> variants = null;
            boolean isAuthorityControlled = MetadataAuthorityManager.getManager()
                    .isAuthorityControlled(meta.schema, meta.element, meta.qualifier);

            int minConfidence = isAuthorityControlled
                    ? MetadataAuthorityManager.getManager().getMinConfidence(meta.schema, meta.element,
                            meta.qualifier)
                    : Choices.CF_ACCEPTED;

            if (isAuthorityControlled && meta.authority != null && meta.confidence >= minConfidence) {
                boolean ignoreAuthority = new DSpace().getConfigurationService().getPropertyAsType(
                        "discovery.index.authority.ignore." + field, new DSpace().getConfigurationService()
                                .getPropertyAsType("discovery.index.authority.ignore", new Boolean(false)),
                        true);
                if (!ignoreAuthority) {
                    authority = meta.authority;

                    boolean ignorePrefered = new DSpace().getConfigurationService()
                            .getPropertyAsType("discovery.index.authority.ignore-prefered." + field,
                                    new DSpace().getConfigurationService().getPropertyAsType(
                                            "discovery.index.authority.ignore-prefered", new Boolean(false)),
                                    true);
                    if (!ignorePrefered) {

                        preferedLabel = ChoiceAuthorityManager.getManager().getLabel(meta.schema, meta.element,
                                meta.qualifier, meta.authority, meta.language);
                    }

                    boolean ignoreVariants = new DSpace().getConfigurationService()
                            .getPropertyAsType("discovery.index.authority.ignore-variants." + field,
                                    new DSpace().getConfigurationService().getPropertyAsType(
                                            "discovery.index.authority.ignore-variants", new Boolean(false)),
                                    true);
                    if (!ignoreVariants) {
                        variants = ChoiceAuthorityManager.getManager().getVariants(meta.schema, meta.element,
                                meta.qualifier, meta.authority, meta.language);
                    }

                }
            }

            if ((searchFilters.get(field) != null
                    || searchFilters.get(unqualifiedField + "." + Item.ANY) != null)) {
                List<DiscoverySearchFilter> searchFilterConfigs = searchFilters.get(field);
                if (searchFilterConfigs == null) {
                    searchFilterConfigs = searchFilters.get(unqualifiedField + "." + Item.ANY);
                }

                for (DiscoverySearchFilter searchFilter : searchFilterConfigs) {
                    Date date = null;
                    String separator = new DSpace().getConfigurationService()
                            .getProperty("discovery.solr.facets.split.char");
                    if (separator == null) {
                        separator = FILTER_SEPARATOR;
                    }
                    if (searchFilter.getType().equals(DiscoveryConfigurationParameters.TYPE_DATE)) {
                        //For our search filters that are dates we format them properly
                        date = toDate(value);
                        if (date != null) {
                            //TODO: make this date format configurable !
                            value = DateFormatUtils.formatUTC(date, "yyyy-MM-dd");
                        }
                    }
                    doc.addField(searchFilter.getIndexFieldName(), value);
                    doc.addField(searchFilter.getIndexFieldName() + "_keyword", value);

                    if (authority != null && preferedLabel == null) {
                        doc.addField(searchFilter.getIndexFieldName() + "_keyword",
                                value + AUTHORITY_SEPARATOR + authority);
                        doc.addField(searchFilter.getIndexFieldName() + "_authority", authority);
                        doc.addField(searchFilter.getIndexFieldName() + "_acid",
                                value.toLowerCase() + separator + value + AUTHORITY_SEPARATOR + authority);
                    }

                    if (preferedLabel != null) {
                        doc.addField(searchFilter.getIndexFieldName(), preferedLabel);
                        doc.addField(searchFilter.getIndexFieldName() + "_keyword", preferedLabel);
                        doc.addField(searchFilter.getIndexFieldName() + "_keyword",
                                preferedLabel + AUTHORITY_SEPARATOR + authority);
                        doc.addField(searchFilter.getIndexFieldName() + "_authority", authority);
                        doc.addField(searchFilter.getIndexFieldName() + "_acid", preferedLabel.toLowerCase()
                                + separator + preferedLabel + AUTHORITY_SEPARATOR + authority);
                    }
                    if (variants != null) {
                        for (String var : variants) {
                            doc.addField(searchFilter.getIndexFieldName() + "_keyword", var);
                            doc.addField(searchFilter.getIndexFieldName() + "_acid",
                                    var.toLowerCase() + separator + var + AUTHORITY_SEPARATOR + authority);
                        }
                    }

                    //Add a dynamic fields for auto complete in search
                    doc.addField(searchFilter.getIndexFieldName() + "_ac",
                            value.toLowerCase() + separator + value);
                    if (preferedLabel != null) {
                        doc.addField(searchFilter.getIndexFieldName() + "_ac",
                                preferedLabel.toLowerCase() + separator + preferedLabel);
                    }
                    if (variants != null) {
                        for (String var : variants) {
                            doc.addField(searchFilter.getIndexFieldName() + "_ac",
                                    var.toLowerCase() + separator + var);
                        }
                    }

                    if (searchFilter.getFilterType().equals(DiscoverySearchFilterFacet.FILTER_TYPE_FACET)) {
                        if (searchFilter.getType().equals(DiscoveryConfigurationParameters.TYPE_TEXT)) {
                            //Add a special filter
                            //We use a separator to split up the lowercase and regular case, this is needed to get our filters in regular case
                            //Solr has issues with facet prefix and cases
                            if (authority != null) {
                                String facetValue = preferedLabel != null ? preferedLabel : value;
                                doc.addField(searchFilter.getIndexFieldName() + "_filter",
                                        facetValue.toLowerCase() + separator + facetValue + AUTHORITY_SEPARATOR
                                                + authority);
                            } else {
                                doc.addField(searchFilter.getIndexFieldName() + "_filter",
                                        value.toLowerCase() + separator + value);
                            }
                        } else if (searchFilter.getType().equals(DiscoveryConfigurationParameters.TYPE_DATE)) {
                            if (date != null) {
                                String indexField = searchFilter.getIndexFieldName() + ".year";
                                doc.addField(searchFilter.getIndexFieldName() + "_keyword",
                                        DateFormatUtils.formatUTC(date, "yyyy"));
                                doc.addField(indexField, DateFormatUtils.formatUTC(date, "yyyy"));
                                //Also save a sort value of this year, this is required for determining the upper & lower bound year of our facet
                                if (doc.getField(indexField + "_sort") == null) {
                                    //We can only add one year so take the first one
                                    doc.addField(indexField + "_sort", DateFormatUtils.formatUTC(date, "yyyy"));
                                }
                            }
                        } else if (searchFilter.getType()
                                .equals(DiscoveryConfigurationParameters.TYPE_HIERARCHICAL)) {
                            HierarchicalSidebarFacetConfiguration hierarchicalSidebarFacetConfiguration = (HierarchicalSidebarFacetConfiguration) searchFilter;
                            String[] subValues = value
                                    .split(hierarchicalSidebarFacetConfiguration.getSplitter());
                            if (hierarchicalSidebarFacetConfiguration.isSkipFirstNodeLevel()
                                    && 1 < subValues.length) {
                                //Remove the first element of our array
                                subValues = (String[]) ArrayUtils.subarray(subValues, 1, subValues.length);
                            }
                            for (int i = 0; i < subValues.length; i++) {
                                StringBuilder valueBuilder = new StringBuilder();
                                for (int j = 0; j <= i; j++) {
                                    valueBuilder.append(subValues[j]);
                                    if (j < i) {
                                        valueBuilder
                                                .append(hierarchicalSidebarFacetConfiguration.getSplitter());
                                    }
                                }

                                String indexValue = valueBuilder.toString().trim();
                                doc.addField(searchFilter.getIndexFieldName() + "_tax_" + i + "_filter",
                                        indexValue.toLowerCase() + separator + indexValue);
                                //We add the field x times that it has occurred
                                for (int j = i; j < subValues.length; j++) {
                                    doc.addField(searchFilter.getIndexFieldName() + "_filter",
                                            indexValue.toLowerCase() + separator + indexValue);
                                    doc.addField(searchFilter.getIndexFieldName() + "_keyword", indexValue);
                                }
                            }
                        }
                    }
                }
            }

            if ((sortFields.get(field) != null || recentSubmissionsConfigurationMap.get(field) != null)
                    && !sortFieldsAdded.contains(field)) {
                //Only add sort value once
                String type;
                if (sortFields.get(field) != null) {
                    type = sortFields.get(field).getType();
                } else {
                    type = recentSubmissionsConfigurationMap.get(field).getType();
                }

                if (type.equals(DiscoveryConfigurationParameters.TYPE_DATE)) {
                    Date date = toDate(value);
                    if (date != null) {
                        doc.addField(field + "_dt", date);
                    } else {
                        log.warn("Error while indexing sort date field, item: " + item.getHandle()
                                + " metadata field: " + field + " date value: " + date);
                    }
                } else {
                    doc.addField(field + "_sort", value);
                }
                sortFieldsAdded.add(field);
            }

            if (hitHighlightingFields.contains(field) || hitHighlightingFields.contains("*")
                    || hitHighlightingFields.contains(unqualifiedField + "." + Item.ANY)) {
                doc.addField(field + "_hl", value);
            }

            if (moreLikeThisFields.contains(field)
                    || moreLikeThisFields.contains(unqualifiedField + "." + Item.ANY)) {
                doc.addField(field + "_mlt", value);
            }

            doc.addField(field, value);
            if (toProjectionFields.contains(field)
                    || toProjectionFields.contains(unqualifiedField + "." + Item.ANY)) {
                StringBuffer variantsToStore = new StringBuffer();
                if (variants != null) {
                    for (String var : variants) {
                        variantsToStore.append(VARIANTS_STORE_SEPARATOR);
                        variantsToStore.append(var);
                    }
                }
                doc.addField(field + "_stored", value + STORE_SEPARATOR + preferedLabel + STORE_SEPARATOR
                        + (variantsToStore.length() > VARIANTS_STORE_SEPARATOR.length()
                                ? variantsToStore.substring(VARIANTS_STORE_SEPARATOR.length())
                                : "null")
                        + STORE_SEPARATOR + authority + STORE_SEPARATOR + meta.language);
            }

            if (meta.language != null && !meta.language.trim().equals("")) {
                String langField = field + "." + meta.language;
                doc.addField(langField, value);
            }
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    log.debug("  Added Metadata");

    try {

        DCValue[] values = item.getMetadata("dc.relation.ispartof");

        if (values != null && values.length > 0 && values[0] != null && values[0].value != null) {
            // group on parent
            String handlePrefix = ConfigurationManager.getProperty("handle.canonical.prefix");
            if (handlePrefix == null || handlePrefix.length() == 0) {
                handlePrefix = "http://hdl.handle.net/";
            }

            doc.addField("publication_grp", values[0].value.replaceFirst(handlePrefix, ""));

        } else {
            // group on self
            doc.addField("publication_grp", item.getHandle());
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    log.debug("  Added Grouping");

    Vector<InputStreamReader> readers = new Vector<InputStreamReader>();

    try {
        // now get full text of any bitstreams in the TEXT bundle
        // trundle through the bundles
        Bundle[] myBundles = item.getBundles();

        for (Bundle myBundle : myBundles) {
            if ((myBundle.getName() != null) && myBundle.getName().equals("TEXT")) {
                // a-ha! grab the text out of the bitstreams
                Bitstream[] myBitstreams = myBundle.getBitstreams();

                for (Bitstream myBitstream : myBitstreams) {
                    try {
                        InputStreamReader is = new InputStreamReader(myBitstream.retrieve()); // get input
                        readers.add(is);

                        // Add each InputStream to the Indexed Document
                        String value = IOUtils.toString(is);
                        doc.addField("fulltext", value);

                        if (hitHighlightingFields.contains("*") || hitHighlightingFields.contains("fulltext")) {
                            doc.addField("fulltext_hl", value);
                        }

                        log.debug("  Added BitStream: " + myBitstream.getStoreNumber() + "   "
                                + myBitstream.getSequenceID() + "   " + myBitstream.getName());

                    } catch (Exception e) {
                        // this will never happen, but compiler is now
                        // happy.
                        log.trace(e.getMessage(), e);
                    }
                }
            }
        }
    } catch (RuntimeException e) {
        log.error(e.getMessage(), e);
    } finally {
        Iterator<InputStreamReader> itr = readers.iterator();
        while (itr.hasNext()) {
            InputStreamReader reader = itr.next();
            if (reader != null) {
                reader.close();
            }
        }
        log.debug("closed " + readers.size() + " readers");
    }

    //Do any additional indexing, depends on the plugins
    List<SolrServiceIndexPlugin> solrServiceIndexPlugins = new DSpace().getServiceManager()
            .getServicesByType(SolrServiceIndexPlugin.class);
    for (SolrServiceIndexPlugin solrServiceIndexPlugin : solrServiceIndexPlugins) {
        solrServiceIndexPlugin.additionalIndex(context, item, doc);
    }

    // write the index and close the inputstreamreaders
    try {
        writeDocument(doc);
        log.info("Wrote Item: " + handle + " to Index");
    } catch (RuntimeException e) {
        log.error("Error while writing item to discovery index: " + handle + " message:" + e.getMessage(), e);
    }
}

From source file:com.flexoodb.engines.FlexJAXBMappedDBDataEngine.java

public void initialize(Object conf) throws Exception {

    if (conf != null) {

        XMLConfiguration config = (XMLConfiguration) conf;

        _pool = new ConnectionPool();

        int i = 0;

        String idlength = config.getString("flexoodb[@idlength]");
        if (idlength != null) {
            _idlength = Integer.parseInt(idlength);

            if (_idlength < 10) {
                throw new Exception("flexoodb ID's must be >=10 in length.");
            }//from   www .ja  va 2s.  c  o  m
        }

        _dbname = config.getString("flexoodb[@dbname]");
        if (_dbname == null) {
            _dbname = "flexoodb";
        }

        _shared = config.getString("flexoodb[@shared]") != null
                ? config.getString("flexoodb[@shared]").equalsIgnoreCase("true")
                : false;

        while (!_pool.set(config.getString("flexoodb[@odbcurl]"), config.getString("flexoodb[@username]"),
                config.getString("flexoodb[@password]"), config.getString("flexoodb[@odbcclass]"),
                config.getInt("flexoodb[@initconnections]"), config.getInt("flexoodb[@maxconnections]"),
                "FlexOODB") && i < 2) {
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
            }
            i++;
        }

        Connection conn = null;
        try {
            conn = (Connection) _pool.getConnection();

            // first we get the tenants if any
            Vector<String> tenants = new Vector<String>();
            List tl = config.getList("flexoodb.tenants.tenant[@name]");
            for (int j = 0; j < tl.size(); j++) {
                String tenant = config.getString("flexoodb.tenants.tenant(" + j + ")[@name]");
                String description = config.getString("flexoodb.tenants.tenant(" + j + ")[@description]");
                String status = config.getString("flexoodb.tenants.tenant(" + j + ")[@status]");

                if (status != null && status.equalsIgnoreCase("Active")) {
                    tenants.add(tenant);
                }
            }

            // then we get the table structures
            List tables = config.getList("flexoodb.table[@name]");
            for (int j = 0; j < tables.size(); j++) {
                String tablename = config.getString("flexoodb.table(" + j + ")[@name]");
                String alias = config.getString("flexoodb.table(" + j + ")[@alias]");
                String idcolumn = config.getString("flexoodb.table(" + j + ")[@idcolumn]");
                String parentidcolumn = config.getString("flexoodb.table(" + j + ")[@parentidcolumn]");
                String includeidcolumns = config.getString("flexoodb.table(" + j + ")[@includeidcolumns]");
                String parenttable = config.getString("flexoodb.table(" + j + ")[@parenttable]");
                String autoincrement = config.getString("flexoodb.table(" + j + ")[@autoincrement]", "false");
                String createstmt = config.getString("flexoodb.table(" + j + ").create");

                FlexElement element = new FlexElement((alias != null ? alias : tablename));
                element.addAttribute((new FlexAttribute("realtablename")).setValue(tablename));
                element.addAttribute((new FlexAttribute("idcolumn")).setValue(idcolumn));
                element.addAttribute((new FlexAttribute("parentidcolumn")).setValue(parentidcolumn));
                element.addAttribute((new FlexAttribute("includeidcolumns")).setValue(includeidcolumns));
                element.addAttribute((new FlexAttribute("parenttable")).setValue(parenttable));
                element.addAttribute((new FlexAttribute("autoincrement")).setValue(autoincrement));
                //element.addAttribute((new FlexAttribute("createstmt")).setValue(createstmt));

                PreparedStatement ps = null;
                ResultSet res = null;

                String sampletenant = "";

                if (createstmt != null) {
                    try {
                        // we check if the tables exists.
                        if (createstmt != null && !createstmt.isEmpty()) {
                            if (tenants.size() > 0 && _shared) {
                                Iterator it = tenants.iterator();

                                while (it.hasNext()) {
                                    String tenant = (String) it.next();

                                    if (sampletenant.isEmpty()) {
                                        sampletenant = sampletenant + tenant;
                                    }

                                    ps = (PreparedStatement) conn.prepareStatement(createstmt.replaceAll(
                                            "::" + tablename + "::",
                                            (_shared ? (tenant + "_" + tablename) : (tenant + tablename))));
                                    ps.execute();
                                }
                            } else // must be a single instance then
                            {
                                ps = (PreparedStatement) conn.prepareStatement(createstmt
                                        .replaceAll("::" + tablename + "::", sampletenant + tablename));
                                ps.execute();
                            }
                        }
                    } catch (Exception e) // if we cant describe then doesnt exist?
                    {
                        System.out.println("unable to initialize table:" + tablename);
                        e.printStackTrace();
                    }
                }
                // we store the structure as well
                //PreparedStatement ps = (PreparedStatement) conn.prepareStatement("select * from "+tablename.toLowerCase()+" LIMIT 1");
                ps = (PreparedStatement) conn
                        .prepareStatement("describe " + (sampletenant.isEmpty() ? tablename.toLowerCase()
                                : (sampletenant + "_" + tablename.toLowerCase())));
                res = ps.executeQuery();

                FlexElement structure = FlexUtils.getRDBMSTableDescriptionAsFlexElement(tablename, res);
                Enumeration en = structure.retrieveElements();
                while (en.hasMoreElements()) {
                    element.addElement((FlexElement) en.nextElement());
                }

                // add field aliases if any
                List fields = config.getList("flexoodb.table(" + j + ").field[@name]");
                for (int k = 0; k < fields.size(); k++) {
                    String fieldname = config.getString("flexoodb.table(" + j + ").field(" + k + ")[@name]");
                    String fieldalias = config.getString("flexoodb.table(" + j + ").field(" + k + ")[@alias]");

                    FlexElement fieldelement = new FlexElement(fieldname.trim().toLowerCase());
                    fieldelement.addAttribute((new FlexAttribute("alias")).setValue(fieldalias));

                    element.addElement(fieldelement);
                }

                _elements.put((alias != null ? alias : tablename), element);
                //_elements.put(tablename, element);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (conn == null) {
                throw new SQLException(
                        "Could not connect to " + (String) config.getString("database[@odbcurl]"));
            }
            _pool.releaseConnection(conn);
        }
    }

}

From source file:it.eng.spagobi.commons.utilities.ExecutionProxy.java

/**
 * Executes a document in background with the given profile.
 * /*w  w w  . j av a2  s. c o m*/
 * @param profile The user profile
 * @param modality The execution modality (for auditing)
 * @param defaultOutputFormat The default output format (optional)
 * , considered if the document has no output format parameter set
 * 
 * @return the byte[]
 */
public byte[] exec(IEngUserProfile profile, String modality, String defaultOutputFormat) {
    logger.debug("IN");
    byte[] response = new byte[0];
    try {
        if (biObject == null)
            return response;
        // get the engine of the biobject
        Engine eng = biObject.getEngine();
        // if engine is not an external it's not possible to call it using
        // url
        if (!EngineUtilities.isExternal(eng))
            if (eng.getClassName().equals("it.eng.spagobi.engines.kpi.SpagoBIKpiInternalEngine")) {
                SourceBean request = null;
                SourceBean resp = null;
                EMFErrorHandler errorHandler = null;

                try {
                    request = new SourceBean("");
                    resp = new SourceBean("");
                } catch (SourceBeanException e1) {
                    e1.printStackTrace();
                }
                RequestContainer reqContainer = new RequestContainer();
                ResponseContainer resContainer = new ResponseContainer();
                reqContainer.setServiceRequest(request);
                resContainer.setServiceResponse(resp);
                DefaultRequestContext defaultRequestContext = new DefaultRequestContext(reqContainer,
                        resContainer);
                resContainer.setErrorHandler(new EMFErrorHandler());
                RequestContainer.setRequestContainer(reqContainer);
                ResponseContainer.setResponseContainer(resContainer);
                Locale locale = new Locale("it", "IT", "");
                SessionContainer session = new SessionContainer(true);
                reqContainer.setSessionContainer(session);
                SessionContainer permSession = session.getPermanentContainer();
                //IEngUserProfile profile = new AnonymousCMSUserProfile();
                permSession.setAttribute(IEngUserProfile.ENG_USER_PROFILE, profile);
                errorHandler = defaultRequestContext.getErrorHandler();

                String className = eng.getClassName();
                logger.debug("Try instantiating class " + className + " for internal engine " + eng.getName()
                        + "...");
                InternalEngineIFace internalEngine = null;
                // tries to instantiate the class for the internal engine
                try {
                    if (className == null && className.trim().equals(""))
                        throw new ClassNotFoundException();
                    internalEngine = (InternalEngineIFace) Class.forName(className).newInstance();
                } catch (ClassNotFoundException cnfe) {
                    logger.error("The class ['" + className + "'] for internal engine " + eng.getName()
                            + " was not found.", cnfe);
                    Vector params = new Vector();
                    params.add(className);
                    params.add(eng.getName());
                    errorHandler.addError(new EMFUserError(EMFErrorSeverity.ERROR, 2001, params));
                    return response;
                } catch (Exception e) {
                    logger.error("Error while instantiating class " + className, e);
                    errorHandler.addError(new EMFUserError(EMFErrorSeverity.ERROR, 100));
                    return response;
                }
                try {
                    reqContainer.setAttribute("scheduledExecution", "true");
                    internalEngine.execute(reqContainer, biObject, resp);
                } catch (EMFUserError e) {
                    logger.error("Error during engine execution", e);
                    errorHandler.addError(e);
                } catch (Exception e) {
                    logger.error("Error while engine execution", e);
                    errorHandler.addError(new EMFUserError(EMFErrorSeverity.ERROR, 100));
                }
                return response;
            } else if (eng.getClassName().equals("it.eng.spagobi.engines.chart.SpagoBIChartInternalEngine")) {
                SourceBean request = null;
                EMFErrorHandler errorHandler = null;
                try {
                    request = new SourceBean("");
                } catch (SourceBeanException e1) {
                    e1.printStackTrace();
                }
                RequestContainer reqContainer = new RequestContainer();
                SpagoBIChartInternalEngine sbcie = new SpagoBIChartInternalEngine();

                // Call chart engine
                File file = sbcie.executeChartCode(reqContainer, biObject, null, profile);

                // read input from file
                InputStream is = new FileInputStream(file);

                // Get the size of the file
                long length = file.length();

                if (length > Integer.MAX_VALUE) {
                    logger.error("file too large");
                    return null;
                }

                // Create the byte array to hold the data
                byte[] bytes = new byte[(int) length];

                // Read in the bytes
                int offset = 0;
                int numRead = 0;
                while (offset < bytes.length
                        && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                    offset += numRead;
                }

                // Ensure all the bytes have been read in
                if (offset < bytes.length) {
                    logger.warn("Could not read all the file");
                }
                // Close the input stream and return bytes
                is.close();
                return bytes;
            } // end chart case
            else {
                return response;
            }
        // get driver class
        String driverClassName = eng.getDriverName();

        // build an instance of the driver
        IEngineDriver aEngineDriver = (IEngineDriver) Class.forName(driverClassName).newInstance();

        // get the map of parameter to send to the engine
        Map mapPars = aEngineDriver.getParameterMap(biObject, profile, "");
        if (defaultOutputFormat != null && !defaultOutputFormat.trim().equals("")) {
            List params = biObject.getBiObjectParameters();
            Iterator iterParams = params.iterator();
            boolean findOutPar = false;
            while (iterParams.hasNext()) {
                BIObjectParameter par = (BIObjectParameter) iterParams.next();
                String parUrlName = par.getParameterUrlName();
                List values = par.getParameterValues();
                logger.debug("processing biparameter with url name " + parUrlName);
                if (parUrlName.equalsIgnoreCase("outputType") && values != null && values.size() > 0) {
                    findOutPar = true;
                    break;
                }
            }
            if (!findOutPar) {
                mapPars.put("outputType", defaultOutputFormat);
            }
        }

        adjustParametersForExecutionProxy(aEngineDriver, mapPars, modality);

        // pass ticket ...
        String pass = SingletonConfig.getInstance().getConfigValue("SPAGOBI_SSO.PASS");
        if (pass == null)
            logger.warn("Pass Ticket is null");
        mapPars.put(SpagoBIConstants.PASS_TICKET, pass);

        // TODO merge with ExecutionInstance.addSystemParametersForExternalEngines for SBI_CONTEXT, locale parameters, etc...

        // set spagobi context
        if (!mapPars.containsKey(SpagoBIConstants.SBI_CONTEXT)) {
            String sbicontext = GeneralUtilities.getSpagoBiContext();
            if (sbicontext != null) {
                mapPars.put(SpagoBIConstants.SBI_CONTEXT, sbicontext);
            }
        }

        // set country and language (locale)
        Locale locale = GeneralUtilities.getDefaultLocale();
        if (!mapPars.containsKey(SpagoBIConstants.SBI_COUNTRY)) {
            String country = locale.getCountry();
            mapPars.put(SpagoBIConstants.SBI_COUNTRY, country);
        }
        if (!mapPars.containsKey(SpagoBIConstants.SBI_LANGUAGE)) {
            String language = locale.getLanguage();
            mapPars.put(SpagoBIConstants.SBI_LANGUAGE, language);
        }

        //set userId if it's a send mail operation (backend operation)
        if (sendMailOperation.equals(modality) || exportOperation.equals(modality)) {
            mapPars.put(SsoServiceInterface.USER_ID, ((UserProfile) profile).getUserUniqueIdentifier());
        }

        // adding SBI_EXECUTION_ID parameter
        if (!mapPars.containsKey("SBI_EXECUTION_ID")) {
            UUIDGenerator uuidGen = UUIDGenerator.getInstance();
            UUID uuidObj = uuidGen.generateTimeBasedUUID();
            String executionId = uuidObj.toString();
            executionId = executionId.replaceAll("-", "");
            mapPars.put("SBI_EXECUTION_ID", executionId);
        }

        // AUDIT
        AuditManager auditManager = AuditManager.getInstance();
        Integer auditId = auditManager.insertAudit(biObject, null, profile, "",
                modality != null ? modality : "");
        // adding parameters for AUDIT updating
        if (auditId != null) {
            mapPars.put(AuditManager.AUDIT_ID, auditId.toString());
        }

        // built the request to sent to the engine
        Iterator iterMapPar = mapPars.keySet().iterator();
        HttpClient client = new HttpClient();
        // get the url of the engine
        String urlEngine = getExternalEngineUrl(eng);
        PostMethod httppost = new PostMethod(urlEngine);
        while (iterMapPar.hasNext()) {
            String parurlname = (String) iterMapPar.next();
            String parvalue = "";
            if (mapPars.get(parurlname) != null)
                parvalue = mapPars.get(parurlname).toString();
            httppost.addParameter(parurlname, parvalue);
        }
        // sent request to the engine
        int statusCode = client.executeMethod(httppost);
        logger.debug("statusCode=" + statusCode);
        response = httppost.getResponseBody();
        logger.debug("response=" + response.toString());
        Header headContetType = httppost.getResponseHeader("Content-Type");
        if (headContetType != null) {
            returnedContentType = headContetType.getValue();
        } else {
            returnedContentType = "application/octet-stream";
        }

        auditManager.updateAudit(auditId, null, new Long(GregorianCalendar.getInstance().getTimeInMillis()),
                "EXECUTION_PERFORMED", null, null);
        httppost.releaseConnection();
    } catch (Exception e) {
        logger.error("Error while executing object ", e);
    }
    logger.debug("OUT");
    return response;
}