r671 - in nuiton-jpa: nuiton-jpa-api/src/main/java/org/nuiton/jpa/api nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/hibernate nuiton-jpa-junit/src/main/java/org/nuiton/jpa/junit nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates
Author: tchemit Date: 2013-06-15 16:27:31 +0200 (Sat, 15 Jun 2013) New Revision: 671 Url: http://nuiton.org/projects/sandbox/repository/revisions/671 Log: - add Entity visitor api - improve entity template (deal with not null collection) - reformat code + optimize dependencies Added: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityVisitable.java nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityVisitor.java Modified: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaEntity.java nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaDao.java nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntities.java nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/hibernate/HibernateUtil.java nuiton-jpa/nuiton-jpa-junit/src/main/java/org/nuiton/jpa/junit/JpaEntityManagerRule.java nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/AbstractJpaTransformer.java nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java Modified: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaEntity.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaEntity.java 2013-06-14 09:20:52 UTC (rev 670) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaEntity.java 2013-06-15 14:27:31 UTC (rev 671) @@ -33,7 +33,7 @@ * @author tchemit <chemit@codelutin.com> * @since 0.1 */ -public abstract class AbstractJpaEntity implements JpaEntity{ +public abstract class AbstractJpaEntity implements JpaEntity { @Override public boolean equals(Object other) { Modified: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaDao.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaDao.java 2013-06-14 09:20:52 UTC (rev 670) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaDao.java 2013-06-15 14:27:31 UTC (rev 671) @@ -30,7 +30,6 @@ * Represents de common operation we can do using JPA * * @param <E> is the type of the entity manipulated with this DAO - * * @author bleny <leny@codelutin.com> * @author tchemit <chemit@codelutin.com> * @since 0.1 Modified: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntities.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntities.java 2013-06-14 09:20:52 UTC (rev 670) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntities.java 2013-06-15 14:27:31 UTC (rev 671) @@ -28,12 +28,10 @@ import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Predicates; -import com.google.common.collect.Collections2; import com.google.common.collect.Iterables; import org.apache.commons.lang3.ObjectUtils; import java.io.Serializable; -import java.util.Collection; import java.util.Comparator; /** Added: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityVisitable.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityVisitable.java (rev 0) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityVisitable.java 2013-06-15 14:27:31 UTC (rev 671) @@ -0,0 +1,37 @@ + +package org.nuiton.jpa.api; + +/* + * #%L + * Nuiton Jpa :: API + * $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% + */ + +/** + * The contract of a visitable entity. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public interface JpaEntityVisitable { + + void accept(JpaEntityVisitor visitor); +} Property changes on: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityVisitable.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityVisitor.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityVisitor.java (rev 0) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityVisitor.java 2013-06-15 14:27:31 UTC (rev 671) @@ -0,0 +1,37 @@ + +package org.nuiton.jpa.api; + +/* + * #%L + * Nuiton Jpa :: API + * $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% + */ + +/** + * The contract of a visitor of any {@link JpaEntity}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public interface JpaEntityVisitor { + + void visit(JpaEntity entity); +} Property changes on: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityVisitor.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/hibernate/HibernateUtil.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/hibernate/HibernateUtil.java 2013-06-14 09:20:52 UTC (rev 670) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/hibernate/HibernateUtil.java 2013-06-15 14:27:31 UTC (rev 671) @@ -72,7 +72,8 @@ } - private HibernateUtil() {} + private HibernateUtil() { + } public static EntityManagerFactory createTempEntityManagerFactory(String persistenceUnitName, String context) { @@ -143,7 +144,7 @@ } } - SchemaExport schemaExport= new SchemaExport(serviceRegistry, configuration); + SchemaExport schemaExport = new SchemaExport(serviceRegistry, configuration); schemaExport.setHaltOnError(true); Modified: nuiton-jpa/nuiton-jpa-junit/src/main/java/org/nuiton/jpa/junit/JpaEntityManagerRule.java =================================================================== --- nuiton-jpa/nuiton-jpa-junit/src/main/java/org/nuiton/jpa/junit/JpaEntityManagerRule.java 2013-06-14 09:20:52 UTC (rev 670) +++ nuiton-jpa/nuiton-jpa-junit/src/main/java/org/nuiton/jpa/junit/JpaEntityManagerRule.java 2013-06-15 14:27:31 UTC (rev 671) @@ -42,7 +42,7 @@ protected String persistenceUnitName; - protected String timestamp = String.valueOf(new Date().getTime()); + protected static String timestamp = String.valueOf(new Date().getTime()); protected EntityManager entityManager; @@ -64,7 +64,7 @@ if (log.isDebugEnabled()) { log.debug("will create entityManager for test class " - + testClassName + " and method " + testMethodName); + + testClassName + " and method " + testMethodName); } return new Statement() { @@ -83,8 +83,8 @@ protected void createEntityManager(String testClassName, String testMethodName) { String context = testClassName - + '_' + testMethodName - + '_' + timestamp; + + '_' + testMethodName + + '_' + timestamp; EntityManagerFactory entityManagerFactory = HibernateUtil.createTempEntityManagerFactory( @@ -102,7 +102,7 @@ public EntityManager getEntityManager() { - if ( ! open) { + if (!open) { throw new IllegalStateException("entity manager is not yet opened"); } @@ -110,9 +110,7 @@ } - /** - * Override to tear down your specific external resource. - */ + /** Override to tear down your specific external resource. */ protected void closeEntityManager() { if (log.isDebugEnabled()) { Modified: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties 2013-06-14 09:20:52 UTC (rev 670) +++ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties 2013-06-15 14:27:31 UTC (rev 671) @@ -23,6 +23,7 @@ ### model.tagValue.idFactory=true model.tagValue.generatePropertyChangeListeners=true +model.tagValue.generateVisitors=true com.franciaflex.magalie.persistence.entity.Article.attribute.fixedLocations.stereotype=unique Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/AbstractJpaTransformer.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/AbstractJpaTransformer.java 2013-06-14 09:20:52 UTC (rev 670) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/AbstractJpaTransformer.java 2013-06-15 14:27:31 UTC (rev 671) @@ -37,8 +37,6 @@ import org.nuiton.eugene.models.object.ObjectModelOperation; import javax.persistence.EntityManager; -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; import java.io.Serializable; import java.util.Collection; import java.util.Set; Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java 2013-06-14 09:20:52 UTC (rev 670) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java 2013-06-15 14:27:31 UTC (rev 671) @@ -41,6 +41,8 @@ import org.nuiton.jpa.api.JpaEntities; import org.nuiton.jpa.api.JpaEntity; import org.nuiton.jpa.api.JpaEntityIdFactoryResolver; +import org.nuiton.jpa.api.JpaEntityVisitable; +import org.nuiton.jpa.api.JpaEntityVisitor; import javax.persistence.CascadeType; import javax.persistence.Entity; @@ -198,8 +200,31 @@ ); } - boolean usePCS = JpaTemplatesGeneratorUtil.useGeneratePropertyChangeListeners(getModel()); + boolean generateVisitor = + JpaTemplatesGeneratorUtil.useGenerateVisitors(getModel()); + if (generateVisitor) { + addInterface(output, JpaEntityVisitable.class); + + // add accept method + ObjectModelOperation operation = addOperation( + output, + "accept", + void.class, + ObjectModelJavaModifier.PUBLIC); + addAnnotation(output, operation, Override.class); + addParameter(operation, JpaEntityVisitor.class, "visitor"); + setOperationBody(operation, "" + /*{ + visitor.visit(this); + }*/ + ); + } + + boolean usePCS = + JpaTemplatesGeneratorUtil.useGeneratePropertyChangeListeners( + getModel()); + Set<String> constantNames = addConstantsFromDependency(input, output); // Get available properties @@ -424,6 +449,12 @@ if (multiple) { + Class<?> collectionType = + JpaTemplatesGeneratorUtil.getCollectionType(attr); + + Class<?> collectionInstanceType = + JpaTemplatesGeneratorUtil.getCollectionInstanceType(attr); + createGetChildMethod(output, attrName, attrType, @@ -444,12 +475,14 @@ attrName, attrType, constantName, + collectionInstanceType, usePCS); createAddAllChildrenMethod(output, attrName, attrType, constantName, + collectionInstanceType, usePCS); createRemoveChildMethod(output, @@ -483,7 +516,6 @@ usePCS); // Change type for Multiple attribute - Class<?> collectionType = JpaTemplatesGeneratorUtil.getCollectionType(attr); attrType = collectionType.getName() + "<" + attrType + ">"; simpleType = JpaTemplatesGeneratorUtil.getSimpleName(attrType); @@ -635,6 +667,7 @@ String attrName, String attrType, String constantName, + Class<?> collectionInstanceType, boolean usePCS) { ObjectModelOperation operation = addOperation( output, @@ -643,11 +676,16 @@ ObjectModelJavaModifier.PUBLIC ); addParameter(operation, attrType, attrName); - - String methodName = getJavaBeanMethodName("get", attrName); + addImport(output, collectionInstanceType); + String getMethodName = getJavaBeanMethodName("get", attrName); + String setMethodName = getJavaBeanMethodName("set", attrName); + String instanceName = collectionInstanceType.getSimpleName() + "<" + JpaTemplatesGeneratorUtil.getSimpleName(attrType) + ">"; StringBuilder buffer = new StringBuilder("" /*{ - <%=methodName%>().add(<%=attrName%>); + if (<%=getMethodName%>() == null) { + <%=setMethodName%>(new <%=instanceName%>()); + } + <%=getMethodName%>().add(<%=attrName%>); }*/ ); if (usePCS) { @@ -663,6 +701,7 @@ String attrName, String attrType, String constantName, + Class<?> collectionInstanceType, boolean usePCS) { ObjectModelOperation operation = addOperation( output, @@ -672,10 +711,15 @@ ); addParameter(operation, Collection.class.getName() + "<" + attrType + ">", attrName); - String methodName = getJavaBeanMethodName("get", attrName); + String getMethodName = getJavaBeanMethodName("get", attrName); + String setMethodName = getJavaBeanMethodName("set", attrName); + String instanceName = collectionInstanceType.getSimpleName() + "<" + JpaTemplatesGeneratorUtil.getSimpleName(attrType) + ">"; StringBuilder buffer = new StringBuilder("" /*{ - <%=methodName%>().addAll(<%=attrName%>); + if (<%=getMethodName%>() == null) { + <%=setMethodName%>(new <%=instanceName%>()); + } + <%=getMethodName%>().addAll(<%=attrName%>); }*/ ); if (usePCS) { @@ -703,7 +747,7 @@ StringBuilder buffer = new StringBuilder(); buffer.append("" /*{ - boolean removed = <%=methodName%>().remove(<%=attrName%>);}*/ + boolean removed = <%=methodName%>() != null && <%=methodName%>().remove(<%=attrName%>);}*/ ); if (usePCS) { @@ -739,7 +783,7 @@ String methodName = getJavaBeanMethodName("get", attrName); buffer.append("" /*{ - boolean removed = <%=methodName%>().removeAll(<%=attrName%>);}*/ + boolean removed = <%=methodName%>() != null && <%=methodName%>().removeAll(<%=attrName%>);}*/ ); if (usePCS) { @@ -775,7 +819,7 @@ String methodName = getJavaBeanMethodName("get", attrName); buffer.append("" /*{ - boolean contains = <%=methodName%>().contains(<%=attrName%>); + boolean contains = <%=methodName%>() != null && <%=methodName%>().contains(<%=attrName%>); return contains; }*/ ); @@ -796,10 +840,11 @@ ); addParameter(operation, "String", "id"); StringBuilder buffer = new StringBuilder(); + String getMethodName = getJavaBeanMethodName("get", attrName); String methodName = getJavaBeanMethodName("get", attrName + "ById"); buffer.append("" /*{ - boolean contains = <%=methodName%>(id) != null; + boolean contains = <%=getMethodName%>() != null && <%=methodName%>(id) != null; return contains; }*/ ); @@ -820,10 +865,11 @@ ); addParameter(operation, Collection.class.getName() + "<" + attrType + ">", attrName); StringBuilder buffer = new StringBuilder(); + String getMethodName = getJavaBeanMethodName("get", attrName); String methodName = getJavaBeanMethodName("get", attrName); buffer.append("" /*{ - boolean contains = <%=methodName%>().containsAll(<%=attrName%>); + boolean contains = <%=getMethodName%>() != null && <%=methodName%>().containsAll(<%=attrName%>); return contains; }*/ ); Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java 2013-06-14 09:20:52 UTC (rev 670) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java 2013-06-15 14:27:31 UTC (rev 671) @@ -36,6 +36,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -185,6 +186,35 @@ return result; } + public static Class<?> getCollectionInstanceType(ObjectModelAttribute attr) { + + boolean ordered = attr.isOrdered() || + JpaTemplatesGeneratorUtil.hasOrderedStereotype(attr); + boolean unique = JpaTemplatesGeneratorUtil.hasUniqueStereotype(attr); + + // Change type for Multiple attribute + Class<?> result; + + if (ordered) { + + if (unique) { + + result = LinkedHashSet.class; + } else { + + result = ArrayList.class; + } + } else { + + if (unique) { + result = HashSet.class; + } else { + result = ArrayList.class; + } + } + return result; + } + public static boolean containsMutiple(Collection<ObjectModelAttribute> attributes) { boolean result = false; @@ -275,6 +305,26 @@ } /** + * Obtain the value of the {@link JpaTemplatesTagValues#TAG_GENERATE_VISITORS} + * tag value on the given model. + * + * @param model model to seek + * @return the none empty value of the found tag value or {@code null} + * if not found nor empty. + * @see JpaTemplatesTagValues#TAG_GENERATE_VISITORS + * @since 0.1 + */ + public static String getGenerateVisitorsTagValue(ObjectModel model) { + String value = findTagValue(JpaTemplatesTagValues.TAG_GENERATE_VISITORS, null, model); + return value; + } + + public static boolean useGenerateVisitors(ObjectModel model) { + String value = getGenerateVisitorsTagValue(model); + return ObjectUtils.equals("true", value); + } + + /** * Obtain the value of the {@link JpaTemplatesTagValues#TAG_ENTITY_SUPER_CLASS} * tag value on the given model or classifier. * <p/> Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java 2013-06-14 09:20:52 UTC (rev 670) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java 2013-06-15 14:27:31 UTC (rev 671) @@ -110,4 +110,15 @@ documentation = "Tag value to generate property change listeners on entities.") String TAG_GENERATE_PROPERTY_CHANGE_LISTENERS = "generatePropertyChangeListeners"; + /** + * Tag value to generate visitors on entities if setted to {@code true}. + * + * @see JpaTemplatesGeneratorUtil#getGeneratePropertyChangeListenersTagValue(ObjectModel) + * @since 0.1 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class}, + documentation = "Tag value to generate visitors on entities.") + String TAG_GENERATE_VISITORS = "generateVisitors"; + }
participants (1)
-
tchemit@users.nuiton.org