Thursday 25 October 2018

Remove Duplicate Values from Array AND Sort the Dates in Ascending Order in ESQL

1.To Remove Duplicate Values from Array


declare tableref ROW;

create field tableref.{}

for ref AS environment.Variables.Message[] DO
declare temp char ref.Value;
create field tableref.{temp};
END FOR;

OR


DECLARE i,j INTEGER 1;

        DECLARE     lastvalue ROW;

        FOR  Finalref AS Environment.Variables.Response.ADACODE[] DO
             DECLARE flref REFERENCE TO Finalref;
             SET Environment.ingh[i].value[] =  SELECT f.AdaCd FROM  
             lastvalue.ADACODE[] as f WHERE f.AdaCd = FIELDVALUE(Finalref.AdaCd);

             IF  NOT EXISTS(Environment.ingh[i].value[]) THEN
                        SET lastvalue.ADACODE[j].AdaCd = Finalref.AdaCd;
                        SET j = j+1;
             END IF;
             SET i = i+1;
       END FOR;

2.Sort the Dates in Acending Order in ESQL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>  
  <xsl:template match="*[local-name()='DepartureDatesSorting']">
      <DepartureDatesSorting>
            <xsl:apply-templates select="*[local-name()='Item']" >                        
                <xsl:sort select="translate(EventDetails/Departure/EventDate,'-','')" data-type="number" order="ascending"/>
            </xsl:apply-templates>
      </DepartureDatesSorting>
      </xsl:template>
</xsl:stylesheet>

-- Sorting number


DECLARE Count INTEGER 1;
DECLARE rInputRef REFERENCE TO InputRoot.XMLNSC.Member.Name[1];
WHILE  LASTMOVE(rInputRef) DO
SET Environment.Variables.Array[Count] = rInputRef.Age;
SET Count = Count + 1;
MOVE rInputRef NEXTSIBLING REPEAT TYPE NAME;
END WHILE;

DECLARE totalElements INTEGER CARDINALITY(Environment.Variables.Array[]);
DECLARE i INTEGER 0;
WHILE i < totalElements-1 DO
SET i = i + 1;
DECLARE j INTEGER 0;
WHILE j <= totalElements-i-1  DO
SET j = j + 1;
IF (Environment.Variables.Array[j] >  Environment.Variables.Array[j+1]) THEN
DECLARE temp INTEGER;
SET temp = Environment.Variables.Array[j];
SET Environment.Variables.Array[j] = Environment.Variables.Array[j+1];
SET Environment.Variables.Array[j+1] = temp;
END IF;

END WHILE;


END WHILE;

--Sorting Dates

 DECLARE Count INTEGER 1;
DECLARE rInputRef REFERENCE TO InputRoot.XMLNSC.Member.Name[1];
  --Storing Input message dates to Environment Variable
WHILE  LASTMOVE(rInputRef) DO
  SET Environment.Variables.Date[Count] = rInputRef.Age;
  SET Count = Count + 1;
  MOVE rInputRef NEXTSIBLING REPEAT TYPE NAME;
END WHILE;

--Logic to sort the dates using bubble sort
  DECLARE totalElements INTEGER CARDINALITY(Environment.Variables.Date[]);
  DECLARE i INTEGER 0;
  DECLARE pattern CHARACTER 'dd-MM-yyyy';
  DECLARE dFirstDate,dSecondDate DATE;
  WHILE i < totalElements-1 DO
  SET i = i + 1;
  DECLARE j INTEGER 0;
  WHILE j <= totalElements-i-1  DO
    SET j = j + 1;
DECLARE Date1 CHARACTER Environment.Variables.Date[j];
DECLARE Date2 CHARACTER Environment.Variables.Date[j+1];
--Converting to Date Format
SET dFirstDate = CAST(Date1 AS DATE FORMAT pattern);
SET dSecondDate = CAST(Date2 AS DATE FORMAT pattern);
--If First Date is Greater then Second date Swap the dates
  IF dFirstDate  > dSecondDate THEN
   DECLARE temp CHARACTER;
   SET temp = Environment.Variables.Date[j];
   SET Environment.Variables.Date[j] = Environment.Variables.Date[j+1];
   SET Environment.Variables.Date[j+1] = temp;
    END IF;
  END WHILE;
  END WHILE;

    SET OutputRoot.XMLNSC.SortedDates.Dates = Environment.Variables;