← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 3230: Added method to MessageService

 

------------------------------------------------------------
revno: 3230
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2011-03-31 13:45:31 +0200
message:
  Added method to MessageService
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/UserMessageStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/hibernate/HibernateUserMessageStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/message/hibernate/Message.hbm.xml
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/message/hibernate/UserMessage.hbm.xml
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MessageServiceTest.java
  dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/ehcache.xml


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageService.java	2011-03-30 16:14:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageService.java	2011-03-31 11:45:31 +0000
@@ -51,4 +51,8 @@
     List<UserMessage> getUserMessages( int first, int max );
     
     List<UserMessage> getUserMessages( User user, int first, int max );
+    
+    long getUnreadMessageCount();
+    
+    long getUnreadMessageCount( User user );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/UserMessageStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/UserMessageStore.java	2011-03-30 16:14:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/UserMessageStore.java	2011-03-31 11:45:31 +0000
@@ -39,4 +39,6 @@
     extends GenericStore<UserMessage>
 {
     List<UserMessage> getUserMessages( User user, int first, int max );
+    
+    long getUnreadUserMessageCount( User user );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java	2011-03-30 16:14:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java	2011-03-31 11:45:31 +0000
@@ -106,4 +106,14 @@
     {
         return userMessageStore.getUserMessages( user, first, max );
     }
+    
+    public long getUnreadMessageCount()
+    {
+        return userMessageStore.getUnreadUserMessageCount( currentUserService.getCurrentUser() );
+    }
+    
+    public long getUnreadMessageCount( User user )
+    {
+        return userMessageStore.getUnreadUserMessageCount( user );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/hibernate/HibernateUserMessageStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/hibernate/HibernateUserMessageStore.java	2011-03-30 16:14:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/hibernate/HibernateUserMessageStore.java	2011-03-31 11:45:31 +0000
@@ -30,6 +30,7 @@
 import java.util.List;
 
 import org.hibernate.Criteria;
+import org.hibernate.Query;
 import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.hibernate.HibernateGenericStore;
@@ -46,13 +47,23 @@
     @SuppressWarnings("unchecked")
     public List<UserMessage> getUserMessages( User user, int first, int max )
     {
-        Criteria criteria = sessionFactory.getCurrentSession().createCriteria( UserMessage.class );
+        Criteria criteria = getCriteria( Restrictions.eq( "user", user ) );
         
-        criteria.add( Restrictions.eq( "user", user ) );
         criteria.setFirstResult( first );
         criteria.setMaxResults( max );
         criteria.addOrder( Order.desc( "messageDate" ) );
         
         return criteria.list();
     }
+    
+    public long getUnreadUserMessageCount( User user )
+    {
+        String hql = "select count(*) from UserMessage where user = :user and read = false";
+        
+        Query query = getQuery( hql );
+        query.setEntity( "user", user );
+        query.setCacheable( true );
+        
+        return (Long) query.uniqueResult();
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/message/hibernate/Message.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/message/hibernate/Message.hbm.xml	2011-03-30 16:14:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/message/hibernate/Message.hbm.xml	2011-03-31 11:45:31 +0000
@@ -5,6 +5,9 @@
 
 <hibernate-mapping>
   <class name="org.hisp.dhis.message.Message" table="message">
+  
+    <cache usage="read-write"/>
+
     <id name="id" column="messageid">
       <generator class="native"/>
     </id>

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/message/hibernate/UserMessage.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/message/hibernate/UserMessage.hbm.xml	2011-03-30 16:14:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/message/hibernate/UserMessage.hbm.xml	2011-03-31 11:45:31 +0000
@@ -5,6 +5,9 @@
 
 <hibernate-mapping>
   <class name="org.hisp.dhis.message.UserMessage" table="usermessage">
+
+    <cache usage="read-write"/>
+  
     <id name="id" column="usermessageid">
       <generator class="native"/>
     </id>

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MessageServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MessageServiceTest.java	2011-03-30 16:14:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MessageServiceTest.java	2011-03-31 11:45:31 +0000
@@ -150,4 +150,23 @@
 
         assertEquals( 0, messageA.getUserMessages().size() );
     }
+    
+    @Test
+    public void testGetUserMessagesCount()
+    {
+        messageService.saveMessage( messageA );
+        messageService.saveMessage( messageB );
+     
+        long count = messageService.getUnreadMessageCount( userA );
+        
+        assertEquals( 2, count );
+        
+        userMessageA.setRead( true );
+        
+        messageService.updateUserMessage( userMessageA );
+        
+        count = messageService.getUnreadMessageCount( userA );
+        
+        assertEquals( 1, count );
+    }
 }

=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/ehcache.xml'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/ehcache.xml	2009-11-12 17:59:58 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/ehcache.xml	2011-03-31 11:45:31 +0000
@@ -4,7 +4,7 @@
   <diskStore path="java.io.tmpdir"/>
 
   <defaultCache
-    maxElementsInMemory="40000"
+    maxElementsInMemory="300000"
     eternal="false"
     timeToIdleSeconds="360"
     timeToLiveSeconds="720"
@@ -67,11 +67,11 @@
   <!-- Hibernate Query Cache -->
   
   <cache name="org.hibernate.cache.StandardQueryCache"
-    maxElementsInMemory="8000"
+    maxElementsInMemory="200000"
   />
 
   <cache name="org.hibernate.cache.UpdateTimestampsCache"
-    maxElementsInMemory="200"
+    maxElementsInMemory="1000"
   />
 
 </ehcache>