Commit 375d8777722dd816ab9cc95283f8b3f025295c6d

Authored by Dancovich
1 parent 5a4268c1
Exists in master

Implementando escopo selecionável no Entity Manager

impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/proxy/EntityManagerProxy.java
@@ -74,6 +74,11 @@ public class EntityManagerProxy implements EntityManager, Serializable { @@ -74,6 +74,11 @@ public class EntityManagerProxy implements EntityManager, Serializable {
74 * Persistence unit of the delegated EntityManager. 74 * Persistence unit of the delegated EntityManager.
75 */ 75 */
76 private String persistenceUnit; 76 private String persistenceUnit;
  77 +
  78 + /**
  79 + * demoiselle-jpa configuration options
  80 + */
  81 + private EntityManagerConfig configuration;
77 82
78 /** 83 /**
79 * Constructor based on persistence unit name. 84 * Constructor based on persistence unit name.
@@ -113,7 +118,8 @@ public class EntityManagerProxy implements EntityManager, Serializable { @@ -113,7 +118,8 @@ public class EntityManagerProxy implements EntityManager, Serializable {
113 @Override 118 @Override
114 public <T> T merge(T entity) { 119 public <T> T merge(T entity) {
115 joinTransactionIfNecessary(); 120 joinTransactionIfNecessary();
116 - checkEntityManagerScopePassivable(entity); 121 + T managedEntity = getEntityManagerDelegate().merge(entity);
  122 + checkEntityManagerScopePassivable(managedEntity);
117 return getEntityManagerDelegate().merge(entity); 123 return getEntityManagerDelegate().merge(entity);
118 } 124 }
119 125
@@ -155,6 +161,7 @@ public class EntityManagerProxy implements EntityManager, Serializable { @@ -155,6 +161,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
155 @Override 161 @Override
156 public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode) { 162 public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode) {
157 joinTransactionIfNecessary(); 163 joinTransactionIfNecessary();
  164 + checkEntityManagerScopePassivable(lockMode);
158 return getEntityManagerDelegate().find(entityClass, primaryKey, lockMode); 165 return getEntityManagerDelegate().find(entityClass, primaryKey, lockMode);
159 } 166 }
160 167
@@ -166,6 +173,7 @@ public class EntityManagerProxy implements EntityManager, Serializable { @@ -166,6 +173,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
166 @Override 173 @Override
167 public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String, Object> properties) { 174 public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String, Object> properties) {
168 joinTransactionIfNecessary(); 175 joinTransactionIfNecessary();
  176 + checkEntityManagerScopePassivable(lockMode);
169 return getEntityManagerDelegate().find(entityClass, primaryKey, lockMode, properties); 177 return getEntityManagerDelegate().find(entityClass, primaryKey, lockMode, properties);
170 } 178 }
171 179
@@ -213,6 +221,7 @@ public class EntityManagerProxy implements EntityManager, Serializable { @@ -213,6 +221,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
213 @Override 221 @Override
214 public void lock(Object entity, LockModeType lockMode) { 222 public void lock(Object entity, LockModeType lockMode) {
215 joinTransactionIfNecessary(); 223 joinTransactionIfNecessary();
  224 + checkEntityManagerScopePassivable(lockMode);
216 getEntityManagerDelegate().lock(entity, lockMode); 225 getEntityManagerDelegate().lock(entity, lockMode);
217 } 226 }
218 227
@@ -223,6 +232,7 @@ public class EntityManagerProxy implements EntityManager, Serializable { @@ -223,6 +232,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
223 @Override 232 @Override
224 public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties) { 233 public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties) {
225 joinTransactionIfNecessary(); 234 joinTransactionIfNecessary();
  235 + checkEntityManagerScopePassivable(lockMode);
226 getEntityManagerDelegate().lock(entity, lockMode, properties); 236 getEntityManagerDelegate().lock(entity, lockMode, properties);
227 } 237 }
228 238
@@ -363,7 +373,7 @@ public class EntityManagerProxy implements EntityManager, Serializable { @@ -363,7 +373,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
363 */ 373 */
364 @Override 374 @Override
365 public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) { 375 public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
366 - return getEntityManagerDelegate().createNamedQuery(name, resultClass); 376 + return new TypedQueryProxy<T>(getEntityManagerDelegate().createNamedQuery(name, resultClass),this);
367 } 377 }
368 378
369 /* 379 /*
@@ -521,29 +531,49 @@ public class EntityManagerProxy implements EntityManager, Serializable { @@ -521,29 +531,49 @@ public class EntityManagerProxy implements EntityManager, Serializable {
521 return getEntityManagerDelegate().toString(); 531 return getEntityManagerDelegate().toString();
522 } 532 }
523 533
524 - public EntityManagerConfig getConfiguration() {  
525 - return Beans.getReference(EntityManagerConfig.class);  
526 - }  
527 -  
528 - public Logger getLogger() {  
529 - return Beans.getReference(Logger.class);  
530 - }  
531 -  
532 - public ResourceBundle getBundle(){  
533 - return Beans.getReference(ResourceBundle.class,new NameQualifier("demoiselle-jpa-bundle")); 534 + private void checkEntityManagerScopePassivable(Object entity) {
  535 + EntityManagerConfig configuration = getConfiguration();
  536 + if (configuration.getEntityManagerScope()==EntityManagerScope.CONVERSATION
  537 + || configuration.getEntityManagerScope()==EntityManagerScope.SESSION
  538 + || configuration.getEntityManagerScope()==EntityManagerScope.VIEW){
  539 +
  540 + LockModeType lockMode = null;
  541 + if (getEntityManagerDelegate().contains(entity)){
  542 + lockMode = getEntityManagerDelegate().getLockMode(entity);
  543 + }
  544 + checkEntityManagerScopePassivable(lockMode);
  545 + }
534 } 546 }
535 547
536 - private void checkEntityManagerScopePassivable(Object entity) { 548 + private void checkEntityManagerScopePassivable(LockModeType lockMode) {
537 EntityManagerConfig configuration = getConfiguration(); 549 EntityManagerConfig configuration = getConfiguration();
538 if (configuration.getEntityManagerScope()==EntityManagerScope.CONVERSATION 550 if (configuration.getEntityManagerScope()==EntityManagerScope.CONVERSATION
539 || configuration.getEntityManagerScope()==EntityManagerScope.SESSION 551 || configuration.getEntityManagerScope()==EntityManagerScope.SESSION
540 || configuration.getEntityManagerScope()==EntityManagerScope.VIEW){ 552 || configuration.getEntityManagerScope()==EntityManagerScope.VIEW){
541 - LockModeType lockMode = getEntityManagerDelegate().getLockMode(entity);  
542 - if (lockMode != LockModeType.OPTIMISTIC_FORCE_INCREMENT && lockMode != LockModeType.WRITE){  
543 - String message = getBundle().getString("passivable-scope-with-pessimistic-lock" , configuration.getEntityManagerScope().toString()); 553 +
  554 + if (lockMode!=null
  555 + && lockMode!=LockModeType.NONE
  556 + && lockMode!=LockModeType.OPTIMISTIC_FORCE_INCREMENT){
  557 + String message = getBundle().getString("passivable-scope-without-optimistic-lock" , configuration.getEntityManagerScope().toString());
544 getLogger().error(message); 558 getLogger().error(message);
545 throw new DemoiselleException(message); 559 throw new DemoiselleException(message);
546 } 560 }
547 } 561 }
548 } 562 }
  563 +
  564 + private EntityManagerConfig getConfiguration(){
  565 + if (configuration==null){
  566 + configuration = Beans.getReference(EntityManagerConfig.class);
  567 + }
  568 +
  569 + return configuration;
  570 + }
  571 +
  572 + private Logger getLogger() {
  573 + return Beans.getReference(Logger.class);
  574 + }
  575 +
  576 + private ResourceBundle getBundle(){
  577 + return Beans.getReference(ResourceBundle.class,new NameQualifier("demoiselle-jpa-bundle"));
  578 + }
549 } 579 }
impl/extension/jpa/src/main/resources/demoiselle-jpa-bundle.properties
@@ -46,5 +46,5 @@ no-transaction-active=Nenhuma transa\u00E7\u00E3o est\u00E1 ativa, verifique a c @@ -46,5 +46,5 @@ no-transaction-active=Nenhuma transa\u00E7\u00E3o est\u00E1 ativa, verifique a c
46 malformed-jpql=Consulta JPQL mal formada para pagina\u00E7\u00E3o de dados. 46 malformed-jpql=Consulta JPQL mal formada para pagina\u00E7\u00E3o de dados.
47 invalid-scope-for-entity-manager=O escopo especificado para o Entity Manager \u00E9 inv\u00E1lido. Por favor informe um dos escopos v\u00E1lidos para a propriedade frameworkdemoiselle.persistence.entitymanager.scope\: request, session, view, conversation, application 47 invalid-scope-for-entity-manager=O escopo especificado para o Entity Manager \u00E9 inv\u00E1lido. Por favor informe um dos escopos v\u00E1lidos para a propriedade frameworkdemoiselle.persistence.entitymanager.scope\: request, session, view, conversation, application
48 entity-manager-scope-not-defined=N\u00E3o foi poss\u00EDvel ler o escopo configurado para o Entity Manager, usando o escopo padr\u00E3o [{0}] 48 entity-manager-scope-not-defined=N\u00E3o foi poss\u00EDvel ler o escopo configurado para o Entity Manager, usando o escopo padr\u00E3o [{0}]
49 -passivable-scope-with-pessimistic-lock=Entity Manager armazenado no escopo [{0}] requer o uso de trava otimista com vers\u00E3o (veja [LockModeType.OPTIMISTIC_FORCE_INCREMENT]) 49 +passivable-scope-without-optimistic-lock=Um Entity Manager armazenado no escopo [{0}] suporta apenas trava otimista com vers\u00E3o. Use o tipo adequado ou remova a trava. (veja [LockModeType.OPTIMISTIC_FORCE_INCREMENT])
50 defining-entity-manager-scope=Definindo escopo [{0}] para produtor de Entity Manager 50 defining-entity-manager-scope=Definindo escopo [{0}] para produtor de Entity Manager
51 \ No newline at end of file 51 \ No newline at end of file
impl/extension/jpa/src/test/java/producer/ProducerTest.java
@@ -22,7 +22,7 @@ public class ProducerTest { @@ -22,7 +22,7 @@ public class ProducerTest {
22 22
23 private static final String PATH = "src/test/resources/producer"; 23 private static final String PATH = "src/test/resources/producer";
24 24
25 - @Deployment//(name="request_scoped_producer") 25 + @Deployment
26 public static WebArchive createDeployment() { 26 public static WebArchive createDeployment() {
27 WebArchive deployment = Tests.createDeployment(ProducerTest.class); 27 WebArchive deployment = Tests.createDeployment(ProducerTest.class);
28 deployment.addAsResource(Tests.createFileAsset(PATH + "/persistence.xml"), "META-INF/persistence.xml"); 28 deployment.addAsResource(Tests.createFileAsset(PATH + "/persistence.xml"), "META-INF/persistence.xml");
@@ -31,15 +31,6 @@ public class ProducerTest { @@ -31,15 +31,6 @@ public class ProducerTest {
31 return deployment; 31 return deployment;
32 } 32 }
33 33
34 - /*@Deployment(name="no_scoped_producer")  
35 - public static WebArchive createNoScopedDeployment() {  
36 - WebArchive deployment = Tests.createDeployment(ProducerTest.class);  
37 - deployment.addAsResource(Tests.createFileAsset(PATH + "/persistence.xml"), "META-INF/persistence.xml");  
38 - deployment.addAsResource(Tests.createFileAsset(PATH + "/demoiselle_noscoped.properties"), "demoiselle.properties");  
39 -  
40 - return deployment;  
41 - }*/  
42 -  
43 @Test 34 @Test
44 public void produceEntityManager() { 35 public void produceEntityManager() {
45 EntityManager manager = Beans.getReference(EntityManager.class); 36 EntityManager manager = Beans.getReference(EntityManager.class);