r1236 - in trunk/eugene-jpa-templates/src: main/java/org/nuiton/eugene/jpa main/resources/META-INF/services site/fr/rst site/rst test/java/org/nuiton/eugene/jpa
Author: tchemit Date: 2013-04-01 22:50:47 +0200 (Mon, 01 Apr 2013) New Revision: 1236 Url: http://nuiton.org/projects/eugene/repository/revisions/1236 Log: refs #2633: Add JPA templates (entity, dao) (renmae JPA to Jpa) Added: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/AbstractJpaTransformer.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaDaoTransformer.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaEntityTransformer.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesGeneratorUtil.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesModelPropertiesProvider.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesStereoTypes.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesTagValues.java trunk/eugene-jpa-templates/src/test/java/org/nuiton/eugene/jpa/JpaTemplatesModelPropertiesProviderTest.java Removed: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/AbstractJPATransformer.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPADaoTransformer.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPAEntityTransformer.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesGeneratorUtil.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesModelPropertiesProvider.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesStereoTypes.java trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesTagValues.java trunk/eugene-jpa-templates/src/test/java/org/nuiton/eugene/jpa/JPATemplatesModelPropertiesProviderTest.java Modified: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/package-info.java trunk/eugene-jpa-templates/src/main/resources/META-INF/services/org.nuiton.eugene.ModelPropertiesUtil$ModelPropertiesProvider trunk/eugene-jpa-templates/src/site/fr/rst/index.rst trunk/eugene-jpa-templates/src/site/rst/index.rst Deleted: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/AbstractJPATransformer.java =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/AbstractJPATransformer.java 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/AbstractJPATransformer.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -1,684 +0,0 @@ -package org.nuiton.eugene.jpa; - -/* - * #%L - * EUGene :: JPA templates - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2013 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -/*{generator option: parentheses = false}*/ -/*{generator option: writeString = +}*/ - -import org.nuiton.eugene.java.ObjectModelTransformerToJava; -import org.nuiton.eugene.models.object.ObjectModelAttribute; -import org.nuiton.eugene.models.object.ObjectModelClass; -import org.nuiton.eugene.models.object.ObjectModelInterface; -import org.nuiton.eugene.models.object.ObjectModelJavaModifier; -import org.nuiton.eugene.models.object.ObjectModelOperation; - -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Set; - -/** - * Common class form jpa like transformer. - * - * @author tchemit <chemit@codelutin.com> - * @see JPAEntityTransformer - * @since 2.7 - */ -public abstract class AbstractJPATransformer extends ObjectModelTransformerToJava { - - public static final String DEFAULT_CONSTANT_PREFIX = "PROPERTY_"; - - protected void createPropertyConstant(ObjectModelClass output, - ObjectModelAttribute attr, - String prefix, - Set<String> constantNames) { - - String attrName = getAttributeName(attr); - - String constantName = prefix + builder.getConstantName(attrName); - - if (!constantNames.contains(constantName)) { - - addConstant(output, - constantName, - String.class, - "\"" + attrName + "\"", - ObjectModelJavaModifier.PUBLIC - ); - } - } - - protected String getAttributeName(ObjectModelAttribute attr) { - String attrName = attr.getName(); - if (attr.hasAssociationClass()) { - String assocAttrName = JPATemplatesGeneratorUtil.getAssocAttrName(attr); - attrName = JPATemplatesGeneratorUtil.toLowerCaseFirstLetter(assocAttrName); - } - return attrName; - } - - protected String getAttributeType(ObjectModelAttribute attr) { - String attrType = attr.getType(); - if (attr.hasAssociationClass()) { - attrType = attr.getAssociationClass().getName(); - } - return attrType; - } - - protected boolean containsMutiple(List<ObjectModelAttribute> attributes) { - - boolean result = false; - - for (ObjectModelAttribute attr : attributes) { - - if (JPATemplatesGeneratorUtil.isNMultiplicity(attr)) { - result = true; - - break; - } - - } - return result; - } - - protected void createProperty(ObjectModelClass output, - ObjectModelAttribute attr, - boolean usePCS, - boolean generateBooleanGetMethods) { - - String attrName = getAttributeName(attr); - String attrType = getAttributeType(attr); - - boolean multiple = JPATemplatesGeneratorUtil.isNMultiplicity(attr); - - String constantName = getConstantName(attrName); - String simpleType = JPATemplatesGeneratorUtil.getSimpleName(attrType); - - if (multiple) { - - createGetChildMethod(output, - attrName, - attrType, - simpleType - ); - - createIsEmptyMethod(output, - attrName - ); - - createSizeMethod(output, - attrName - ); - - createAddChildMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createAddAllChildrenMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createRemoveChildMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createRemoveAllChildrenMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createContainsChildMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createContainsAllChildrenMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - // Change type for Multiple attribute - if (attr.isOrdered()) { - attrType = List.class.getName() + "<" + attrType + ">"; - } else { - attrType = Collection.class.getName() + "<" + attrType + ">"; - } - - simpleType = JPATemplatesGeneratorUtil.getSimpleName(attrType); - } - - boolean booleanProperty = JPATemplatesGeneratorUtil.isBooleanPrimitive(attr); - - if (booleanProperty && !multiple) { - - // creates a isXXX method - createGetMethod(output, - attrName, - attrType, - JPATemplatesGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX - ); - } - - if (multiple || !booleanProperty || generateBooleanGetMethods) { - - createGetMethod(output, - attrName, - attrType, - JPATemplatesGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX - ); - - } - createSetMethod(output, - attrName, - attrType, - simpleType, - constantName, - usePCS - ); - - // Add attribute to the class - addAttribute(output, - attrName, - attrType, - "", - ObjectModelJavaModifier.PROTECTED - ); - - } - - protected List<ObjectModelAttribute> getProperties(ObjectModelClass input) { - List<ObjectModelAttribute> attributes = - (List<ObjectModelAttribute>) input.getAttributes(); - - List<ObjectModelAttribute> attrs = - new ArrayList<ObjectModelAttribute>(); - for (ObjectModelAttribute attr : attributes) { - if (attr.isNavigable()) { - - // only keep navigable attributes - attrs.add(attr); - } - } - return attrs; - } - - protected void createGetMethod(ObjectModelClass output, - String attrName, - String attrType, - String methodPrefix) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName(methodPrefix , attrName), - attrType, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return <%=attrName%>; - }*/ - ); - } - - protected void createGetChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String simpleType) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("get", attrName), - attrType, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "int", "index"); - setOperationBody(operation, "" - /*{ - <%=simpleType%> o = getChild(<%=attrName%>, index); - return o; - }*/ - ); - } - - protected void createIsEmptyMethod(ObjectModelClass output, - String attrName) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("is", attrName)+"Empty", - boolean.class, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return <%=attrName%> == null || <%=attrName%>.isEmpty(); - }*/ - ); - } - - protected void createSizeMethod(ObjectModelClass output, - String attrName) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("size", attrName), - int.class, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return <%=attrName%> == null ? 0 : <%=attrName%>.size(); - }*/ - ); - } - protected void createAddChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("add", attrName), - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - - String methodName = getJavaBeanMethodName("get", attrName); - StringBuilder buffer = new StringBuilder("" - /*{ - <%=methodName%>().add(<%=attrName%>); - }*/ - ); - if (usePCS) { - buffer.append("" - /*{ firePropertyChange(<%=constantName%>, null, <%=attrName%>); - }*/ - ); - } - setOperationBody(operation, buffer.toString()); - } - - protected void createAddAllChildrenMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("addAll", attrName), - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); - - String methodName = getJavaBeanMethodName("get", attrName); - StringBuilder buffer = new StringBuilder("" - /*{ - <%=methodName%>().addAll(<%=attrName%>); - }*/ - ); - if (usePCS) { - buffer.append("" - /*{ firePropertyChange(<%=constantName%>, null, <%=attrName%>); - }*/ - ); - } - setOperationBody(operation, buffer.toString()); - } - - protected void createRemoveChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("remove", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - String methodName = getJavaBeanMethodName("get", attrName); - StringBuilder buffer = new StringBuilder(); - buffer.append("" - /*{ - boolean removed = <%=methodName%>().remove(<%=attrName%>);}*/ - ); - - if (usePCS) { - buffer.append("" - /*{ - if (removed) { - firePropertyChange(<%=constantName%>, <%=attrName%>, null); - }}*/ - ); - } - buffer.append("" - /*{ - return removed; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createRemoveAllChildrenMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("removeAll", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); - StringBuilder buffer = new StringBuilder(); - String methodName = getJavaBeanMethodName("get", attrName); - buffer.append("" - /*{ - boolean removed = <%=methodName%>().removeAll(<%=attrName%>);}*/ - ); - - if (usePCS) { - buffer.append("" - /*{ - if (removed) { - firePropertyChange(<%=constantName%>, <%=attrName%>, null); - }}*/ - ); - } - buffer.append("" - /*{ - return removed; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createContainsChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("contains", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - StringBuilder buffer = new StringBuilder(); - String methodName = getJavaBeanMethodName("get", attrName); - buffer.append("" - /*{ - boolean contains = <%=methodName%>().contains(<%=attrName%>); - return contains; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createContainsAllChildrenMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("containsAll", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); - StringBuilder buffer = new StringBuilder(); - String methodName = getJavaBeanMethodName("get", attrName); - buffer.append("" - /*{ - boolean contains = <%=methodName%>().containsAll(<%=attrName%>); - return contains; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createSetMethod(ObjectModelClass output, - String attrName, - String attrType, - String simpleType, - String constantName, - boolean usePCS) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("set", attrName), - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - - if (usePCS) { - String methodName = getJavaBeanMethodName("get", attrName); - setOperationBody(operation, "" - /*{ - <%=simpleType%> oldValue = <%=methodName%>(); - this.<%=attrName%> = <%=attrName%>; - firePropertyChange(<%=constantName%>, oldValue, <%=attrName%>); - }*/ - ); - } else { - setOperationBody(operation, "" - /*{ - this.<%=attrName%> = <%=attrName%>; - }*/ - ); - } - } - - protected void addSerializable(ObjectModelClass input, - ObjectModelClass output, - boolean interfaceFound) { - if (!interfaceFound) { - addInterface(output, Serializable.class); - } - - // Generate the serialVersionUID - long serialVersionUID = JPATemplatesGeneratorUtil.generateSerialVersionUID(input); - - addConstant(output, - JPATemplatesGeneratorUtil.SERIAL_VERSION_UID, - "long", - serialVersionUID + "L", - ObjectModelJavaModifier.PRIVATE - ); - } - - /** - * Add all interfaces defines in input class and returns if - * {@link Serializable} interface was found. - * - * @param input the input model class to process - * @param output the output generated class - * @return {@code true} if {@link Serializable} was found from input, - * {@code false} otherwise - */ - protected boolean addInterfaces(ObjectModelClass input, - ObjectModelClass output) { - boolean foundSerializable = false; - for (ObjectModelInterface parentInterface : input.getInterfaces()) { - String fqn = parentInterface.getQualifiedName(); - addInterface(output, fqn); - if (Serializable.class.getName().equals(fqn)) { - foundSerializable = true; - } - } - return foundSerializable; - } - - protected void createPropertyChangeSupport(ObjectModelClass output) { - - addAttribute(output, - "pcs", - PropertyChangeSupport.class, - "new PropertyChangeSupport(this)", - ObjectModelJavaModifier.PROTECTED, - ObjectModelJavaModifier.FINAL, - ObjectModelJavaModifier.TRANSIENT - ); - - // Add PropertyListener - - ObjectModelOperation operation; - - operation = addOperation(output, - "addPropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.addPropertyChangeListener(listener); - }*/ - ); - - operation = addOperation(output, - "addPropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.addPropertyChangeListener(propertyName, listener); - }*/ - ); - - operation = addOperation(output, - "removePropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.removePropertyChangeListener(listener); - }*/ - ); - - operation = addOperation(output, - "removePropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.removePropertyChangeListener(propertyName, listener); - }*/ - ); - - operation = addOperation(output, - "firePropertyChange", - "void", - ObjectModelJavaModifier.PROTECTED - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, Object.class, "oldValue"); - addParameter(operation, Object.class, "newValue"); - setOperationBody(operation, "" - /*{ - pcs.firePropertyChange(propertyName, oldValue, newValue); - }*/ - ); - - operation = addOperation(output, - "firePropertyChange", - "void", - ObjectModelJavaModifier.PROTECTED - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, Object.class, "newValue"); - setOperationBody(operation, "" - /*{ - firePropertyChange(propertyName, null, newValue); - }*/ - ); - } - - protected void createGetChildMethod(ObjectModelClass output) { - ObjectModelOperation getChild = addOperation( - output, - "getChild", "<T> T", - ObjectModelJavaModifier.PROTECTED - ); - addImport(output, List.class); - - addParameter(getChild, "java.util.Collection<T>", "childs"); - addParameter(getChild, "int", "index"); - setOperationBody(getChild, "" -/*{ - T result = null; - if (childs != null) { - if (childs instanceof List) { - if (index < childs.size()) { - result = ((List<T>) childs).get(index); - } - } else { - int i = 0; - for (T o : childs) { - if (index == i) { - result = o; - break; - } - i++; - } - } - } - return result; -}*/ - ); - } -} Copied: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/AbstractJpaTransformer.java (from rev 1234, trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/AbstractJPATransformer.java) =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/AbstractJpaTransformer.java (rev 0) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/AbstractJpaTransformer.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -0,0 +1,684 @@ +package org.nuiton.eugene.jpa; + +/* + * #%L + * EUGene :: JPA templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +/*{generator option: parentheses = false}*/ +/*{generator option: writeString = +}*/ + +import org.nuiton.eugene.java.ObjectModelTransformerToJava; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelInterface; +import org.nuiton.eugene.models.object.ObjectModelJavaModifier; +import org.nuiton.eugene.models.object.ObjectModelOperation; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Set; + +/** + * Common class form jpa like transformer. + * + * @author tchemit <chemit@codelutin.com> + * @see JpaEntityTransformer + * @since 2.7 + */ +public abstract class AbstractJpaTransformer extends ObjectModelTransformerToJava { + + public static final String DEFAULT_CONSTANT_PREFIX = "PROPERTY_"; + + protected void createPropertyConstant(ObjectModelClass output, + ObjectModelAttribute attr, + String prefix, + Set<String> constantNames) { + + String attrName = getAttributeName(attr); + + String constantName = prefix + builder.getConstantName(attrName); + + if (!constantNames.contains(constantName)) { + + addConstant(output, + constantName, + String.class, + "\"" + attrName + "\"", + ObjectModelJavaModifier.PUBLIC + ); + } + } + + protected String getAttributeName(ObjectModelAttribute attr) { + String attrName = attr.getName(); + if (attr.hasAssociationClass()) { + String assocAttrName = JpaTemplatesGeneratorUtil.getAssocAttrName(attr); + attrName = JpaTemplatesGeneratorUtil.toLowerCaseFirstLetter(assocAttrName); + } + return attrName; + } + + protected String getAttributeType(ObjectModelAttribute attr) { + String attrType = attr.getType(); + if (attr.hasAssociationClass()) { + attrType = attr.getAssociationClass().getName(); + } + return attrType; + } + + protected boolean containsMutiple(List<ObjectModelAttribute> attributes) { + + boolean result = false; + + for (ObjectModelAttribute attr : attributes) { + + if (JpaTemplatesGeneratorUtil.isNMultiplicity(attr)) { + result = true; + + break; + } + + } + return result; + } + + protected void createProperty(ObjectModelClass output, + ObjectModelAttribute attr, + boolean usePCS, + boolean generateBooleanGetMethods) { + + String attrName = getAttributeName(attr); + String attrType = getAttributeType(attr); + + boolean multiple = JpaTemplatesGeneratorUtil.isNMultiplicity(attr); + + String constantName = getConstantName(attrName); + String simpleType = JpaTemplatesGeneratorUtil.getSimpleName(attrType); + + if (multiple) { + + createGetChildMethod(output, + attrName, + attrType, + simpleType + ); + + createIsEmptyMethod(output, + attrName + ); + + createSizeMethod(output, + attrName + ); + + createAddChildMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createAddAllChildrenMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createRemoveChildMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createRemoveAllChildrenMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createContainsChildMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createContainsAllChildrenMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + // Change type for Multiple attribute + if (attr.isOrdered()) { + attrType = List.class.getName() + "<" + attrType + ">"; + } else { + attrType = Collection.class.getName() + "<" + attrType + ">"; + } + + simpleType = JpaTemplatesGeneratorUtil.getSimpleName(attrType); + } + + boolean booleanProperty = JpaTemplatesGeneratorUtil.isBooleanPrimitive(attr); + + if (booleanProperty && !multiple) { + + // creates a isXXX method + createGetMethod(output, + attrName, + attrType, + JpaTemplatesGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX + ); + } + + if (multiple || !booleanProperty || generateBooleanGetMethods) { + + createGetMethod(output, + attrName, + attrType, + JpaTemplatesGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX + ); + + } + createSetMethod(output, + attrName, + attrType, + simpleType, + constantName, + usePCS + ); + + // Add attribute to the class + addAttribute(output, + attrName, + attrType, + "", + ObjectModelJavaModifier.PROTECTED + ); + + } + + protected List<ObjectModelAttribute> getProperties(ObjectModelClass input) { + List<ObjectModelAttribute> attributes = + (List<ObjectModelAttribute>) input.getAttributes(); + + List<ObjectModelAttribute> attrs = + new ArrayList<ObjectModelAttribute>(); + for (ObjectModelAttribute attr : attributes) { + if (attr.isNavigable()) { + + // only keep navigable attributes + attrs.add(attr); + } + } + return attrs; + } + + protected void createGetMethod(ObjectModelClass output, + String attrName, + String attrType, + String methodPrefix) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName(methodPrefix , attrName), + attrType, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(operation, "" + /*{ + return <%=attrName%>; + }*/ + ); + } + + protected void createGetChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String simpleType) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("get", attrName), + attrType, + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "int", "index"); + setOperationBody(operation, "" + /*{ + <%=simpleType%> o = getChild(<%=attrName%>, index); + return o; + }*/ + ); + } + + protected void createIsEmptyMethod(ObjectModelClass output, + String attrName) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("is", attrName)+"Empty", + boolean.class, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(operation, "" + /*{ + return <%=attrName%> == null || <%=attrName%>.isEmpty(); + }*/ + ); + } + + protected void createSizeMethod(ObjectModelClass output, + String attrName) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("size", attrName), + int.class, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(operation, "" + /*{ + return <%=attrName%> == null ? 0 : <%=attrName%>.size(); + }*/ + ); + } + protected void createAddChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("add", attrName), + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + + String methodName = getJavaBeanMethodName("get", attrName); + StringBuilder buffer = new StringBuilder("" + /*{ + <%=methodName%>().add(<%=attrName%>); + }*/ + ); + if (usePCS) { + buffer.append("" + /*{ firePropertyChange(<%=constantName%>, null, <%=attrName%>); + }*/ + ); + } + setOperationBody(operation, buffer.toString()); + } + + protected void createAddAllChildrenMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("addAll", attrName), + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); + + String methodName = getJavaBeanMethodName("get", attrName); + StringBuilder buffer = new StringBuilder("" + /*{ + <%=methodName%>().addAll(<%=attrName%>); + }*/ + ); + if (usePCS) { + buffer.append("" + /*{ firePropertyChange(<%=constantName%>, null, <%=attrName%>); + }*/ + ); + } + setOperationBody(operation, buffer.toString()); + } + + protected void createRemoveChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("remove", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + String methodName = getJavaBeanMethodName("get", attrName); + StringBuilder buffer = new StringBuilder(); + buffer.append("" + /*{ + boolean removed = <%=methodName%>().remove(<%=attrName%>);}*/ + ); + + if (usePCS) { + buffer.append("" + /*{ + if (removed) { + firePropertyChange(<%=constantName%>, <%=attrName%>, null); + }}*/ + ); + } + buffer.append("" + /*{ + return removed; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createRemoveAllChildrenMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("removeAll", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); + StringBuilder buffer = new StringBuilder(); + String methodName = getJavaBeanMethodName("get", attrName); + buffer.append("" + /*{ + boolean removed = <%=methodName%>().removeAll(<%=attrName%>);}*/ + ); + + if (usePCS) { + buffer.append("" + /*{ + if (removed) { + firePropertyChange(<%=constantName%>, <%=attrName%>, null); + }}*/ + ); + } + buffer.append("" + /*{ + return removed; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createContainsChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("contains", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + StringBuilder buffer = new StringBuilder(); + String methodName = getJavaBeanMethodName("get", attrName); + buffer.append("" + /*{ + boolean contains = <%=methodName%>().contains(<%=attrName%>); + return contains; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createContainsAllChildrenMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("containsAll", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); + StringBuilder buffer = new StringBuilder(); + String methodName = getJavaBeanMethodName("get", attrName); + buffer.append("" + /*{ + boolean contains = <%=methodName%>().containsAll(<%=attrName%>); + return contains; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createSetMethod(ObjectModelClass output, + String attrName, + String attrType, + String simpleType, + String constantName, + boolean usePCS) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("set", attrName), + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + + if (usePCS) { + String methodName = getJavaBeanMethodName("get", attrName); + setOperationBody(operation, "" + /*{ + <%=simpleType%> oldValue = <%=methodName%>(); + this.<%=attrName%> = <%=attrName%>; + firePropertyChange(<%=constantName%>, oldValue, <%=attrName%>); + }*/ + ); + } else { + setOperationBody(operation, "" + /*{ + this.<%=attrName%> = <%=attrName%>; + }*/ + ); + } + } + + protected void addSerializable(ObjectModelClass input, + ObjectModelClass output, + boolean interfaceFound) { + if (!interfaceFound) { + addInterface(output, Serializable.class); + } + + // Generate the serialVersionUID + long serialVersionUID = JpaTemplatesGeneratorUtil.generateSerialVersionUID(input); + + addConstant(output, + JpaTemplatesGeneratorUtil.SERIAL_VERSION_UID, + "long", + serialVersionUID + "L", + ObjectModelJavaModifier.PRIVATE + ); + } + + /** + * Add all interfaces defines in input class and returns if + * {@link Serializable} interface was found. + * + * @param input the input model class to process + * @param output the output generated class + * @return {@code true} if {@link Serializable} was found from input, + * {@code false} otherwise + */ + protected boolean addInterfaces(ObjectModelClass input, + ObjectModelClass output) { + boolean foundSerializable = false; + for (ObjectModelInterface parentInterface : input.getInterfaces()) { + String fqn = parentInterface.getQualifiedName(); + addInterface(output, fqn); + if (Serializable.class.getName().equals(fqn)) { + foundSerializable = true; + } + } + return foundSerializable; + } + + protected void createPropertyChangeSupport(ObjectModelClass output) { + + addAttribute(output, + "pcs", + PropertyChangeSupport.class, + "new PropertyChangeSupport(this)", + ObjectModelJavaModifier.PROTECTED, + ObjectModelJavaModifier.FINAL, + ObjectModelJavaModifier.TRANSIENT + ); + + // Add PropertyListener + + ObjectModelOperation operation; + + operation = addOperation(output, + "addPropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.addPropertyChangeListener(listener); + }*/ + ); + + operation = addOperation(output, + "addPropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.addPropertyChangeListener(propertyName, listener); + }*/ + ); + + operation = addOperation(output, + "removePropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.removePropertyChangeListener(listener); + }*/ + ); + + operation = addOperation(output, + "removePropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.removePropertyChangeListener(propertyName, listener); + }*/ + ); + + operation = addOperation(output, + "firePropertyChange", + "void", + ObjectModelJavaModifier.PROTECTED + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, Object.class, "oldValue"); + addParameter(operation, Object.class, "newValue"); + setOperationBody(operation, "" + /*{ + pcs.firePropertyChange(propertyName, oldValue, newValue); + }*/ + ); + + operation = addOperation(output, + "firePropertyChange", + "void", + ObjectModelJavaModifier.PROTECTED + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, Object.class, "newValue"); + setOperationBody(operation, "" + /*{ + firePropertyChange(propertyName, null, newValue); + }*/ + ); + } + + protected void createGetChildMethod(ObjectModelClass output) { + ObjectModelOperation getChild = addOperation( + output, + "getChild", "<T> T", + ObjectModelJavaModifier.PROTECTED + ); + addImport(output, List.class); + + addParameter(getChild, "java.util.Collection<T>", "childs"); + addParameter(getChild, "int", "index"); + setOperationBody(getChild, "" +/*{ + T result = null; + if (childs != null) { + if (childs instanceof List) { + if (index < childs.size()) { + result = ((List<T>) childs).get(index); + } + } else { + int i = 0; + for (T o : childs) { + if (index == i) { + result = o; + break; + } + i++; + } + } + } + return result; +}*/ + ); + } +} Deleted: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPADaoTransformer.java =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPADaoTransformer.java 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPADaoTransformer.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -1,72 +0,0 @@ -package org.nuiton.eugene.jpa; - -/* - * #%L - * EUGene :: JPA templates - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2013 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - - -/*{generator option: parentheses = false}*/ -/*{generator option: writeString = +}*/ - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.eugene.java.ObjectModelTransformerToJava; -import org.nuiton.eugene.models.object.ObjectModelClass; - -import java.util.Collection; - -/** - * JPADaoTransformer generates a dao for an entity. - * - * @author tchemit <chemit@codelutin.com> - * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.jpa.JPADaoTransformer" - * @since 2.7 - */ -public class JPADaoTransformer extends ObjectModelTransformerToJava { - - private static final Log log = LogFactory.getLog(JPADaoTransformer.class); - - @Override - public void transformFromClass(ObjectModelClass input) { - - if (!canGenerate(input)) { - - // not an entity, or skip, or already in classpath - return; - } - - //TODO - ObjectModelClass output = null; - } - - protected boolean canGenerate(ObjectModelClass input) { - boolean b = !JPATemplatesGeneratorUtil.hasSkipStereotype(input) && - JPATemplatesGeneratorUtil.hasEntityStereotype(input); - if (b) { - - // check if not found in class-path - b = !isInClassPath(input); - } - return b; - } -} \ No newline at end of file Deleted: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPAEntityTransformer.java =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPAEntityTransformer.java 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPAEntityTransformer.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -1,303 +0,0 @@ -package org.nuiton.eugene.jpa; - -/* - * #%L - * EUGene :: JPA templates - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2013 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.eugene.models.object.ObjectModelAttribute; -import org.nuiton.eugene.models.object.ObjectModelClass; -import org.nuiton.eugene.models.object.ObjectModelJavaModifier; -import org.nuiton.eugene.models.object.ObjectModelOperation; - -import javax.persistence.EntityManager; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -/*{generator option: parentheses = false}*/ -/*{generator option: writeString = +}*/ - -/** - * JavaBeanTransformer generates simple bean with pcs support - * (and nothing else) according to the JavaBeans 1.1 norm. - * - * Since version 2.2.1, it is possible to - * <ul> - * <li>generate a simple POJO (says with no PCS support) by using the tag value {@link JPATemplatesTagValues#TAG_NO_PCS}.</li> - * <li>generate i18n keys using the tag value {@link JPATemplatesTagValues#TAG_I18N_PREFIX}.</li> - * </ul> - * - * @author tchemit <chemit@codelutin.com> - * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.jpa.JPAEntityTransformer" - * @since 2.7 - */ -public class JPAEntityTransformer extends AbstractJPATransformer{ - - private static final Log log = LogFactory.getLog(JPAEntityTransformer.class); - - @Override - public void transformFromClass(ObjectModelClass input) { - - if (!canGenerate(input)) { - - // not an entity, or skip - return; - } - - EntityManager m; - - ObjectModelClass output = null; - - if (canGenerateAbstract(input)) { - - // nothing more to do - output = generateAbstract(input); - } - - if (canGenerateImpl(input, output)) { - - generateImpl(input); - } - } - - protected boolean canGenerate(ObjectModelClass input) { - boolean b = !JPATemplatesGeneratorUtil.hasSkipStereotype(input) && - JPATemplatesGeneratorUtil.hasEntityStereotype(input); - return b; - } - - protected boolean canGenerateAbstract(ObjectModelClass input) { - boolean b = !isInClassPath(input); - return b; - } - - protected boolean canGenerateImpl(ObjectModelClass input, - ObjectModelClass abstractOutput) { - String fqn = input.getQualifiedName() + "Impl"; - - if (isInClassPath(fqn)) { - - // already in class-path - return false; - } - - Collection<ObjectModelOperation> operations = input.getOperations(); - if (!operations.isEmpty()) { - - // this input have some specific operations (so Impl can not be generated) - return false; - } - - Collection<ObjectModelOperation> allOtherOperations = - input.getAllOtherOperations(true); - - if (!allOtherOperations.isEmpty()) { - - Collection<ObjectModelOperation> allExistingOperations = - new ArrayList<ObjectModelOperation>(input.getAllSuperclassOperations(true)); - - if (abstractOutput!=null) { - allExistingOperations.addAll(abstractOutput.getOperations()); - } - - for (ObjectModelOperation operation : allOtherOperations) { - if (!allExistingOperations.contains(operation)) { - - // found one method not on super classe, so can't generate Impl - return false; - } - } - } - return true; - -// boolean hasOperations = !allOtherOperations.isEmpty() || -// !operations.isEmpty(); -// -// // generate only if no user operations found -// return !hasOperations; - } - - - - - protected void createAbstractOperations(ObjectModelClass ouput, - Iterable<ObjectModelOperation> operations) { - JPATemplatesGeneratorUtil.cloneOperations( - this, - operations, - ouput, - true, - ObjectModelJavaModifier.ABSTRACT - ); - } - - protected ObjectModelClass generateAbstract(ObjectModelClass input) { - // test if a super class has bean stereotype - boolean superClassIsBean = false; - Collection<ObjectModelClass> superclasses = input.getSuperclasses(); - if(CollectionUtils.isNotEmpty(superclasses)) { - for (ObjectModelClass superclass : superclasses) { - if (JPATemplatesGeneratorUtil.hasEntityStereotype(superclass)) { - superClassIsBean = true; - break; - } - } - } - - String superClass = null; - - if (!superClassIsBean) { - - // try to find a super class by tag-value - superClass = - JPATemplatesGeneratorUtil.getEntitySuperClassTagValue( - model, input); - if (superClass != null) { - - // will act as if super class is a bean - superClassIsBean = true; - } - } - - ObjectModelClass output = - createAbstractClass(input.getName(), input.getPackageName()); - - if (superClass != null) { - setSuperClass(output, superClass); - } - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - String i18nPrefix = JPATemplatesGeneratorUtil.getI18nPrefixTagValue(input, model); - if (!StringUtils.isEmpty(i18nPrefix)) { - generateI18nBlock(input, output, i18nPrefix); - } - - String noPCSTagValue = JPATemplatesGeneratorUtil.getNoPCSTagValue(model, input); - boolean usePCS = StringUtils.isEmpty(noPCSTagValue) || - !"true".equals(noPCSTagValue.trim()); - - String noGenerateBooleanGetMethods = - JPATemplatesGeneratorUtil.getDoNotGenerateBooleanGetMethods(model, input); - boolean generateBooleanGetMethods = - StringUtils.isEmpty(noGenerateBooleanGetMethods) || - !"true".equals(noGenerateBooleanGetMethods.trim()); - - String prefix = getConstantPrefix(input, DEFAULT_CONSTANT_PREFIX); - - setConstantPrefix(prefix); - - addSuperClass(input, output); - - boolean serializableFound = addInterfaces(input, output); - - if (superClassIsBean) { - serializableFound = true; - } - - addSerializable(input, output, serializableFound); - - Set<String> constantNames = addConstantsFromDependency(input, output); - - // Get available properties - List<ObjectModelAttribute> properties = getProperties(input); - - // Add properties constant - for (ObjectModelAttribute attr : properties) { - - createPropertyConstant(output, attr, prefix, constantNames); - } - - // Add properties field + javabean methods - for (ObjectModelAttribute attr : properties) { - - createProperty(output, attr, usePCS, generateBooleanGetMethods); - } - - // Add operations - createAbstractOperations(output, input.getOperations()); - - if (!superClassIsBean) { - - if (usePCS) { - - // Add property change support - createPropertyChangeSupport(output); - } - } - - boolean hasAMultipleProperty = containsMutiple(properties); - - // Add helper operations - if (hasAMultipleProperty) { - - if (!superClassIsBean) { - - // add getChild methods - createGetChildMethod(output); - } - - } - return output; - } - - protected ObjectModelClass generateImpl(ObjectModelClass input) { - - ObjectModelClass resultClassImpl = createClass( - input.getName() + "Impl", - input.getPackageName() - ); - - // set the abstract resulClass as the resultClassImpl super class - setSuperClass(resultClassImpl, input.getQualifiedName()); - - // add a fix serialVersionUID, since the class has no field nor method - addConstant(resultClassImpl, - JPATemplatesGeneratorUtil.SERIAL_VERSION_UID, - "long", - "1L", - ObjectModelJavaModifier.PRIVATE - ); - return resultClassImpl; - } - - protected void addSuperClass(ObjectModelClass input, - ObjectModelClass output) { - // Set superclass - Iterator<ObjectModelClass> j = input.getSuperclasses().iterator(); - if (j.hasNext()) { - ObjectModelClass p = j.next(); - // We want to set the inheritance to the implementation class of the father - // Ex for model : A -> B (a inherits B) we want : A -> BImpl -> B - String qualifiedName = p.getQualifiedName() + "Impl"; - setSuperClass(output, qualifiedName); - } - } -} \ No newline at end of file Deleted: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesGeneratorUtil.java =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesGeneratorUtil.java 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesGeneratorUtil.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -1,84 +0,0 @@ -package org.nuiton.eugene.jpa; - -/* - * #%L - * EUGene :: JPA templates - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2013 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -import org.nuiton.eugene.java.JavaGeneratorUtil; -import org.nuiton.eugene.models.object.ObjectModel; -import org.nuiton.eugene.models.object.ObjectModelClassifier; - -/** - * Utility class for pure jpa templates. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.7 - */ -public class JPATemplatesGeneratorUtil extends JavaGeneratorUtil { - - /** - * Check if the given classifier has the - * {@link JPATemplatesStereoTypes#STEREOTYPE_ENTITY} stereotype. - * - * @param classifier classifier to test - * @return {@code true} if stereotype was found, {@code false otherwise} - * @see JPATemplatesStereoTypes#STEREOTYPE_ENTITY - */ - public static boolean hasEntityStereotype(ObjectModelClassifier classifier) { - return classifier.hasStereotype(JPATemplatesStereoTypes.STEREOTYPE_ENTITY); - } - - /** - * Obtain the value of the {@link JPATemplatesTagValues#TAG_NO_PCS} - * tag value on the given model or classifier. - * <p/> - * It will first look on the model, and then in the given classifier. - * - * @param model model to seek - * @param classifier classifier to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see JPATemplatesTagValues#TAG_NO_PCS - * @since 2.3 - */ - public static String getNoPCSTagValue(ObjectModel model, ObjectModelClassifier classifier) { - String value = findTagValue(JPATemplatesTagValues.TAG_NO_PCS, classifier, model); - return value; - } - - /** - * Obtain the value of the {@link JPATemplatesTagValues#TAG_ENTITY_SUPER_CLASS} - * tag value on the given model or classifier. - * <p/> - * It will first look on the model, and then in the given classifier. - * - * @param model model to seek - * @param classifier classifier to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see JPATemplatesTagValues#TAG_ENTITY_SUPER_CLASS - * @since 2.3 - */ - public static String getEntitySuperClassTagValue(ObjectModel model, ObjectModelClassifier classifier) { - String value = findTagValue(JPATemplatesTagValues.TAG_ENTITY_SUPER_CLASS, classifier, model); - return value; - } -} Deleted: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesModelPropertiesProvider.java =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesModelPropertiesProvider.java 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesModelPropertiesProvider.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -1,43 +0,0 @@ -package org.nuiton.eugene.jpa; - -/* - * #%L - * EUGene :: JPA templates - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2013 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -import org.nuiton.eugene.ModelPropertiesUtil; - -/** - * The JPA templates provider of tag values and stereotypes. - * - * @author tchemit <chemit@codelutin.com> - * @plexus.component role="org.nuiton.eugene.ModelPropertiesUtil$ModelPropertiesProvider" role-hint="jpa" - * @since 2.7 - */ -public class JPATemplatesModelPropertiesProvider extends ModelPropertiesUtil.ModelPropertiesProvider { - - @Override - public void init() throws IllegalAccessException { - scanStereotypeClass(JPATemplatesStereoTypes.class); - scanTagValueClass(JPATemplatesTagValues.class); - } -} Deleted: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesStereoTypes.java =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesStereoTypes.java 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesStereoTypes.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -1,49 +0,0 @@ -package org.nuiton.eugene.jpa; - -/* - * #%L - * EUGene :: JPA templates - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2013 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -import org.nuiton.eugene.EugeneStereoTypes; -import org.nuiton.eugene.ModelPropertiesUtil; -import org.nuiton.eugene.models.object.ObjectModelClassifier; - -/** - * Defines all stereotypes managed by JPA templates. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.7 - */ -public interface JPATemplatesStereoTypes extends EugeneStereoTypes { - - /** - * Stereotype for JPA entities to place on a classifier. - * - * @see JPATemplatesGeneratorUtil#hasEntityStereotype(ObjectModelClassifier) - * @since 2.5.6 - */ - @ModelPropertiesUtil.StereotypeDefinition( - target = ObjectModelClassifier.class, - documentation = "To specify that a class is a JPA Entity") - String STEREOTYPE_ENTITY = "entity"; -} Deleted: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesTagValues.java =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesTagValues.java 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesTagValues.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -1,116 +0,0 @@ -package org.nuiton.eugene.jpa; - -/* - * #%L - * EUGene :: JPA templates - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2013 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -import org.nuiton.eugene.EugeneTagValues; -import org.nuiton.eugene.ModelPropertiesUtil; -import org.nuiton.eugene.models.object.ObjectModel; -import org.nuiton.eugene.models.object.ObjectModelClassifier; - -/** - * Defines all tag values managed by JPA templates. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.7 - */ -public interface JPATemplatesTagValues extends EugeneTagValues { - - /** - * Tag value to set if do not want any pcs (says PropertyChangeSupport in a generated bean). - * <p/> - * You can globaly use it on the complete model or to a specific classifier. - * - * @see JPATemplatesGeneratorUtil#getNoPCSTagValue(ObjectModel, ObjectModelClassifier) - * @since 2.7 - */ - @ModelPropertiesUtil.TagValueDefinition( - target = {ObjectModel.class, ObjectModelClassifier.class}, - documentation = "To specify to not generate any propertyChange " + - "code for a class or any class of a model") - String TAG_NO_PCS = "noPCS"; - - /** - * Tag value to use a super class for generated bean. - * <p/> - * If the bean needs Property change support (says you do not add the {@link #TAG_NO_PCS} on classifier or model, - * then your class must provide evrything for it. - * <p/> - * More over, if you use some collections in your bean you must also define - * two method named {@code getChild(Collection list, int index)} and - * {@code getChild(List list, int index)} - * <p/> - * See new code to know minimum stuff to add in your class for this purpose. - * <pre> - * public abstract class AbstractEntity implements Serializable { - * - * private static final long serialVersionUID = 1L; - * - * protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this); - * - * public void addPropertyChangeListener(PropertyChangeListener listener) { - * pcs.addPropertyChangeListener(listener); - * } - * - * public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { - * pcs.addPropertyChangeListener(propertyName, listener); - * } - * - * public void removePropertyChangeListener(PropertyChangeListener listener) { - * pcs.removePropertyChangeListener(listener); - * } - * - * public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { - * pcs.removePropertyChangeListener(propertyName, listener); - * } - * - * protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { - * pcs.firePropertyChange(propertyName, oldValue, newValue); - * } - * - * protected void firePropertyChange(String propertyName, Object newValue) { - * firePropertyChange(propertyName, null, newValue); - * } - * - * protected <T> T getChild(Collection<T> list, int index) { - * return CollectionUtil.getOrNull(list, index); - * } - * - * protected <T> T getChild(List<T> list, int index) { - * return CollectionUtil.getOrNull(list, index); - * } - * } - * </pre> - * <p/> - * You can globaly use it on the complete model or to a specific classifier. - * - * @see JPATemplatesGeneratorUtil#getEntitySuperClassTagValue(ObjectModel, ObjectModelClassifier) - * @since 2.7 - */ - @ModelPropertiesUtil.TagValueDefinition( - target = {ObjectModel.class, ObjectModelClassifier.class}, - documentation = "To specify a super-class to used on generated entities " + - "for a class or any class of a model") - String TAG_ENTITY_SUPER_CLASS = "entitySuperClass"; -} Copied: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaDaoTransformer.java (from rev 1234, trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPADaoTransformer.java) =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaDaoTransformer.java (rev 0) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaDaoTransformer.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -0,0 +1,70 @@ +package org.nuiton.eugene.jpa; + +/* + * #%L + * EUGene :: JPA templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + + +/*{generator option: parentheses = false}*/ +/*{generator option: writeString = +}*/ + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.java.ObjectModelTransformerToJava; +import org.nuiton.eugene.models.object.ObjectModelClass; + +/** + * JpaDaoTransformer generates a dao for an entity. + * + * @author tchemit <chemit@codelutin.com> + * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.jpa.JpaDaoTransformer" + * @since 2.7 + */ +public class JpaDaoTransformer extends ObjectModelTransformerToJava { + + private static final Log log = LogFactory.getLog(JpaDaoTransformer.class); + + @Override + public void transformFromClass(ObjectModelClass input) { + + if (!canGenerate(input)) { + + // not an entity, or skip, or already in classpath + return; + } + + //TODO + ObjectModelClass output = null; + } + + protected boolean canGenerate(ObjectModelClass input) { + boolean b = !JpaTemplatesGeneratorUtil.hasSkipStereotype(input) && + JpaTemplatesGeneratorUtil.hasEntityStereotype(input); + if (b) { + + // check if not found in class-path + b = !isInClassPath(input); + } + return b; + } +} \ No newline at end of file Copied: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaEntityTransformer.java (from rev 1234, trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPAEntityTransformer.java) =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaEntityTransformer.java (rev 0) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaEntityTransformer.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -0,0 +1,303 @@ +package org.nuiton.eugene.jpa; + +/* + * #%L + * EUGene :: JPA templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelJavaModifier; +import org.nuiton.eugene.models.object.ObjectModelOperation; + +import javax.persistence.EntityManager; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +/*{generator option: parentheses = false}*/ +/*{generator option: writeString = +}*/ + +/** + * JavaBeanTransformer generates simple bean with pcs support + * (and nothing else) according to the JavaBeans 1.1 norm. + * + * Since version 2.2.1, it is possible to + * <ul> + * <li>generate a simple POJO (says with no PCS support) by using the tag value {@link JpaTemplatesTagValues#TAG_NO_PCS}.</li> + * <li>generate i18n keys using the tag value {@link JpaTemplatesTagValues#TAG_I18N_PREFIX}.</li> + * </ul> + * + * @author tchemit <chemit@codelutin.com> + * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.jpa.JpaEntityTransformer" + * @since 2.7 + */ +public class JpaEntityTransformer extends AbstractJpaTransformer { + + private static final Log log = LogFactory.getLog(JpaEntityTransformer.class); + + @Override + public void transformFromClass(ObjectModelClass input) { + + if (!canGenerate(input)) { + + // not an entity, or skip + return; + } + + EntityManager m; + + ObjectModelClass output = null; + + if (canGenerateAbstract(input)) { + + // nothing more to do + output = generateAbstract(input); + } + + if (canGenerateImpl(input, output)) { + + generateImpl(input); + } + } + + protected boolean canGenerate(ObjectModelClass input) { + boolean b = !JpaTemplatesGeneratorUtil.hasSkipStereotype(input) && + JpaTemplatesGeneratorUtil.hasEntityStereotype(input); + return b; + } + + protected boolean canGenerateAbstract(ObjectModelClass input) { + boolean b = !isInClassPath(input); + return b; + } + + protected boolean canGenerateImpl(ObjectModelClass input, + ObjectModelClass abstractOutput) { + String fqn = input.getQualifiedName() + "Impl"; + + if (isInClassPath(fqn)) { + + // already in class-path + return false; + } + + Collection<ObjectModelOperation> operations = input.getOperations(); + if (!operations.isEmpty()) { + + // this input have some specific operations (so Impl can not be generated) + return false; + } + + Collection<ObjectModelOperation> allOtherOperations = + input.getAllOtherOperations(true); + + if (!allOtherOperations.isEmpty()) { + + Collection<ObjectModelOperation> allExistingOperations = + new ArrayList<ObjectModelOperation>(input.getAllSuperclassOperations(true)); + + if (abstractOutput!=null) { + allExistingOperations.addAll(abstractOutput.getOperations()); + } + + for (ObjectModelOperation operation : allOtherOperations) { + if (!allExistingOperations.contains(operation)) { + + // found one method not on super classe, so can't generate Impl + return false; + } + } + } + return true; + +// boolean hasOperations = !allOtherOperations.isEmpty() || +// !operations.isEmpty(); +// +// // generate only if no user operations found +// return !hasOperations; + } + + + + + protected void createAbstractOperations(ObjectModelClass ouput, + Iterable<ObjectModelOperation> operations) { + JpaTemplatesGeneratorUtil.cloneOperations( + this, + operations, + ouput, + true, + ObjectModelJavaModifier.ABSTRACT + ); + } + + protected ObjectModelClass generateAbstract(ObjectModelClass input) { + // test if a super class has bean stereotype + boolean superClassIsBean = false; + Collection<ObjectModelClass> superclasses = input.getSuperclasses(); + if(CollectionUtils.isNotEmpty(superclasses)) { + for (ObjectModelClass superclass : superclasses) { + if (JpaTemplatesGeneratorUtil.hasEntityStereotype(superclass)) { + superClassIsBean = true; + break; + } + } + } + + String superClass = null; + + if (!superClassIsBean) { + + // try to find a super class by tag-value + superClass = + JpaTemplatesGeneratorUtil.getEntitySuperClassTagValue( + model, input); + if (superClass != null) { + + // will act as if super class is a bean + superClassIsBean = true; + } + } + + ObjectModelClass output = + createAbstractClass(input.getName(), input.getPackageName()); + + if (superClass != null) { + setSuperClass(output, superClass); + } + if (log.isDebugEnabled()) { + log.debug("will generate " + output.getQualifiedName()); + } + + String i18nPrefix = JpaTemplatesGeneratorUtil.getI18nPrefixTagValue(input, model); + if (!StringUtils.isEmpty(i18nPrefix)) { + generateI18nBlock(input, output, i18nPrefix); + } + + String noPCSTagValue = JpaTemplatesGeneratorUtil.getNoPCSTagValue(model, input); + boolean usePCS = StringUtils.isEmpty(noPCSTagValue) || + !"true".equals(noPCSTagValue.trim()); + + String noGenerateBooleanGetMethods = + JpaTemplatesGeneratorUtil.getDoNotGenerateBooleanGetMethods(model, input); + boolean generateBooleanGetMethods = + StringUtils.isEmpty(noGenerateBooleanGetMethods) || + !"true".equals(noGenerateBooleanGetMethods.trim()); + + String prefix = getConstantPrefix(input, DEFAULT_CONSTANT_PREFIX); + + setConstantPrefix(prefix); + + addSuperClass(input, output); + + boolean serializableFound = addInterfaces(input, output); + + if (superClassIsBean) { + serializableFound = true; + } + + addSerializable(input, output, serializableFound); + + Set<String> constantNames = addConstantsFromDependency(input, output); + + // Get available properties + List<ObjectModelAttribute> properties = getProperties(input); + + // Add properties constant + for (ObjectModelAttribute attr : properties) { + + createPropertyConstant(output, attr, prefix, constantNames); + } + + // Add properties field + javabean methods + for (ObjectModelAttribute attr : properties) { + + createProperty(output, attr, usePCS, generateBooleanGetMethods); + } + + // Add operations + createAbstractOperations(output, input.getOperations()); + + if (!superClassIsBean) { + + if (usePCS) { + + // Add property change support + createPropertyChangeSupport(output); + } + } + + boolean hasAMultipleProperty = containsMutiple(properties); + + // Add helper operations + if (hasAMultipleProperty) { + + if (!superClassIsBean) { + + // add getChild methods + createGetChildMethod(output); + } + + } + return output; + } + + protected ObjectModelClass generateImpl(ObjectModelClass input) { + + ObjectModelClass resultClassImpl = createClass( + input.getName() + "Impl", + input.getPackageName() + ); + + // set the abstract resulClass as the resultClassImpl super class + setSuperClass(resultClassImpl, input.getQualifiedName()); + + // add a fix serialVersionUID, since the class has no field nor method + addConstant(resultClassImpl, + JpaTemplatesGeneratorUtil.SERIAL_VERSION_UID, + "long", + "1L", + ObjectModelJavaModifier.PRIVATE + ); + return resultClassImpl; + } + + protected void addSuperClass(ObjectModelClass input, + ObjectModelClass output) { + // Set superclass + Iterator<ObjectModelClass> j = input.getSuperclasses().iterator(); + if (j.hasNext()) { + ObjectModelClass p = j.next(); + // We want to set the inheritance to the implementation class of the father + // Ex for model : A -> B (a inherits B) we want : A -> BImpl -> B + String qualifiedName = p.getQualifiedName() + "Impl"; + setSuperClass(output, qualifiedName); + } + } +} \ No newline at end of file Copied: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesGeneratorUtil.java (from rev 1234, trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesGeneratorUtil.java) =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesGeneratorUtil.java (rev 0) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesGeneratorUtil.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -0,0 +1,84 @@ +package org.nuiton.eugene.jpa; + +/* + * #%L + * EUGene :: JPA templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.nuiton.eugene.java.JavaGeneratorUtil; +import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.eugene.models.object.ObjectModelClassifier; + +/** + * Utility class for pure jpa templates. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.7 + */ +public class JpaTemplatesGeneratorUtil extends JavaGeneratorUtil { + + /** + * Check if the given classifier has the + * {@link JpaTemplatesStereoTypes#STEREOTYPE_ENTITY} stereotype. + * + * @param classifier classifier to test + * @return {@code true} if stereotype was found, {@code false otherwise} + * @see JpaTemplatesStereoTypes#STEREOTYPE_ENTITY + */ + public static boolean hasEntityStereotype(ObjectModelClassifier classifier) { + return classifier.hasStereotype(JpaTemplatesStereoTypes.STEREOTYPE_ENTITY); + } + + /** + * Obtain the value of the {@link JpaTemplatesTagValues#TAG_NO_PCS} + * tag value on the given model or classifier. + * <p/> + * It will first look on the model, and then in the given classifier. + * + * @param model model to seek + * @param classifier classifier to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see JpaTemplatesTagValues#TAG_NO_PCS + * @since 2.3 + */ + public static String getNoPCSTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JpaTemplatesTagValues.TAG_NO_PCS, classifier, model); + return value; + } + + /** + * Obtain the value of the {@link JpaTemplatesTagValues#TAG_ENTITY_SUPER_CLASS} + * tag value on the given model or classifier. + * <p/> + * It will first look on the model, and then in the given classifier. + * + * @param model model to seek + * @param classifier classifier to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see JpaTemplatesTagValues#TAG_ENTITY_SUPER_CLASS + * @since 2.3 + */ + public static String getEntitySuperClassTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JpaTemplatesTagValues.TAG_ENTITY_SUPER_CLASS, classifier, model); + return value; + } +} Copied: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesModelPropertiesProvider.java (from rev 1234, trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesModelPropertiesProvider.java) =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesModelPropertiesProvider.java (rev 0) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesModelPropertiesProvider.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -0,0 +1,43 @@ +package org.nuiton.eugene.jpa; + +/* + * #%L + * EUGene :: JPA templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.nuiton.eugene.ModelPropertiesUtil; + +/** + * The JPA templates provider of tag values and stereotypes. + * + * @author tchemit <chemit@codelutin.com> + * @plexus.component role="org.nuiton.eugene.ModelPropertiesUtil$ModelPropertiesProvider" role-hint="jpa" + * @since 2.7 + */ +public class JpaTemplatesModelPropertiesProvider extends ModelPropertiesUtil.ModelPropertiesProvider { + + @Override + public void init() throws IllegalAccessException { + scanStereotypeClass(JpaTemplatesStereoTypes.class); + scanTagValueClass(JpaTemplatesTagValues.class); + } +} Copied: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesStereoTypes.java (from rev 1234, trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesStereoTypes.java) =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesStereoTypes.java (rev 0) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesStereoTypes.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -0,0 +1,49 @@ +package org.nuiton.eugene.jpa; + +/* + * #%L + * EUGene :: JPA templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.nuiton.eugene.EugeneStereoTypes; +import org.nuiton.eugene.ModelPropertiesUtil; +import org.nuiton.eugene.models.object.ObjectModelClassifier; + +/** + * Defines all stereotypes managed by JPA templates. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.7 + */ +public interface JpaTemplatesStereoTypes extends EugeneStereoTypes { + + /** + * Stereotype for JPA entities to place on a classifier. + * + * @see JpaTemplatesGeneratorUtil#hasEntityStereotype(ObjectModelClassifier) + * @since 2.5.6 + */ + @ModelPropertiesUtil.StereotypeDefinition( + target = ObjectModelClassifier.class, + documentation = "To specify that a class is a JPA Entity") + String STEREOTYPE_ENTITY = "entity"; +} Copied: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesTagValues.java (from rev 1234, trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JPATemplatesTagValues.java) =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesTagValues.java (rev 0) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/JpaTemplatesTagValues.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -0,0 +1,116 @@ +package org.nuiton.eugene.jpa; + +/* + * #%L + * EUGene :: JPA templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.nuiton.eugene.EugeneTagValues; +import org.nuiton.eugene.ModelPropertiesUtil; +import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.eugene.models.object.ObjectModelClassifier; + +/** + * Defines all tag values managed by JPA templates. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.7 + */ +public interface JpaTemplatesTagValues extends EugeneTagValues { + + /** + * Tag value to set if do not want any pcs (says PropertyChangeSupport in a generated bean). + * <p/> + * You can globaly use it on the complete model or to a specific classifier. + * + * @see JpaTemplatesGeneratorUtil#getNoPCSTagValue(ObjectModel, ObjectModelClassifier) + * @since 2.7 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "To specify to not generate any propertyChange " + + "code for a class or any class of a model") + String TAG_NO_PCS = "noPCS"; + + /** + * Tag value to use a super class for generated bean. + * <p/> + * If the bean needs Property change support (says you do not add the {@link #TAG_NO_PCS} on classifier or model, + * then your class must provide evrything for it. + * <p/> + * More over, if you use some collections in your bean you must also define + * two method named {@code getChild(Collection list, int index)} and + * {@code getChild(List list, int index)} + * <p/> + * See new code to know minimum stuff to add in your class for this purpose. + * <pre> + * public abstract class AbstractEntity implements Serializable { + * + * private static final long serialVersionUID = 1L; + * + * protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this); + * + * public void addPropertyChangeListener(PropertyChangeListener listener) { + * pcs.addPropertyChangeListener(listener); + * } + * + * public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + * pcs.addPropertyChangeListener(propertyName, listener); + * } + * + * public void removePropertyChangeListener(PropertyChangeListener listener) { + * pcs.removePropertyChangeListener(listener); + * } + * + * public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { + * pcs.removePropertyChangeListener(propertyName, listener); + * } + * + * protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { + * pcs.firePropertyChange(propertyName, oldValue, newValue); + * } + * + * protected void firePropertyChange(String propertyName, Object newValue) { + * firePropertyChange(propertyName, null, newValue); + * } + * + * protected <T> T getChild(Collection<T> list, int index) { + * return CollectionUtil.getOrNull(list, index); + * } + * + * protected <T> T getChild(List<T> list, int index) { + * return CollectionUtil.getOrNull(list, index); + * } + * } + * </pre> + * <p/> + * You can globaly use it on the complete model or to a specific classifier. + * + * @see JpaTemplatesGeneratorUtil#getEntitySuperClassTagValue(ObjectModel, ObjectModelClassifier) + * @since 2.7 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "To specify a super-class to used on generated entities " + + "for a class or any class of a model") + String TAG_ENTITY_SUPER_CLASS = "entitySuperClass"; +} Modified: trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/package-info.java =================================================================== --- trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/package-info.java 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/main/java/org/nuiton/eugene/jpa/package-info.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -1,8 +1,8 @@ /** * Eugene jpa package : JPA generators. * <pre> - * - Generator : {@link JPAEntityTransformer } to generate a jap entity. - * - Generator : {@link JPADaoTransformer } to generate a dao. + * - Generator : {@link JpaEntityTransformer } to generate a jap entity. + * - Generator : {@link JpaDaoTransformer } to generate a dao. * </pre> */ package org.nuiton.eugene.jpa; Modified: trunk/eugene-jpa-templates/src/main/resources/META-INF/services/org.nuiton.eugene.ModelPropertiesUtil$ModelPropertiesProvider =================================================================== --- trunk/eugene-jpa-templates/src/main/resources/META-INF/services/org.nuiton.eugene.ModelPropertiesUtil$ModelPropertiesProvider 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/main/resources/META-INF/services/org.nuiton.eugene.ModelPropertiesUtil$ModelPropertiesProvider 2013-04-01 20:50:47 UTC (rev 1236) @@ -1 +1 @@ -org.nuiton.eugene.jpa.JPATemplatesModelPropertiesProvider \ No newline at end of file +org.nuiton.eugene.jpa.JpaTemplatesModelPropertiesProvider \ No newline at end of file Modified: trunk/eugene-jpa-templates/src/site/fr/rst/index.rst =================================================================== --- trunk/eugene-jpa-templates/src/site/fr/rst/index.rst 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/site/fr/rst/index.rst 2013-04-01 20:50:47 UTC (rev 1236) @@ -38,7 +38,7 @@ Ce module offre des templates pour générer des objets jpa, à savoir : -- des entités (*org.nuiton.eugene.jpa.JPAEntityTransformer*) -- des dao (*org.nuiton.eugene.jpa.JPADaoTransformer*) +- des entités (*org.nuiton.eugene.jpa.JpaEntityTransformer*) +- des dao (*org.nuiton.eugene.jpa.JpaDaoTransformer*) *TODO Finir cette documentation avant la version 3.0 en donnant un exemple complet d'utilisation.* \ No newline at end of file Modified: trunk/eugene-jpa-templates/src/site/rst/index.rst =================================================================== --- trunk/eugene-jpa-templates/src/site/rst/index.rst 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/site/rst/index.rst 2013-04-01 20:50:47 UTC (rev 1236) @@ -38,7 +38,7 @@ This module offers you some templates to generate some JPA stuff: -- entities (*org.nuiton.eugene.jpa.JPAEntityTransformer*) -- dao (*org.nuiton.eugene.jpa.JPADaoTransformer*) +- entities (*org.nuiton.eugene.jpa.JpaEntityTransformer*) +- dao (*org.nuiton.eugene.jpa.JpaDaoTransformer*) * TODO Finish this documentation before version 3.0*. Deleted: trunk/eugene-jpa-templates/src/test/java/org/nuiton/eugene/jpa/JPATemplatesModelPropertiesProviderTest.java =================================================================== --- trunk/eugene-jpa-templates/src/test/java/org/nuiton/eugene/jpa/JPATemplatesModelPropertiesProviderTest.java 2013-03-29 23:36:59 UTC (rev 1235) +++ trunk/eugene-jpa-templates/src/test/java/org/nuiton/eugene/jpa/JPATemplatesModelPropertiesProviderTest.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -1,146 +0,0 @@ -package org.nuiton.eugene.jpa; - -/* - * #%L - * EUGene :: JPA templates - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2013 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.nuiton.eugene.ModelPropertiesUtil; -import org.nuiton.eugene.models.object.ObjectModel; -import org.nuiton.eugene.models.object.ObjectModelAttribute; -import org.nuiton.eugene.models.object.ObjectModelClass; -import org.nuiton.eugene.models.object.ObjectModelClassifier; -import org.nuiton.eugene.models.object.ObjectModelEnumeration; -import org.nuiton.eugene.models.object.ObjectModelInterface; -import org.nuiton.eugene.models.object.ObjectModelOperation; - -/** - * To test stereotypes and tag values of this module. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.7 - */ -public class JPATemplatesModelPropertiesProviderTest { - - protected ModelPropertiesUtil.ModelPropertiesProvider provider; - - @Before - public void setUp() throws Exception { - provider = new JPATemplatesModelPropertiesProvider(); - provider.init(); - } - - @Test - public void getTagValueTarget() throws Exception { - getTagValueTarget(JPATemplatesTagValues.TAG_NO_PCS, ObjectModel.class, ObjectModelClassifier.class); - } - - @Test - public void containsTagValue() throws Exception { - containsTagValue(JPATemplatesTagValues.TAG_NO_PCS, true); - containsTagValue(JPATemplatesTagValues.TAG_ENTITY_SUPER_CLASS, true); - - long l = System.nanoTime(); - containsTagValue(JPATemplatesTagValues.TAG_NO_PCS + l, false); - containsTagValue(JPATemplatesTagValues.TAG_ENTITY_SUPER_CLASS + l, false); - } - - @Test - public void acceptTagValue() throws Exception { - acceptTagValue(JPATemplatesTagValues.TAG_NO_PCS, true, ObjectModel.class, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class); - acceptTagValue(JPATemplatesTagValues.TAG_ENTITY_SUPER_CLASS, true, ObjectModel.class, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class); - - acceptTagValue(JPATemplatesTagValues.TAG_NO_PCS, false, ObjectModelOperation.class, ObjectModelAttribute.class); - acceptTagValue(JPATemplatesTagValues.TAG_ENTITY_SUPER_CLASS, false, ObjectModelOperation.class, ObjectModelAttribute.class); - - long l = System.nanoTime(); - acceptTagValue(JPATemplatesTagValues.TAG_NO_PCS + l, false, ObjectModelOperation.class, ObjectModelAttribute.class); - acceptTagValue(JPATemplatesTagValues.TAG_ENTITY_SUPER_CLASS + l, false, ObjectModelOperation.class, ObjectModelAttribute.class); - } - - @Test - public void getStereotypeTarget() throws Exception { - - getStereotypeTarget(JPATemplatesStereoTypes.STEREOTYPE_ENTITY, ObjectModelClassifier.class); - } - - @Test - public void containsStereotype() throws Exception { - containsStereotype(JPATemplatesStereoTypes.STEREOTYPE_ENTITY, true); - - long l = System.nanoTime(); - containsStereotype(JPATemplatesStereoTypes.STEREOTYPE_ENTITY + l, false); - } - - @Test - public void acceptStereotype() throws Exception { - - acceptStereotype(JPATemplatesStereoTypes.STEREOTYPE_ENTITY, true, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class, ObjectModelInterface.class); - - acceptStereotype(JPATemplatesStereoTypes.STEREOTYPE_ENTITY, false, ObjectModel.class, ObjectModelOperation.class); - - long l = System.nanoTime(); - acceptStereotype(JPATemplatesStereoTypes.STEREOTYPE_ENTITY + l, false, ObjectModel.class, ObjectModelOperation.class); - - } - - protected void getTagValueTarget(String name, Class<?>... expected) { - Class<?>[] classes = provider.getTagValueTarget(name); - Assert.assertNotNull("Could not find target for " + name, classes); - Assert.assertEquals("Should have " + expected.length + " targets for " + name + " but had " + classes.length, classes.length, expected.length); - Assert.assertArrayEquals(expected, classes); - } - - protected void containsTagValue(String name, boolean expected) { - boolean classes = provider.containsTagValue(name); - Assert.assertEquals(expected, classes); - } - - protected void acceptTagValue(String name, boolean expected, Class<?>... types) { - for (Class<?> type : types) { - boolean actual = provider.acceptTagValue(name, type); - Assert.assertEquals(expected, actual); - } - } - - protected void getStereotypeTarget(String name, Class<?>... expected) { - Class<?>[] classes = provider.getStereotypeTarget(name); - Assert.assertNotNull("Could not find target for " + name, classes); - Assert.assertEquals("Should have " + expected.length + " targets for " + name + " but had " + classes.length, classes.length, expected.length); - Assert.assertArrayEquals(expected, classes); - } - - protected void containsStereotype(String name, boolean expected) { - boolean classes = provider.containsStereotype(name); - Assert.assertEquals(expected, classes); - } - - protected void acceptStereotype(String name, boolean expected, Class<?>... types) { - for (Class<?> type : types) { - boolean actual = provider.acceptStereotype(name, type); - Assert.assertEquals(expected, actual); - } - } -} Copied: trunk/eugene-jpa-templates/src/test/java/org/nuiton/eugene/jpa/JpaTemplatesModelPropertiesProviderTest.java (from rev 1234, trunk/eugene-jpa-templates/src/test/java/org/nuiton/eugene/jpa/JPATemplatesModelPropertiesProviderTest.java) =================================================================== --- trunk/eugene-jpa-templates/src/test/java/org/nuiton/eugene/jpa/JpaTemplatesModelPropertiesProviderTest.java (rev 0) +++ trunk/eugene-jpa-templates/src/test/java/org/nuiton/eugene/jpa/JpaTemplatesModelPropertiesProviderTest.java 2013-04-01 20:50:47 UTC (rev 1236) @@ -0,0 +1,146 @@ +package org.nuiton.eugene.jpa; + +/* + * #%L + * EUGene :: JPA templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.eugene.ModelPropertiesUtil; +import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelClassifier; +import org.nuiton.eugene.models.object.ObjectModelEnumeration; +import org.nuiton.eugene.models.object.ObjectModelInterface; +import org.nuiton.eugene.models.object.ObjectModelOperation; + +/** + * To test stereotypes and tag values of this module. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.7 + */ +public class JpaTemplatesModelPropertiesProviderTest { + + protected ModelPropertiesUtil.ModelPropertiesProvider provider; + + @Before + public void setUp() throws Exception { + provider = new JpaTemplatesModelPropertiesProvider(); + provider.init(); + } + + @Test + public void getTagValueTarget() throws Exception { + getTagValueTarget(JpaTemplatesTagValues.TAG_NO_PCS, ObjectModel.class, ObjectModelClassifier.class); + } + + @Test + public void containsTagValue() throws Exception { + containsTagValue(JpaTemplatesTagValues.TAG_NO_PCS, true); + containsTagValue(JpaTemplatesTagValues.TAG_ENTITY_SUPER_CLASS, true); + + long l = System.nanoTime(); + containsTagValue(JpaTemplatesTagValues.TAG_NO_PCS + l, false); + containsTagValue(JpaTemplatesTagValues.TAG_ENTITY_SUPER_CLASS + l, false); + } + + @Test + public void acceptTagValue() throws Exception { + acceptTagValue(JpaTemplatesTagValues.TAG_NO_PCS, true, ObjectModel.class, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class); + acceptTagValue(JpaTemplatesTagValues.TAG_ENTITY_SUPER_CLASS, true, ObjectModel.class, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class); + + acceptTagValue(JpaTemplatesTagValues.TAG_NO_PCS, false, ObjectModelOperation.class, ObjectModelAttribute.class); + acceptTagValue(JpaTemplatesTagValues.TAG_ENTITY_SUPER_CLASS, false, ObjectModelOperation.class, ObjectModelAttribute.class); + + long l = System.nanoTime(); + acceptTagValue(JpaTemplatesTagValues.TAG_NO_PCS + l, false, ObjectModelOperation.class, ObjectModelAttribute.class); + acceptTagValue(JpaTemplatesTagValues.TAG_ENTITY_SUPER_CLASS + l, false, ObjectModelOperation.class, ObjectModelAttribute.class); + } + + @Test + public void getStereotypeTarget() throws Exception { + + getStereotypeTarget(JpaTemplatesStereoTypes.STEREOTYPE_ENTITY, ObjectModelClassifier.class); + } + + @Test + public void containsStereotype() throws Exception { + containsStereotype(JpaTemplatesStereoTypes.STEREOTYPE_ENTITY, true); + + long l = System.nanoTime(); + containsStereotype(JpaTemplatesStereoTypes.STEREOTYPE_ENTITY + l, false); + } + + @Test + public void acceptStereotype() throws Exception { + + acceptStereotype(JpaTemplatesStereoTypes.STEREOTYPE_ENTITY, true, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class, ObjectModelInterface.class); + + acceptStereotype(JpaTemplatesStereoTypes.STEREOTYPE_ENTITY, false, ObjectModel.class, ObjectModelOperation.class); + + long l = System.nanoTime(); + acceptStereotype(JpaTemplatesStereoTypes.STEREOTYPE_ENTITY + l, false, ObjectModel.class, ObjectModelOperation.class); + + } + + protected void getTagValueTarget(String name, Class<?>... expected) { + Class<?>[] classes = provider.getTagValueTarget(name); + Assert.assertNotNull("Could not find target for " + name, classes); + Assert.assertEquals("Should have " + expected.length + " targets for " + name + " but had " + classes.length, classes.length, expected.length); + Assert.assertArrayEquals(expected, classes); + } + + protected void containsTagValue(String name, boolean expected) { + boolean classes = provider.containsTagValue(name); + Assert.assertEquals(expected, classes); + } + + protected void acceptTagValue(String name, boolean expected, Class<?>... types) { + for (Class<?> type : types) { + boolean actual = provider.acceptTagValue(name, type); + Assert.assertEquals(expected, actual); + } + } + + protected void getStereotypeTarget(String name, Class<?>... expected) { + Class<?>[] classes = provider.getStereotypeTarget(name); + Assert.assertNotNull("Could not find target for " + name, classes); + Assert.assertEquals("Should have " + expected.length + " targets for " + name + " but had " + classes.length, classes.length, expected.length); + Assert.assertArrayEquals(expected, classes); + } + + protected void containsStereotype(String name, boolean expected) { + boolean classes = provider.containsStereotype(name); + Assert.assertEquals(expected, classes); + } + + protected void acceptStereotype(String name, boolean expected, Class<?>... types) { + for (Class<?> type : types) { + boolean actual = provider.acceptStereotype(name, type); + Assert.assertEquals(expected, actual); + } + } +}
participants (1)
-
tchemit@users.nuiton.org