cancel
Showing results for 
Search instead for 
Did you mean: 

calling a ModuleRoot's function in other class

adam_bo_
Star Contributor
Star Contributor

I have created a new class:

@Path("/pidedit")
@WebObject(type = "pidedit")
public class PIDEditorModule extends ModuleRoot {
    public static final String PID_FIELD = "note:note";

    @GET
    @Path(value = "{id}/html")
    @Produces("text/html;charset=UTF-8")
    public Object getShow(@PathParam("id") String id)
            throws ClientException {
	
        IdRef ref = new IdRef(id);
        CoreSession session = ctx.getCoreSession();

        if (session.exists(ref)) {
        	if (session.hasPermission(ref, SecurityConstants.READ)) {
                DocumentModel doc = session.getDocument(ref);
                log.debug(doc.getPropertyValue(PID_FIELD));
            
                return getTemplate("show.ftl").arg("uid", id);
            }
            return Response.status(Status.UNAUTHORIZED).build();
        }
        return Response.status(Status.NOT_FOUND).build();        
    }

    public String getPreview(String id)
            throws ClientException {
    return getTemplate("show.ftl").arg("uid", id).toString();
    }    
}

The getShow(..) function works as it is expected, but I want to call the getPreview(..) function in a new PIDPreviewer class which extends AbstractPreviewer and I can not find a way to do it properly. I have tried something like this:

        ModuleManager moduleManager = Framework.getLocalService(WebEngine.class).getModuleManager();        	
        ModuleConfiguration moduleConfig = moduleManager.getModule("pidedit");
        Module mod =  moduleConfig.get(); 

but finally I have a trouble to create the instance of the PIDEditorModule class with a context.

Any help is going to be appreciated.

1 REPLY 1

adam_bo_
Star Contributor
Star Contributor

Ok. I have solved the problem in this way:

        ModuleManager moduleManager = Framework.getLocalService(WebEngine.class).getModuleManager();        	
        ModuleConfiguration moduleConfig = moduleManager.getModule("pidedit");
        Module mod =  moduleConfig.get();
        
        WebContext ctx = mod.getEngine().getActiveContext();
        HttpServletRequest request = new HttpSRW();
        ctx = new DefaultContext(request);
        ((DefaultContext) ctx).setModule(mod);
   
        PIDEditorModule preview = (PIDEditorModule)mod.getRootObject(ctx);
        String previewStr = ((PIDEditorModule)preview).getPreview(dm.getId());
        
    	htmlPage.append(previewStr);