WCP: Get the Page Variants of a Page

I decided to create this blog post based on a quesiton I found on OTN WebCenter Portal Forum.

How to retrieve the Page Variants from a given Page Path programatically?

Page Variants are created in the same folder of the Page and it uses the following naming convenion:

[Source Page Name] +  "_" + [Device Group Name] + "jspx" 

Using the following code snippet you can get the Page Variants for the Current Page and then modify it as desired :)
public void changeVariantsOfCurrentPage() {
  // Get Page Service for editing Page Attributes
  PageService pageService = getEditModePageService(); 
  // Get Current Page Path
  final String pagePath = (String)ADFContext.getCurrent().getExpressionEvaluator().evaluate("#{pageDocBean.pagePath}");
  if ((pagePath != null) && (pagePath.length() > 0)) {
  // For each possible Device Agent detect if it has a pageVariant
  List<DeviceGroup> allDeviceGroups = getAllDeviceGroups();
  for (DeviceGroup dg : allDevicesGroups) {
   int iLastSlash = pagePath.lastIndexOf('/');
   int iLastDot = pagePath.lastIndexOf('.');
   String srcFolderPath = pagePath.substring(0, iLastSlash + 1);
   String srcPageName = pagePath.substring(iLastSlash + 1, iLastDot);
   String newPageTitle = pageService.getPage(pagePath).getTitle();
   String pageNameFormat = srcPageName + "_" + dg.getName() + ".jspx";
   String variantPagePath = srcFolderPath + pageNameFormat;
   PageDef variantPage = getEditModePageService().getPageDefAttributes(variantPagePath);
   // TODO: Check if it is not null and apply changes
   pageService.updatePageDefAttributes(variantPage);
   pageService.saveChanges();
  }
 }
}

private PageService getEditModePageService() {
    if (this.pageService == null) {
      MDSSession sess = getEditModeMDSSession();
      this.pageService = PageServiceFactory.createInstance(new PageServiceConfig(sess, ServiceContext().getContext().getScope().getName()));
    }
    return this.pageService;
  }

private MDSSession getEditModeMDSSession() {
    if (this.editSess == null) {
      this.editSess = WCApplicationContext.getCurrentInstance().createEditModeMDSSession();
    }
    return this.editSess;
}


private List<DeviceGroup> getAllDeviceGroups() {
    List<DeviceGroup> deviceGroups = null;
    try {
      DeviceSupport deviceSupport = DeviceSupport.getInstance();
      deviceGroups = deviceSupport.getAllDeviceGroups(ServiceContext.getContext().getScope());
    }
    catch (Exception e) {
  // TODO: Log
    }
    return deviceGroups;
}

Comments

Popular posts from this blog

OJET: Build and Deploy in an Application Server

OJET: Select All options using only Checkboxset

OFMW: 12.2.1.4 Released