Chapter 7. Creating a Content Type

7.1. Create the Content Type

We extend from the basic ContentItem class to include the extended attributes we need (species, sex, and wingspan).

7.1.1. Create PDL file

In ct-birdwatch/pdl/com/arsdigita/package1/BirdWatch.pdl:

model com.arsdigita.package1;

import com.arsdigita.cms.*;

object type BirdWatch extends TextPage {

Define the extended attributes of this new class.

String[1..1] species = ct_birdwatch.species VARCHAR(1000);
String[1..1] sex = ct_birdwatch.sex VARCHAR(100);
Integer[1..1] wingspan = ct_birdwatch.wingspan INTEGER;

reference key (ct_birdwatch.bird_id);

species and sex are both strings, and wingspan is stored as an integer. We specify exactly one of each attribute.

7.1.2. Create the Java Class for your ContentItem Extension

The class you will create is simple. You use boilerplate constructors. The only methods you must write are the get() and set() methods of each extended attribute.

package com.arsdigita.package1;

import com.arsdigita.cms.ContentType;
import com.arsdigita.cms.TextPage;
import com.arsdigita.domain.DataObjectNotFoundException;
import com.arsdigita.persistence.DataObject;
import com.arsdigita.persistence.OID;

Here is the boilerplate constructor code:

public class Birdwatch extends TextPage {

    /** Data object type for this domain object */
    public static final String BASE_DATA_OBJECT_TYPE
        = "com.arsdigita.package1.BirdWatch";

    public BirdWatch()  {
        super(BASE_DATA_OBJECT_TYPE);
    }

    public BirdWatch(String type) {
        super(type);
    }

    public BirdWatch(OID oid) throws DataObjectNotFoundException {
        super(oid);
    }

    public BirdWatch(BigDecimal id) throws DataObjectNotFoundException {
        super(new OID(BASE_DATA_OBJECT_TYPE, id));
    }

    public BirdWatch(DataObject obj)  {
        super(obj);
    }

}

Now we create a get() and set() method for each extended attribute. The constants, like SPECIES, are the persistence layer's identifier for the attribute. Notice that Wingspan returns a Double, while everything else returns a string.

public static final String SEX = "sex";
public static final String SPECIES = "species";
public static final String WINGSPAN = "wingspan";

public String getSpecies() {
    return (String) get(SPECIES);
}

public void setSpecies(String species) {
    set(SPECIES, species);
}

public String getSex() {
    return (String) get(SEX);
}

public void setSex(String sex) {
    set(SEX, sex);
}

public Double getWingspan() {
    return (Integer) get(WINGSPAN);
}

public void setWingspan(Integer wingspan) {
    set(WINGSPAN, wingspan);
}