There might be some scenarios where you want to get list of all the portlets on a portal page.
Following code snipped would be helpful for you to get list of portlets on a page.
private void listPortletsOnPage(ContentModel model, RenderRequest request, RenderResponse response) {
try {
ContentModel model = getContentModel(request, response)
Locator locator = model.getLocator();
ObjectID oID = PortletURLHelper.getCurrentPage(request, response);
ContentPage cPage = (ContentPage)locator.findByID(oID);
PrintWriter out = response.getWriter();
List allPortlets = getPortletsOnPage(model, cPage);
Iterator it = allPortlets.iterator();
while(it.hasNext()){
PortletWindow window = (PortletWindow)it.next();
out.print("PortletWindow " + window.getObjectID() + "
");
}
} catch (StateException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private ContentModel getContentModel(RenderRequest renderRequest, RenderResponse renderResponse) {
try {
ContentModelProvider provider = (ContentModelProvider) psh.getPortletService(ContentModelProvider.class);
return provider.getContentModel(renderRequest, renderResponse);
} catch (PortletServiceUnavailableException e) {
e.printStackTrace();
} catch (ModelException e) {
e.printStackTrace();
}
return null;
}
public List getPortletsOnPage(final ContentModel aContentModel, final ContentPage aPage) {
List result = Collections.EMPTY_LIST;
final LayoutModel aLayoutModel = aContentModel.getLayoutModel(aPage);
if (aLayoutModel != null) {
result = new ArrayList();
try {
collector(aLayoutModel, aLayoutModel.getRoot(), result);
} catch (ModelException e) {
e.printStackTrace();
}
}
return result;
}
try {
ContentModel model = getContentModel(request, response)
Locator locator = model.getLocator();
ObjectID oID = PortletURLHelper.getCurrentPage(request, response);
ContentPage cPage = (ContentPage)locator.findByID(oID);
PrintWriter out = response.getWriter();
List allPortlets = getPortletsOnPage(model, cPage);
Iterator it = allPortlets.iterator();
while(it.hasNext()){
PortletWindow window = (PortletWindow)it.next();
out.print("PortletWindow " + window.getObjectID() + "
");
}
} catch (StateException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private ContentModel getContentModel(RenderRequest renderRequest, RenderResponse renderResponse) {
try {
ContentModelProvider provider = (ContentModelProvider) psh.getPortletService(ContentModelProvider.class);
return provider.getContentModel(renderRequest, renderResponse);
} catch (PortletServiceUnavailableException e) {
e.printStackTrace();
} catch (ModelException e) {
e.printStackTrace();
}
return null;
}
public List getPortletsOnPage(final ContentModel aContentModel, final ContentPage aPage) {
List result = Collections.EMPTY_LIST;
final LayoutModel aLayoutModel = aContentModel.getLayoutModel(aPage);
if (aLayoutModel != null) {
result = new ArrayList();
try {
collector(aLayoutModel, aLayoutModel.getRoot(), result);
} catch (ModelException e) {
e.printStackTrace();
}
}
return result;
}
3 comments:
and how does the collector method looks like?
Hi ,
can you please tell me , what all jars to import to get your code working?
what is collector under the method getPortletsOnPage?
Post a Comment