Author: jcouteau Date: 2010-11-23 08:43:11 +0100 (Tue, 23 Nov 2010) New Revision: 495 Url: http://nuiton.org/repositories/revision/wikitty/495 Log: Add wikitty-dto module Added: trunk/wikitty-dto/ trunk/wikitty-dto/LICENSE.txt trunk/wikitty-dto/README.txt trunk/wikitty-dto/changelog.txt trunk/wikitty-dto/pom.xml trunk/wikitty-dto/src/ trunk/wikitty-dto/src/license/ trunk/wikitty-dto/src/license/THIRD-PARTY.properties trunk/wikitty-dto/src/main/ trunk/wikitty-dto/src/main/java/ trunk/wikitty-dto/src/main/java/org/ trunk/wikitty-dto/src/main/java/org/nuiton/ trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/ trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/dto/ trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/dto/DTOHelper.java trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/generator/ trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java Property changes on: trunk/wikitty-dto ___________________________________________________________________ Added: svn:ignore + target *.iml *.iws *.ipr Added: trunk/wikitty-dto/LICENSE.txt =================================================================== --- trunk/wikitty-dto/LICENSE.txt (rev 0) +++ trunk/wikitty-dto/LICENSE.txt 2010-11-23 07:43:11 UTC (rev 495) @@ -0,0 +1,166 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + Added: trunk/wikitty-dto/pom.xml =================================================================== --- trunk/wikitty-dto/pom.xml (rev 0) +++ trunk/wikitty-dto/pom.xml 2010-11-23 07:43:11 UTC (rev 495) @@ -0,0 +1,163 @@ +<?xml version="1.0" encoding="UTF-8"?> +<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/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.nuiton</groupId> + <artifactId>wikitty</artifactId> + <version>3.0-SNAPSHOT</version> + </parent> + + <!-- ************************************************************* --> + <!-- *** POM Relationships *************************************** --> + <!-- ************************************************************* --> + <groupId>org.nuiton.wikitty</groupId> + <artifactId>wikitty-dto</artifactId> + + <dependencies> + + <!-- sibling dependencies --> + + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>wikitty-generators</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>wikitty-api</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.nuiton.eugene</groupId> + <artifactId>eugene</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + </dependency> + + <dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-utils</artifactId> + </dependency> + + <dependency> + <groupId>commons-beanutils</groupId> + <artifactId>commons-beanutils</artifactId> + </dependency> + + </dependencies> + + <!-- ************************************************************* --> + <!-- *** Project Information ************************************* --> + <!-- ************************************************************* --> + + <name>Wikitty :: dto</name> + + <description>Provide DTOs for Wikitty</description> + <inceptionYear>2010</inceptionYear> + + <!-- ************************************************************* --> + <!-- *** Build Settings ****************************************** --> + <!-- ************************************************************* --> + + <packaging>jar</packaging> + <properties> + + <!-- extra files to include in release --> + <redmine.releaseFiles> + target/${project.build.finalName}.${project.packaging} + </redmine.releaseFiles> + </properties> + + <build> + + <pluginManagement> + <plugins> + <!-- eugene plugin --> + <plugin> + <groupId>org.nuiton.eugene</groupId> + <artifactId>maven-eugene-plugin</artifactId> + <version>${eugeneVersion}</version> + </plugin> + + </plugins> + </pluginManagement> + + <plugins> + <!-- processor plugin --> + <plugin> + <groupId>org.nuiton.processor</groupId> + <artifactId>maven-processor-plugin</artifactId> + <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> + + <plugin> + <groupId>org.nuiton.eugene</groupId> + <artifactId>maven-eugene-plugin</artifactId> + <executions> + <execution> + <id>api-dto-generation</id> + <phase>generate-test-sources</phase> + <configuration> + <testPhase>true</testPhase> + <inputs> + <input>classpath:model:/:wikitty.objectmodel</input> + </inputs> + <fullPackagePath>org.nuiton.wikitty.entities + </fullPackagePath> + <defaultPackage>org.nuiton.wikitty.entities + </defaultPackage> + <extractedPackages>org.nuiton.wikitty.entities + </extractedPackages> + <templates> + org.nuiton.wikitty.generator.WikittyDTOGenerator + </templates> + </configuration> + <goals> + <goal>smart-generate</goal> + </goals> + </execution> + </executions> + </plugin> + + <!-- expose new plexus components --> + <plugin> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-component-metadata</artifactId> + <executions> + <execution> + <goals> + <goal>generate-metadata</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + + </build> + +</project> Added: trunk/wikitty-dto/src/license/THIRD-PARTY.properties =================================================================== --- trunk/wikitty-dto/src/license/THIRD-PARTY.properties (rev 0) +++ trunk/wikitty-dto/src/license/THIRD-PARTY.properties 2010-11-23 07:43:11 UTC (rev 495) @@ -0,0 +1,51 @@ +### +# #%L +# Wikitty :: dto +# +# $Id$ +# $HeadURL$ +# %% +# Copyright (C) 2010 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% +### +# Generated by org.nuiton.license.plugin.AddThirdPartyMojo +#------------------------------------------------------------------------------- +# Already used licenses in project : +# - Apache 2 +# - Apache Software License, version 1.1 +# - BSD License +# - BSD style +# - Indiana University Extreme! Lab Software License, vesion 1.1.1 +# - LGPL +# - LGPL 2.1 +# - Lesser General Public License (LGPL) v 3.0 +# - Public Domain +# - The Apache Software License, Version 2.0 +# - https://svn.codehaus.org/picocontainer/java/picocontainer/2.x/trunk/LICENSE.... +#------------------------------------------------------------------------------- +# Please fill the missing licenses for dependencies : +# +# +#Sat Nov 20 14:37:54 CET 2010 +commons-primitives--commons-primitives--1.0=The Apache Software License, Version 2.0 +dom4j--dom4j--1.6.1=BSD License +javax.transaction--jta--1.1=COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 +jaxen--jaxen--1.1.1=http\://jaxen.codehaus.org/license.html +jdom--jdom--1.0=The Apache Software License, Version 2.0 +org.jgroups--jgroups--2.10.0.GA=LGPL 2.1 +xalan--xalan--2.6.0=The Apache Software License, Version 2.0 +xpp3--xpp3_min--1.1.3.4.O=Indiana University Extreme! Lab Software License Added: trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/dto/DTOHelper.java =================================================================== --- trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/dto/DTOHelper.java (rev 0) +++ trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/dto/DTOHelper.java 2010-11-23 07:43:11 UTC (rev 495) @@ -0,0 +1,72 @@ +/* + * #%L + * Wikitty :: dto + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 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% + */ +package org.nuiton.wikitty.dto; + +import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.ObjectUtil; +import org.nuiton.wikitty.entities.BusinessEntity; +import org.nuiton.wikitty.entities.Wikitty; +import org.nuiton.wikitty.entities.WikittyImpl; + +/** + * User: couteau + * Date: 19 nov. 2010 + */ +public class DTOHelper { + + static Log log = LogFactory.getLog(DTOHelper.class); + + public static <E extends BusinessEntity, F extends BusinessEntity> E toDto(F w) { + Class clazz = w.getClass(); + String dtoClassName = clazz.getName().replace("Impl", "DTO").replace("CopyOnWrite", "DTO"); + E result = null; + try { + result = (E)ObjectUtil.newInstance(dtoClassName+"()"); + BeanUtils.copyProperties(result, w); + }catch (Exception eee){ + log.error("Could not transform Wikitty to DTO", eee); + } + return result; + } + + public static <E extends BusinessEntity, F extends BusinessEntity> E fromDto(F dto) { + Class clazz = dto.getClass(); + String wClassName = clazz.getName().replace("DTO", "Impl"); + E result = null; + try { + Class clazzImpl = Class.forName(wClassName); + Wikitty w = new WikittyImpl(dto.getWikittyId()); + result = (E)ObjectUtil.newInstance(clazzImpl+"(w)"); + BeanUtils.copyProperties(result, dto); + } catch (Exception eee){ + log.error("Could not transform DTO to Wikitty", eee); + } + return result; + + } + +} Added: trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java =================================================================== --- trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java (rev 0) +++ trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java 2010-11-23 07:43:11 UTC (rev 495) @@ -0,0 +1,385 @@ +/* + * #%L + * Wikitty :: dto + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 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% + */ +package org.nuiton.wikitty.generator; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.Transformer; +import org.nuiton.eugene.java.ObjectModelTransformerToJava; +import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelModifier; +import org.nuiton.eugene.models.object.ObjectModelOperation; + + +/*{generator option: writeString = }*/ +/*{generator option: parentheses = false}*/ +/*{generator option: writeString = +}*/ + +/** + * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.wikitty.generator.WikittyDTOGenerator" + */ +public class WikittyDTOGenerator extends ObjectModelTransformerToJava { + + private static final Log log = LogFactory.getLog(WikittyDTOGenerator.class); + + /** + * map busines entity from source model to generated abstract class + */ + protected Map<ObjectModelClass, ObjectModelClass> processedClasses = + new HashMap<ObjectModelClass, ObjectModelClass>(); + + /** + * pattern to parse toString tagValue + * for the string "abc{foo|bar}defrzeg{uvw|xyz}oeira" + * will match {foo|bar} and {uvw|xyz} with groups for foo, bar, uvw and xyz + */ + protected Pattern toStringTagValuePattern = Pattern.compile("\\{(([^|}])*)(?:\\|([^}]*))?\\}"); + + /** + * map "Client.name" to "getName()" or any getter to read this attribute + * those getter names are stored while generating in order to be found + * later when generating toString() + */ + protected Map<String, String> attributeToGetterName = new HashMap<String, String>(); + + @Override + protected Transformer<ObjectModel, ObjectModel> initPreviousTransformer() { + return new WikittyPurifierTransformer(); + } + + protected List<ObjectModelClass> entitiesWithInheritedOperations = + new ArrayList<ObjectModelClass>(); + + @Override + public void transformFromModel(ObjectModel model) { + + // contains boths businessEntities and metaExtensions + // elements may have one of the two sterotypes or both + // but elements with none of them aren't in this list + List<ObjectModelClass> modelBoth = new ArrayList<ObjectModelClass>(); + + // fill modelBusinessEntities with entities found in model + for (ObjectModelClass clazz : model.getClasses()) { + if (WikittyTransformerUtil.isBusinessEntity(clazz) || + WikittyTransformerUtil.isMetaExtension(clazz)) { + modelBoth.add(clazz); + } + } + + for (ObjectModelClass clazz : modelBoth) { + ObjectModelClass abstractClass = createAbstractClass( + businessEntityToDTOName(clazz), + clazz.getPackageName()); + processedClasses.put(clazz, abstractClass); + addInterface(abstractClass, clazz.getQualifiedName()); + } + + for (ObjectModelClass clazz : modelBoth) { + if (WikittyTransformerUtil.isMetaExtension(clazz)) { + addMetaExtensionOperations(clazz, processedClasses.get(clazz)); + } else if (WikittyTransformerUtil.isBusinessEntity(clazz)) { + addOperations(clazz, processedClasses.get(clazz)); + } + } + + // at this time, all operations in generated abstracts are just the operations + // like get/set etc. we will copy all operations of a given class to all children + // that's why constructors and others operations are not yet added + for (ObjectModelClass clazz : modelBoth) { + if (WikittyTransformerUtil.isBusinessEntity(clazz)) { + addInheritedOperations(clazz, processedClasses.get(clazz)); + } + } + + for (ObjectModelClass clazz : modelBoth) { + ObjectModelClass abstractClassForThisEntity = processedClasses.get(clazz); + addConstructors(abstractClassForThisEntity); + addToString(clazz, abstractClassForThisEntity); + } + + processedClasses.clear(); + } + + protected void addSerialVersionUID(ObjectModelClass clazz) { + // adding a generated serialVersionUID + Random random = new Random(); + Long serialVersionUIDs = random.nextLong(); + addConstant(clazz, + "serialVersionUID", + "long", + serialVersionUIDs.toString() + "L", + ObjectModelModifier.PRIVATE); + } + + protected void addConstructors(ObjectModelClass clazz) { + + ObjectModelOperation constructor = addConstructor(clazz, ObjectModelModifier.PUBLIC); + setOperationBody(constructor, "" +/*{ +}*/); + + } + + protected void addOperations(ObjectModelClass businessEntity, ObjectModelClass abstractClass) { + // generating operations with bodies to realize contract + for (ObjectModelAttribute attribute : businessEntity.getAttributes()) { + if (attribute.isNavigable()) { + // needed below, in templates + String attributeType = WikittyTransformerUtil.generateResultType(attribute, false); + String attributeName = attribute.getName(); + + //add necessary import + addImport(abstractClass, attribute.getType()); + + //If alternative name, use it + if (attribute.hasTagValue(WikittyTransformerUtil.TAG_ALTERNATIVE_NAME)) { + attributeName = attribute.getTagValue(WikittyTransformerUtil.TAG_ALTERNATIVE_NAME); + } + + String getterName; + + if (WikittyTransformerUtil.isAttributeCollection(attribute)) { + // attribute is a collection, we will generate operations + // get, add, remove and clear + String collectionType = WikittyTransformerUtil.getCollectionTypeName(attribute); + if (collectionType.equals("Set")){ + addImport(abstractClass, java.util.Set.class); + } + if (collectionType.equals("List")){ + addImport(abstractClass, java.util.List.class); + + } + + String attributeTypeInSet = WikittyTransformerUtil.generateResultType(attribute, true); + + //adding the attribute + addAttribute(abstractClass, attributeName, attributeTypeInSet); + + // adding the getter + getterName = "get" + StringUtils.capitalize(attributeName); + ObjectModelOperation getter = addOperation(abstractClass, getterName, attributeTypeInSet); + addAnnotation(abstractClass, getter, "Override"); + String getterBody = "" +/*{ + return <%=attributeName%>; +}*/; + setOperationBody(getter, getterBody); + + // adding the setter + getterName = "set" + StringUtils.capitalize(attributeName); + ObjectModelOperation setter = addOperation(abstractClass, getterName, "void"); + addAnnotation(abstractClass, setter, "Override"); + addParameter(setter, attributeTypeInSet, attributeName); + String setterBody = "" +/*{ + this.<%=attributeName%>=<%=attributeName%>; +}*/; + setOperationBody(setter, setterBody); + + //adding the add method + String addName = "add" + StringUtils.capitalize(attributeName); + ObjectModelOperation adder = addOperation(abstractClass, addName, "void"); + addAnnotation(abstractClass, adder, "Override"); + addParameter(adder, attributeType, "element"); + String adderBody = "" +/*{ + <%=attributeName%>.add(element); +}*/; + setOperationBody(adder, adderBody); + + //adding the remove method + String removeName = "remove" + StringUtils.capitalize(attributeName); + ObjectModelOperation remover = addOperation(abstractClass, removeName, "void"); + addAnnotation(abstractClass, remover, "Override"); + addParameter(remover, attributeType, "element"); + String removerBody = "" +/*{ + <%=attributeName%>.remove(element); +}*/; + setOperationBody(remover, removerBody); + + //adding the clear method + String clearName = "clear" + StringUtils.capitalize(attributeName); + ObjectModelOperation clear = addOperation(abstractClass, clearName, "void"); + addAnnotation(abstractClass, clear, "Override"); + String clearBody = "" +/*{ + <%=attributeName%>.clear(); +}*/; + setOperationBody(clear, clearBody); + + } else { + //No multiplicity + + //add necessary import + addImport(abstractClass, attribute.getType()); + + //adding the attribute + addAttribute(abstractClass, attributeName, attributeType); + + // adding getter + getterName = "get" + StringUtils.capitalize(attributeName); + ObjectModelOperation getter = addOperation(abstractClass, getterName, attributeType); + addAnnotation(abstractClass, getter, "Override"); + setOperationBody(getter, "" +/*{ + return <%=attributeName%>; +}*/); + + //adding setter + String setterName = "set" + StringUtils.capitalize(attributeName); + ObjectModelOperation setter = addOperation(abstractClass, setterName, "void"); + addAnnotation(abstractClass, setter, "Override"); + addParameter(setter, attributeType, attributeName); + setOperationBody(setter, "" +/*{ + this.<%=attributeName%>=<%=attributeName%>; +}*/); + } + + // save the getter name for this attribute + attributeToGetterName.put(businessEntity.getName() + "." + attributeName, getterName); + } + } + } + + /** + * Add inherited operations to the abstract generated from an entity. + * Method browse superClasses to copy methods and their bodies. The method + * deal with imports, cause bodies call the Helper of the classe where + * the attribute was declared. + * <p/> + * If A inherit from B and B inherit from and A, B, C not in the same package + * we B need to import CHelper and A need to import BHelper. But, since A + * has C attributes too, it needs CHelper too. So when we will process B, + * we will save needed imports, A will get them thus import CHelper. + */ + protected void addInheritedOperations(ObjectModelClass businessEntity, ObjectModelClass abstractClass) { + + if (!entitiesWithInheritedOperations.contains(businessEntity)) { + + // add attributes and methods inherited from super classes + for (ObjectModelClass superClass : businessEntity.getSuperclasses()) { + + // process super classes first, so we will get inherited operations from superclass + addInheritedOperations(superClass, processedClasses.get(superClass)); + + if (WikittyTransformerUtil.isBusinessEntity(superClass)) { + + //Add operations for the super class + addOperations(superClass, abstractClass); + } + } + + entitiesWithInheritedOperations.add(businessEntity); + } + } + + /** + * add a toString method + * if a toString tagValue is attached to businessEntity, it will be used + * to generate a toString as this : + * <p/> + * given "hello {Person.name|unknow}" + * <p/> + * will try to replace first {...} by name field value for extension Person. + * if this information is not available, will do unknow. + */ + protected void addToString(ObjectModelClass businessEntity, ObjectModelClass abstractClass) { + + String toStringOperationBody; + + if (businessEntity.hasTagValue(WikittyTransformerUtil.TAG_TO_STRING)) { + String toStringPattern = businessEntity.getTagValue(WikittyTransformerUtil.TAG_TO_STRING); + + // toStringPattern is something like + // "hello {Person.name|unknow} employe of {Company.name|unknow}" + // + + Matcher matcher = toStringTagValuePattern.matcher(toStringPattern); + + while (matcher.find()) { + String wholeMatch = matcher.group(0); // "{foo|bar}" + String variableName = matcher.group(1); // "foo" + String defaultValue = matcher.group(3); // "bar", may be null + + if (defaultValue == null) { + defaultValue = ""; + } + + if (attributeToGetterName.containsKey(variableName)) { + String getterName = attributeToGetterName.get(variableName); + toStringPattern = toStringPattern.replace(wholeMatch, "" +/*{" + + <%=getterName%>().toString() + + "}*/); + } else { + log.warn("no field " + variableName + " in " + businessEntity.getQualifiedName()); + toStringPattern = toStringPattern.replace(wholeMatch, defaultValue); + } + } + + toStringOperationBody = "" +/*{ + return "<%=toStringPattern%>"; +}*/; + } else { + // no toString tagValue provided, generating a default toString + toStringOperationBody = "" +/*{ + return "dto:"+getWikittyId()+":"+getWikittyVersion(); +}*/; + } + + ObjectModelOperation toString = addOperation(abstractClass, "toString", "String"); + addAnnotation(abstractClass, toString, "Override"); + setOperationBody(toString, toStringOperationBody); + } + + protected void addMetaExtensionOperations(ObjectModelClass metaExtension, + ObjectModelClass abstractClassForThisMetaExtension) { + + //add standard attribute operations for meta-extension + addOperations(metaExtension, abstractClassForThisMetaExtension); + + } + + /** + * Method to get the generated class name + */ + protected String businessEntityToDTOName(ObjectModelClass clazz) { + return clazz.getName() + "DTO"; + } +}