Getting property values from a repository item fetched via RQL

This is just a quick snippet that shows how you can print all property values for a given ItemDescriptor. In this case, I wanted to print the properties for a given product id in the ProductCatalog repository.

    try {
      GSARepository r = (GSARepository)Nucleus.getGlobalNucleus().resolveName("/atg/commerce/catalog/ProductCatalog");
      RepositoryView view = r.getView("product");
      RqlStatement rql = RqlStatement.parseRqlStatement("id = ?0");
      Object params[] = new Object[1];
      params[0] = getProductId();
      RepositoryItem[] answer = rql.executeQuery(view,params);
      if (answer != null) {
        for(int i =0; i < answer.length; i++) {
          MutableRepository mr = (MutableRepository)answer[i].getRepository();
          MutableRepositoryItem mri = mr.getItemForUpdate(answer[i].getRepositoryId(),answer[i].getItemDescriptor().getItemDescriptorName());
          String[] properties = answer[i].getItemDescriptor().getPropertyNames();
          for (int j = 0; j <  properties.length; j++ ) {
            System.out.println(properties[j] + " " + answer[i].getPropertyValue(properties[j]));
          }
        }
      }
      else {
        System.out.println("No product found");
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.