Menu

Freemarker Template Error While Type Cast From Super Class to Sub-Class Object

Help
DEBA
2014-10-30
2014-11-04
  • DEBA

    DEBA - 2014-10-30

    Hi

    I have been using FT seen a long time but first time i got stuck with one issue which i wanted to resolve it here. I know there are lot of people who would have came across with the same situation and it will great if anyone can help me with here to resolve the issue.

    My Design : I am passing a Object called Accounts to my ftl which will contain list of Account objects. Account has one attribute called Category which will define the type of Account(LOAN/DEPOSITE etc...). If its a loan account i wanted to display the NextPaymentDate and NextAmountDue in me xml. For that i have to check if its type "LOAN" than convert Account object to LoanAccount. Because LoanAccount is the sub-class which has those two properties which i wanted.

    Thanks in advance...

    Thanks,
    Deba

    Similar Java Code :
    for (Account aact : newaccountList) {
    if("LOAN".equals(aact.getCategory().value())){
    LoanAccount lAct = (LoanAccount) aact;
    if(null != lAct.getNextPaymentAmount()){
    lAct.getNextPaymentAmount().getAmount();
    }
    }
    }

    Freemarker Template:

    <@compress single_line=true> <#assign fsgCalls = "accounts"> <File Version="1.0"> <AccountList> <#if accounts?exists && accounts.getAccount()?exists> <#assign accountList = accounts.getAccount()> <#list accountList as account> <Account> <#if account?exists && account.getAccountNumber()?exists> <AccountNumber>${account.getAccountNumber().getHostValue()}</AccountNumber> <AccountType>${account.getAccountType()}</AccountType> <NickName>${account.getNickName()}</NickName>
            <#if account.getCategory().value() == "LOAN">
                <#assign loanAccount = (LoanAccount) account>   
               <#if loanAccount?exists>
                   <#if loanAccount.getNextPaymentDate()?exists>
                        <NextPaymentDate>${loanAccount.getNextPaymentDate().getTime().toString()}</NextPaymentDate>
                   </#if>   
                   <#if loanAccount.getNextPaymentAmount()?exists>
                        <AmountDue>${nextPaymentAmount.getAmount()}</AmountDue>
                   </#if>
                </#if>
            </#if>
    
            <#if account.getBalance()?exists>
               <#assign balance = account.getBalance()>
                <#if balance.getAvailableBalance()?exists>
                    <AvailableBalance>${balance.getAvailableBalance().getAmount()}</AvailableBalance>
                </#if>
                <#if balance.getCurrentBalance()?exists>
                    <CurrentBalance>${balance.getCurrentBalance().getAmount()}</CurrentBalance>
                </#if>    
            </#if>
          </Account>
      </#list>
    


    </AccountList>
    </File>
    /@compress

    Error:

    2014-10-30 14:03:49,382 ERROR com.intuit.tpv.utils.TPVUtils [V0000->04715:sdpdi1:V1-244-Q3oqo1zh9osmatb71me1br] - IOException while preparing final pay load
    freemarker.core.ParseException: Encountered "account >" at line 1, column 782 in TPV_ST.
    Was expecting one of:
    "in" ...
    ">" ...
    <EMPTY_DIRECTIVE_END> ...
    "." ...
    "[" ...
    "(" ...
    "?" ...
    "!" ...
    <TERMINATING_EXCLAM> ...
    "??" ...
    "+" ...
    "-" ...
    "*" ...
    "/" ...
    "%" ...
    "!=" ...
    "=" ...
    "==" ...
    ">=" ...
    <ESCAPED_GTE> ...
    ">" ...
    <ESCAPED_GT> ...
    <LESS_THAN_EQUALS> ...
    <LESS_THAN> ...
    ".." ...
    <AND> ...
    <OR> ...
    "," ...
    <ID> "=" ...

    at freemarker.core.FMParser.generateParseException(FMParser.java:4697) [nestedjar7298345289082707254.tmp:2.3.18]
    at freemarker.core.FMParser.jj_consume_token(FMParser.java:4568) [nestedjar7298345289082707254.tmp:2.3.18]
    
     
  • Dániel Dékány

    You are showing a parse exception here, so it can't be related to subclassing or such. Check line 1, column 782 (that's a long line...) in TPV_ST, or if you don't see why is that syntactically wrong, quote the interesting part.

     
  • DEBA

    DEBA - 2014-10-31

    Hi Daniel,

    Sorry if i am confusing with the details provided here.

    I am attaching the screenshot and the FTL once again.

    Now i have added the FTL tool plugin in my eclipse, and i see the error in that line which i am seeing in the log when i am running the application. I no need to test teh template to get the error, its visible in the FTL tool editor only.

    I have attached the screenshot which will show the error details clearly.

    FTL:::

    <@compress single_line=true> <#assign fsgCalls = "accounts"> <File Version="1.0"> <AccountList> <#if accounts?exists && accounts.getAccount()?exists> <#assign accountList = accounts.getAccount()> <#list accountList as account> <Account> <#if account?exists && account.getAccountNumber()?exists> <AccountNumber>${account.getAccountNumber().getHostValue()}</AccountNumber> <AccountType>${account.getAccountType()}</AccountType> <NickName>${account.getNickName()}</NickName>
            <#if account.getCategory().value() == "LOAN">
                <#assign loanAccount = (LoanAccount)account>    
               <#if loanAccount?exists>
                   <#if loanAccount.getNextPaymentDate()?exists>
                        <NextPaymentDate>${loanAccount.getNextPaymentDate().getTime().toString()}</NextPaymentDate>
                   </#if>   
                   <#if loanAccount.getNextPaymentAmount()?exists>
                        <AmountDue>${loanAccount.getNextPaymentAmount.getAmount()}</AmountDue>
                   </#if>
                </#if>
            </#if>
    
            <#if account.getBalance()?exists>
               <#assign balance = account.getBalance()>
                <#if balance.getAvailableBalance()?exists>
                    <AvailableBalance>${balance.getAvailableBalance().getAmount()}</AvailableBalance>
                </#if>
                <#if balance.getCurrentBalance()?exists>
                    <CurrentBalance>${balance.getCurrentBalance().getAmount()}</CurrentBalance>
                </#if>    
            </#if>
          </Account>
      </#list>
    


    </AccountList>
    </File>
    /@compress

     
  • Dániel Dékány

    FTL is not a statically typed language, so you don't need to cast, and you can't cast either.

     
  • DEBA

    DEBA - 2014-11-03

    So what is teh alternative way to handle this casting in FTL ? Or any other way i can fetch the value from Account object in FTL ?

    I am not getting any clue how can i read the value from nextPaymentAmount node from the Object.

    My Response XML will be like below :

    <ns8:account xsi:type="ns8:LoanAccount">
    <ns8:id>fQrp-Uxro6q7vhrKJ9K9ytKEzd-ka3yrW3L-rSnyO4Q</ns8:id>
    <ns8:fiCustomerId>c0a8e4400000d9cc4f21cf47130f3b00</ns8:fiCustomerId>
    <ns8:fiId>DI4715</ns8:fiId>
    <ns8:description>CREDIT_CARD</ns8:description>
    <ns8:nickName>CREDIT_CARD</ns8:nickName>
    <ns8:displayAccountNumber>*1234</ns8:displayAccountNumber>
    <ns8:accountNumber>
    <ns8:hostValue>641234</ns8:hostValue>
    <ns8:displayValue>641234</ns8:displayValue>
    <ns8:pfmValue>641234</ns8:pfmValue>
    <ns8:rawHostValue>641234</ns8:rawHostValue>
    </ns8:accountNumber>
    <ns8:category>LOAN</ns8:category>
    <ns8:accountType>CREDIT_CARD_LOAN</ns8:accountType>
    <ns8:fiAccountType>
    <ns8:type>64</ns8:type>
    <ns8:description>Credit Card</ns8:description>
    </ns8:fiAccountType>
    <ns8:ownershipType>PRIMARY</ns8:ownershipType>
    <ns8:balance>
    <ns8:currentBalance>
    <ns4:amount>0.0</ns4:amount>
    </ns8:currentBalance>
    <ns8:availableBalance>
    <ns4:amount>34536.45</ns4:amount>
    </ns8:availableBalance>
    <ns8:lastStatementBalance>
    <ns4:amount>588.6</ns4:amount>
    </ns8:lastStatementBalance>
    </ns8:balance>
    <ns8:accountStatus>OPEN</ns8:accountStatus>
    <ns8:accountStatuses>
    <ns8:open>true</ns8:open>
    <ns8:closed>false</ns8:closed>
    <ns8:negativeBalance>false</ns8:negativeBalance>
    <ns8:delinquent>false</ns8:delinquent>
    <ns8:inCollection>false</ns8:inCollection>
    <ns8:overLimit>false</ns8:overLimit>
    <ns8:writtenOff>false</ns8:writtenOff>
    <ns8:creditBalance>false</ns8:creditBalance>
    <ns8:paymentCoupon>false</ns8:paymentCoupon>
    <ns8:retirementPlan>false</ns8:retirementPlan>
    <ns8:retPlanOwnedByDeceased>false</ns8:retPlanOwnedByDeceased>
    </ns8:accountStatuses>
    <ns8:displayFlag>
    <ns8:summary>true</ns8:summary>
    <ns8:transferFrom>true</ns8:transferFrom>
    <ns8:transferTo>true</ns8:transferTo>
    <ns8:isHistoryEnabled>true</ns8:isHistoryEnabled>
    </ns8:displayFlag>
    <ns8:interestYearToDate>
    <ns4:amount>12.34</ns4:amount>
    </ns8:interestYearToDate>
    <ns8:interestPriorYearToDate>
    <ns4:amount>3.12</ns4:amount>
    </ns8:interestPriorYearToDate>
    <ns8:enabled>true</ns8:enabled>
    <ns8:accountHidden>false</ns8:accountHidden>
    <ns8:memberNumber>depositslip</ns8:memberNumber>
    <ns8:ccAccountId>XX1234^64</ns8:ccAccountId>
    <ns8:nextPaymentAmount>
    <ns4:amount>34.53</ns4:amount>
    </ns8:nextPaymentAmount>
    </ns8:account>

     
  • Dániel Dékány

    If for example obj has a getNextPaymentAmount() method, then you just write obj.nextPaymentAmount. (If you want to query if obj has such property, you can do it like obj.nextPaymentAmount??.)

    Note that both the class and the method must be public for this to work.

     
  • DEBA

    DEBA - 2014-11-04

    The methods are available in the sub-class not in the parent class. That's the reason i am getting error when i am trying to read the value from the response. To read that i need to get the sub-class object (LoanAccount).

    Any idea how i can get the sub-class object within freemarker syntax ?

     
  • Dániel Dékány

    I think you misunderstand how subclassing works in Java. You should access the LoanAccount members without telling FreeMarker that the object is a LoanAccount (even if in Java it was only told that it's an Account). There's some other oversight there.

     
  • DEBA

    DEBA - 2014-11-04

    I understood clearly about sub-classing. You cant access the properties or behaviour from a subclass when you are dealing with the super class object.

    I also tried doing that in freemarker template and i am getting error saying nextPaymentAmount is not defined at line 1.

    Do you have a logic which i can use it to read the values from subclass object ? That will be really great.

    Thanks in advance

     
  • pramod tyagi

    pramod tyagi - 2017-06-29

    Deba Did you get the solution for your problem.
    I am stuck with similar case where i have some objects in list and i have to typecast before using them in FTL.

     

Log in to post a comment.