Author: fdesbois Date: 2009-11-12 12:48:05 +0100 (Thu, 12 Nov 2009) New Revision: 1666 Modified: branches/from2.2.2-eugene2-beta/topia-persistence/src/main/java/org/nuiton/topia/generator/BeanTransformer.java branches/from2.2.2-eugene2-beta/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java Log: Add support for array attribute type in Bean/DTO generation Modified: branches/from2.2.2-eugene2-beta/topia-persistence/src/main/java/org/nuiton/topia/generator/BeanTransformer.java =================================================================== --- branches/from2.2.2-eugene2-beta/topia-persistence/src/main/java/org/nuiton/topia/generator/BeanTransformer.java 2009-11-07 17:24:52 UTC (rev 1665) +++ branches/from2.2.2-eugene2-beta/topia-persistence/src/main/java/org/nuiton/topia/generator/BeanTransformer.java 2009-11-12 11:48:05 UTC (rev 1666) @@ -80,11 +80,15 @@ addInterface(resultClass, parentInterface.getQualifiedName()); } + // Default constructor + ObjectModelOperation constructor = addConstructor(resultClass, ObjectModelModifier.PUBLIC); + createListeners(resultClass, clazz); boolean hasEntity = false; boolean hasMultipleAttribute = false; - String toStringAppend = ""; + String toStringAppend = ""; // Append pour la méthode toString() + String initTabs = ""; // initialisation des tableaux dans le constructeur // Add attributes with getter/setter for (ObjectModelAttribute attr : attributes) { @@ -96,7 +100,49 @@ String attrNameCapitalized = StringUtils.capitalize(attrName); // multiple attribute - if (GeneratorUtil.isNMultiplicity(attr)) { + if (attr.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ARRAY)) { + + int maxSize = attr.getMaxMultiplicity(); + int maxSizeMoinsUn = maxSize - 1; + + initTabs += "\n\tthis." + attrName + " = new " + attrType + "[" + maxSize + "];"; + + // Set Value + ObjectModelOperation setValue = addOperation(resultClass, "set" + attrNameCapitalized, + "void", ObjectModelModifier.PUBLIC); + addParameter(setValue, "int", "index"); + addParameter(setValue, attrType, "value"); + addException(setValue, "java.lang.ArrayIndexOutOfBoundsException"); + setOperationBody(setValue, "" + /*{ + if (index >= <%=maxSize%> || index < 0) { + throw new ArrayIndexOutOfBoundsException("Wrong index [" + index + "] for array <%=attrName%>," + + "index must be between 0 and <%=maxSizeMoinsUn%>"); + } + <%=simpleType%>[] oldValue = get<%=attrNameCapitalized%>(); + this.<%=attrName%>[index] = value; + firePropertyChange("<%=attrName%>", oldValue, this.<%=attrName%>); + }*/ + ); + + // Get Value + ObjectModelOperation getValue = addOperation(resultClass, "get" + attrNameCapitalized, + attrType, ObjectModelModifier.PUBLIC); + addParameter(getValue, "int", "index"); + addException(setValue, "java.lang.ArrayIndexOutOfBoundsException"); + setOperationBody(getValue, "" + /*{ + if (index >= <%=maxSize%> || index < 0) { + throw new ArrayIndexOutOfBoundsException("Wrong index [" + index + "] for array <%=attrName%>," + + "index must be between 0 and <%=maxSizeMoinsUn%>"); + } + return this.<%=attrName%>[index]; + }*/ + ); + + attrType += "[]"; + simpleType = GeneratorUtil.getSimpleName(attrType); + } else if (GeneratorUtil.isNMultiplicity(attr)) { hasMultipleAttribute = true; // Add getChild @@ -245,6 +291,13 @@ ); } + // Set body for default constructor + setOperationBody(constructor, "" + /*{ + pcs = new PropertyChangeSupport(this);<%=initTabs%> + }*/ + ); + // Add operations for (ObjectModelOperation op : clazz.getOperations()) { String visibility = op.getVisibility(); @@ -313,14 +366,6 @@ addAttribute(resultClass, "pcs", "java.beans.PropertyChangeSupport", "", ObjectModelModifier.PROTECTED, ObjectModelModifier.FINAL); - // Default constructor - ObjectModelOperation constructor = addConstructor(resultClass, ObjectModelModifier.PUBLIC); - setOperationBody(constructor, "" - /*{ - pcs = new PropertyChangeSupport(this); - }*/ - ); - // Add PropertyListener String propType = "java.beans.PropertyChangeListener"; String strType = String.class.getName(); Modified: branches/from2.2.2-eugene2-beta/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java =================================================================== --- branches/from2.2.2-eugene2-beta/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java 2009-11-07 17:24:52 UTC (rev 1665) +++ branches/from2.2.2-eugene2-beta/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java 2009-11-12 11:48:05 UTC (rev 1666) @@ -73,6 +73,8 @@ public static final String STEREOTYPE_UNIQUE = "unique"; /** Stéréotype pour les attributs étant des clés primaires */ public static final String STEREOTYPE_PRIMARYKAY = "primaryKey"; + /** Stéréotype pour les attributs considérés comme des tableaux */ + public static final String STEREOTYPE_ARRAY = "array"; /** Tag pour le type de persistence */ public static final String TAG_PERSISTENCE_TYPE = "persistenceType"; /** Tag pour le nom du champ / entité en BD */