r6 - in trunk: . wikitty-api wikitty-api/src/main/java/org/sharengo/wikitty wikitty-api/src/test/java/org/sharengo/wikitty/test/api wikitty-api/src/test/java/org/sharengo/wikitty/test/conform wikitty-generators wikitty-generators/src wikitty-generators/src/main wikitty-generators/src/main/java wikitty-generators/src/main/java/org wikitty-generators/src/main/java/org/sharengo wikitty-generators/src/main/java/org/sharengo/wikitty wikitty-generators/src/main/java/org/sharengo/wikitty/generato
Author: echatellier Date: 2010-04-20 12:05:45 +0200 (Tue, 20 Apr 2010) New Revision: 6 Log: Add generation as wikitty module (currently based on eugengo 0.6.1 branche) Added: trunk/wikitty-generators/ trunk/wikitty-generators/pom.xml trunk/wikitty-generators/src/ trunk/wikitty-generators/src/main/ trunk/wikitty-generators/src/main/java/ trunk/wikitty-generators/src/main/java/org/ trunk/wikitty-generators/src/main/java/org/sharengo/ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityBeanGenerator.java trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityGenerator.java trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityInterfaceGenerator.java trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EnumGenerator.java trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EugengoConstants.java trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EugengoUtils.java trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/InterfaceGenerator.java trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/WikengoCommonGenerator.java trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/WikittyMetaGenerator.java Modified: trunk/pom.xml trunk/wikitty-api/pom.xml trunk/wikitty-api/src/main/java/org/sharengo/wikitty/AbstractWikittyService.java trunk/wikitty-api/src/main/java/org/sharengo/wikitty/Tree.java trunk/wikitty-api/src/main/java/org/sharengo/wikitty/WikittyProxy.java trunk/wikitty-api/src/test/java/org/sharengo/wikitty/test/api/WikittyUtilTest.java trunk/wikitty-api/src/test/java/org/sharengo/wikitty/test/conform/StorageTest.java trunk/wikitty-solr-impl/src/test/java/org/sharengo/wikitty/solr/test/TreeTest.java Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2010-04-16 09:59:15 UTC (rev 5) +++ trunk/pom.xml 2010-04-20 10:05:45 UTC (rev 6) @@ -12,6 +12,7 @@ <version>1.5-cl-SNAPSHOT</version> <modules> + <module>wikitty-generators</module> <module>wikitty-api</module> <module>wikitty-solr-impl</module> <module>wikitty-hbase-impl</module> Modified: trunk/wikitty-api/pom.xml =================================================================== --- trunk/wikitty-api/pom.xml 2010-04-16 09:59:15 UTC (rev 5) +++ trunk/wikitty-api/pom.xml 2010-04-20 10:05:45 UTC (rev 6) @@ -99,7 +99,6 @@ <properties> <eugene.version>1.0.0</eugene.version> - <eugengo.version>0.7</eugengo.version> <!-- where to generate sources --> <maven.gen.dir>${project.build.directory}/generated-sources</maven.gen.dir> @@ -138,9 +137,9 @@ <version>${eugene.version}</version> <dependencies> <dependency> - <groupId>org.sharengo</groupId> - <artifactId>eugengo</artifactId> - <version>${eugengo.version}</version> + <groupId>org.sharengo.wikengo</groupId> + <artifactId>core-wikitty-generators</artifactId> + <version>${project.version}</version> </dependency> <!-- Meta model uml d'eclipse --> <dependency> @@ -191,7 +190,7 @@ <input>${maven.src.dir}/main/uml/wikitty-api</input> </zargoResources> <includes>**/*.objectmodel</includes> - <templates>org.nuiton.eugengo.generator.WikittyMetaGenerator</templates> + <templates>org.sharengo.wikitty.generator.WikittyMetaGenerator</templates> <defaultPackage>org.sharengo.wikitty</defaultPackage> <extractedPackages>org.sharengo.wikitty</extractedPackages> <generatedPackages>org.sharengo.wikitty</generatedPackages> Modified: trunk/wikitty-api/src/main/java/org/sharengo/wikitty/AbstractWikittyService.java =================================================================== --- trunk/wikitty-api/src/main/java/org/sharengo/wikitty/AbstractWikittyService.java 2010-04-16 09:59:15 UTC (rev 5) +++ trunk/wikitty-api/src/main/java/org/sharengo/wikitty/AbstractWikittyService.java 2010-04-20 10:05:45 UTC (rev 6) @@ -49,8 +49,8 @@ import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.sharengo.internal.LabelImpl; -import org.sharengo.internal.TreeNodeImpl; +import org.sharengo.wikitty.LabelImpl; +import org.sharengo.wikitty.TreeNodeImpl; import org.sharengo.wikitty.search.Search; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; @@ -443,8 +443,8 @@ for (Wikitty wikittyNode : wikittyNodes) { String wikittyNodeId = wikittyNode.getId(); if(!ids.contains(wikittyNodeId)) { - TreeNodeType treeNode = new TreeNodeImpl(wikittyNode); - treeNode.setTreeNodeParent(null); + TreeNode treeNode = new TreeNodeImpl(wikittyNode); + treeNode.setParent(null); storedWikitties.add(wikittyNode); } } @@ -455,8 +455,8 @@ for (Wikitty wikittyNode : wikittyNodes) { String wikittyNodeId = wikittyNode.getId(); if(!ids.contains(wikittyNodeId)) { - TreeNodeType treeNode = new TreeNodeImpl(wikittyNode); - treeNode.removeTreeNodeChildren(id); + TreeNode treeNode = new TreeNodeImpl(wikittyNode); + treeNode.removeChildren(id); storedWikitties.add(wikittyNode); } } @@ -589,7 +589,7 @@ Wikitty w = restore(transaction, wikittyId); w.addExtension(LabelImpl.extensions); LabelImpl l = new LabelImpl(w); - l.addLabelLabels(label); + l.addLabels(label); store(transaction, Arrays.asList(w), false); transaction.commit(); @@ -607,7 +607,7 @@ transaction.begin(); LabelImpl l = new LabelImpl(); - l.addLabelLabels(label); + l.addLabels(label); Criteria criteria = Search.query(l.getWikitty()).criteria() .setFirstIndex(firstIndex).setEndIndex(endIndex); PagedResult<Wikitty> result = findAllByCriteria(transaction, criteria); @@ -628,7 +628,7 @@ transaction.begin(); LabelImpl l = new LabelImpl(); - l.addLabelLabels(label); + l.addLabels(label); Criteria criteria = Search.query(l.getWikitty()).criteria(); Wikitty result = findByCriteria(transaction, criteria); @@ -649,7 +649,7 @@ Wikitty w = restore(transaction, wikittyId); LabelImpl l = new LabelImpl(w); - Set<String> result = l.getLabelLabels(); + Set<String> result = l.getLabels(); transaction.commit(); return result; @@ -672,11 +672,11 @@ wikittyId, TreeNode.EXT_TREENODE )); } Tree tree = new Tree(); - TreeNode node = TreeNodeImpl.toBean(new TreeNodeImpl(w)); + TreeNode node = AbstractWikittyService.toBean(new TreeNodeImpl(w)); tree.setNode(node); TreeNodeImpl exempleNode = new TreeNodeImpl(); - exempleNode.setTreeNodeParent(wikittyId); + exempleNode.setParent(wikittyId); Criteria criteria = Search.query(exempleNode.getWikitty()).criteria() .setFirstIndex(0).setEndIndex(Criteria.ALL_ELEMENTS); @@ -722,7 +722,7 @@ wikittyId, TreeNode.EXT_TREENODE )); } - TreeNode node = TreeNodeImpl.toBean(new TreeNodeImpl(w)); + TreeNode node = AbstractWikittyService.toBean(new TreeNodeImpl(w)); Integer count = getSearchEngin().findNodeCount(transaction, w, filter); HashMap.SimpleEntry<TreeNode, Integer> result = @@ -763,7 +763,7 @@ String id = child.getKey(); Wikitty wikitty = restore(transaction, id); - TreeNode node = TreeNodeImpl.toBean(new TreeNodeImpl(wikitty)); + TreeNode node = AbstractWikittyService.toBean(new TreeNodeImpl(wikitty)); result.put(node, count); } @@ -1177,4 +1177,50 @@ } } + /** + * Method copied from eugengo-0.7 generators + * + * @param bean + * @return + */ + /*public static TreeNodeImpl toImpl(TreeNode bean) { + if (bean == null) return null; + TreeNodeImpl impl = new TreeNodeImpl(bean); + return impl; + }*/ + + /** + * Method copied from eugengo-0.7 generators + * + * @param bean + * @return + */ + protected static void fillBeanAttributes(TreeNode fromBean, TreeNodeBean toBean) { + String beanId = fromBean.getWikittyId(); + toBean.id = beanId; + + String beanVersion = fromBean.getWikittyVersion(); + toBean.setWikittyVersion(beanVersion); + + toBean.setName(fromBean.getName()); + toBean.setParent(fromBean.getParent()); + // WARNING: Copy collection to other collection to not manipulate storage collection directly + Collection<String> treeNodeChildren = fromBean.getChildren(); + if(treeNodeChildren != null) { + toBean.TreeNode$children = new HashSet<String>(treeNodeChildren); + } + } + + /** + * Method copied from eugengo-0.7 generators + * + * @param impl + * @return + */ + public static TreeNode toBean(TreeNodeImpl impl) { + if (impl == null) return null; + TreeNodeBean bean = new TreeNodeBean(); + AbstractWikittyService.fillBeanAttributes(impl, bean); + return bean; + } } Modified: trunk/wikitty-api/src/main/java/org/sharengo/wikitty/Tree.java =================================================================== --- trunk/wikitty-api/src/main/java/org/sharengo/wikitty/Tree.java 2010-04-16 09:59:15 UTC (rev 5) +++ trunk/wikitty-api/src/main/java/org/sharengo/wikitty/Tree.java 2010-04-20 10:05:45 UTC (rev 6) @@ -25,6 +25,6 @@ } public String getName() { - return node.getTreeNodeName(); + return node.getName(); } } Modified: trunk/wikitty-api/src/main/java/org/sharengo/wikitty/WikittyProxy.java =================================================================== --- trunk/wikitty-api/src/main/java/org/sharengo/wikitty/WikittyProxy.java 2010-04-16 09:59:15 UTC (rev 5) +++ trunk/wikitty-api/src/main/java/org/sharengo/wikitty/WikittyProxy.java 2010-04-20 10:05:45 UTC (rev 6) @@ -348,7 +348,9 @@ for (Entry<TreeNode, Integer> entry : entries) { TreeNode node = entry.getKey(); - String nodeId = node.getId(); + + //TODO EC-20100420 was : String nodeId = node.getId(); + String nodeId = node.getWikittyId(); E businessEntity = null; HashSet<String> extNames = null; Modified: trunk/wikitty-api/src/test/java/org/sharengo/wikitty/test/api/WikittyUtilTest.java =================================================================== --- trunk/wikitty-api/src/test/java/org/sharengo/wikitty/test/api/WikittyUtilTest.java 2010-04-16 09:59:15 UTC (rev 5) +++ trunk/wikitty-api/src/test/java/org/sharengo/wikitty/test/api/WikittyUtilTest.java 2010-04-20 10:05:45 UTC (rev 6) @@ -29,7 +29,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Test; -import org.sharengo.internal.TreeNodeImpl; +import org.sharengo.wikitty.AbstractWikittyService; +import org.sharengo.wikitty.LabelBean; +import org.sharengo.wikitty.TreeNodeImpl; import org.sharengo.wikitty.FieldType; import org.sharengo.wikitty.Label; import org.sharengo.wikitty.TreeNode; @@ -402,7 +404,7 @@ } // use in testNewInstance - static class MonLabel extends Label { + static class MonLabel extends LabelBean { } /** @@ -425,11 +427,11 @@ @Test public void testConvert() throws Exception { TreeNodeImpl impl = new TreeNodeImpl(); - impl.setTreeNodeName("toto"); - impl.setTreeNodeParent("idparent"); + impl.setName("toto"); + impl.setParent("idparent"); - TreeNode bean = TreeNodeImpl.toBean(impl); // WikittyUtil.wikittyToBean(TreeNodeBean.class, impl); - assertEquals(impl.getTreeNodeName(), bean.getTreeNodeName()); - assertEquals(impl.getTreeNodeParent(), bean.getTreeNodeParent()); + TreeNode bean = AbstractWikittyService.toBean(impl); // WikittyUtil.wikittyToBean(TreeNodeBean.class, impl); + assertEquals(impl.getName(), bean.getName()); + assertEquals(impl.getParent(), bean.getParent()); } } Modified: trunk/wikitty-api/src/test/java/org/sharengo/wikitty/test/conform/StorageTest.java =================================================================== --- trunk/wikitty-api/src/test/java/org/sharengo/wikitty/test/conform/StorageTest.java 2010-04-16 09:59:15 UTC (rev 5) +++ trunk/wikitty-api/src/test/java/org/sharengo/wikitty/test/conform/StorageTest.java 2010-04-20 10:05:45 UTC (rev 6) @@ -21,7 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Test; -import org.sharengo.internal.TreeNodeImpl; +import org.sharengo.wikitty.TreeNodeImpl; import org.sharengo.wikitty.Criteria; import org.sharengo.wikitty.PagedResult; import org.sharengo.wikitty.Tree; @@ -396,18 +396,18 @@ TreeNodeImpl root = new TreeNodeImpl(); wikittyNodes.add(root.getWikitty()); - root.setTreeNodeName("MyCategoryRoot"); + root.setName("MyCategoryRoot"); for ( int i = 0; i < 3; i++ ) { TreeNodeImpl leaf = new TreeNodeImpl(); wikittyNodes.add( leaf.getWikitty() ); - leaf.setTreeNodeName( "cat-"+i ); - leaf.setTreeNodeParent( root.getWikittyId() ); + leaf.setName( "cat-"+i ); + leaf.setParent( root.getWikittyId() ); // root.addChild( leaf.getWikittyId() ); for ( int j = 0; j < 5; j++ ) { TreeNodeImpl subLeaf = new TreeNodeImpl(); - subLeaf.setTreeNodeName( "subcat-"+i+"-"+j ); - subLeaf.setTreeNodeParent( leaf.getWikittyId() ); + subLeaf.setName( "subcat-"+i+"-"+j ); + subLeaf.setParent( leaf.getWikittyId() ); wikittyNodes.add( subLeaf.getWikitty() ); // leaf.addChild( subLeaf.getWikittyId() ); } @@ -433,7 +433,7 @@ TreeNodeImpl currentNode = root; outerloop : for( String nodeName : nodeNames ) { TreeNodeImpl node = new TreeNodeImpl(); - node.setTreeNodeName(nodeName); + node.setName(nodeName); Criteria criteria = Search.query(node.getWikitty()).criteria() .setFirstIndex(0).setEndIndex(WikittyService.ALL_ELEMENTS); @@ -443,14 +443,14 @@ continue; } node = new TreeNodeImpl(child); - if ( node.getTreeNodeName().equals(nodeName) ) { + if ( node.getName().equals(nodeName) ) { currentNode = node; continue outerloop; } } - fail( "Unable to find node " + nodeName + " inside " + currentNode.getTreeNodeName() ); + fail( "Unable to find node " + nodeName + " inside " + currentNode.getName() ); } - currentNode.addTreeNodeChildren( wikitty.getId() ); + currentNode.addChildren( wikitty.getId() ); ws.store(currentNode.getWikitty()); } } Property changes on: trunk/wikitty-generators ___________________________________________________________________ Added: svn:ignore + bin .project target .settings .classpath Added: trunk/wikitty-generators/pom.xml =================================================================== --- trunk/wikitty-generators/pom.xml (rev 0) +++ trunk/wikitty-generators/pom.xml 2010-04-20 10:05:45 UTC (rev 6) @@ -0,0 +1,75 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <!-- ************************************************************* --> + <!-- *** POM Relationships *************************************** --> + <!-- ************************************************************* --> + + <groupId>org.sharengo.wikengo</groupId> + <artifactId>core-wikitty-generators</artifactId> + + <parent> + <groupId>org.sharengo.wikengo</groupId> + <artifactId>core-wikitty</artifactId> + <version>1.5-cl-SNAPSHOT</version> + </parent> + + <dependencies> + <dependency> + <groupId>org.nuiton.eugene</groupId> + <artifactId>maven-eugene-plugin</artifactId> + <version>${eugene.version}</version> + </dependency> + </dependencies> + + <!-- ************************************************************* --> + <!-- *** Project Information ************************************* --> + <!-- ************************************************************* --> + + <name>org.sharengo.wikengo.core-wikitty-generators</name> + + <description>Wikitty generators</description> + <inceptionYear>2009</inceptionYear> + + <!-- ************************************************************* --> + <!-- *** Build Settings ****************************************** --> + <!-- ************************************************************* --> + + <packaging>jar</packaging> + + <properties> + <eugene.version>1.0.0</eugene.version> + + <!-- where to generate sources --> + <maven.gen.dir>${project.build.directory}/generated-sources</maven.gen.dir> + </properties> + + <build> + + <plugins> + <!-- processsor plugin --> + <plugin> + <groupId>org.nuiton.processor</groupId> + <artifactId>maven-processor-plugin</artifactId> + <version>1.0.3</version> + <executions> + <execution> + <phase>generate-sources</phase> + <goals> + <goal>process</goal> + </goals> + </execution> + </executions> + <configuration> + <includes>**/*.java</includes> + <filters> + org.nuiton.processor.filters.GeneratorTemplatesFilter + </filters> + </configuration> + </plugin> + </plugins> + + </build> + +</project> Added: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityBeanGenerator.java =================================================================== --- trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityBeanGenerator.java (rev 0) +++ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityBeanGenerator.java 2010-04-20 10:05:45 UTC (rev 6) @@ -0,0 +1,307 @@ +package org.sharengo.wikitty.generator; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.io.File; +import java.io.IOException; +import java.io.Serializable; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.GeneratorUtil; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; + +/** + * Generate pure Bean Object (no wikitty object internaly stored) + * + * @author poussin + */ +public class BusinessEntityBeanGenerator extends WikengoCommonGenerator { + + private static final Log log = LogFactory.getLog(BusinessEntityBeanGenerator.class); + + protected String EXT_NAME; + + /** + * A flag to generate on setter a propertyChangeEvent. + */ + protected Boolean generatePropertyChangeListener; + + @Override + public String getFilenameForClass(ObjectModelClass clazz) { + String fqn = clazz.getQualifiedName(); + log.info( "Filename for " + clazz.getName() + " is " + fqn.replace('.', File.separatorChar) + ".java"); + return fqn.replace('.', File.separatorChar) + "Bean.java"; + } + + public void generateFromClass(Writer output, ObjectModelClass clazz) + throws IOException { + if (!EugengoUtils.isBusinessEntity(clazz)) { + log.info( clazz.getName() + " is not a business entity"); + return; + } + + log.info("Generate Bean Business entity " + clazz.getName() + "... "); + String genPCS = EugengoUtils.findTagValue("generatePropertyChangeListener",clazz,model); + generatePropertyChangeListener = genPCS != null && "true".equals(genPCS.trim()); + + generateCopyright(output); + + String packageName = clazz.getPackageName(); + String name = clazz.getName() + "Bean"; +/*{package <%=packageName%>; + +}*/ + ObjectModelClass superClass = findSuperClass(clazz); + + clearImports(); + addImport(clazz); + addImport(superClass); + addImport("org.sharengo.wikitty.BusinessEntityBean"); + String parentBean = null; + for (ObjectModelClass parent : clazz.getSuperclasses()) { + if (EugengoUtils.isBusinessEntity(parent)) { + addImport(parent); + parentBean = parent.getQualifiedName() + "Bean"; + addImport( parentBean ); + } + } + if (generatePropertyChangeListener) { + addImport(PropertyChangeSupport.class.getName()); + addImport(PropertyChangeListener.class.getName()); + } + lookForAttributeImports(clazz); + generateImports(output, packageName); + + generateClazzDocumentation(output, clazz); + String extendsString = " extends " + ( parentBean != null ? parentBean : getType("org.sharengo.wikitty.BusinessEntityBean") ); + String abstractString = ""; + if (clazz.isAbstract()) { + abstractString += "abstract "; + } + + String implementsString = "implements " + getType(clazz.getQualifiedName()); + for (ObjectModelClass parent : clazz.getSuperclasses()) { + if (EugengoUtils.isBusinessEntity(parent)) { + implementsString += ", " + getType(parent.getQualifiedName()); + } + } + +/*{public <%=abstractString%>class <%=name%><%=extendsString%> <%=implementsString%> { + +}*/ + + String svUID = GeneratorUtil.computeSerialVersionUID(clazz); +/*{ private static final long serialVersionUID = <%=svUID%>; + + public <%=name%>() { + super(); + } + + }*/ + if (generatePropertyChangeListener) { + +/*{ protected transient PropertyChangeSupport propertyChange = new PropertyChangeSupport(this); + + public void addPropertyChangeListener(PropertyChangeListener listener) { + propertyChange.addPropertyChangeListener(listener); + } + + public void removePropertyChangeListener(PropertyChangeListener listener) { + propertyChange.removePropertyChangeListener(listener); + } + + public void addPropertyChangeListener(String propertyName,PropertyChangeListener listener) { + propertyChange.addPropertyChangeListener(propertyName, listener); + } + + public void removePropertyChangeListener(String propertyName,PropertyChangeListener listener) { + propertyChange.removePropertyChangeListener(propertyName, listener); + } + +}*/ + } + generateFieldAndMethod(output, clazz); + +/*{ +} //<%=name%> +}*/ + + } + + + + // Utilitarian methods + + private void generateFieldAndMethod(Writer output, + ObjectModelClass clazz) throws IOException { + generateWikittyField(output, clazz); + generateAttributeAccessMethod(output, clazz); + + // we must generate method for parent of parent + for (ObjectModelClass parent : clazz.getSuperclasses()) { + if (EugengoUtils.isBusinessEntity(parent)) { + generateFieldAndMethod(output, parent); + } + } + + } + + + private void generateWikittyField(Writer output, + ObjectModelClass clazz) throws IOException { + + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && !attr.isStatic() && + (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + generateAttribute(output, attr); + } + } + } + + private void generateAttributeAccessMethod(Writer output, ObjectModelClass clazz) throws IOException { + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && !attr.isStatic() + && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + if ((attr.getMaxMultiplicity() != 0 && attr.getMaxMultiplicity() != 1)) { + //TODO ymartel 20090812: when dataType "List", "Set" or "Collection" in model, must be here! + generateCollectionAttributeAccessors(output, attr); + } else { + generateWikittyAttributeAccessors(output, attr); + } + } + } + } + + private void generateAttribute(Writer output, ObjectModelAttribute attr) + throws IOException { + EXT_NAME = attr.getDeclaringElement().getName(); + + String attrType = computeType(attr); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType, true); + } else { + return; + } + + String attrName = EXT_NAME + "$" + attr.getName(); +/*{ public <%=attrType%> <%=attrName%>; + + }*/ + } + + private void generateWikittyAttributeAccessors(Writer output, + ObjectModelAttribute attr) throws IOException { + + EXT_NAME = attr.getDeclaringElement().getName(); + + String attrType = computeType(attr); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType, true); + } else { + return; + } + + String simpleAttrName = attr.getName(); + String attrNameCapitalized = EugengoUtils.toUpperCaseFirstLetter(simpleAttrName); + String attrName = EXT_NAME + "$" + simpleAttrName; + String genPCS = ""; + if(!generatePropertyChangeListener) { + genPCS = "//"; + } + +/*{ + public void set<%=attrNameCapitalized%>(<%=attrType%> value) { + <%=genPCS%>Object oldValue = <%=attrName%>; + this.<%=attrName%> = value; + <%=genPCS%>propertyChange.firePropertyChange("<%=simpleAttrName%>", oldValue, value); + } + + public <%=attrType%> get<%=attrNameCapitalized%>() { + return <%=attrName%>; + } + +}*/ + } + + private void generateCollectionAttributeAccessors(Writer output, + ObjectModelAttribute attr) throws IOException { + + EXT_NAME = attr.getDeclaringElement().getName(); + + String attrType = computeType(attr); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType, true); + } else { + return; + } + + // get collection element type for add and remove method arguement type + String elementType = getType(attr.getType(), true); + + String simpleAttrName = attr.getName(); + String attrNameCapitalized = EugengoUtils.toUpperCaseFirstLetter(simpleAttrName ); + String attrName = EXT_NAME + "$" + simpleAttrName; + String genPCS = ""; + if(!generatePropertyChangeListener) { + genPCS = "//"; + } + +/*{ public <%=attrType%> get<%=attrNameCapitalized%>() { + return <%=attrName%>; + } + + public void add<%=attrNameCapitalized%>(<%=elementType%> element) { + <%=attrName%>.add(element); + <%=genPCS%>propertyChange.firePropertyChange("<%=simpleAttrName%>", null, <%=attrName%>); + } + + public void remove<%=attrNameCapitalized%>(<%=elementType%> element) { + <%=attrName%>.remove(element); + <%=genPCS%>propertyChange.firePropertyChange("<%=simpleAttrName%>", null, <%=attrName%>); + } + + public void clear<%=attrNameCapitalized%>() { + <%=attrName%>.clear(); + <%=genPCS%>propertyChange.firePropertyChange("<%=simpleAttrName%>", null, <%=attrName%>); + } + +}*/ + } + + private static Set<String> commonTypes; + static { + commonTypes = new HashSet<String>(); + commonTypes.add("byte"); + commonTypes.add("Byte"); + commonTypes.add("short"); + commonTypes.add("Short"); + commonTypes.add("int"); + commonTypes.add("Integer"); + commonTypes.add("long"); + commonTypes.add("Long"); + commonTypes.add("float"); + commonTypes.add("Float"); + commonTypes.add("double"); + commonTypes.add("Double"); + + commonTypes.add("char"); + commonTypes.add("Char"); + + commonTypes.add("boolean"); + commonTypes.add("Boolean"); + + commonTypes.add("Date"); + commonTypes.add("String"); + } + +} Property changes on: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityBeanGenerator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityGenerator.java =================================================================== --- trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityGenerator.java (rev 0) +++ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityGenerator.java 2010-04-20 10:05:45 UTC (rev 6) @@ -0,0 +1,414 @@ +package org.sharengo.wikitty.generator; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.GeneratorUtil; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; + +/** + * Possible enhancement: + * - generateParentMethod can generate attribut method access that call + * the same method on parent instance class. For that we must have one attribut + * instance by parent. This attribut we must be created in setWikitty method + * and used same wikitty object. + * + * @author poussin + */ +public class BusinessEntityGenerator extends WikengoCommonGenerator { + + private static final Log log = LogFactory.getLog(BusinessEntityGenerator.class); + + static protected Pattern extractTypeOnCollection = Pattern.compile("\\w*<(\\w+)>"); + + protected String EXT_NAME; + + @Override + public String getFilenameForClass(ObjectModelClass clazz) { + String fqn = clazz.getQualifiedName(); + log.info( "Filename for " + clazz.getName() + " is " + fqn.replace('.', File.separatorChar) + ".java"); + return fqn.replace('.', File.separatorChar) + "Impl.java"; + } + + public void generateFromClass(Writer output, ObjectModelClass clazz) + throws IOException { + if (!EugengoUtils.isBusinessEntity(clazz)) { + log.info( clazz.getName() + " is not a business entity"); + return; + } + + log.info("Generate Business entity " + clazz.getName() + "... "); + generateCopyright(output); + + EXT_NAME = "EXT_" + clazz.getName().toUpperCase(); + + String packageName = clazz.getPackageName(); + String name = clazz.getName() + "Impl"; +/*{package <%=packageName%>; + +}*/ + ObjectModelClass superClass = findSuperClass(clazz); + + clearImports(); + addImport(clazz); + addImport(superClass); + addImport("org.sharengo.wikitty.WikittyUtil"); + addImport("org.sharengo.wikitty.Wikitty"); + addImport("org.sharengo.wikitty.BusinessEntityWikitty"); + addImport("org.sharengo.wikitty.WikittyExtension"); + addImport(Collection.class); + addImport(Collections.class); + addImport(List.class); + addImport(ArrayList.class); + String parentImpl = null; + for (ObjectModelClass parent : clazz.getSuperclasses()) { + if (EugengoUtils.isBusinessEntity(parent)) { + addImport(parent); + parentImpl = parent.getQualifiedName() + "Impl"; + addImport( parentImpl ); + } + } + lookForAttributeImports(clazz); + generateImports(output, packageName); + + generateClazzDocumentation(output, clazz); + String extendsString = " extends " + ( parentImpl != null ? parentImpl : getType("org.sharengo.wikitty.BusinessEntityWikitty") ); + String abstractString = ""; + if (clazz.isAbstract()) { + abstractString += "abstract "; + } + + String implementsString = "implements " + getType(clazz.getQualifiedName()); + for (ObjectModelClass parent : clazz.getSuperclasses()) { + if (EugengoUtils.isBusinessEntity(parent)) { + implementsString += ", " + getType(parent.getQualifiedName()); + } + } + +/*{public <%=abstractString%>class <%=name%><%=extendsString%> <%=implementsString%> { + +}*/ + + String svUID = GeneratorUtil.computeSerialVersionUID(clazz); +/*{ private static final long serialVersionUID = <%=svUID%>; + +}*/ + + generateWikittyExtension(output, clazz); + + generateStaticAttributes(output, clazz); + +/*{ + public <%=name%>() { + super(); + } + + public <%=name%>(BusinessEntityWikitty wi) { + super(wi.getWikitty()); + } + + public <%=name%>(Wikitty wi) { + super(wi); + } + +}*/ + + generateAttributeAccessMethod(output, clazz); + + for (ObjectModelClass parent : clazz.getSuperclasses()) { + if (EugengoUtils.isBusinessEntity(parent)) { + generateParentMethod(output, parent); + } + } + +/*{ @Override + public Collection<WikittyExtension> getStaticExtensions() { + return extensions; + } + + +} //<%=name%> +}*/ + + } + + + + // Utilitarian methods + + public void generateAttributeAccessMethod(Writer output, ObjectModelClass clazz) throws IOException { + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && !attr.isStatic() + && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + if ((attr.getMaxMultiplicity() != 0 && attr.getMaxMultiplicity() != 1)) { + //TODO ymartel 20090812: when dataType "List", "Set" or "Collection" in model, must be here! + generateCollectionAttributeAccessors(output, attr); + } else { + generateWikittyAttributeAccessors(output, attr); + } + } + } + } + + private void generateWikittyExtension(Writer output, + ObjectModelClass clazz) throws IOException { + String version = clazz.getTagValue("version"); + if ( version == null ) { + version = "1"; + } +/*{ + static final public List<WikittyExtension> extensions; + static final public WikittyExtension extension<%=clazz.getName()%> = + new WikittyExtension(<%=EXT_NAME%>, "<%=version%>", null, + WikittyUtil.buildFieldMapExtension(}*/ + + String separator = ""; + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && !attr.isStatic() && + (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { +/*{<%=separator%> + }*/ + generateAttribute(output, attr); + separator = ","; + } + } +/*{)); + static { + ArrayList<WikittyExtension> exts = new ArrayList<WikittyExtension>(); + exts.add(extension<%=clazz.getName()%>); +}*/ + for (ObjectModelClass parent : clazz.getSuperclasses()) { + if (EugengoUtils.isBusinessEntity(parent)) { +/*{ + exts.addAll(<%=parent.getName()%>Impl.extensions);}*/ + } + } +/*{ + extensions = Collections.unmodifiableList(exts); + } + }*/ + } + + private void generateAttribute(Writer output, ObjectModelAttribute attr) + throws IOException { + String attrType = computeType(attr); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType); + } else { + return; + } + + String attrName = attr.getName(); + String card = ""; + + //TODO ymartel 20090812: a better way to manage those DataTypes in the model? + if (attrType.contains("Collection") || attrType.contains("List") || attrType.contains("Set")) { + card = "[0-*]"; + // List<String> + Matcher match = extractTypeOnCollection.matcher(attrType); + if (match.matches()) { + attrType = match.group(1); + } + } + + if (!commonTypes.contains(attrType)) { + attrType = "Wikitty"; + } else if(commonNumerics.contains(attrType)) { + attrType = "Numeric"; + } else if(commonStrings.contains(attrType)) { + attrType = "String"; + } + + + int maxMultiplicity = attr.getMaxMultiplicity(); + if ((maxMultiplicity != 0 && maxMultiplicity != 1)){ + card = "[" + attr.getMinMultiplicity() + "-"; + if (maxMultiplicity == -1) { + card += "*]"; + } else { + card += maxMultiplicity + "]"; + } + } + + String unique = attr.isUnique() ? " unique" : ""; + + String tagValues = ""; + if ("designation".equals(attr.getName())) { + System.out.println("+++ DEBUG +++ " + attr.getName() + " " + attr.getTagValues()); + } + for (String tag : attr.getTagValues().keySet()) { + String value = attr.getTagValue(tag); + tagValues += " " + tag + "=" + value; + } + +/*{"<%=attrType%> <%=attrName%><%=card%><%=unique%><%=tagValues%>"}*/ + } + + private void generateParentMethod(Writer output, + ObjectModelClass clazz) throws IOException { + + // we must generate method for parent of parent + for (ObjectModelClass parent : clazz.getSuperclasses()) { + if (EugengoUtils.isBusinessEntity(parent)) { + generateParentMethod(output, parent); + } + } + + // generate method acces for parent attribut + generateAttributeAccessMethod(output, clazz); + } + + protected void generateWikittyAttributeAccessors(Writer output, + ObjectModelAttribute attr) throws IOException { + + EXT_NAME = "EXT_" + attr.getDeclaringElement().getName().toUpperCase(); + + String attrType = computeType(attr); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType, true); + } else { + return; + } + + String methodAccessName = getFieldAccessMethodName(attr); + + String attrName = attr.getName(); + String attrNameCapitalized = EugengoUtils.toUpperCaseFirstLetter(attrName); + +/*{ + public void set<%=attrNameCapitalized%>(<%=attrType%> <%=attrName%>) { + getWikitty().setField(<%=EXT_NAME%>, "<%=attrName%>", <%=attrName%>); + } + + public <%=attrType%> get<%=attrNameCapitalized%>() { + <%=attrType%> result = getWikitty().getFieldAs<%=methodAccessName%>(<%=EXT_NAME%>, "<%=attrName%>"); + return result; + } + +}*/ + } + + /** + * Give the string to put after getFieldAs???, only some type is accepted + * and we must convert BusinessEntity to Wikitty string + * @param type + * @return + */ + protected String getFieldAccessMethodName(ObjectModelAttribute attr) { + String result = computeType(attr); + result = getType(result, true); + + boolean isCollection = (attr.getMaxMultiplicity() != 0 + && attr.getMaxMultiplicity() != 1); + if (isCollection) { + if (attr.isUnique()) { + result = "Set"; + } else { + result = "List"; + } + } else { + // test for Date + if ("java.util.Date".equals(result) || "Date".equals(result)) { + result = "Date"; + } else if (getModel().hasClass(result)) { // test for Wikitty object + ObjectModelClass fieldClass = getModel().getClass(result); + if (EugengoUtils.isBusinessEntity(fieldClass)) { + // for wikittyDto we use String for Id + result = "Wikitty"; + } + } else if (null != getModel().getEnumeration(result)) { + result = "String"; + } + } + result = EugengoUtils.toUpperCaseFirstLetter(result); + + return result; + } + + protected void generateCollectionAttributeAccessors(Writer output, + ObjectModelAttribute attr) throws IOException { + + EXT_NAME = "EXT_" + attr.getDeclaringElement().getName().toUpperCase(); + + String attrType = computeType(attr); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType, true); + } else { + return; + } + + // get collection element type for add and remove method arguement type + String elementType = getType(attr.getType(), true); + + String methodAccessName = getFieldAccessMethodName(attr); + + String attrName = attr.getName(); + String attrNameCapitalized = EugengoUtils.toUpperCaseFirstLetter(attrName); +/*{ public <%=attrType%> get<%=attrNameCapitalized%>() { + <%=attrType%> result = getWikitty().getFieldAs<%=methodAccessName%>(<%=EXT_NAME%>, "<%=attrName%>", <%=getClassAndGeneric(attrType)[1]%>.class); + return result; + } + + public void add<%=attrNameCapitalized%>(<%=elementType%> element) { + getWikitty().addToField(<%=EXT_NAME%>, "<%=attrName%>", element); + } + + public void remove<%=attrNameCapitalized%>(<%=elementType%> element) { + getWikitty().removeFromField(<%=EXT_NAME%>, "<%=attrName%>", element); + } + + public void clear<%=attrNameCapitalized%>() { + getWikitty().clearField(<%=EXT_NAME%>, "<%=attrName%>"); + } + +}*/ + } + + private static Set<String> commonNumerics; + static { + commonNumerics = new HashSet<String>(); + commonNumerics.add("byte"); + commonNumerics.add("Byte"); + commonNumerics.add("short"); + commonNumerics.add("Short"); + commonNumerics.add("int"); + commonNumerics.add("Integer"); + commonNumerics.add("long"); + commonNumerics.add("Long"); + commonNumerics.add("float"); + commonNumerics.add("Float"); + commonNumerics.add("double"); + commonNumerics.add("Double"); + } + + private static Set<String> commonStrings; + static { + commonStrings = new HashSet<String>(); + commonStrings.add("char"); + commonStrings.add("Char"); + commonStrings.add("String"); + } + + private static Set<String> commonTypes; + static { + commonTypes = new HashSet<String>(); + commonTypes.addAll(commonNumerics); + commonTypes.addAll(commonStrings); + commonTypes.add("boolean"); + commonTypes.add("Boolean"); + commonTypes.add("Date"); + } + +} Property changes on: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityGenerator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityInterfaceGenerator.java =================================================================== --- trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityInterfaceGenerator.java (rev 0) +++ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityInterfaceGenerator.java 2010-04-20 10:05:45 UTC (rev 6) @@ -0,0 +1,152 @@ +package org.sharengo.wikitty.generator; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; + +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; + +/** + * Interface for BusinessEntity, interfaces are needed for multiple inheritance + * + * @author poussin + */ +public class BusinessEntityInterfaceGenerator extends WikengoCommonGenerator { + + private static final Log log = LogFactory.getLog(BusinessEntityInterfaceGenerator.class); + + protected String EXT_NAME; + + @Override + public String getFilenameForClass(ObjectModelClass clazz) { + String fqn = clazz.getQualifiedName(); + log.info( "Filename for " + clazz.getName() + " is " + fqn.replace('.', File.separatorChar) + ".java"); + return fqn.replace('.', File.separatorChar) + ".java"; + } + + public void generateFromClass(Writer output, ObjectModelClass clazz) + throws IOException { + if (!EugengoUtils.isBusinessEntity(clazz)) { + log.info( clazz.getName() + " is not a business entity"); + return; + } + + log.info("Generate Business entity " + clazz.getName() + "... "); + generateCopyright(output); + + EXT_NAME = "EXT_" + clazz.getName().toUpperCase(); + String packageName = clazz.getPackageName(); + String name = clazz.getName(); +/*{package <%=packageName%>; + +}*/ + ObjectModelClass superClass = findSuperClass(clazz); + + clearImports(); + addImport(superClass); + addImport("org.sharengo.wikitty.BusinessEntity"); + + for (ObjectModelClass parent : clazz.getSuperclasses()) { + if (EugengoUtils.isBusinessEntity(parent)) { + addImport(parent); + } + } + lookForAttributeImports(clazz); + generateImports(output, packageName); + + String extendsString = "extends " + getType("org.sharengo.wikitty.BusinessEntity"); + for (ObjectModelClass parent : clazz.getSuperclasses()) { + if (EugengoUtils.isBusinessEntity(parent)) { + extendsString += ", " + getType(parent.getName()); + } + } + +/*{public interface <%=name%> <%=extendsString%> { + + static final public String <%=EXT_NAME%> = "<%=clazz.getName()%>"; + +}*/ + generateFieldNameConstant(output, clazz); + + generateStaticAttributes(output, clazz); + + generateAttributeAccessMethod(output, clazz); + +/*{ +} //<%=name%> +}*/ + + } + + // Utilitarian methods + private void generateFieldNameConstant(Writer output, + ObjectModelClass clazz) throws IOException { + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && !attr.isStatic() && + (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { +/*{ static final public String FIELD_<%=attr.getName().toUpperCase()%> = "<%=attr.getName()%>"; + static final public String FQ_FIELD_<%=attr.getName().toUpperCase()%> = <%=EXT_NAME%> + ".<%=attr.getName()%>"; +}*/ + } + } + } + + public void generateAttributeAccessMethod(Writer output, ObjectModelClass clazz) throws IOException { + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && !attr.isStatic() + && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + if ((attr.getMaxMultiplicity() != 0 && attr.getMaxMultiplicity() != 1)) { + //TODO ymartel 20090812: when dataType "List", "Set" or "Collection" in model, must be here! + generateCollectionAttributeAccessors(output, attr); + } else { + generateWikittyAttributeAccessors(output, attr); + } + } + } + } + + protected void generateWikittyAttributeAccessors(Writer output, + ObjectModelAttribute attr) throws IOException { + String attrType = computeType(attr); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType, true); + } else { + return; + } + + String attrName = attr.getName(); + String attrNameCapitalized = EugengoUtils.toUpperCaseFirstLetter(attrName); + +/*{ + public void set<%=attrNameCapitalized%>(<%=attrType%> <%=attrName%>); + public <%=attrType%> get<%=attrNameCapitalized%>(); + +}*/ + } + + protected void generateCollectionAttributeAccessors(Writer output, + ObjectModelAttribute attr) throws IOException { + String attrType = computeType(attr); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType, true); + } else { + return; + } + + // get collection element type for add and remove method arguement type + String elementType = getType(attr.getType(), true); + + String attrName = attr.getName(); + String attrNameCapitalized = EugengoUtils.toUpperCaseFirstLetter(attrName); +/*{ public <%=attrType%> get<%=attrNameCapitalized%>(); + public void add<%=attrNameCapitalized%>(<%=elementType%> element); + public void remove<%=attrNameCapitalized%>(<%=elementType%> element); + public void clear<%=attrNameCapitalized%>(); + +}*/ + } + +} Property changes on: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/BusinessEntityInterfaceGenerator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EnumGenerator.java =================================================================== --- trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EnumGenerator.java (rev 0) +++ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EnumGenerator.java 2010-04-20 10:05:45 UTC (rev 6) @@ -0,0 +1,53 @@ +package org.sharengo.wikitty.generator; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; + +import org.nuiton.eugene.models.object.ObjectModelEnumeration; + +public class EnumGenerator extends WikengoCommonGenerator { + + @Override + public String getFilenameForEnumeration(ObjectModelEnumeration enumeration) { + String fqn = enumeration.getQualifiedName(); + return fqn.replace('.', File.separatorChar) + ".java"; + } + + @Override + public void generateFromEnumeration(Writer output, + ObjectModelEnumeration enumeration) throws IOException { + + generateCopyright(output); + + String packageName = enumeration.getPackageName(); + String enumName = enumeration.getName(); + +/*{package <%=packageName%>; + +}*/ + generateDocumentation(output, enumeration, ""); +/*{public enum <%=enumName%> { + +}*/ + + boolean isFirst = true; + for (String literal: enumeration.getLiterals()) { + if (isFirst) { +/*{ }*/ + } else { +/*{, }*/ + } + isFirst = false; +/*{<%=literal%>}*/ + } + +/*{ + +}*/ + +/*{} //<%=enumName%> +}*/ + } + +} Property changes on: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EnumGenerator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EugengoConstants.java =================================================================== --- trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EugengoConstants.java (rev 0) +++ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EugengoConstants.java 2010-04-20 10:05:45 UTC (rev 6) @@ -0,0 +1,21 @@ +package org.sharengo.wikitty.generator; + +public class EugengoConstants { + + public final static String STEREOTYPE_SERVICE = "Service"; + public final static String STEREOTYPE_CRUD_SERVICE = "CrudService"; + public final static String STEREOTYPE_DTO = "Dto"; + public final static String STEREOTYPE_BUSINESS_ENTITY = "BusinessEntity"; + public final static String STEREOTYPE_DAO = "Dao"; + public final static String STEREOTYPE_ENTITY = "Entity"; + public final static String STEREOTYPE_EXCEPTION = "Exception"; + public final static String STEREOTYPE_BUSINESS_EXCEPTION = "BusinessException"; + public final static String STEREOTYPE_REMOTE = "Remote"; + + public final static String TAG_COPYRIGHT = "copyright"; + public final static String TAG_LENGTH = "length"; + public static final String TAG_LAZY = "lazy"; + + public final static String PREFIX_DATATYPE = "dataType-"; + +} // EugengoConstants Property changes on: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EugengoConstants.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EugengoUtils.java =================================================================== --- trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EugengoUtils.java (rev 0) +++ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EugengoUtils.java 2010-04-20 10:05:45 UTC (rev 6) @@ -0,0 +1,157 @@ +package org.sharengo.wikitty.generator; + +import java.util.HashSet; +import java.util.Set; + +import org.nuiton.eugene.GeneratorUtil; +import org.nuiton.eugene.models.Model; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClassifier; +import org.nuiton.eugene.models.object.ObjectModelOperation; + +public class EugengoUtils extends GeneratorUtil { + + /** + * Cherches et renvoie le copyright a utiliser sur le model. + * + * @param model le modele utilisé + * @return le texte du copyright ou null + */ + public static String getCopyright(Model model) { + return GeneratorUtil.findTagValue(EugengoConstants.TAG_COPYRIGHT, null, model); + } + + public static boolean isService(ObjectModelClassifier classifier) { + return hasStereotype(classifier, EugengoConstants.STEREOTYPE_SERVICE) || isCrudService(classifier); + } + + public static boolean isCrudService(ObjectModelClassifier classifier) { + return hasStereotype(classifier, EugengoConstants.STEREOTYPE_CRUD_SERVICE); + } + + public static boolean isWebService(ObjectModelClassifier classifier) { + // any crud service is remote + if (isCrudService(classifier)) { + return true; + } + // cannot be a webservice if class is not a service + if (!isService(classifier)) { + return false; + } + // Look for an operation that has the stereotype remote + for (ObjectModelOperation op : classifier.getOperations()) { + if (isRemote(op)) { + return true; + } + } + return false; + } + + public static boolean isDto(ObjectModelClassifier classifier) { + return hasStereotype(classifier, EugengoConstants.STEREOTYPE_DTO); + } + + public static boolean isBusinessEntity(ObjectModelClassifier classifier) { + return hasStereotype(classifier, EugengoConstants.STEREOTYPE_BUSINESS_ENTITY); + } + + public static boolean isDao(ObjectModelClassifier classifier) { + return hasStereotype(classifier, EugengoConstants.STEREOTYPE_DAO); + } + + public static boolean isEntity(ObjectModelClassifier classifier) { + return hasStereotype(classifier, EugengoConstants.STEREOTYPE_ENTITY); + } + + public static boolean isException(ObjectModelClassifier classifier) { + return hasStereotype(classifier, EugengoConstants.STEREOTYPE_EXCEPTION) || isBusinessException(classifier); + } + + public static boolean isBusinessException(ObjectModelClassifier classifier) { + return hasStereotype(classifier, EugengoConstants.STEREOTYPE_BUSINESS_EXCEPTION); + } + + public static boolean isRemote(ObjectModelOperation op) { + return hasStereotype(op, EugengoConstants.STEREOTYPE_REMOTE); + } + + private static Set<String> simpleTypes; + static { + simpleTypes = new HashSet<String>(); + simpleTypes.add("void"); + simpleTypes.add("boolean"); + simpleTypes.add("byte"); + simpleTypes.add("short"); + simpleTypes.add("int"); + simpleTypes.add("long"); + simpleTypes.add("float"); + simpleTypes.add("double"); + } + + + private static Set<String> primitiveTypes; + static { + primitiveTypes = new HashSet<String>(); + primitiveTypes.add("byte"); + primitiveTypes.add("Byte"); + primitiveTypes.add("short"); + primitiveTypes.add("Short"); + primitiveTypes.add("int"); + primitiveTypes.add("Integer"); + primitiveTypes.add("long"); + primitiveTypes.add("Long"); + primitiveTypes.add("float"); + primitiveTypes.add("Float"); + primitiveTypes.add("double"); + primitiveTypes.add("Double"); + + primitiveTypes.add("char"); + primitiveTypes.add("Char"); + + primitiveTypes.add("boolean"); + primitiveTypes.add("Boolean"); + } + + public static String extractModelName(Model model) { + String result = model.getName(); + if (result.contains("::")) { + result = result.replaceAll("::", "."); + } + if (result.endsWith(".")) { + result = result.substring(0, result.length() - 1); + } + return result; + } + + public static String normalizeCapitalName(String name) { + String result = ""; + for (int idx = 0; idx < name.length(); idx++) { + char c = name.charAt(idx); + if (Character.isUpperCase(c)) { + result += "_"; + } + result += Character.toUpperCase(c); + } + if (result.startsWith("_")) { + result = result.substring(1); + } + return result; + } + + public static String getTagValue(ObjectModelAttribute attr, String tagName, String defaultValue) { + String value = attr.getTagValue(tagName); + if (value == null) { + value = defaultValue; + } + return value; + } + + public static boolean isPrimitiveType(ObjectModelAttribute attr) { + String type = attr.getType(); + if (type.startsWith("java.lang.")) { + type = type.substring(11); + } + return primitiveTypes.contains(type); + } + +} //EugengoUtils Property changes on: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/EugengoUtils.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/InterfaceGenerator.java =================================================================== --- trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/InterfaceGenerator.java (rev 0) +++ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/InterfaceGenerator.java 2010-04-20 10:05:45 UTC (rev 6) @@ -0,0 +1,52 @@ +package org.sharengo.wikitty.generator; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; + +import org.nuiton.eugene.models.object.ObjectModelInterface; +import org.nuiton.eugene.models.object.ObjectModelOperation; + +public class InterfaceGenerator extends WikengoCommonGenerator { + + @Override + public String getFilenameForInterface(ObjectModelInterface interfacez) { + String fqn = interfacez.getQualifiedName(); + return fqn.replace('.', File.separatorChar) + ".java"; + } + + @Override + public void generateFromInterface(Writer output, + ObjectModelInterface interfacez) throws IOException { + // Generate only is not stereotype is specified + if (!interfacez.getStereotypes().isEmpty()) { + return; + } + + generateCopyright(output); + + String packageName = interfacez.getPackageName(); + String name = interfacez.getName(); +/*{package <%=packageName%>; + +}*/ + + clearImports(); + lookForOperationImports(interfacez); + generateImports(output, packageName); + + generateClazzDocumentation(output, interfacez); +/*{public interface <%=name%> { + +}*/ + + for (ObjectModelOperation op : interfacez.getOperations()) { + generateOperationHeader(output, op, true); + } + +/*{} //<%=name%> +}*/ + + } + +} Property changes on: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/InterfaceGenerator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/WikengoCommonGenerator.java =================================================================== --- trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/WikengoCommonGenerator.java (rev 0) +++ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/WikengoCommonGenerator.java 2010-04-20 10:05:45 UTC (rev 6) @@ -0,0 +1,711 @@ +package org.sharengo.wikitty.generator; + +import java.io.IOException; +import java.io.Writer; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.GeneratorUtil; +import org.nuiton.eugene.ImportsManager; +import org.nuiton.eugene.ObjectModelGenerator; +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.ObjectModelDependency; +import org.nuiton.eugene.models.object.ObjectModelElement; +import org.nuiton.eugene.models.object.ObjectModelOperation; +import org.nuiton.eugene.models.object.ObjectModelParameter; + +public class WikengoCommonGenerator extends ObjectModelGenerator { + + private static final Log log = LogFactory.getLog(WikengoCommonGenerator.class); + + protected ImportsManager imports; + + protected void clearImports() { + if (imports == null) { + imports = new ImportsManager(); + } else { + imports.clearImports(); + } + } + + protected void addImport(String fqn) { + if (containsClassAndGeneric(fqn)) { + String[] type = getClassAndGeneric(fqn); + imports.addImport(checkForDatatype(type[0])); + addImport(checkForDatatype(type[1])); + } else if (isArray(fqn)) { + imports.addImport(checkForDatatype(fqn.substring(0, fqn.length() - 2))); + } else if (containsComma(fqn)) { + String[] fqnCommaSeparated = splitByComma(fqn); + for (String str : fqnCommaSeparated) { + if (str != null && !str.isEmpty()) { + addImport(str.trim()); + } + } + } else { + fqn = checkForDatatype(fqn); + imports.addImport(fqn); + } + } + + private String[] splitByComma(String fqn) { + return fqn.split(","); + } + + private boolean containsComma(String fqn) { + return fqn != null && fqn.indexOf(",") != -1; + } + + protected void addImport(Class<?> clazz) { + imports.addImport(clazz); + } + + protected void addImport(ObjectModelClass clazz) { + if (clazz != null) { + addImport(clazz.getQualifiedName()); + } + } + + /** + * Return the minimum syntax for the type. The result depend of import added + * by addImport. + * + * @param fqn the fully qualified name of type + * @return minimum needed type + */ + protected String getType(String fqn) { + String result = getType(fqn, false); + return result; + } + + /** + * Return the minimum syntax for the type. The result depend of import added + * by addImport. + * + * @param fqn the fully qualified name of type + * @param convert if true try to convert some type to other + * (ex: enum to string, dto to string) + * @return minimum needed type + */ + protected String getType(String fqn, boolean convert) { + // if type is Wikitty then we used String + if ("org.sharengo.wikitty.Wikitty".equals(fqn) + || "Wikitty".equals(fqn)) { + fqn = "String"; + } else if (convert && null != getModel().getEnumeration(fqn)) { + // if type is Wikitty then we used String + fqn = "String"; + } else if (convert && getModel().hasClass(fqn) + && EugengoUtils.isBusinessEntity(getModel().getClass(fqn))) { + // for wikittyDto we use String for Id + fqn = "String"; + } + + if (containsClassAndGeneric(fqn)) { + String[] type = getClassAndGeneric(fqn); + return imports.getType(checkForDatatype(type[0])) + "<" + getType(type[1], convert) + ">"; + } else if (containsComma(fqn)) { + String result = ""; + for (String str : splitByComma(fqn)) { + if (str != null && !str.isEmpty()) { + if (!result.isEmpty()) { + result += ", "; + } + result += str.trim(); + } + } + return result; + } else { + return imports.getType(checkForDatatype(fqn)); + } + } + + protected boolean isArray(String fqn) { + return fqn != null && fqn.trim().endsWith("[]"); + } + + protected boolean containsClassAndGeneric(String fqn) { + return fqn != null && fqn.indexOf("<") != -1; + } + + protected String[] getClassAndGeneric(String fqn) { + int idx = fqn.indexOf("<"); + String[] result = new String[2]; + result[0] = fqn.substring(0, idx); + result[1] = fqn.substring(idx+1, fqn.length() - 1); + return result; + } + + protected void generateImports(Writer output, String currentPackage) throws IOException { + List<String> imports = this.imports.getImports(currentPackage); + if (!imports.isEmpty()) { + for (String importLine : imports) { +/*{import <%=importLine%>; +}*/ + } +/*{ +}*/ + } + } + + protected void generateCopyright(Writer output) throws IOException { + String copyright = EugengoUtils.getCopyright(model); + if (GeneratorUtil.notEmpty(copyright)) { +/*{<%=copyright%> +}*/ + } + } + + protected void generateClazzDocumentation(Writer output, ObjectModelClassifier classifier, String ... defaultDoc) + throws IOException { + generateDocumentation(output, classifier, "", defaultDoc); + } + + protected void generateDocumentation(Writer output, ObjectModelElement element, String prefix, String ... defaultDoc) + throws IOException { + String doc = null; + if (GeneratorUtil.hasDocumentation(element)) { + doc = element.getDocumentation(); + } else { + //TODO Manage defaultDoc + } + + if (doc != null) { + // Manage RC in the doc + Pattern p = Pattern.compile("(\n)"); + Matcher m = p.matcher(doc); + String docOk = m.replaceAll("\n"+prefix+" * "); + +/*{<%=prefix%>/** +<%=prefix%> * <%=docOk%> +<%=prefix%> *) +}*/ + } + } + + /** + * Generates a header for the given operation. + * @param output The stream to write inside + * @param op the operation which header is to generate + * @param hasBody need to generate a body ? + * <li>true (for classes) : generates ' {' at the end</li> + * <li>false (for interfaces) : generates ';' at the end</li> + * @param hasBody + * @throws IOException + */ + protected void generateOperationHeader(Writer output, ObjectModelOperation op, + boolean generateForInterface, String ... additionalExceptions) throws IOException { + String opVisibility = op.getVisibility(); + //If generate for interface, only public methods are allowed + if (generateForInterface && !"".equals(opVisibility) && !"public".equals(opVisibility)) { + return; + } + String opName = op.getName(); +/*{ // Operation "<%=opName%>" +}*/ + generateDocumentation(output, op, " "); + if (generateForInterface || "package".equals(opVisibility)) { + opVisibility = ""; + } else { + opVisibility += " "; + } + String opType = computeType(op.getReturnParameter()); + opType = getType(opType); + String opAbstract = ""; + if (!generateForInterface && op.isAbstract()) { + opAbstract = "abstract "; + } +/*{ <%=opVisibility%><%=opAbstract%><%=opType%> <%=opName%>(}*/ + boolean isFirst = true; + for (ObjectModelParameter opParam : op.getParameters()) { + String paramName = opParam.getName(); + String paramType = computeType(opParam); + paramType = getType(paramType); + if (!isFirst) { +/*{, }*/ + } + isFirst = false; +/*{<%=paramType%> <%=paramName%>}*/ + } +/*{)}*/ + if ((op.getExceptions() != null && !op.getExceptions().isEmpty()) || (additionalExceptions != null && additionalExceptions.length > 0)) { +/*{ throws}*/ + isFirst = true; + Set<String> exceptions = new LinkedHashSet<String>(); + + if (additionalExceptions != null) { + for (String exception : additionalExceptions) { + exceptions.add(exception); + } + } + if (op.getExceptions() != null) { + for (String exception : op.getExceptions()) { + exceptions.add(exception); + } + } + for (String exception : exceptions) { + exception = getType(exception); + if (!isFirst) { +/*{,}*/ + } + isFirst = false; +/*{ <%=exception%>}*/ + } + } + if (generateForInterface || op.isAbstract()) { +/*{; + +}*/ + } else { +/*{ { +}*/ + } + } + + /** + * Generates a ioc name and injection. Will generate the class attribute + * and getter/setter. + * The name used is the name specified in the dependency class name. + * @param output The stream to write inside + * @param dep the dependency to generate. + * @throws IOException + */ + protected void generateIocDependency(Writer output, ObjectModelDependency dep) + throws IOException { + ObjectModelClassifier supplier = dep.getSupplier(); + if (supplier == null || EugengoUtils.isDao(supplier)) { + return; + } + String supplierType = getType(supplier.getQualifiedName()); + String supplierVarName = EugengoUtils.toLowerCaseFirstLetter(supplier.getName()); + String supplierMethodSuffix = EugengoUtils.toUpperCaseFirstLetter(supplier.getName()); +/*{ // Dependency injection for "<%=supplierVarName%>" + private <%=supplierType%> <%=supplierVarName%>; + + public <%=supplierType%> get<%=supplierMethodSuffix%>() { + return <%=supplierVarName%>; + } + + public void set<%=supplierMethodSuffix%>(<%=supplierType%> <%=supplierVarName%>) { + this.<%=supplierVarName%> = <%=supplierVarName%>; + } + +}*/ + } + + protected void generateAttributesDeclaration(Writer output, + ObjectModelClass clazz) throws IOException { + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && !attr.isStatic() && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + generateAttributeDeclaration(output, attr); + } + } + } + + protected void generateAttributeDeclaration(Writer output, ObjectModelAttribute attr) + throws IOException { + String attrType = computeType(attr); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType); + } else { + return; + } + String attrVisibility = attr.getVisibility(); + String attrName = attr.getName(); +/*{ // Declaration of attribute "<%=attrName%>" +}*/ + generateDocumentation(output, attr, " "); + String value = computeDefaultValue(attr); +/*{ <%=attrVisibility%> <%=attrType%> <%=attrName%><%=value%>; + +}*/ + } + + /** + * Compute correct type of param. If param is + * <li> null : void + * <li> cardinality > 1 : Collection of type + * <li> cardinality > 1 and ordered: List of type + * <li> cardinality > 1 and unique: Set of type + * <li> other : the type + * @param param + * @return + */ + protected String computeType(ObjectModelParameter param) { + if (param == null) { + return "void"; + } + String result = param.getType(); + + // if type is Wikitty then we used String + if ("org.sharengo.wikitty.Wikitty".equals(result) + || "Wikitty".equals(result)) { + result = "String"; + } + + boolean isCollection = (param.getMaxMultiplicity() != 0 + && param.getMaxMultiplicity() != 1); + if (isCollection) { + Class<?> type = Collection.class; + if (param.isOrdered()) { + type = List.class; + } + if (param.isUnique()) { + type = Set.class; + } + result = type.getName() + "<" + result + ">"; + } + return result; + } + + protected void generateAttributesAccessors(Writer output, + ObjectModelClass clazz) throws IOException { + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && !attr.isStatic() && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + generateAttributeAccessors(output, attr); + } + } + } + + protected void generateAttributeAccessors(Writer output, ObjectModelAttribute attr) + throws IOException { + String attrType = computeType(attr); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType); + } else { + return; + } + String attrName = attr.getName(); + String attrNameCapitalized = EugengoUtils.toUpperCaseFirstLetter(attrName); +/*{ // Accessors for attribute "<%=attrName%>" + public void set<%=attrNameCapitalized%>(<%=attrType%> <%=attrName%>) { + this.<%=attrName%> = <%=attrName%>; + } + + public <%=attrType%> get<%=attrNameCapitalized%>() { + return this.<%=attrName%>; + } + +}*/ + } + + protected void generateStaticAttributes(Writer output, ObjectModelClass clazz) + throws IOException { + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isStatic() && "public".equals(attr.getVisibility())) { + String type = computeType(attr); + type = getType(type); + String name = attr.getName(); + String value = computeDefaultValue(attr); +/*{ // static attribute "<%=name%>" + public static <%=type%> <%=name%><%=value%>; + +}*/ + } + } + } + + protected String computeDefaultValue(ObjectModelAttribute attr) { + String result = ""; + String value = attr.getDefaultValue(); + if (value != null) { + String type = computeType(attr); + type = getType(type); + if ("String".equals(type)) { + result = "\"" + value + "\""; + } else if ("boolean".equalsIgnoreCase(type)) { + result = "Boolean." + ("true".equalsIgnoreCase(value) + "").toUpperCase(); + } else if ("byte".equalsIgnoreCase(type) || "short".equalsIgnoreCase(type) || "int".equalsIgnoreCase(type) || "integer".equalsIgnoreCase(type)) { + result = value; + } else if ("long".equalsIgnoreCase(type)) { + result = value + "L"; + } else if ("float".equalsIgnoreCase(type)) { + result = value + "F"; + } else if ("double".equalsIgnoreCase(type)) { + result = value + "D"; + } else if ("Date".equals(type)) { + try { + Date d = new SimpleDateFormat().parse(value); + result = "new Date(" + d.getTime() + "l)"; + } catch (ParseException pe) { + log.warn("Unable to parse date", pe); + // Nothing else to do + } + } else { + result = value; + } + result = " = " + result; + } + return result; + } + + protected void generateDefaultConstructor(Writer output, String name) + throws IOException { +/*{ /** + * Default constructor + *) + public <%=name%>() { + super(); + } + +}*/ + } + + protected void generateExceptionConstructors(Writer output, ObjectModelClass clazz) + throws IOException { + String name = clazz.getName(); + generateDefaultConstructor(output, name); + +/*{ public <%=name%>(Throwable cause) { + super(); + initCause(cause); + } + +}*/ + } + + protected void generateFullConstructor(Writer output, + String name, Collection<ObjectModelAttribute> attrs) throws IOException { + int nb = 0; + int total = 0; + if (hasNavigableAndNonStaticAttributes(attrs)) { +/*{ /** + * Constructor with all parameters initialized + * +}*/ + for (ObjectModelAttribute attr : attrs) { + if (attr.isNavigable() && !attr.isStatic() && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + total ++; + String attName = attr.getName(); +/*{ * @param <%=attName%> +}*/ + } + } + +/*{ *) + public <%=name%>(}*/ + for (ObjectModelAttribute attr : attrs) { + if (attr.isNavigable() && !attr.isStatic() && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + String attrName = attr.getName(); + String attrType = getType(computeType(attr)); + if (EugengoUtils.notEmpty(attrType)) { + attrType = getType(attrType); + } else { + return; + } +/*{<%=attrType%> <%=attrName%>}*/ + nb ++; + if (nb < total){ +/*{, }*/ + } + } + } +/*{) { + super(); +}*/ + for (ObjectModelAttribute attr : attrs) { + if (attr.isNavigable() && !attr.isStatic() && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + String attrName = attr.getName(); +/*{ this.<%=attrName%> = <%=attrName%>; +}*/ + } + } + +/*{ } + +}*/ + } + } + + protected boolean hasNavigableAndNonStaticAttributes(ObjectModelClass clazz) { + return hasNavigableAndNonStaticAttributes(clazz.getAttributes()); + } + + protected boolean hasNavigableAndNonStaticAttributes(Collection<ObjectModelAttribute> attrs) { + if (attrs != null && !attrs.isEmpty()) { + for (ObjectModelAttribute attr : attrs) { + if (attr.isNavigable() && !attr.isStatic()) { + return true; + } + } + } + return false; + } + + /** + * Run throw the given ObjectModelClass and declare as an import each found + * attribute type + * @param clazz the class to run throw + */ + protected void lookForAttributeImports(ObjectModelClass clazz) { + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + String type = computeType(attr); + addImport(type); + } + } + } + + /** + * Run throw the given ObjectModelClass and declare as an import each found + * static attribute type + * @param clazz the class to run throw + */ + protected void lookForStaticAttributeImports(ObjectModelClass clazz) { + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && attr.isStatic() && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + String type = computeType(attr); + addImport(type); + } + } + } + + /** + * Run throw the given ObjectModelClassifier and declare each type found on + * the operation declaration (return type, parameters type, exception + * thrown) + * @param classifier the classifier to run throw + */ + protected void lookForOperationImports(ObjectModelClassifier classifier) { + for (ObjectModelOperation op : classifier.getOperations()) { + String returnType = computeType(op.getReturnParameter()); + addImport(returnType); + for (ObjectModelParameter param : op.getParameters()) { + String paramType = computeType(param); + addImport(paramType); + } + for (String exceptionType : op.getExceptions()) { + addImport(exceptionType); + } + } + } + + /** + * Run throw the given ObjectModelClassifier and declare as an import each + * dependency's type found. The import is added only if the dependency's + * supplier is a service or a dao + * @param classifier the classifier to run throw + */ + protected void lookForIocImports(ObjectModelClassifier classifier) { + for (ObjectModelDependency dep : classifier.getDependencies()) { + ObjectModelClassifier supplier = dep.getSupplier(); + if (supplier != null && EugengoUtils.isService(supplier)) { + addImport(supplier.getQualifiedName()); + } + } + } + + /** + * Look on the model for a tag value that indicates an implementation for + * a specific datatype + * @param type the type to look for a declared implementation + * @return the found type or the original type + */ + protected String checkForDatatype(String type) { + if (type != null) { + // Look for simple dataType + String tag = model.getTagValue(EugengoConstants.PREFIX_DATATYPE + type); + if (tag != null) { + return tag; + } + // Look for generic dataType + int idx = type.indexOf("<"); + if (idx != -1) { + tag = model.getTagValue(EugengoConstants.PREFIX_DATATYPE + type.substring(0, idx)); + } + if (tag != null) { + return tag; + } + } + // No dataType found, return type + return type; + } + + /** + * Run throw the superclasses to get the first one. + * + * @param clazz the class to run throw + * @return the first found superClass or null + */ + protected ObjectModelClass findSuperClass(ObjectModelClass clazz) { + if (clazz.getSuperclasses() != null && !clazz.getSuperclasses().isEmpty()) { + return clazz.getSuperclasses().iterator().next(); + } + return null; + } + + protected Collection<ObjectModelClass> findSubClasses(ObjectModelClass clazz) { + Collection<ObjectModelClass> result = new ArrayList<ObjectModelClass>(); + for (ObjectModelClass potentialSubClass : model.getClasses()) { + if (clazz.equals(findSuperClass(potentialSubClass))) { + result.add(potentialSubClass); + } + } + return result; + } + + protected void generateHashCode(Writer output, ObjectModelClass clazz) throws IOException { +/*{ public int hashCode() { + int result = 0; +}*/ + String prefix = ""; + if (EugengoUtils.isEntity(clazz)) { +/*{ if (id != null) { + result = id.hashCode(); + } else { +}*/ + prefix = " "; + } + generateHashCodeFromAttributes(output, clazz, prefix); + if (EugengoUtils.isEntity(clazz)) { +/*{ } +}*/ + } +/*{ return result; + } + +}*/ + } + + private void generateHashCodeFromAttributes(Writer output, ObjectModelClass clazz, String prefix) throws IOException { + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable() && (attr.getStereotypes() == null || attr.getStereotypes().isEmpty())) { + String attrName = attr.getName(); + String attrType = getType(attr.getType()); + if (EugengoUtils.isPrimitiveType(attr)) { + // If the leading character is an uppercased letter... + if (attrType.charAt(0) == attrType.toUpperCase().charAt(0)) { +/*{<%=prefix%> if (<%=attrName%> != null) { +<%=prefix%> result = 29 * result + <%=attrName%>.hashCode(); +<%=prefix%> } +}*/ + } else { + attrType = EugengoUtils.toUpperCaseFirstLetter(attrType); + if ("Int".equals(attrType)) { + attrType = "Integer"; + } +/*{<%=prefix%> result = 29 * result + new <%=attrType%>(<%=attrName%>).hashCode(); +}*/ + } + } else { +/*{<%=prefix%> if (<%=attrName%> != null) { +<%=prefix%> result = 29 * result + <%=attrName%>.hashCode(); +<%=prefix%> } +}*/ + } + } + } + } +} Property changes on: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/WikengoCommonGenerator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/WikittyMetaGenerator.java =================================================================== --- trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/WikittyMetaGenerator.java (rev 0) +++ trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/WikittyMetaGenerator.java 2010-04-20 10:05:45 UTC (rev 6) @@ -0,0 +1,135 @@ +package org.sharengo.wikitty.generator; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.ObjectModelGenerator; +import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.util.Resource; + +public class WikittyMetaGenerator extends ObjectModelGenerator { + + protected static Log log = LogFactory.getLog(WikittyMetaGenerator.class); + + /** + * la liste des generateurs par defaut a utiliser + */ + protected static final List<Class<? extends WikengoCommonGenerator>> DEFAULT_GENERATORS = + Collections.unmodifiableList(Arrays.asList( + + // Enum + InterfaceGenerator.class, + EnumGenerator.class, + + // Wikitty (Interface, impl...) + BusinessEntityInterfaceGenerator.class, + BusinessEntityGenerator.class, + BusinessEntityBeanGenerator.class + + )); + + protected List<Class<? extends WikengoCommonGenerator>> getGenerators() { + List<Class<? extends WikengoCommonGenerator>> result = DEFAULT_GENERATORS; + Properties props = null; + try { + Properties tmpProperties = Resource.getConfigProperties("WikittyMetaGenerator.properties"); + + props = new Properties(); + + // Mise à plat du fichier (sans parent) + for (Enumeration<?> e = tmpProperties.propertyNames(); e.hasMoreElements();) { + String key = e.nextElement() + ""; + String value = tmpProperties.getProperty(key); + props.setProperty(key, value); + } + + } catch (IOException ioe) { + log.error("Unable to load file WikengoMetaGenerator.properties : " + ioe.getMessage()); + } + + if (props != null) { + List<Class<? extends WikengoCommonGenerator>> propsGenerators = new ArrayList<Class<? extends WikengoCommonGenerator>>(); + String basePackage = props.getProperty("basePackage"); + String suffix = props.getProperty("suffix"); + if (basePackage != null && !basePackage.endsWith(".")) { + basePackage += "."; + } + for (Object key : props.keySet()) { + String className = (String)key; + if ( suffix != null ) className += suffix; + + // Try with "className" + Class<? extends WikengoCommonGenerator> clazz = getClass(className); + + // Try with "baseName.className" + if (clazz == null && basePackage != null) { + clazz = getClass(basePackage + className); + } + + if (clazz != null) { + propsGenerators.add(clazz); + } else { + log.warn("No generator found for property '" + className + "'"); + } + } + if (!propsGenerators.isEmpty()) { + result = propsGenerators; + } + } + + log.warn("Enabled generators: " + result); + return result; + } + + protected Class<? extends WikengoCommonGenerator> getClass(String className) { + Class<? extends WikengoCommonGenerator> result = null; + try { + Class<?> clazz = Class.forName(className); + if (WikengoCommonGenerator.class.isAssignableFrom(clazz)) { + result = (Class<? extends WikengoCommonGenerator>)clazz; + } + } catch (ClassNotFoundException e) { + // Nothing to do + } + return result; + } + + @Override + public void generate(ObjectModel model, File destDir) throws IOException { + for (Class<? extends ObjectModelGenerator> generatorClass : getGenerators()) { + ObjectModelGenerator generator; + if (excludeTemplates != null && excludeTemplates.contains(generatorClass.getName())) { + // exclude generator + log.info("exclude generator " + generatorClass); + continue; + } + try { + + generator = generatorClass.newInstance(); + generator.setParent(this); + + } catch (Exception e) { + // should never happens + if (log.isErrorEnabled()) { + log.error("An error occurs when generating persistence", e); + } + throw new RuntimeException(e); + } + + // log + if (log.isDebugEnabled()) { + log.debug("call template : " + generatorClass.getSimpleName()); + } + generator.generate(model, destDir); + } + } + +} Property changes on: trunk/wikitty-generators/src/main/java/org/sharengo/wikitty/generator/WikittyMetaGenerator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/wikitty-solr-impl/src/test/java/org/sharengo/wikitty/solr/test/TreeTest.java =================================================================== --- trunk/wikitty-solr-impl/src/test/java/org/sharengo/wikitty/solr/test/TreeTest.java 2010-04-16 09:59:15 UTC (rev 5) +++ trunk/wikitty-solr-impl/src/test/java/org/sharengo/wikitty/solr/test/TreeTest.java 2010-04-20 10:05:45 UTC (rev 6) @@ -30,7 +30,7 @@ import org.apache.commons.logging.LogFactory; import org.junit.Before; import org.junit.Test; -import org.sharengo.internal.TreeNodeImpl; +import org.sharengo.wikitty.TreeNodeImpl; import org.sharengo.wikitty.Criteria; import org.sharengo.wikitty.ExtensionFactory; import org.sharengo.wikitty.Tree; @@ -38,7 +38,7 @@ import org.sharengo.wikitty.Wikitty; import org.sharengo.wikitty.WikittyExtension; import org.sharengo.wikitty.FieldType.TYPE; -import org.sharengo.wikitty.TreeNodeType; +import org.sharengo.wikitty.TreeNode; import org.sharengo.wikitty.search.Search; import org.sharengo.wikitty.test.conform.StorageTest; @@ -162,8 +162,8 @@ int sum = 0; // Sum value in node - Set<String> values = node.getFieldAsSet(TreeNodeType.EXT_TREENODE, - TreeNodeType.FIELD_CHILDREN, String.class); + Set<String> values = node.getFieldAsSet(TreeNode.EXT_TREENODE, + TreeNode.FIELD_CHILDREN, String.class); if(values != null) { sum = values.size(); } @@ -190,8 +190,8 @@ */ protected Wikitty createNode(String name, String parentId) { TreeNodeImpl node = new TreeNodeImpl(); - node.setTreeNodeName(name); - node.setTreeNodeParent(parentId); + node.setName(name); + node.setParent(parentId); Wikitty nodeWikitty = node.getWikitty(); return nodeWikitty; } @@ -383,7 +383,7 @@ int childSum2 = sum(node2); // Move node 1 in mode 2 - node121.setField(TreeNodeType.EXT_TREENODE, TreeNodeType.FIELD_PARENT, node2); + node121.setField(TreeNode.EXT_TREENODE, TreeNode.FIELD_PARENT, node2); ws.store(node121); // now, there is less value node 121 for the node 1
participants (1)
-
echatellier@users.nuiton.org