Subscribe to Mailing List
Submit requests and bugs
Follow Astroboa on Twitter
Installation Guide
Maven Repository
Get Source code at GITHUB
Building from Sources
API Documentation
Submit a Bug or Request

Hello BetaCMS World example will try to introduce to you BetaCMS Java API and BetaCMS Services. This example shows how to connect to a BetaCMS repository, how to create a content object and provide values for its properties and finally how to search for this content object using BetaCMS Criteria API.

More detailed code samples can be downloaded here.

This example assumes that BetaCMS has been successfully installed and a new BetaCMS repository has been created.

Connect to a BetaCMS repository

The starting point for using BetaCMS services is to successfully login to a BetaCMS repository which is hosted in a BetaCMS server. This is done by a BetaCMS client

//Create a new client to BetaCMS repository server
BetaCmsRepositoryClient betaCmsRepositoryClient = new BetaCmsRepositoryClient("localhost");

//Create user credentials
BetaCmsCredentials credentials = new BetaCmsCredentials("SYSTEM","betaconcept".toCharArray());

//Login to repository with ID 'betacms'
betaCmsRepositoryClient.login("betacms", credentials);

A BetaCMS server host name or IP is the same with your's machine host name or IP. Bear in mind though, that there is a BetaCMS server for each JBoss AS installation which contains a BetaCMS installation.

Password for SYSTEM user is the default. Change this value if you have already use BetaCMS web administration and have changed SYSTEM's password.

Create a new ContentObject

Now that we have successfully logged in, we can create our first content object of type 'genericContentResourceObject'.

//Create a new content object instance of type 'genericContentResourceObject'
ContentObject helloWorldContentObject = betaCmsRepositoryClient
        .getCmsRepositoryEntityFactory()
        .newContentObjectForType("genericContentResourceObject", "en");

This type is provided by BetaCMS installation and its definition is described by an Xml Schema (XSD) file which is available in URL (assuming your server host is localhost and is listening to port 8080)

 

http://localhost:8080/content-api/f/definitionSchema/repository/betacms/definitionFullPath/genericContentResourceObject

 

If you replace localhost:8080 with www.betacms.org you can view XSD  for genericContentResourceObject content type defined in BetaCMS repository which contains the content of this site! Download here this XSD...

 

Provide values for ContentObject properties

Every content object in a BetaCMS repository has an owner. Our content object will have SYSTEM user as owner.

//Provide owner for content object. Select system repository user
RepositoryUser owner = 
        betaCmsRepositoryClient.getRepositoryUserService().getSystemRepositoryUser();

helloWorldContentObject.setOwner(owner);

ContentObject properties are defined by content object's type XSD. As you may see from the aforementionned XSD, properties are either simple properties (String, Date, Long, etc) or complex properties which define other simple or complex properties in their own XSD. For example property profile has its own XSD located in   

http://localhost:8080/content-api/f/definitionSchema/repository/betacms/definitionFullPath/administrativeMetadataType

Let's provide some values

//Profile properties
//Title
StringProperty profileTitleProperty = 
        (StringProperty)helloWorldContentObject.getCmsProperty("profile.title");

profileTitleProperty.setSimpleTypeValue("Hello BetaCMS world !");

//Language
StringProperty profileLanguageProperty = 
        (StringProperty)helloWorldContentObject.getCmsProperty("profile.language");

profileLanguageProperty.setSimpleTypeValue("en");

//Last Modification Date
CalendarProperty profileModifiedProperty = 
        (CalendarProperty)helloWorldContentObject.getCmsProperty("profile.modified");

profileModifiedProperty.setSimpleTypeValue(Calendar.getInstance());

//Accessibility properties
//Notice the method used to add value : addSimpleTypeValue
//This is because property 'accessibility.canBeReadBy' can contain multiple values
StringProperty canBeReadByProperty = 
        (StringProperty)helloWorldContentObject
        .getCmsProperty("accessibility.canBeReadBy");

canBeReadByProperty.addSimpleTypeValue("ALL");

StringProperty canBeUpdatedByProperty = 
        (StringProperty)helloWorldContentObject
        .getCmsProperty("accessibility.canBeUpdatedBy");

canBeUpdatedByProperty.addSimpleTypeValue("NONE");

StringProperty canBeDeletedByProperty = 
        (StringProperty)helloWorldContentObject
        .getCmsProperty("accessibility.canBeDeletedBy");

canBeDeletedByProperty.addSimpleTypeValue("NONE");

StringProperty canBeTaggedByProperty = 
        (StringProperty)helloWorldContentObject
        .getCmsProperty("accessibility.canBeTaggedBy");

canBeTaggedByProperty.addSimpleTypeValue("ALL");

//Property 'body' is of type RichText according to XSD
//hence we can add html code...
String body = "My first BetaCMS content object";
StringProperty bodyProperty = 
        (StringProperty)helloWorldContentObject.getCmsProperty("body");

