Search:

Apache » Lenya
project logo
  • Project
  • Developer
  • Community
  • Version 2.0
  • Version 1.2
  • Version 2.0
    • FAQs
    • Installation
      • Download
      • Subversion Access
      • Install Instructions
    • Tutorials
      • Create a Publication
      • Create a Resource Type
        • Declaration
        • Creation
        • Presentation
        • Editing (One-Form)
        • Editing (BXE)
      • Implement a Usecase
        • Prerequisites
        • The Usecase
      • Setting up Eclipse
      • Proxying
        • Proxying
        • mod_proxy
        • mod_proxy_ajp
      • Best Practises
      • Production Checklist
      • Writing Tests
    • Concepts
      • Publication
      • Working with Documents
      • Authoring and Live mode
      • WYSIWYG
    • Technical Reference
      • Overview of Lenya Sitemaps
      • Repository
      • Access Control Specification
      • Usecase Framework
        • Overview
        • AbstractUsecase
      • Publications
        • Configuration
        • Publication Templating
      • Resource Types
      • Modules
      • Meta data
      • Protocols
        • lenya Protocol
        • lenya-document Protocol
        • site Protocol
        • lenyadoc Protocol
        • fallback Protocol
      • URLs and Links
        • URL Mapping
        • Link Management
      • Clustering
    • Core API
    • Core Modules
      • ac
        • Overview
        • API
      • acusecases
        • Overview
        • API
      • administration
        • Overview
        • API
      • cache
        • Overview
        • API
      • janitor
        • Overview
        • API
      • ldap
        • Overview
        • API
      • linking
        • Overview
        • API
      • observation
        • Overview
        • API
      • properties
        • Overview
        • API
      • sitemanagement
        • Overview
        • API
      • templating
        • Overview
        • API
      • usecase
        • Overview
        • API
      • workflow
        • Overview
        • API
    • Standard Modules
      • blog
        • Overview
        • API
      • bxe
        • Overview
      • cforms
        • Overview
        • API
      • collection
        • Overview
        • API
      • contactform
        • Overview
        • API
      • development
        • Overview
        • API
      • editors
        • Overview
        • API
      • export
        • Overview
        • API
      • fckeditor
        • Overview
        • API
      • kupu
        • Overview
        • API
      • languageselector
        • Overview
      • lenyadoc
        • Overview
        • API
      • linkcheck
        • Overview
        • API
      • links
        • Overview
        • API
      • lucene
        • Overview
        • API
      • migration
        • Overview
        • API
      • navigation
      • neutron
        • Overview
      • news
        • Overview
        • API
      • notification
        • Overview
        • API
      • opendocument
        • Overview
        • API
      • prettyprinting
        • Overview
      • profiling
        • Overview
      • resource
        • Overview
        • API
      • simplesite
        • Overview
        • API
      • sitetree
        • Overview
        • API
      • sourcerepository
        • Overview
        • API
      • svg
        • Rounded Corners
        • Resizing Images
        • API
      • tinymce
        • Overview
        • API
      • usecasedocument
        • Overview
        • API
      • webdav
        • Getting started
        • Monitoring
        • WebDAV Servers
        • API
      • xhtml
        • Overview
        • API
      • xopus
        • Overview

Current Event

Built with Apache Lenya

Working with Documents

Table of Contents
  • The Session
  • The Document Factory
  • Browsing Content and Site Structure
  • FAQ
    • What is the preferred method in lenya to get all documents of a certain resource type?
    • How can I control the caching of the this call, since I can use a cached version of the listing until one of the documents get edit?

This document shows some simple scenarios to access the Lenya repository. For more information, refer to the repository documentation.

The Session

An o.a.l.cms.repository.Session is a temporary container for repository nodes which you want to work with. If you want to change or remove nodes - for instance in a usecase handler - you have to start a transaction. To avoid overriding or losing someone else's changes, you should lock any nodes which are potentially affected or read during your transaction.

A convenient way to get the session which is attached to the current request is provided by the RepositoryUtil:

Session session = RepositoryUtil.getSession(this.manager, request);

The Document Factory

The o.a.l.cms.publication.DocumentFactory is the main entry point to the content repository. It is tied to a session. You get the document factory which is attached to the current session this way:

DocumentFactory factory = DocumentUtil.getDocumentFactory(this.manager, request);

Browsing Content and Site Structure

From the document factory, you can access a publication:

String webappUrl = ServletHelper.getWebappUrl(request);
URLInformation info = new URLInformation(webappUrl);
Publication pub = factory.getPublication(info.getPublicationId());

The publication provides access to all areas (pun intended). An area object enables you to obtain documents by their UUID.

Area authoring = pub.getArea("authoring");
Document[] docs = authoring.getDocuments();
Document doc = authoring.getDocument(uuid, language);

If you want to obtain a document by its path in the site structure, get the site structure from the area:

SiteStructure site = authoring.getSite();
SiteNode node = site.getNode("/tutorial");
String[] languages = node.getLanguages();
Link link = node.getLink(language);
Document doc = link.getDocument();

You can also browse the document structure in a bottom-up way:

Document doc = ...;
doc.area().getPublication();
String area = doc.getLink().getNode().getStructure().getArea();

The Document class allows to access different language and area versions of the document:

if (doc.existsTranslation("en")) {
    englishVersion = doc.getTranslation("en");
}
if (doc.existsVersion("live", doc.getLanguage()) {
    addInfoMessage("Live version exists!");
    liveVersion = doc.getVersion("live", doc.getLanguage());
}

How to get only the direct parents of the node till the first node?

SiteNode node = doc.getLink().getNode();
List<SiteNode> ancestors = new ArrayList<SiteNode>();
while (!node.isTopLevel()) {
   SiteNode parent = node.getParent();
   ancestors.add(parent);
   node = parent;
}

FAQ

What is the preferred method in lenya to get all documents of a certain resource type?

The most performant way is to use Lucene. The query would be:

{http://apache.org/lenya/metadata/document/1.0}resourceType:foobar

See also https://lenya.zones.apache.org/cms/docu/authoring/docu20/reference/metadata.html#N100BF

A while ago Andreas started to implement a convenient API to do such queries, see thread API for document search on the users list. Maybe we can discuss this again?

How can I control the caching of the this call, since I can use a cached version of the listing until one of the documents get edit?

The Lucene query should be pretty fast, so caching the resulting list might not be necessary.

If you don't want to use Lucene, you could write a custom generator that stores an index of the relevant meta data and uses the last modification date for the cache validity. The generator also has to check the sitetree to see if documents have been added or removed. This is a rather complex task, but we used it for a customer in Lenya 1.2 and up to now it works very well (in combination with mod_cache, though).

Copyright © 1999-2011 The Apache Software Foundation. All rights reserved.

Apache Lenya, Apache, and the Apache feather logo are trademarks of The Apache Software Foundation.