Chapter 8. Using a Content Type

8.1. Retrieve an Item From The Repository

Similar to querying subfolders of a parent folder into a FolderCollection, we can walk through the folder structure and examine all the items which are children of a given folder, returning an ItemCollection.

ItemCollection items = subFolder.getChildItems();

ContentItem oneContentItem;
BirdWatch oneBirdWatchItem;	

while (items.next()) {
  oneContentItem = items.getContentItem();
  oneBirdWatchItem = new BirdWatch(oneContentItem);

  _page.add(new Label("Sighting: " + oneBirdWatchItem.getSpecies() + \
oneBirdWatchItem.TYPE));

}

Since items.getContentItem() returns a ContentItem, and it cannot be downcast to a BirdWatch item, you need to convert it to a BirdWatch item with another BirdWatch constructor before you can call BirdWatch methods, such as getSpecies(), on it. Here is an example constructor:

/* Pass this a contentItem and returns it upcasted to a BirdWatch item */
public BirdWatch(ContentItem contentItem) throws \
DataObjectNotFoundException {

    this(new OID(TYPE, contentItem.getID() ) );

}

NoteNote
 

Realize that this is very brittle code, as it assumes that all the items in the FolderCollection are BirdWatch items. In a real, working Content Repository this might not be true.