bodyProperty.setSimpleTypeValue(body);

Last but not least, BetaCMS API allows us to upload any kind of file in a BetaCMS repository. Java interface BinaryChannel represents such a file or resource and defines some extra properties that should be provided along with resource content. In the following example, we provide an image as the value of property image of content type genericContentResourceObject.

//Property 'image' is of type 'binaryChannelType' which allows us
//to upload any type of file
File file = new File("myImage.jpg");
BinaryChannel image = 
        betaCmsRepositoryClient.getCmsRepositoryEntityFactory().newBinaryChannel();

//ALWAYS the same name with property
image.setName("image");

//Must provide a byte array.  
image.setContent(getFileAsByteArray(file));
image.setSize(file.length());
image.setSourceFilename(file.getName());
image.setMimeType("image/jpeg");

Calendar lastModifiedDate = Calendar.getInstance();
lastModifiedDate.setTimeInMillis(file.lastModified());

image.setModified(lastModifiedDate);

//Finally add binary channel to content object
BinaryProperty imageProperty = 
        (BinaryProperty)helloWorldContentObject.getCmsProperty("image");
imageProperty.setSimpleTypeValue(image);

That's it! We are ready to save our content object.

helloWorldContentObject = 
        betaCmsRepositoryClient.getContentService()
        .saveContentObject(helloWorldContentObject, false);

Search ContentObject

You can search a content object using its id

helloWorldContentObject = 
        betaCmsRepositoryClient.getContentService()
        .getContentObjectById(helloWorldContentObject.getId(), CacheRegion.FIVE_MINUTES);

or you can use BetaCMS criteria API, which is a very simple API used for searching for content objects or any other BetaCMS entity by generating criterion objects. For every BetaCMS entity (ContentObject, Topic, Taxonomy, RepositoryUser), there is a corresponding CmsCriteria object which can be obtained  from factory CmsCriteriaFactory. Accordingly, a criterion object can be constructing using CriterionFactory.

//Create search criteria
ContentObjectCriteria contentObjectCriteria = 
        CmsCriteriaFactory.newContentObjectCriteria("genericContentResourceObject");

//You can enable full text search which is conducted not only on property values 
//but in pdfs, docs, xls, etc which have been saved as BinaryChannels
contentObjectCriteria.addFullTextSearchCriterion("Hello BetaCMS world");

//You can create criterion for specific property 
Criterion profileTitleCriterion = 
        CriterionFactory.equals("profile.title", "Hello BetaCMS world !");

contentObjectCriteria.addCriterion(profileTitleCriterion);

//You can create ANDed or ORed criteria
Criterion bodyCriterion = 
        CriterionFactory.simpleCmsPropertycontains("body", "My first");

Criterion languageCriterion = 
        CriterionFactory.equals("profile.language", "en");

Criterion andCriterion = 
        CriterionFactory.and(bodyCriterion, languageCriterion);

contentObjectCriteria.addCriterion(andCriterion);

//You can specify offset and limit in your search
//enabling pagination functionality 
contentObjectCriteria.setOffsetAndLimit(0, 30);

//You can cache results
contentObjectCriteria.setCacheable(CacheRegion.FIVE_MINUTES);

Query resulted by the above criteria will search for the first 30 content objects of type 'genericContentResourceObject', whose property 'profile.title' is the phrase 'Hello BetaCMS world !' , property 'body' contains phrase 'My first' and property 'profile.language' has value 'en' and at the same time phrase 'Hello BetaCMS world' is found inside any property's values or inside any resource content uploaded. If results are returned, then BetaCMS is instructed to cache them for 5 minutes. Finally execute query and print 'profile.title' 's values for all content objects found

//Execute query
CmsOutcome> cmsOutcome = 
        betaCmsRepositoryClient.getContentService()
        .searchContentObjects(contentObjectCriteria);

//Iterate through results
for (CmsRankedOutcome cmsRankedOutcome : cmsOutcome.getResults()) {

ContentObject contentObject = cmsRankedOutcome.getCmsRepositoryEntity();

StringProperty titleProperty = 
        (StringProperty)contentObject.getCmsProperty("profile.title");

String contentObjectType = contentObject.getContentObjectType();

System.out.println("Found content object of type "+contentObjectType+
        " with title "+titleProperty.getSimpleTypeValue());

}

Export content to XML

One of the most important features of BetaCMS is the ability to export content in XML which is valid against XSD provided for content object types. This means that you can define a content type using XSD, register this XSD to BetaCMS, insert content using BetaCMS API or BetaCMS web interface and then export this content in XML format. Exporting content object to XML using BetaCMS Java API is very simple. Once you obtain content object just call method 'toXml()'

//Export ContentObject to XML
helloWorldContentObject.toXml();

 


Last Modified: 20 January 2011
ajax activity image Loading...