HOW TO: Add a custom function to a nodebuilder

Abstract

This How To provides information on how to specialize the MMObjectBuilder class so that custom functionality for a builder can be implemented.

This How-To is based on functionality provided by MMBase 1.7.0


First we are going to take a look at the implementation of our node builder, person.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE builder PUBLIC "-//MMBase//DTD builder config 1.1//EN" "http://www.mmbase.org/dtd/builder_1_1.dtd">

<builder name="persons" maintainer="thedutchrepublic.com" version="0" extends="object">
  <status>active</status>
  <classfile>org.mmbase.examples.howtos.builders.PersonBuilder</classfile>
  <names>
    <singular xml:lang="en">Person</singular>
    <singular xml:lang="nl">Persoon</singular>
    <plural xml:lang="en">Persons</plural>
    <plural xml:lang="nl">Personen</plural>
  </names>

  <descriptions>
    <description xml:lang="en">A Person</description>
    <description xml:lang="nl">Een Persoon</description>
  </descriptions>

  <fieldlist>
    <field>
      <descriptions>
        <description xml:lang="en">The name of the person</description>
        <description xml:lang="nl">De naam van de persoon</description>
      </descriptions>
      <gui>
        <guiname xml:lang="en">Name</guiname>
        <guiname xml:lang="nl">Naam</guiname>
        <guitype>string</guitype>
      </gui>
      <editor>
        <positions>
          <input>2</input>
          <list>2</list>
          <search>2</search>
        </positions>
      </editor>
      <db>
        <name>name</name>
        <type state="persistent" size="255" notnull="false" key="false">STRING</type>
      </db>
    </field>
  </fieldlist>

</builder>
        

In this builder a person object is specified, with just one attribute, the persons name. Of course there are much more attributes possible for a person object, such as different fields for surname and familyname, infixes, birth date and stuff like that. For this how to however, we just need to know a persons name.

We want a function on the person object that greets the person, when called. This will be just a simple message as “Hello [NameOfPerson]”.

We will call the file PersonBuilder.java so that it is evident to which builderfile it is related.

/* PersonBuilder.java
 * Created on Apr 8, 2004
 *
 * Reviewed By	| Review date	|Modified By  | Modified date | Modification
 *  - - - - - - +  - - - - -  + -- - - - - -+ - - - - - - - + - - - - - - - -
 *              |             |Willem Voogd | Apr 8, 2004	  | Initial Creation
 */

package org.mmbase.examples.howtos.builders;

import java.util.List;

import org.mmbase.module.core.MMObjectBuilder;
import org.mmbase.module.core.MMObjectNode;
import org.mmbase.util.functions.Parameter;

/**
 * PersonBuilder
 * @author willem
 *
 * The PersonBuilder is the source class voor the Person builder.
 * It suplies a function that says Hello to the person.
 */
public class PersonBuilder extends MMObjectBuilder {
    public static final String F_GREETING="hello";

    /**
     * Says hello.
     *
     * @param node, the node (menuitem) for which the string must be build.
     * @return helloString, the greeting.
     */
    protected String getGreeting(MMObjectNode node) {
        String helloString = "Hello ";
        helloString += node.getStringValue("name");
        return helloString;
    }

    /**
     * overridden from MMObjectBuilder
     *
     * Gets the parameters for the functions.
     * We only have one custom function w/o parameters, so we need an empty array.
     */
    public Parameter[] getParameterDefinition(String function) {
        if (function.equals(F_GREETING)) return new Parameter[0];
        return null;
    }


    /**
     * overridden from MMObjectBuilder
     * executes a given function and returns the result of that particular function.
     *
     * @param node
     * @param function
     * @param arguments
     */
    protected Object executeFunction(MMObjectNode node,String function,List arguments) {
        if (function.equals(F_GREETING)) {
            return getGreeting(node);
        } else {
            return super.executeFunction(node,function,arguments);
        }
    }
}


This is part of the MMBase documentation.

For questions and remarks about this documentation mail to: [email protected]