* Generate the Identity class for the specified RepositoryObject.
* @param info The representation of a RepositoryObject class.
*/
protected void generateIdentityClass (RepositoryObjectInfo info) {
ClassInfo objectClassInfo = info.getObjectClassInfo ();
ClassInfo identityClassInfo = info.getIdentityClassInfo ();
// Open a file.
PackageInfo identityPackageInfo = identityClassInfo.getPackageInfo();
String currentPackageName = identityPackageInfo.getName();
String identityClassName = identityClassInfo.getName();
if (identityClassName.endsWith("ImplIdentity")) {
int end = identityClassName.length() - "ImplIdentity".length();
identityClassName =
identityClassName.substring(0, end) + "Identity";
}
String qualifiedClassName = currentPackageName + "." + identityClassName;
PrintStream out = openFileForClass(qualifiedClassName);
// Write the header.
GenerateUtilities.writeHeader (out, this.getClass().getName());
// Write the package statement. The identity class is added to the same
// package as the class it identifies.
out.println ();
out.println ("package " + currentPackageName + ";");
// Write the imports.
SortedSet imports = new TreeSet ();
ClassInfo identityBaseClassInfo = identityClassInfo.getBaseClassInfo ();
imports.add (identityBaseClassInfo.getQualifiedName ());
// Find out if this identity includes a device name and if so add
// an import for RepositoryObjectDeviceIdentity.
List identityFields = identityClassInfo.getFields ();
Iterator fields = identityFields.iterator();
String anotherFieldName = "";
while(fields.hasNext() && !anotherFieldName.equals("deviceName")) {
anotherFieldName = ((FieldInfo) fields.next()).getName();
}
if(anotherFieldName.equals("deviceName")) {
imports.add("com.volantis.mcs.objects.RepositoryObjectDeviceIdentity");
}
if (currentPackageName.indexOf( "com.volantis.mcs.project" ) == -1 ) {
imports.add("com.volantis.mcs.project.Project");
}
GenerateUtilities.writeImports (out, imports, currentPackageName);
String comment;
String separator = null;
// Write the class comment.
String objectClassName = objectClassInfo.getName ();
comment = "Encapsulates those properties of a "
+ objectClassName + " which uniquely identify it.\n";
// @todo should this condition still apply?
if (currentPackageName.indexOf("com.volantis.mcs.objects") == -1) {
comment += getReplicateTagsText(info);
}
GenerateUtilities.writeJavaDocComment (out, "", comment);
// Write the class header.
out.println ("public class " + identityClassName);
out.print (" extends " + identityBaseClassInfo.getName () );
// If this identity includes a deviceName the add
// "implements RepositoryObjectDeviceIdentity to the class declaration
if(anotherFieldName.equals("deviceName")) {
out.print("\n implements RepositoryObjectDeviceIdentity");
}
out.println(" {");
// Write the copyright statement.
GenerateUtilities.writeCopyright (out);
// Write the fields.
for (Iterator i = identityFields.iterator (); i.hasNext ();) {
FieldInfo field = (FieldInfo) i.next ();
String fieldName = field.getName ();
GenerateUtilities.writeJavaDocComment (out, " ", field.getComment ());
out.println (" private " + field.getTypeName ()
+ " " + fieldName + ";");
}
// Write the initialisation of the base class.
List baseConstructors = identityBaseClassInfo.getConstructors();
System.err.println("base constructors = " + baseConstructors);
// Based on two constructors in the absolute base class of all assets
if (baseConstructors.size () != 2) {
throw new IllegalStateException ("Identity base class has wrong number"
+ " of constructors\n" +
"identityBaseClassInfo = " + identityBaseClassInfo.getName());
}
// Iterate over all constructors and replicate them in this class
for (Iterator i = baseConstructors.iterator(); i.hasNext(); /* No ++ */) {
ConstructorInfo baseConstructor = (ConstructorInfo)i.next();
// Write the constructor comment.
GenerateUtilities.openJavaDocComment(out, " ");
GenerateUtilities.addJavaDocComment(out, " ", "Create a new <code>"
+ identityClassName + "</code>.");
List constructorParameters = baseConstructor.getParameters();
for (Iterator innerI = constructorParameters.iterator();
innerI.hasNext(); /* No ++ */) {
ParameterInfo param = (ParameterInfo)innerI.next();
String paramName = param.getName();
comment = "@param " + paramName + " " +
identityBaseClassInfo.getField(paramName, true).getComment();
GenerateUtilities.addJavaDocComment(out, " ", comment);
}
GenerateUtilities.closeJavaDocComment(out, " ");