org.nuiton.topia.templates.DTOTransformer Maven / Gradle / Ivy
package org.nuiton.topia.templates;
/*
* #%L
* ToPIA :: Templates
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2004 - 2014 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
* .
* #L%
*/
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.eugene.GeneratorUtil;
import org.nuiton.eugene.java.JavaGeneratorUtil;
import org.nuiton.eugene.java.ObjectModelTransformerToJava;
import org.nuiton.eugene.models.object.ObjectModelAssociationClass;
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.ObjectModelInterface;
import org.nuiton.eugene.models.object.ObjectModelJavaModifier;
import org.nuiton.eugene.models.object.ObjectModelOperation;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
/**
* Created: 20 déc. 2009
*
* @author Tony Chemit - [email protected]
* @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.topia.templates.DTOTransformer"
* @since 2.3.0
*/
public class DTOTransformer extends ObjectModelTransformerToJava {
/**
* Logger
*/
private static final Log log = LogFactory.getLog(DTOTransformer.class);
protected TopiaTemplateHelper templateHelper;
protected TopiaTagValues topiaTagValues;
@Override
public void transformFromClass(ObjectModelClass clazz) {
if (!TopiaStereoTypes.hasDtoStereotype(clazz)) {
return;
}
if (templateHelper == null) {
templateHelper = new TopiaTemplateHelper(model);
}
if (topiaTagValues == null) {
topiaTagValues = templateHelper.getTopiaTagValues();
}
String clazzName = clazz.getName();
ObjectModelClass result;
result = createClass(clazzName + "DTO", clazz.getPackageName());
addImport(result, ToStringBuilder.class);
addImport(result, PropertyChangeListener.class);
setDocumentation(result, "Implantation DTO pour l'entité " + StringUtils.capitalize(clazzName) + ".");
String extendClass = "";
for (ObjectModelClass parent : clazz.getSuperclasses()) {
extendClass = parent.getQualifiedName() + "DTO";
// no multi-inheritance in java
break;
}
if (extendClass.length() > 0) {
setSuperClass(result, extendClass);
}
addInterface(result, Serializable.class);
for (ObjectModelInterface parentInterface : clazz.getInterfaces()) {
if (TopiaStereoTypes.hasDtoStereotype(parentInterface)) {
addInterface(result, parentInterface.getName() + "DTO");
} else {
addInterface(result, parentInterface.getName());
}
}
addAttributes(result, clazz);
addOperations(result, clazz);
}
protected void addAttributes(ObjectModelClass result, ObjectModelClass clazz) {
String svUID = topiaTagValues.findTagValue("dto-serialVersionUID", clazz, model);
if (StringUtils.isNotEmpty(svUID)) {
addAttribute(result, "serialVersionUID", long.class, svUID, ObjectModelJavaModifier.FINAL, ObjectModelJavaModifier.PUBLIC, ObjectModelJavaModifier.STATIC);
}
addAttribute(result, "p", PropertyChangeSupport.class, null, ObjectModelJavaModifier.PROTECTED);
/*
* Définition des attributs
*/
ObjectModelAttribute attr2;
for (ObjectModelAttribute attr : clazz.getAttributes()) {
ObjectModelAttribute reverse = attr.getReverseAttribute();
String attributeName;
String attributeType;
if (!(attr.isNavigable()
|| attr.hasAssociationClass())) {
continue;
}
String attrName = attr.getName();
String attrVisibility = attr.getVisibility();
String attrType = attr.getType();
if (!GeneratorUtil.isNMultiplicity(attr)) {
if (!attr.hasAssociationClass()) {
if (isDTO(attrType)) {
attrType += "DTO";
}
attributeType = attrType;
attributeName = attrName;
} else {
String assocAttrName = GeneratorUtil.getAssocAttrName(attr);
attributeType = attr.getAssociationClass().getQualifiedName();
attributeName = GeneratorUtil.toLowerCaseFirstLetter(assocAttrName);
}
} else {
if (!attr.hasAssociationClass()) {
String nMultType;
if (JavaGeneratorUtil.isOrdered(attr)) {
nMultType = List.class.getName() + "<";
} else {
nMultType = Collection.class.getName() + "<";
}
nMultType += attrType;
if (isDTO(attrType)) {
nMultType += "DTO";
}
nMultType += ">";
attributeType = nMultType;
attributeName = attrName;
} else {
String assocAttrName = GeneratorUtil.getAssocAttrName(attr);
String assocClassFQN = attr.getAssociationClass().getQualifiedName();
String nMultType;
if (JavaGeneratorUtil.isOrdered(attr)) {
nMultType = List.class.getName() + "<";
} else {
nMultType = Collection.class.getName() + "<";
}
nMultType += assocClassFQN;
if (isDTO(attrType)) {
nMultType += "DTO";
}
nMultType += ">";
attributeType = nMultType;
attributeName = GeneratorUtil.toLowerCaseFirstLetter(assocAttrName);
}
}
attr2 = addAttribute(result, attributeName, attributeType, null, ObjectModelJavaModifier.PROTECTED);
if (attr2 != null) {
if (GeneratorUtil.hasDocumentation(attr)) {
setDocumentation(attr2, attr.getDocumentation());
}
String annotation = topiaTagValues.getAnnotationTagValue(attr);
if (StringUtils.isNotEmpty(annotation)) {
addAnnotation(result, attr2, annotation);
}
}
} /* end for*/
//Déclaration des attributs d'une classe d'associations
if (clazz instanceof ObjectModelAssociationClass) {
ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) clazz;
for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) {
if (attr != null) {
String attrName = attr.getName();
String attrVisibility = attr.getVisibility();
String attrType = attr.getType();
if (isDTO(attrType)) {
attrType += "DTO";
}
addAttribute(result, GeneratorUtil.toLowerCaseFirstLetter(attrName), attrType);
}
}
}
}
protected void addOperations(ObjectModelClass result, ObjectModelClass clazz) {
ObjectModelOperation op;
op = addOperation(result, "addPropertyChangeListener", "void", ObjectModelJavaModifier.PUBLIC);
addParameter(op, PropertyChangeListener.class, "listener");
setOperationBody(op, ""
+"\n"
+" p.addPropertyChangeListener(listener);\n"
+" "
);
op = addOperation(result, "addPropertyChangeListener", "void", ObjectModelJavaModifier.PUBLIC);
addParameter(op, String.class, "propertyName");
addParameter(op, PropertyChangeListener.class, "listener");
setOperationBody(op, ""
+"\n"
+" p.addPropertyChangeListener(propertyName, listener);\n"
+" "
);
op = addOperation(result, "removePropertyChangeListener", "void", ObjectModelJavaModifier.PUBLIC);
addParameter(op, PropertyChangeListener.class, "listener");
setOperationBody(op, ""
+"\n"
+" p.removePropertyChangeListener(listener);\n"
+" "
);
op = addOperation(result, "removePropertyChangeListener", "void", ObjectModelJavaModifier.PUBLIC);
addParameter(op, String.class, "propertyName");
addParameter(op, PropertyChangeListener.class, "listener");
setOperationBody(op, ""
+"\n"
+" p.removePropertyChangeListener(propertyName, listener);\n"
+" "
);
/*
* Définition des getteurs et setteurs
*/
for (ObjectModelAttribute attr : clazz.getAttributes()) {
ObjectModelAttribute reverse = attr.getReverseAttribute();
// if (!(attr.isNavigable() || hasUnidirectionalRelationOnAbstractType(reverse, model))) {
if (!attr.isNavigable()) {
continue;
}
String attrName = attr.getName();
String attrType = attr.getType();
String attrTypeDTO = attr.getType();
if (isDTO(attrType)) {
attrTypeDTO += "DTO";
}
if (!GeneratorUtil.isNMultiplicity(attr)) {
if (!attr.hasAssociationClass()) {
op = addOperation(result, "set" + StringUtils.capitalize(attrName), "void", ObjectModelJavaModifier.PUBLIC);
addParameter(op, attrTypeDTO, "value");
setOperationBody(op, ""
+"\n"
+" "+attrTypeDTO+" oldValue = this."+attrName+";\n"
+" this."+attrName+" = value;\n"
+" p.firePropertyChange(\""+attrName+"\", oldValue, value);\n"
+" "
);
op = addOperation(result, "get" + StringUtils.capitalize(attrName), attrTypeDTO, ObjectModelJavaModifier.PUBLIC);
setOperationBody(op, ""
+"\n"
+" return "+attrName+";\n"
+" "
);
} else {
String assocAttrName = GeneratorUtil.getAssocAttrName(attr);
String assocClassFQN = attr.getAssociationClass().getQualifiedName();
if (log.isTraceEnabled()) {
log.trace("assocAttrName: " + assocAttrName);
}
op = addOperation(result, "set" + StringUtils.capitalize(assocAttrName), "void", ObjectModelJavaModifier.PUBLIC);
addParameter(op, assocClassFQN + "DTO", "association");
setOperationBody(op, ""
+"\n"
+" "+assocClassFQN+"DTO oldAssocation = this."+GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)+";\n"
+" this."+GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)+" = association;\n"
+" p.firePropertyChange(\""+attrName+"\", oldAssocation, assocation);\n"
+" "
);
op = addOperation(result, "get" + StringUtils.capitalize(assocAttrName), assocClassFQN + "DTO", ObjectModelJavaModifier.PUBLIC);
setOperationBody(op, ""
+"\n"
+" return "+GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)+";\n"
+" "
);
}
} else { //NMultiplicity
if (!attr.hasAssociationClass()) { //Méthodes remplacées par des accesseurs sur les classes d'assoc
String nMultType;
if (JavaGeneratorUtil.isOrdered(attr)) {
nMultType = List.class.getName() + "<" + attrTypeDTO + ">";
} else {
nMultType = Collection.class.getName() + "<" + attrTypeDTO + ">";
}
op = addOperation(result, "set" + StringUtils.capitalize(attrName), "void", ObjectModelJavaModifier.PUBLIC);
addParameter(op, nMultType, "values");
setOperationBody(op, ""
+"\n"
+" "+nMultType+" oldValues = this."+attrName+";\n"
+" this."+attrName+" = values;\n"
+" p.firePropertyChange(\""+attrName+"\", oldValues, values);\n"
+" "
);
op = addOperation(result, "addChild" + StringUtils.capitalize(attrName), attrTypeDTO, ObjectModelJavaModifier.PUBLIC);
addParameter(op, attrTypeDTO, attrName);
StringBuilder buffercode = new StringBuilder();
buffercode.append(""
+"\n"
+" this."+attrName+".add("+attrName+");\n"
+" "
);
if (reverse != null && reverse.isNavigable()) {
String reverseAttrName = reverse.getName();
buffercode.append(""
+" "+attrName+".set"+StringUtils.capitalize(reverseAttrName)+"(this);\n"
+" "
);
}
buffercode.append(""
+" return "+attrName+";\n"
+" "
);
setOperationBody(op, buffercode.toString());
op = addOperation(result, "removeChild", "void");
addParameter(op, attrTypeDTO, attrName);
buffercode = new StringBuilder();
buffercode.append(""
+"\n"
+" this."+attrName+".remove("+attrName+");\n"
+" "
);
if (reverse != null && reverse.isNavigable()) {
String reverseAttrName = reverse.getName();
buffercode.append(""
+" "+attrName+".set"+StringUtils.capitalize(reverseAttrName)+"(null);\n"
+" "
);
}
setOperationBody(op, buffercode.toString());
} else {
String assocAttrName = GeneratorUtil.getAssocAttrName(attr);
String assocClassFQN = attr.getAssociationClass().getQualifiedName();
String nMultType;
if (JavaGeneratorUtil.isOrdered(attr)) {
nMultType = List.class.getName() + "<" + assocClassFQN + "DTO>";
} else {
nMultType = Collection.class.getName() + "<" + assocClassFQN + "DTO>";
}
if (log.isTraceEnabled()) {
log.trace("assocAttrName: " + assocAttrName);
}
op = addOperation(result, "set" + StringUtils.capitalize(assocAttrName), "void");
addParameter(op, nMultType, "values");
setOperationBody(op, ""
+"\n"
+" "+nMultType+" oldValues = this."+GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)+";\n"
+" this."+GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)+" = values;\n"
+" p.firePropertyChange(\""+attrName+"\", oldValues, values);\n"
+" "
);
}
if (!attr.hasAssociationClass()) {
String nMultType;
if (JavaGeneratorUtil.isOrdered(attr)) {
nMultType = List.class.getName() + "<" + attrTypeDTO + ">";
} else {
nMultType = Collection.class.getName() + "<" + attrTypeDTO + ">";
}
op = addOperation(result, "get" + StringUtils.capitalize(attrName), nMultType);
setOperationBody(op, ""
+"\n"
+" return this."+attrName+";\n"
+" "
);
} else {
String assocAttrName = GeneratorUtil.getAssocAttrName(attr);
String assocClassFQN = attr.getAssociationClass().getQualifiedName();
String nMultType;
if (JavaGeneratorUtil.isOrdered(attr)) {
nMultType = List.class.getName() + "<" + assocClassFQN + "DTO>";
} else {
nMultType = Collection.class.getName() + "<" + assocClassFQN + "DTO>";
}
if (log.isTraceEnabled()) {
log.trace("assocAttrName: " + assocAttrName);
}
op = addOperation(result, "get" + StringUtils.capitalize(assocAttrName), nMultType);
setOperationBody(op, ""
+"\n"
+" return this."+GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)+";\n"
+" "
);
}
}
}
op = addOperation(result, "toString", String.class, ObjectModelJavaModifier.PUBLIC);
StringBuilder buffer = new StringBuilder();
buffer.append(""
+"\n"
+" String result = new ToStringBuilder(this).\n"
+""
);
for (Object o : clazz.getAttributes()) {
ObjectModelAttribute attr = (ObjectModelAttribute) o;
if (!(attr.isNavigable()
|| attr.hasAssociationClass())) {
continue;
}
//FIXME possibilité de boucles (non directes)
ObjectModelClass attrEntity = null;
if (model.hasClass(attr.getType())) {
attrEntity = model.getClass(attr.getType());
}
boolean isDTO = attrEntity != null &&
templateHelper.isEntity(attrEntity); //THIMEL : STEREOTYPE ENTITY ???
ObjectModelAttribute reverse = attr.getReverseAttribute();
if (isDTO && (reverse == null || !reverse.isNavigable()) && !attr.hasAssociationClass() || !isDTO) {
String attrName = attr.getName();
buffer.append(""
+" append(\""+attrName+"\", this."+attrName+").\n"
+""
);
}
}
buffer.append(""
+" toString();\n"
+" return result;\n"
+" "
);
setOperationBody(op, buffer.toString());
}
public boolean isDTO(String type) {
ObjectModelClassifier clazz = model.getClassifier(type);
return clazz != null && TopiaStereoTypes.hasDtoStereotype(clazz);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy