calling a ModuleRoot's function in other class

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.

0 votes

1 answers

1625 views

ANSWER



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);
0 votes