Skip to content

Instantly share code, notes, and snippets.

@roanbester
Created January 5, 2016 11:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roanbester/0c1dafece0d0fac699e4 to your computer and use it in GitHub Desktop.
Save roanbester/0c1dafece0d0fac699e4 to your computer and use it in GitHub Desktop.
virtual-portal-scoped actions
package example.wcm.vpscopedaction;
import com.ibm.workplace.wcm.api.Category;
import com.ibm.workplace.wcm.api.Repository;
import com.ibm.workplace.wcm.api.VirtualPortalContext;
import com.ibm.workplace.wcm.api.exceptions.WCMException;
/**
* Hypothetical Portlet that runs the query against a VP to fetch Categories.
* This can be a servlet or REST service as well.
*/
public class CategoriesViewerPortlet extends javax.portlet.GenericPortlet {
@Override
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
try {
// Get the Repository and generate VP Context from our VP name.
Repository repository = WCM_API.getRepository();
VirtualPortalContext vpContext = repository.generateVPContextFromContextPath("virtualPortalName");
// execute our action on that VP
FindCategoriesAction findCategoriesAction = new FindCategoriesAction();
repository.executeInVP(vpContext, findCategoriesAction);
// extract the info
List<Category> categories = findCategoriesAction.getCategories();
request.setAttribute("categories", categories); // or a String[] by accessing Category.getName() or whatever.
} catch (VirtualPortalNotFoundException e) {
// log this out somewhere or handle it
} catch (WCMException e) {
// log this out somewhere or handle it
}
getPortletContext().getRequestDispatcher("some/jsp/path.jsp").include(request, response);
}
}
package example.wcm.vpscopedaction;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import com.ibm.workplace.wcm.api.DocumentLibrary;
import com.ibm.workplace.wcm.api.Repository;
import com.ibm.workplace.wcm.api.VirtualPortalContext;
import com.ibm.workplace.wcm.api.VirtualPortalScopedAction;
import com.ibm.workplace.wcm.api.WCM_API;
import com.ibm.workplace.wcm.api.Workspace;
import com.ibm.workplace.wcm.api.exceptions.QueryServiceException;
import com.ibm.workplace.wcm.api.exceptions.WCMException;
import com.ibm.workplace.wcm.api.Category;
import com.ibm.workplace.wcm.api.DocumentLibrary;
import com.ibm.workplace.wcm.api.Workspace;
import com.ibm.workplace.wcm.api.exceptions.QueryServiceException;
import com.ibm.workplace.wcm.api.query.ResultIterator;
import com.ibm.workplace.wcm.api.query.Selectors;
import com.ibm.workplace.wcm.api.query.SortDirection;
import com.ibm.workplace.wcm.api.query.Sorts;
/**
* Virtual Portal Scoped Action to demonstrate how to access WCM items on a different Virtual Portal than the one
* the user is currently in.
*/
public class FindCategoriesAction implements VirtualPortalScopedAction {
private static final String LIBRARY_NAME = "WCM Test library";
private List<Category> categories = new ArrayList<Category>();
@Override
public void run() throws WCMException {
// Create a Repository and an anonymous workspace (can also use an authenticated workspace, but we'll keep it simple for now)
Repository repository = WCM_API.getRepository();
Workspace workspace = repository.getAnonymousWorkspace();
workspace.login();
DocumentLibrary library = workspace.getDocumentLibrary(LIBRARY_NAME);
/* Constuct the query. For the example, we're fetching all Category items in our library, sorted ascending.
*/
com.ibm.workplace.wcm.api.query.Query query = workspace.getQueryService().createQuery();
query.addSelector(Selectors.typeIn(Category.class));
query.addSelector(Selectors.libraryEquals(library));
query.addSort(Sorts.byName(SortDirection.ASCENDING));
query.returnObjects();
/* Make the categories available to client code.
* This is needed as the run() method does not return a value.
*/
ResultIterator resultIterator = workspace.getQueryService().execute(query);
categories.clear();
while (resultIterator.hasNext()) {
categories.add((Category) resultIterator.next());
}
repository.endWorkspace();
}
public List<Category> getCategories() {
return categories;
}
}
@roanbester
Copy link
Author

Accessing WCM content on another virtual portal using virtual-portal-scoped actions

The code snippets explain how you can access WCM content (Content, Category, Library or any of the domain available in the WCM API) on another virtual portal.

Since service pack 13 (for Portal 8.0, added standard to Portal 8.5) a facility is provided to execute actions from one VP scope onto another. This makes it possible to expose WCM content from a library on another virtual portal in your portlet, or write a custom service (e.g. REST service) that can access WCM content without having the context of a VP to start with.

The code uses the WCM Query API to fetch Category items (from the WCM API) on another virtual portal and library, using a hypothetical portlet. The main players in the code are:

  • VirtualPortalScopedAction interface to implement. In the run() method you can access the WCM API as if you are running in the correct virtual portal.
  • Repository.generateVPContextFromContextPath which is a CF13+ method to generate/get a context for a virtual portal different than the one you're currently on.
  • Repository.executeInVP(VirtualPortalContext, VirtualPortalScopedAction) executes your implementation of VirtualPortalScopedAction on the required virtual portal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment