1 19 20 package org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator; 21 22 import org.netbeans.modules.j2ee.persistence.action.*; 23 import com.sun.source.tree.AnnotationTree; 24 import com.sun.source.tree.ClassTree; 25 import com.sun.source.tree.ExpressionTree; 26 import com.sun.source.tree.MethodTree; 27 import com.sun.source.tree.ModifiersTree; 28 import com.sun.source.tree.TypeParameterTree; 29 import java.util.Collections ; 30 import javax.lang.model.element.Modifier; 31 import javax.lang.model.type.TypeKind; 32 33 39 public final class ApplicationManagedResourceTransactionInJ2SE extends EntityManagerGenerationStrategySupport{ 40 41 public ClassTree generate() { 42 43 ModifiersTree methodModifiers = getTreeMaker().Modifiers( 44 Collections.<Modifier>singleton(Modifier.PUBLIC), 45 Collections.<AnnotationTree>emptyList() 46 ); 47 48 String text = 49 "javax.persistence.EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory(\"" + getPersistenceUnitName() + "\");\n" + 50 "javax.persistence.EntityManager em = emf.createEntityManager();\n" + 51 "em.getTransaction().begin();\n" + 52 "try {\n" + 53 generateCallLines() + 54 " em.getTransaction().commit();\n" + 55 "} catch (Exception e) {\n" + 56 " e.printStackTrace();\n" + 57 " em.getTransaction().rollback();\n" + 58 "} finally {\n" + 59 " em.close();\n" + 60 "}"; 61 62 MethodTree newMethod = getTreeMaker().Method( 63 methodModifiers, 64 computeMethodName(), 65 getTreeMaker().PrimitiveType(TypeKind.VOID), 66 Collections.<TypeParameterTree>emptyList(), 67 getParameterList(), 68 Collections.<ExpressionTree>emptyList(), 69 "{ " + text + "}", 70 null 71 ); 72 73 return getTreeMaker().addClassMember(getClassTree(), newMethod); 74 } 75 76 } 77 | Popular Tags |