The Domino Enumeration Code Generator

This simple form will generate the code you need to create a web service Enumeration structure in Lotus Notes/Domino 7.0. Just enter the information below and click the "Submit" button.


"> Enumeration Name (no spaces, letters only):

LotusScript Data type:

Values (separate values with linefeeds):


"; echo "

LotusScript Enumeration code:

"; echo getLotusScriptCode($enumname, $datatype, $values); echo "

"; echo "

Java Enumeration code:

"; echo getJavaCode($enumname, $datatype, $values); echo "
"; } function getLotusScriptCode($enumname, $datatype, $values) { $q = ""; if ($datatype == "String") $q = "\""; $valueArray = explode("\n", $values); $retval = "
'++++++++++ Enumeration $enumname Begins Here ++++++++++

%REM
These are all the things we have to do to create an Enumeration
in LotusScript. See the Domino Designer Help for more information.

PLEASE NOTE that you should NOT use enumerations unless you are
using at least Domino 7.0.1. The 7.0 release had a problem where the
data type of an enumeration was handled incorrectly.
%END REM

'** these constant names MUST begin with \"" . $enumname . "_\":\n";
	
	foreach ($valueArray as $key=>$value){
		$retval .= "Const " . $enumname . "_value" . ($key+1) . " = " . 
			$q . trim(str_replace("\"", "\"\"", $value)) . $q . "\n";
	}
	
	$retval .= "
'** these global variable names MUST end with \"_" . $enumname . "\":\n";
	
	foreach ($valueArray as $key=>$value){
		$retval .= "Dim Value" . ($key+1) . "_" . $enumname . " As " . $enumname . "\n";
	}
	
	$retval .= "\n
'** this list of " . $enumname . " objects MUST be called \"Enum_" . $enumname . "\":
Dim Enum_" . $enumname . " List As " . $enumname;
	
	$retval .= "\n\n
'** the actual " . $enumname . " enumeration class:
Class " . $enumname . "
	'** we MUST have a Public property called \"Value\",
	'** of the same data type as the Const values above
	Public Value As " . $datatype . "
	
	Public Sub Initialize (typeVal As " . $datatype . ")
		Value = typeVal
		Set Enum_" . $enumname . "(Value) = Me
	End Sub
	
	\"** optional helper functions
	Public Function Equals (you As " . $enumname . ") As Boolean
		If (Me.Value = you.Value) Then
			Equals = True
		End If
	End Function
	
	Public Function ToString () As String
		ToString = Cstr(Value)
	End Function
End Class
";
	
	$retval .= "

'** We also need to have some way of initializing all the _" . $enumname . "
'** objects, so we'll write a small class that can be called when the
'** web service class is instantiated. All we have to do is create
'** a new instance of this class in the \"New\" sub of the class that
'** implements the web service. For example:
'**   Public Sub New ()
'**      Dim enumInit As New " . $enumname . "Initializer()
'**      '** other web service initialization code here...
'**   End Sub
'** Don't forget to update this class if you add new " . $enumname . " values!!!

Class " . $enumname . "Initializer
	Public Sub New ()
	
";
	
	foreach ($valueArray as $key=>$value){
		$obj = "Value" . ($key+1) . "_" . $enumname;
		$val = $enumname . "_value" . ($key+1);
		$retval .= "		Set $obj = New $enumname\n";
		$retval .= "		Call $obj.Initialize($val)\n\n";
	}
	
	$retval .= "	End Sub
End Class
";
	
	$retval .= "\n'++++++++++ Enumeration $enumname Ends Here ++++++++++
"; return $retval; } function getJavaCode($enumname, $datatype, $values) { $q = ""; $p = ")"; $typemap = array("String" => array("String", "", ""), "Integer" => array("short", "new Short(", "Short.parseShort("), "Long" => array("int", "new Integer(", "Integer.parseInt(")); $jdatatype = $typemap[$datatype]; if ($datatype == "String") { $q = "\""; $p = ""; } $valueArray = explode("\n", $values); $retval = "
/*
This is the pattern for creating an Enumeration in Java. 
It's a little easier than creating one in LotusScript, 
and it has the advantage of being \"self-initializing\" 
(so you don't have to initialize the values when the web
service class is instantiated like you have to in LotusScript).
See the Domino Designer Help for more information.

PLEASE NOTE that you should NOT use enumerations unless 
you are using at least Domino 7.0.1. The 7.0 release had 
a problem where the data type of an enumeration was handled 
incorrectly.
*/

import java.util.HashMap;


public class $enumname  {
	private $jdatatype[0] _value_;
	private static HashMap _table_ = new HashMap();
	
	// these constant names can be anything you want
";
	
	$i = 0;
	foreach ($valueArray as $key=>$value){
		$retval .= "	public static final $jdatatype[0] _value" . ++$i .
			" = " . $q . trim(str_replace("\"", "\\\"", $value)) . $q . ";\n";
	}
	
	$retval .= "	
	
	// these are all the $enumname objects that are available
	// (note that all these values are automatically added to
	// the internal _table_ when they are declared like this)
	// Like the values above, they can be named anything
	// you want, as long as they use the values above.
";
	
	for ($j = 1; $j <= $i; $j++) {
		$retval .= "	public static final $enumname value" . $j .
				" = new $enumname(_value" . $j . ");\n";
	}
	
	$retval .= "	
	
	// $enumname can never be instantiated directly, 
	// you must use the static fromValue or fromString 
	// methods, or one of the public $enumname values above
	protected $enumname ($jdatatype[0] value) {
		_value_ = value;
		_table_.put(" . $jdatatype[1] . "value" . $p . ", this);
	}
	
	
	// getValue, fromValue, and fromString are REQUIRED
	public $jdatatype[0] getValue() { return _value_; }
	
	public static $enumname fromValue ($jdatatype[0] value)
			throws IllegalStateException {
		$enumname enum = ($enumname)_table_.get( " . $jdatatype[1] . "value" . $p . " );
		if (enum == null)
			throw new IllegalStateException();
		return enum;
	}
	
	public static $enumname fromString (String value)
			throws IllegalStateException {
		try {
			return fromValue( " . $jdatatype[2] . "value" . $p . " );
		} catch (Exception e) {
			throw new IllegalStateException();
		}
	}
	
	
	// optional (but good-to-have) helper methods
	public boolean equals (Object obj) { 
		return (obj.toString().equals(toString())); 
	}
	public int hashCode () { return toString().hashCode(); }
	public String toString () { return String.valueOf(_value_); }
}

"; return $retval; } ?>