Plone 3 Products Development Cookbook
上QQ阅读APP看书,第一时间看更新

Creating a model

We are now ready to start with the creation of a new UML model. Once finished, we will use ArchGenXML to turn our model into the expected content type.

Getting ready

Before going on, let's create a new models folder inside our ArchGenXML installation folder to store the following example code:

cd ~/libexec/archgenxml
mkdir ./models

Note

Yes, we could have also created this folder during buildout. If you want, help yourself.

And don't forget to set project properties as explained in Configuring ArgoUML.

How to do it…

After having created a new project and set its properties as above, and before starting to draw, we must define the product name:

  1. Click on the untitledModel branch on the left navigator to select it. You'll now be able to set its properties on the bottom-right panel.
  2. In the Name field, write the model's name; it will be the product name in the installation area of Plone. In our case, type poxContentTypes (after PloneOpenX ContentTypes).

    Note

    All products created with ArchGenXML will have a name beginning with Products followed by the model name. In this example, it is Products.poxContentTypes.

    How to do it…

The top-right panel is where we're going to draw the model. Add a new package to our model:

  1. Click on the third icon (New Package) in the toolbar.
  2. Click on the top-right "drawing" panel to create the package.
  3. You can drag and drop its corners to make it bigger.
  4. Double-click its header and change its title into content to set its name. Using content as the package name for content type definitions is common practice in Plone products.

Inside this package we'll create the class (or classes) we need.

  1. Click on the fourth icon (New Class) in the toolbar.
  2. Click inside the content package to create the class inside it. If you are not sure whether it is inside the package, check on the left-hand navigator and expand the branches.
  3. Double-click the class header (first rectangle) and change its title to XNewsItem (after eXtended News Item).

Many times during products development we will already have a base class to inherit from. In this particular case, we want the XNewsItem content type to inherit all the functionalities, which the native Plone News Item content type has.

To inherit from existing classes, we have several options; the most straightforward one is applying stereotypes.

  1. Click on the XNewsItem class just drawn and go to the Stereotype tab in the bottom-right panel.
  2. Click on the atnewsitem [Class] option in the Available Stereotypes list on the left and press the >> button to apply that stereotype. This will add a <<atnewsitem>> line on the class header.
    How to do it…

After having turned our class into a News Item, let's add new fields to get it closer to the requirements.

  1. Double-click on the class attributes panel (middle rectangle) to add a new attribute. Add a name, a colon, and a type. The new attribute Properties tab will automatically open. You can change the name, type, and other properties here.

    Note

    There's a corresponding Archetypes data type for every type we choose for our attributes.

  2. If not specified during creation, add the name of the new attribute: lead.
  3. If not specified on creation, add the type of the new attribute: string.
  4. Go to the Tagged Values tab to set some other advanced properties:

    Note

    Almost all the Tagged Values will be treated by ArchGenXML as string values. There are a few exceptions like required, which expects a bool value. If you want to make sure ArchGenXML will get the correct data type, add a python: prefix; for instance: python:True or python:['high', 'medium', 'low'].

    For a complete list of available Archetypes fields and their properties, go to http://plone.org/documentation/manual/archetypes-developer-manual/fields/fields-reference.

  5. Let's add another field with some more options. Double-click on the class attributes panel and add a new attribute with these characteristics:
    • Name: country
    • Type: string
  6. Then go to the Tagged Values tab and add the following tags:

Note

For a complete list of available Archetypes widgets and their configuration options go to http://plone.org/documentation/manual/archetypes-developer-manual/fields/widgets-reference.

Sometimes we learn that there are already some fields in the base class we're working with (ATNewsItem, in our example) that might need various minor changes to meet our requirements. Instead of creating new fields and hiding or removing them, we can reuse the existing fields.

Plone's News Item content type has a special relatedItems field in the Categorization tab to select already created content that will be displayed as links when reading the document.

  1. Double-click on the class attributes panel (second rectangle) to add a new attribute. Add relatedItems as its name, a colon, and copy as its type. The new attribute Properties tab will open automatically. Ensure that copy is the selected type: copy is a special data type that tells ArchGenXML that it must reuse an existing field and override some of its properties.
  2. Go to the Tagged Values tab to make the relatedItems field the one we want: add a schemata tag and set its value to default. This is the way to change the tab where this field will appear in edition mode.

We now have an XNewsItem content type that looks a lot like how we expected. However, there's a countryVocabulary method required by country field that we must define in order to prevent errors on instance start-up. Let's see how to add methods to the content type class:

  1. Double-click on the class operations panel (third rectangle) to add a new method. Add its name. The new method Properties tab will automatically open. You can change its name there if you have misspelled it.
  2. If not specified on creation, add the name of the new method: countryVocabulary.
  3. Go to the Tagged Values tab to add some other changes: if your method is a one-liner, go ahead and add a code tag and set its value:
    return (('AQ', 'Antartiqa'), ('BS', 'Bahamas'), ('CM', 'Cameroon'), ). 
    

How it works…

After having defined the product name (Steps 1 and 2) and added a new package (Steps 3 to 6), we finally created a new class (Steps 7 to 9), which will be, at the end of the day, our content type's class.

At that stage XNewsItem is just a copy of BaseContent class, which is the core of almost every content type in Plone. It has an id, a title, description, and all the metadata fields and methods to support the Dublin Core convention.

By applying atnewsitem stereotype (Steps 10 to 11), we change the base class of XNewsItem from BaseContent to ATNewsItem. But again, there already is a News Item content type in Plone; we must keep making changes in our model for it to be of use. In Steps 12 to 17, we added new fields and reused some other existing ones.

Finally, in Steps 20 to 22, we added a short method that will be used to provide the options in the just created country field.

There's more…

We have specified the countryVocabulary method as the list of options for the country field. This isn't a really good example. It's more likely that we will want to use a configurable list of countries; even better, a list of countries with their ISO 3166 two-digit codes. To achieve this, we can use ATVocabularyManager and take advantage of its integration on ArchGenXML. Nevertheless, take our choice as a simple example.

Note

The ATVocabularyManager project page is http://plone.org/products/atvocabularymanager.

More about ATVocabularyManager-ArchGenXML integration can be found at http://plone.org/documentation/manual/archgenxml2/3rdparty/atvocabularymanager.

We can also use a Zope 3 vocabulary—it is actually a named utility —together with the vocabulary_factory attribute for an options field. Although there's no vocabulary_factory tagged value in AGXProfile, you can manually add it in ArgoUML. For instructions on creating a Zope 3 vocabulary, check Adding configuration options in Plone control panel.

See also

  • Adding configuration options in Plone control panel
  • Configuring ArgoUML