dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #23783
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11588: extended hibgenstor/idobjman to support getting by shortName (fallback to name if class is idObje...
------------------------------------------------------------
revno: 11588
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-08-06 17:26:03 +0700
message:
extended hibgenstor/idobjman to support getting by shortName (fallback to name if class is idObject and not nameableObject), also added support in /api/{type}/query/{q}
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericIdentifiableObjectStore.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/PagerUtils.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java
dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java
--
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/common/GenericIdentifiableObjectStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericIdentifiableObjectStore.java 2013-03-22 09:48:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericIdentifiableObjectStore.java 2013-08-06 10:26:03 +0000
@@ -46,10 +46,10 @@
* @return the object with the given uid.
*/
T getByUid( String uid );
-
+
/**
* Retrieves the object with the given uid. Bypasses the ACL system.
- *
+ *
* @param uid the uid.
* @return the object with the given uid.
*/
@@ -131,6 +131,14 @@
List<T> getAllLikeName( String name );
/**
+ * Retrieves a List of objects where the shortName is like the given shortName.
+ *
+ * @param shortName the shortName.
+ * @return a List of objects.
+ */
+ List<T> getAllLikeShortName( String shortName );
+
+ /**
* Retrieves the objects determined by the given first result and max result.
* The returned list is ordered by the last updated property descending.
*
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java 2013-08-06 09:40:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java 2013-08-06 10:26:03 +0000
@@ -63,6 +63,8 @@
<T extends IdentifiableObject> Collection<T> getLikeName( Class<T> clazz, String name );
+ <T extends IdentifiableObject> Collection<T> getLikeShortName( Class<T> clazz, String shortName );
+
<T extends IdentifiableObject> List<T> getBetween( Class<T> clazz, int first, int max );
<T extends IdentifiableObject> List<T> getBetweenByName( Class<T> clazz, String name, int first, int max );
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/PagerUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/PagerUtils.java 2013-08-06 09:40:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/PagerUtils.java 2013-08-06 10:26:03 +0000
@@ -47,7 +47,7 @@
if ( offset >= objects.size() )
{
- offset = objects.size() - 1;
+ offset = objects.isEmpty() ? objects.size() : objects.size() - 1;
}
if ( (offset + limit) > objects.size() )
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java 2013-08-06 09:40:40 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java 2013-08-06 10:26:03 +0000
@@ -204,6 +204,7 @@
}
uniqueObjects.addAll( getLikeName( clazz, query ) );
+ uniqueObjects.addAll( getLikeShortName( clazz, query ) );
List<T> objects = new ArrayList<T>( uniqueObjects );
@@ -270,6 +271,20 @@
@Override
@SuppressWarnings( "unchecked" )
+ public <T extends IdentifiableObject> Collection<T> getLikeShortName( Class<T> clazz, String shortName )
+ {
+ GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );
+
+ if ( store == null )
+ {
+ return new ArrayList<T>();
+ }
+
+ return (Collection<T>) store.getAllLikeShortName( shortName );
+ }
+
+ @Override
+ @SuppressWarnings( "unchecked" )
public <T extends IdentifiableObject> List<T> getBetween( Class<T> clazz, int first, int max )
{
GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );
=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java 2013-07-19 09:29:54 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java 2013-08-06 10:26:03 +0000
@@ -40,6 +40,7 @@
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.GenericNameableObjectStore;
import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.NameableObject;
import org.hisp.dhis.common.SharingUtils;
import org.hisp.dhis.hibernate.exception.CreateAccessDeniedException;
import org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException;
@@ -567,6 +568,43 @@
@Override
@SuppressWarnings( "unchecked" )
+ public List<T> getAllLikeShortName( String shortName )
+ {
+ if ( NameableObject.class.isAssignableFrom( clazz ) )
+ {
+ Query query = sharingEnabled() ? getQueryAllLikeShortNameAcl( shortName ) : getQueryAllLikeShortName( shortName );
+ return query.list();
+ }
+
+ // fallback to using name
+ return getAllLikeName( shortName );
+ }
+
+ private Query getQueryAllLikeShortNameAcl( String shortName )
+ {
+ String hql = "select distinct c from " + clazz.getName() + " c"
+ + " where lower(shortName) like :shortName and ( c.publicAccess like 'r%' or c.user IS NULL or c.user=:user"
+ + " or exists "
+ + " (from c.userGroupAccesses uga join uga.userGroup ug join ug.members ugm where ugm = :user and uga.access like 'r%')"
+ + " ) order by c.shortName";
+
+ Query query = getQuery( hql );
+ query.setEntity( "user", currentUserService.getCurrentUser() );
+ query.setString( "shortName", "%" + shortName.toLowerCase() + "%" );
+
+ return query;
+ }
+
+ private Query getQueryAllLikeShortName( String shortName )
+ {
+ Query query = getQuery( "from " + clazz.getName() + " c where lower(shortName) like :shortName order by c.shortName" );
+ query.setString( "shortName", "%" + shortName.toLowerCase() + "%" );
+
+ return query;
+ }
+
+ @Override
+ @SuppressWarnings( "unchecked" )
public final List<T> getAllOrderedName()
{
Query query = sharingEnabled() ? getQueryAllOrderedNameAcl() : getQueryAllOrderedName();