/**
  * poplulate declared parameters on temp synapse properties
  *
  * @param synCtx
  * @param templateQualifiedName
  */
 private void populateParameters(MessageContext synCtx, String templateQualifiedName) {
   Iterator<String> params = pName2ExpressionMap.keySet().iterator();
   while (params.hasNext()) {
     String parameter = params.next();
     if (!"".equals(parameter)) {
       Value expression = pName2ExpressionMap.get(parameter);
       if (expression != null) {
         EIPUtils.createSynapseEIPTemplateProperty(
             synCtx, templateQualifiedName,
             parameter, expression);
       }
     }
   }
 }
  /**
   * Get the aggregated message from the specified Aggregate instance
   *
   * @param aggregate the Aggregate object that holds collected messages and properties of the
   *     aggregation
   * @return the aggregated message context
   */
  private MessageContext getAggregatedMessage(Aggregate aggregate) {

    MessageContext newCtx = null;
    StatisticsRecord destinationStatRecord = null;

    for (MessageContext synCtx : aggregate.getMessages()) {

      if (newCtx == null) {
        try {
          newCtx = MessageHelper.cloneMessageContextForAggregateMediator(synCtx);
          destinationStatRecord =
              (StatisticsRecord) newCtx.getProperty(SynapseConstants.STATISTICS_STACK);
        } catch (AxisFault axisFault) {
          handleException("Error creating a copy of the message", axisFault, synCtx);
        }

        if (log.isDebugEnabled()) {
          log.debug("Generating Aggregated message from : " + newCtx.getEnvelope());
        }

      } else {
        try {
          if (log.isDebugEnabled()) {
            log.debug(
                "Merging message : "
                    + synCtx.getEnvelope()
                    + " using XPath : "
                    + aggregationExpression);
          }

          EIPUtils.enrichEnvelope(
              newCtx.getEnvelope(), synCtx.getEnvelope(), synCtx, aggregationExpression);
          if (destinationStatRecord != null
              && synCtx.getProperty(SynapseConstants.STATISTICS_STACK) != null) {
            // Merge the statistics logs to one single message
            // context.
            mergeStatisticsRecords(
                (StatisticsRecord) synCtx.getProperty(SynapseConstants.STATISTICS_STACK),
                destinationStatRecord);
          }

          if (log.isDebugEnabled()) {
            log.debug("Merged result : " + newCtx.getEnvelope());
          }

        } catch (JaxenException e) {
          handleException(
              "Error merging aggregation results using XPath : " + aggregationExpression.toString(),
              e,
              synCtx);
        } catch (SynapseException e) {
          handleException(
              "Error evaluating expression: " + aggregationExpression.toString(), e, synCtx);
        }
      }
    }

    // Enclose with a parent element if EnclosingElement is defined
    if (enclosingElementPropertyName != null) {

      if (log.isDebugEnabled()) {
        log.debug(
            "Enclosing the aggregated message with enclosing element: "
                + enclosingElementPropertyName);
      }

      Object enclosingElementProperty = newCtx.getProperty(enclosingElementPropertyName);

      if (enclosingElementProperty != null) {
        if (enclosingElementProperty instanceof OMElement) {
          OMElement enclosingElement = ((OMElement) enclosingElementProperty).cloneOMElement();
          EIPUtils.encloseWithElement(newCtx.getEnvelope(), enclosingElement);

          return newCtx;

        } else {
          handleException(
              "Enclosing Element defined in the property: "
                  + enclosingElementPropertyName
                  + " is not an OMElement ",
              newCtx);
        }
      } else {
        handleException(
            "Enclosing Element property: " + enclosingElementPropertyName + " not found ", newCtx);
      }
    }

    if (MessageFlowTracingDataCollector.isMessageFlowTracingEnabled()) {
      List<String> newMessageFlowTrace = new ArrayList<String>();
      for (MessageContext synCtx : aggregate.getMessages()) {
        List<String> messageFlowTrace =
            (List<String>) synCtx.getProperty(MessageFlowTracerConstants.MESSAGE_FLOW);
        if (null != messageFlowTrace) {
          newMessageFlowTrace.addAll(messageFlowTrace);
        }
      }
      newCtx.setProperty(MessageFlowTracerConstants.MESSAGE_FLOW, newMessageFlowTrace);
    }

    return newCtx;
  }