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 74 * Persistence unit of the delegated EntityManager.
75 75 */
76 76 private String persistenceUnit;
  77 +
  78 + /**
  79 + * demoiselle-jpa configuration options
  80 + */
  81 + private EntityManagerConfig configuration;
77 82  
78 83 /**
79 84 * Constructor based on persistence unit name.
... ... @@ -113,7 +118,8 @@ public class EntityManagerProxy implements EntityManager, Serializable {
113 118 @Override
114 119 public <T> T merge(T entity) {
115 120 joinTransactionIfNecessary();
116   - checkEntityManagerScopePassivable(entity);
  121 + T managedEntity = getEntityManagerDelegate().merge(entity);
  122 + checkEntityManagerScopePassivable(managedEntity);
117 123 return getEntityManagerDelegate().merge(entity);
118 124 }
119 125  
... ... @@ -155,6 +161,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
155 161 @Override
156 162 public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode) {
157 163 joinTransactionIfNecessary();
  164 + checkEntityManagerScopePassivable(lockMode);
158 165 return getEntityManagerDelegate().find(entityClass, primaryKey, lockMode);
159 166 }
160 167  
... ... @@ -166,6 +173,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
166 173 @Override
167 174 public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String, Object> properties) {
168 175 joinTransactionIfNecessary();
  176 + checkEntityManagerScopePassivable(lockMode);
169 177 return getEntityManagerDelegate().find(entityClass, primaryKey, lockMode, properties);
170 178 }
171 179  
... ... @@ -213,6 +221,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
213 221 @Override
214 222 public void lock(Object entity, LockModeType lockMode) {
215 223 joinTransactionIfNecessary();
  224 + checkEntityManagerScopePassivable(lockMode);
216 225 getEntityManagerDelegate().lock(entity, lockMode);
217 226 }
218 227  
... ... @@ -223,6 +232,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
223 232 @Override
224 233 public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties) {
225 234 joinTransactionIfNecessary();
  235 + checkEntityManagerScopePassivable(lockMode);
226 236 getEntityManagerDelegate().lock(entity, lockMode, properties);
227 237 }
228 238  
... ... @@ -363,7 +373,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
363 373 */
364 374 @Override
365 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 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 549 EntityManagerConfig configuration = getConfiguration();
538 550 if (configuration.getEntityManagerScope()==EntityManagerScope.CONVERSATION
539 551 || configuration.getEntityManagerScope()==EntityManagerScope.SESSION
540 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 558 getLogger().error(message);
545 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 46 malformed-jpql=Consulta JPQL mal formada para pagina\u00E7\u00E3o de dados.
47 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 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 50 defining-entity-manager-scope=Definindo escopo [{0}] para produtor de Entity Manager
51 51 \ No newline at end of file
... ...
impl/extension/jpa/src/test/java/producer/ProducerTest.java
... ... @@ -22,7 +22,7 @@ public class ProducerTest {
22 22  
23 23 private static final String PATH = "src/test/resources/producer";
24 24  
25   - @Deployment//(name="request_scoped_producer")
  25 + @Deployment
26 26 public static WebArchive createDeployment() {
27 27 WebArchive deployment = Tests.createDeployment(ProducerTest.class);
28 28 deployment.addAsResource(Tests.createFileAsset(PATH + "/persistence.xml"), "META-INF/persistence.xml");
... ... @@ -31,15 +31,6 @@ public class ProducerTest {
31 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 34 @Test
44 35 public void produceEntityManager() {
45 36 EntityManager manager = Beans.getReference(EntityManager.class);
... ...