All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.nakeduml.javageneration.auditing.AuditEntryGenerator Maven / Gradle / Ivy

The newest version!
package net.sf.nakeduml.javageneration.auditing;

import java.sql.Timestamp;
import java.util.List;

import net.sf.nakeduml.feature.visit.VisitAfter;
import net.sf.nakeduml.feature.visit.VisitBefore;
import net.sf.nakeduml.javageneration.AbstractJavaProducingVisitor;
import net.sf.nakeduml.javageneration.JavaTextSource;
import net.sf.nakeduml.javageneration.NakedStructuralFeatureMap;
import net.sf.nakeduml.javageneration.persistence.JpaAnnotator;
import net.sf.nakeduml.javageneration.util.OJUtil;
import net.sf.nakeduml.javageneration.util.ReflectionUtil;
import net.sf.nakeduml.javametamodel.OJBlock;
import net.sf.nakeduml.javametamodel.OJClass;
import net.sf.nakeduml.javametamodel.OJClassifier;
import net.sf.nakeduml.javametamodel.OJInterface;
import net.sf.nakeduml.javametamodel.OJOperation;
import net.sf.nakeduml.javametamodel.OJPackage;
import net.sf.nakeduml.javametamodel.OJPathName;
import net.sf.nakeduml.javametamodel.OJWhileStatement;
import net.sf.nakeduml.javametamodel.annotation.OJAnnotatedField;
import net.sf.nakeduml.javametamodel.annotation.OJAnnotatedOperation;
import net.sf.nakeduml.metamodel.core.INakedClassifier;
import net.sf.nakeduml.metamodel.core.INakedEntity;
import net.sf.nakeduml.metamodel.core.INakedEnumeration;
import net.sf.nakeduml.metamodel.core.INakedProperty;
import net.sf.nakeduml.metamodel.core.INakedSimpleType;
import net.sf.nakeduml.metamodel.core.INakedStructuredDataType;
import net.sf.nakeduml.metamodel.models.INakedModel;
import net.sf.nakeduml.util.AbstractEntity;
import net.sf.nakeduml.util.AbstractProcess;
import net.sf.nakeduml.util.AbstractUserRole;
import nl.klasse.octopus.codegen.umlToJava.maps.StructuralFeatureMap;
import nl.klasse.octopus.model.IAttribute;
import nl.klasse.octopus.model.IModelElement;

/**
 * This class builds all the operations specified by the AbstractEntity interface. It also provides an implementation for the equals method
 * that uses the id of the instance involved
 */
public class AuditEntryGenerator extends AbstractJavaProducingVisitor{
	OJInterface abstractEntry;
	private OJClass userEntity;
	@VisitBefore(matchSubclasses = true)
	public void visitPackage(INakedModel model){
		String modelName = model.getMappingInfo().getJavaName().getLowerCase().toString();
		OJPackage ojp = this.javaModel.findPackage(new OJPathName(modelName));
		if(ojp == null){
			ojp = new OJPackage();
			ojp.setName(modelName);
			this.javaModel.addToSubpackages(ojp);
		}
		this.abstractEntry = new OJInterface();
		this.abstractEntry.setName("Abstract" + model.getMappingInfo().getJavaName().getCapped() + "AuditEntry");
		ojp.addToInterfaces(this.abstractEntry);
		this.abstractEntry.addToSuperInterfaces(new OJPathName("net.sf.nakeduml.util.Audited"));
		addGetUser(this.abstractEntry, false);
		OJUtil.addProperty(this.abstractEntry, "changedOn", new OJPathName(Timestamp.class.getName()), false);
		OJUtil.addProperty(this.abstractEntry, "auditedAction", new OJPathName("String"), false);
		OJUtil.addProperty(this.abstractEntry, "objectVersion", new OJPathName("int"), false);
		OJClass auditDummyDataGenerator = new OJClass();
		auditDummyDataGenerator.setName(model.getMappingInfo().getJavaName().getCapped() + "AuditDummyDataGenerator");
		ojp.addToClasses(auditDummyDataGenerator);
		super.createTextPath(auditDummyDataGenerator, JavaTextSource.GEN_SRC);
		OJAnnotatedField allClasses = new OJAnnotatedField();
		allClasses.setName("ALL_CLASSES");
		allClasses.setType(new OJPathName("Class[]"));
		StringBuilder init = new StringBuilder("new Class[]{");
		List entityList = workspace.getClasses(INakedEntity.class);
		for(INakedEntity entity:entityList){
			if(!entity.getIsAbstract()){
				init.append(entity.getMappingInfo().getQualifiedJavaName());
				init.append(".class,");
			}
		}
		if(entityList.size() > 0){
			init.delete(init.length() - 1, init.length());
		}
		allClasses.setInitExp(init + "}");
		allClasses.setStatic(true);
		auditDummyDataGenerator.addToFields(allClasses);
		OJAnnotatedField allAuditClasses = new OJAnnotatedField();
		allAuditClasses.setName("ALL_AUDIT_CLASSES");
		allAuditClasses.setType(new OJPathName("Class[]"));
		init = new StringBuilder("new Class[]{");
		for(INakedEntity entity:entityList){
			if(!entity.getIsAbstract()){
				init.append(entity.getMappingInfo().getQualifiedJavaName());
				init.append("AuditEntry.class,");
			}
		}
		if(entityList.size() > 0){
			init.delete(init.length() - 1, init.length());
		}
		allAuditClasses.setInitExp(init + "}");
		allAuditClasses.setStatic(true);
		auditDummyDataGenerator.addToFields(allAuditClasses);
		auditDummyDataGenerator.addToImports(new OJPathName(AbstractEntity.class.getName()));
		auditDummyDataGenerator.addToImports(new OJPathName("net.sf.nakeduml.util.Audited"));
		auditDummyDataGenerator.addToImports("java.util.Iterator");
		OJOperation main = new OJAnnotatedOperation();
		main.setStatic(true);
		main.setName("main");
		main.addToThrows("Exception");
		main.addParam("args", new OJPathName("String[]"));
		OJBlock mainBlock = new OJBlock();
		main.setBody(mainBlock);
		auditDummyDataGenerator.addToOperations(main);
		addToLocals(auditDummyDataGenerator, main.getBody(), "cfg", "new Configuration()", "org.hibernate.cfg.Configuration");
		main.getBody().addToStatements("cfg.configure(Thread.currentThread().getContextClassLoader().getResource(\"hibernate.cfg.xml\"))");
		addToLocals(auditDummyDataGenerator, main.getBody(), "factory", "cfg.buildSessionFactory()", "org.hibernate.SessionFactory");
		addToLocals(auditDummyDataGenerator, main.getBody(), "session", "factory.openSession()", "org.hibernate.Session");
		addToLocals(auditDummyDataGenerator, main.getBody(), "tx", "session.beginTransaction()", "org.hibernate.Transaction");
//		addToLocals(auditDummyDataGenerator, main.getBody(), "user", "(" + getUserEntity().getPathName().toJavaString()
//				+ ")session.createQuery(\"from " + getUserEntity().getPathName().toJavaString() + "\").iterate().next()", getUserEntity()
//				.getPathName().toJavaString());
		addToLocals(auditDummyDataGenerator, main.getBody(), "i", "0", "int");
		OJWhileStatement ws = new OJWhileStatement();
		main.getBody().addToStatements(ws);
		ws.setCondition("i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy