java - How to create a dynamic object based on different entity

I have created two fetch methods

  1. to get Invoice Details
  2. to get Sales order details

I got the result set as list. I need to create common method to send an email (the email method should be generic) by passing the invoice, Sales, etc. arguments to send an email, without creating a common entity is there any other way to solve this. Can any one help me out?

    List<Tableinvoiceheader> inv_List=rpc.getInvoiceDetails();
    List<TableSalesorder> sales_List=rpc.getsalesDetails();
 --email trigger method
     mailObj.sendemailMessage(From,to,subject,body,"(Generic object)");

Generic object can be either salesdetails or invoicedetails

1 Answer

  1. Leonard- Reply

    2019-11-13

    Not sure what you mean by not creating a Generic Entity. Here is one way:

    You will first need to create a java interface for converting an object to text suitable for email. Let us call this Interface Email

    A sample prototype quality code:

    public interface TextForEmail {
    
    String getText();
    }
    

    Your InvoiceDetail and SalesDetail classes should implement this interface. Their implementations should take their respective object properties and convert it into text suitable for email text.

    Your sendMailMessage signature needs to accept the interface as a parameter and that is the Generic Object. You could also do a List of the interface if you choose so.

    You should then invoke sendMailMessage passing the SalesDetails or InvoiceDetails or both (depending on how you implemented your sendMailMessage as your "Generic Object", and what went into your Generic Object List) as parameter(s).

    Your sendMailMessage implementation will then need to call the getText method to get the text and append it to the email as appropriate.

    Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>