Expand my Community achievements bar.

How to send information from a backend file to another backend file.

Avatar

Level 5

Hello, what happens is that I have a template that has a java model with information (name, description, etc), this model receives the information from AiO and then map it in the frontend html file, but what I want to implement in the frontend is a button that prints a pdf file in the using information from the java model of the template.

 

The creation of the pdf is done in a java file using a library called itext7 to create the pdf and to be able to call this function from the frontend I use a JavaScript file to make the request by ajax.

 

But I have the problem that to make the pdf I need to use the information of the template model, my question then is: how do I pass the information of the java model of the template to my java servlet that makes the pdf?

 

I try to send the information of the java model of the template to the java servlet that makes the pdf but through an interface but java doesn't allow it, because the java model of the template is a class and it doesn't allow an interface to extend a class.

 

This is the diagram of how I think the logic should work:

 

The info is on productDetailImpl.java and needs to be send to getPDFServlet.java

 

Aaron_Dempwolff_0-1697065831511.png

 

8 Replies

Avatar

Community Advisor

You can try creating model using interface pattern, example, core components



Arun Patidar

Avatar

Community Advisor

1. Create a service and serviceImpl with all the logics

2. create a Sling Model to use service

3. Create a servket GetPDF and use service, if you need to pass the information , pass as get/post payload



Arun Patidar

Avatar

Administrator

@Aaron_Dempwolff  Did you find the suggestions from @arunpatidar helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Avatar

Level 5

Hello, if I have a doubt in the step:

Create a servket GetPDF and use service, if you need to pass the information , pass as get/post payload.

How to use a service to call from the template model class to the java servlet where I generate my pdf?

And how do I use or map the information with get/post payload, should I call the java model template or how is it done?

Avatar

Community Advisor

Hi @Aaron_Dempwolff 
Could you please explain the business requirement in more detail here?

 

However, you can use osgi services directly in the sling model using @slingObject annotatation.

When you click on the button to call servelt , there you can pass the parameters from Model to HTL to  servlet.



Arun Patidar

Avatar

Level 5

They want to add a button in the frontend of the template that when clicking on it generates and downloads a pdf with the information that is in that template (that information comes from the AiO, the user doesn't enter anything).

As I must create a structure and design for it, we use the itext7 library that allows to create pdf, for that, I create a servlet in our code that when you click on the frontend, send to call that servlet, but to generate the pdf we need the information, that information is already ready in the template model, how would I call the servlet method that generates the pdf from the frontend sending the template model information? this is the example code that I use:

-----------------------------------

ProductViewModel.java (template model):

public class ProductViewModel implements ProductInterface{

 
    @Self
    @Via(type = ResourceSuperType.class)
    private Product product;
   
    @Inject
    protected ResourceResolverFactory resourceResolverFactory;
 
    private String quantity;
   
    private String weight;
   .....
.......
.....
 
@Override
    public String getWeight() {
        return weight;
    }
 
@Override
    public String getQuantity() {
        return quantity;
    }

}

-----------------------------------

productTemplate.html (frontend):

<div class="tfs-popover">
                   <button id="download-or-print" class="tfs-popover__button icon-button" data-toggle="popover" data-placement="bottom" data-original-title="" title="" aria-label="Print page">
                     <span class="fa-solid fa-file-arrow-down fa-2xl"></span>
                   </button>
                   <div class="hidden popover-content">
                       <div class="popover-title" style="display:none">
                           
                       </div>
                       <div class="popover-body">
                           <p>Download Product Details</p>
                       </div>
                  </div>
               </div>

-----------------------------------

getPDFServlet.java (servlet in the code that have the pdf logic to create the structure and design):

@Component(service = {Servlet.class})
@SlingServletResourceTypes(
        resourceTypes = "tfs/components/page",
        selectors = "downloadpdf",
        methods = HttpConstants.METHOD_GET,
        extensions = "html")

@ServiceDescription("Get PDF Servlet")
public class GetPDFServlet extends SlingSafeMethodsServlet {

    private static final Logger LOG = LoggerFactory.getLogger(GetPDFServlet.class);

    @Reference
    PdfDownloadEndpointService pdfService;

    String responseJson = StringUtils.EMPTY;
   
   
    public Document generatePDF() {
       
        // create pdf here, but I need to take the information from productViewModel.java to map it in the pdf

    }

}

Avatar

Community Advisor

You can try adapting resource in the servlet to Model.

1. get Resource from request in the servlet

2. Adapt to Product Model



Arun Patidar