Author: tchemit Date: 2013-06-19 09:57:46 +0200 (Wed, 19 Jun 2013) New Revision: 672 Url: http://nuiton.org/projects/sandbox/repository/revisions/672 Log: add generate extra technical fields tag value Modified: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java Modified: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties 2013-06-15 14:27:31 UTC (rev 671) +++ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties 2013-06-19 07:57:46 UTC (rev 672) @@ -24,6 +24,7 @@ model.tagValue.idFactory=true model.tagValue.generatePropertyChangeListeners=true model.tagValue.generateVisitors=true +model.tagValue.generateExtraTechnicalFields=true com.franciaflex.magalie.persistence.entity.Article.attribute.fixedLocations.stereotype=unique Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java 2013-06-15 14:27:31 UTC (rev 671) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java 2013-06-19 07:57:46 UTC (rev 672) @@ -57,9 +57,11 @@ import javax.persistence.OneToOne; import javax.persistence.OrderColumn; import javax.persistence.PrePersist; +import javax.persistence.Version; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.util.Collection; +import java.util.Date; import java.util.List; import java.util.Set; @@ -84,6 +86,10 @@ private static final Log log = LogFactory.getLog(JpaEntityTransformer.class); + public static final String PROPERTY_VERSION = "version"; + + public static final String PROPERTY_CREATE_DATE = "createDate"; + @Override public void transformFromClass(ObjectModelClass input) { @@ -238,11 +244,36 @@ idAttr.setType(String.class.getName()); properties.add(0, idAttr); + boolean generateTechnicalFields = + JpaTemplatesGeneratorUtil.useGenerateExtraTechnicalFields(getModel(), input); + + if (generateTechnicalFields) { + + // add version field + ObjectModelAttributeImpl versionAttr = new ObjectModelAttributeImpl(); + versionAttr.setDeclaringElement(input); + versionAttr.setName(PROPERTY_VERSION); + versionAttr.setType(int.class.getName()); + properties.add(1, versionAttr); + + // add createDate field + ObjectModelAttributeImpl createDateAttr = new ObjectModelAttributeImpl(); + createDateAttr.setDeclaringElement(input); + createDateAttr.setName(PROPERTY_CREATE_DATE); + createDateAttr.setType(Date.class.getName()); + createDateAttr.setDefaultValue("new Date()"); + properties.add(2, createDateAttr); + } + + //TODO Si ORDERED ou et UNIQUE choisir le bon type de collection (O+U = LHS (laisser ça comme definition), (U = HS + S), (O = ArrayList +L), ( = ArrayList + C) //TODO Si enumeration @Enumeration(value = EnumType.STRING) (Voir pour mettre une tag-value pour mettre en ordinal) + voir comment ça marche pour les collections Multimap<ObjectModelAttribute, String> annotationsForAttributes = - generateAnnotationsForProperties(input, output, properties); + generateAnnotationsForProperties(input, + output, + properties, + generateTechnicalFields); // Add properties constant for (ObjectModelAttribute attr : properties) { @@ -308,7 +339,8 @@ protected Multimap<ObjectModelAttribute, String> generateAnnotationsForProperties(ObjectModelClass input, ObjectModelClass output, - List<ObjectModelAttribute> properties) { + List<ObjectModelAttribute> properties, + boolean generateTechnicalFields) { ArrayListMultimap<ObjectModelAttribute, String> result = ArrayListMultimap.create(); @@ -324,6 +356,12 @@ continue; } + if (PROPERTY_VERSION.equals(propertyName) && generateTechnicalFields) { + + addAnnotation(result, property, output, Version.class); + continue; + } + ObjectModelAttribute reverseAttribute = property.getReverseAttribute(); @@ -566,7 +604,7 @@ ObjectModelAttribute attribute = addAttribute(output, attrName, attrType, - "", + attr.getDefaultValue(), ObjectModelJavaModifier.PROTECTED ); Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java 2013-06-15 14:27:31 UTC (rev 671) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java 2013-06-19 07:57:46 UTC (rev 672) @@ -325,6 +325,26 @@ } /** + * Obtain the value of the {@link JpaTemplatesTagValues#TAG_GENERATE_EXTRA_TECHNICAL_FIELDS} + * tag value on the given model. + * + * @param model model to seek + * @return the none empty value of the found tag value or {@code null} + * if not found nor empty. + * @see JpaTemplatesTagValues#TAG_GENERATE_EXTRA_TECHNICAL_FIELDS + * @since 0.1 + */ + public static String getGenerateExtraTechnicalFieldsTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JpaTemplatesTagValues.TAG_GENERATE_EXTRA_TECHNICAL_FIELDS, null, model); + return value; + } + + public static boolean useGenerateExtraTechnicalFields(ObjectModel model,ObjectModelClassifier classifier) { + String value = getGenerateExtraTechnicalFieldsTagValue(model, classifier); + return ObjectUtils.equals("true", value); + } + + /** * Obtain the value of the {@link JpaTemplatesTagValues#TAG_ENTITY_SUPER_CLASS} * tag value on the given model or classifier. * <p/> Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java 2013-06-15 14:27:31 UTC (rev 671) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java 2013-06-19 07:57:46 UTC (rev 672) @@ -113,7 +113,7 @@ /** * Tag value to generate visitors on entities if setted to {@code true}. * - * @see JpaTemplatesGeneratorUtil#getGeneratePropertyChangeListenersTagValue(ObjectModel) + * @see JpaTemplatesGeneratorUtil#getGenerateVisitorsTagValue(ObjectModel) * @since 0.1 */ @ModelPropertiesUtil.TagValueDefinition( @@ -121,4 +121,17 @@ documentation = "Tag value to generate visitors on entities.") String TAG_GENERATE_VISITORS = "generateVisitors"; + /** + * Tag value to generate extra technical fields (version, createDate) for + * a specific entity or for all entities of a model. + * + * @see JpaTemplatesGeneratorUtil#getGenerateExtraTechnicalFieldsTagValue(ObjectModel, ObjectModelClassifier) + * @since 0.1 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "Tag value to generate extra technical fields (version, createDate) for \n" + + "a specific entity or for all entities of a model.") + String TAG_GENERATE_EXTRA_TECHNICAL_FIELDS = "generateExtraTechnicalFields"; + }