------------------------------------------------------------ revno: 19532 committer: Morten Olav Hansen <morte...@gmail.com> branch nick: dhis2 timestamp: Fri 2015-07-03 14:33:54 +0700 message: wip, TEI query for /api/trackedEntityInstance endpoint, returns full TEI objects (dxf2 variant), old grid query is now only available on /api/trackedEntityInstances/query modified: dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceDeletionHandler.java dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstance.java dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.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/trackedentity/TrackedEntityInstanceService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java 2015-05-01 14:30:18 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java 2015-07-03 07:33:54 +0000 @@ -28,11 +28,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Set; - import org.hisp.dhis.common.Grid; import org.hisp.dhis.common.IllegalQueryException; import org.hisp.dhis.common.OrganisationUnitSelectionMode; @@ -43,17 +38,22 @@ import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; import org.hisp.dhis.validation.ValidationCriteria; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.Set; + /** * <p>This interface is responsible for retrieving tracked entity instances (TEI). * The query methods accepts a TrackedEntityInstanceQueryParams object which - * encapsulates all arguments.</p> - * - * <p>The TEIs are returned as a Grid object, which is a two-dimensional list with + * encapsulates all arguments.</p> + * <p/> + * <p>The TEIs are returned as a Grid object, which is a two-dimensional list with * headers. The TEI attribute values are returned in the same order as specified * in the arguments. The grid has a set of columns which are always present * starting at index 0, followed by attributes specified for the query. All * values in the grid are of type String. The order is:</p> - * + * <p/> * <ul> * <li>0: Tracked entity instance UID</li> * <li>1: Created time stamp</li> @@ -61,11 +61,11 @@ * <li>3: Organisation unit UID</li> * <li>4: Tracked entity UID</li> * <ul> - * + * <p/> * <p>Attributes specified in the query follows on the next column indexes. - * Example usage for retrieving TEIs with two attributes using one attribute as + * Example usage for retrieving TEIs with two attributes using one attribute as * filter:</p> - * + * <p/> * <pre> * <code> * TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams(); @@ -74,9 +74,9 @@ * params.addAttribute( new QueryItem( age, QueryOperator.LT, "5", true ) ); * params.addFilter( new QueryItem( weight, QueryOperator.GT, "2500", true ) ); * params.addOrganistionUnit( unit ); - * - * Grid instances = teiService.getTrackedEntityInstances( params ); - * + * + * Grid instances = teiService.getTrackedEntityInstancesGrid( params ); + * * for ( List<Object> row : instances.getRows() ) * { * String tei = row.get( 0 ); @@ -86,7 +86,7 @@ * } * </code> * </pre> - * + * * @author Abyot Asalefew Gizaw * @author Lars Helge Overland */ @@ -97,43 +97,45 @@ final int ERROR_NONE = 0; final int ERROR_DUPLICATE_IDENTIFIER = 1; final int ERROR_ENROLLMENT = 2; - + final String SEPARATOR = "_"; - + final String F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS = "F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS"; /** * Returns a grid with tracked entity instance values based on the given * TrackedEntityInstanceQueryParams. - * + * * @param params the TrackedEntityInstanceQueryParams. * @return a grid. */ - Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params ); + Grid getTrackedEntityInstancesGrid( TrackedEntityInstanceQueryParams params ); + + List<TrackedEntityInstance> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params ); /** * Returns a TrackedEntityInstanceQueryParams based on the given input. - * - * @param query the query string. - * @param attribute the set of attributes. - * @param filter the set of filters. - * @param ou the set of organisatio unit identifiers. - * @param ouMode the OrganisationUnitSelectionMode. - * @param program the Program identifier. - * @param programStatus the ProgramStatus in the given orogram. - * @param followUp indicates follow up status in the given Program. + * + * @param query the query string. + * @param attribute the set of attributes. + * @param filter the set of filters. + * @param ou the set of organisatio unit identifiers. + * @param ouMode the OrganisationUnitSelectionMode. + * @param program the Program identifier. + * @param programStatus the ProgramStatus in the given orogram. + * @param followUp indicates follow up status in the given Program. * @param programStartDate the start date for enrollment in the given - * Program. - * @param programEndDate the end date for enrollment in the given Program. - * @param trackedEntity the TrackedEntity uid. - * @param eventStatus the event status for the given Program. - * @param eventStartDate the event start date for the given Program. - * @param eventEndDate the event end date for the given Program. - * @param skipMeta indicates whether to include meta data in the response. - * @param page the page number. - * @param pageSize the page size. - * @param totalPages indicates whether to include the total number of pages. - * @param skipPaging whether to skip paging. + * Program. + * @param programEndDate the end date for enrollment in the given Program. + * @param trackedEntity the TrackedEntity uid. + * @param eventStatus the event status for the given Program. + * @param eventStartDate the event start date for the given Program. + * @param eventEndDate the event end date for the given Program. + * @param skipMeta indicates whether to include meta data in the response. + * @param page the page number. + * @param pageSize the page size. + * @param totalPages indicates whether to include the total number of pages. + * @param skipPaging whether to skip paging. * @return a TrackedEntityInstanceQueryParams. */ TrackedEntityInstanceQueryParams getFromUrl( String query, Set<String> attribute, Set<String> filter, @@ -144,16 +146,16 @@ /** * Decides whether current user is authorized to perform the given query. * IllegalQueryException is thrown if not. - * + * * @param params the TrackedEntityInstanceQueryParams. */ void decideAccess( TrackedEntityInstanceQueryParams params ); - + /** * Validates the given TrackedEntityInstanceQueryParams. The params is * considered valid if no exception are thrown and the method returns * normally. - * + * * @param params the TrackedEntityInstanceQueryParams. * @throws IllegalQueryException if the given params is invalid. */ @@ -162,53 +164,50 @@ /** * Adds an {@link TrackedEntityInstance} - * + * * @param entityInstance The to TrackedEntityInstance add. - * * @return A generated unique id of the added {@link TrackedEntityInstance}. */ int addTrackedEntityInstance( TrackedEntityInstance entityInstance ); /** * Deletes a {@link TrackedEntityInstance}. - * + * * @param entityInstance the TrackedEntityInstance to delete. */ void deleteTrackedEntityInstance( TrackedEntityInstance entityInstance ); /** * Updates a {@link TrackedEntityInstance}. - * + * * @param entityInstance the TrackedEntityInstance to update. */ void updateTrackedEntityInstance( TrackedEntityInstance entityInstance ); /** * Returns a {@link TrackedEntityInstance}. - * + * * @param id the id of the TrackedEntityInstanceAttribute to return. - * * @return the TrackedEntityInstanceAttribute with the given id */ TrackedEntityInstance getTrackedEntityInstance( int id ); /** * Returns the {@link TrackedEntityAttribute} with the given UID. - * + * * @param uid the UID. * @return the TrackedEntityInstanceAttribute with the given UID, or null if - * no match. + * no match. */ TrackedEntityInstance getTrackedEntityInstance( String uid ); /** * Register a new entityInstance - * - * @param entityInstance TrackedEntityInstance - * @param representativeId The uid of entityInstance who is representative + * + * @param entityInstance TrackedEntityInstance + * @param representativeId The uid of entityInstance who is representative * @param relationshipTypeId The id of relationship type defined - * @param attributeValues Set of attribute values - * + * @param attributeValues Set of attribute values * @return The error code after registering entityInstance */ int createTrackedEntityInstance( TrackedEntityInstance entityInstance, String representativeId, @@ -216,14 +215,13 @@ /** * Update information of an entityInstance existed - * - * @param entityInstance TrackedEntityInstance - * @param representativeId The id of representative of this entityInstance + * + * @param entityInstance TrackedEntityInstance + * @param representativeId The id of representative of this entityInstance * @param relationshipTypeId The id of relationship type of this person - * @param valuesForSave The entityInstance attribute values for adding - * @param valuesForUpdate The entityInstance attribute values for updating - * @param valuesForDelete The entityInstance attribute values for deleting - * + * @param valuesForSave The entityInstance attribute values for adding + * @param valuesForUpdate The entityInstance attribute values for updating + * @param valuesForDelete The entityInstance attribute values for deleting */ void updateTrackedEntityInstance( TrackedEntityInstance entityInstance, String representativeId, Integer relationshipTypeId, List<TrackedEntityAttributeValue> valuesForSave, @@ -232,26 +230,25 @@ /** * Validate entityInstance attributes and validation criteria by program * before registering or updating information - * + * * @param entityInstance TrackedEntityInstance object - * @param program Program which person needs to enroll. If this parameter is - * null, the system check unique attribute values of the - * entityInstance - * @param format I18nFormat + * @param program Program which person needs to enroll. If this parameter is + * null, the system check unique attribute values of the + * entityInstance + * @param format I18nFormat * @return Error code 0 : Validation is OK 1_<duplicate-value> : The - * attribute value is duplicated 2_<validation-criteria-id> : - * Violate validation criteria of the program + * attribute value is duplicated 2_<validation-criteria-id> : + * Violate validation criteria of the program */ String validateTrackedEntityInstance( TrackedEntityInstance entityInstance, Program program, I18nFormat format ); /** * Validate tracked entity instance enrollment - * + * * @param entityInstance TrackedEntityInstance object - * @param program Program which person needs to enroll. If this parameter is - * null, the system check identifiers of the tracked entity instance - * @param format I18nFormat - * + * @param program Program which person needs to enroll. If this parameter is + * null, the system check identifiers of the tracked entity instance + * @param format I18nFormat * @return ValidationCriteria object which is violated */ ValidationCriteria validateEnrollment( TrackedEntityInstance entityInstance, Program program, I18nFormat format ); === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java 2015-01-17 07:41:26 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java 2015-07-03 07:33:54 +0000 @@ -28,13 +28,13 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import java.util.List; -import java.util.Map; - import org.hisp.dhis.common.GenericIdentifiableObjectStore; import org.hisp.dhis.program.Program; import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; +import java.util.List; +import java.util.Map; + /** * @author Abyot Asalefew Gizaw * @version $Id$ @@ -46,13 +46,15 @@ final int MAX_RESULTS = 50000; - List<Map<String, String>> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params ); + List<TrackedEntityInstance> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params ); + + List<Map<String, String>> getTrackedEntityInstancesGrid( TrackedEntityInstanceQueryParams params ); int getTrackedEntityInstanceCount( TrackedEntityInstanceQueryParams params ); /** * Returns null if valid, a descriptive, non-null string if invalid. - * + * * @param instance * @param attributeValue * @param program === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java 2015-06-23 15:59:19 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java 2015-07-03 07:33:54 +0000 @@ -28,23 +28,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.CREATED_ID; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.LAST_UPDATED_ID; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.META_DATA_NAMES_KEY; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.ORG_UNIT_ID; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.PAGER_META_KEY; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.TRACKED_ENTITY_ID; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.TRACKED_ENTITY_INSTANCE_ID; -import static org.hisp.dhis.common.OrganisationUnitSelectionMode.*; - -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hisp.dhis.common.DimensionalObjectUtils; @@ -75,6 +58,18 @@ import org.hisp.dhis.validation.ValidationCriteria; import org.springframework.transaction.annotation.Transactional; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.hisp.dhis.common.OrganisationUnitSelectionMode.ACCESSIBLE; +import static org.hisp.dhis.common.OrganisationUnitSelectionMode.ALL; +import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.*; + /** * @author Abyot Asalefew Gizaw */ @@ -143,7 +138,7 @@ { this.organisationUnitService = organisationUnitService; } - + private CurrentUserService currentUserService; public void setCurrentUserService( CurrentUserService currentUserService ) @@ -155,13 +150,45 @@ // Implementation methods // ------------------------------------------------------------------------- + @Override + public List<TrackedEntityInstance> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params ) + { + decideAccess( params ); + validate( params ); + + // --------------------------------------------------------------------- + // Verify params + // --------------------------------------------------------------------- + + User user = currentUserService.getCurrentUser(); + + if ( user != null && params.isOrganisationUnitMode( OrganisationUnitSelectionMode.ACCESSIBLE ) ) + { + params.setOrganisationUnits( user.getDataViewOrganisationUnitsWithFallback() ); + params.setOrganisationUnitMode( OrganisationUnitSelectionMode.DESCENDANTS ); + } + + for ( OrganisationUnit organisationUnit : params.getOrganisationUnits() ) + { + if ( !organisationUnit.hasLevel() ) + { + organisationUnit.setLevel( organisationUnitService.getLevelOfOrganisationUnit( organisationUnit.getId() ) ); + } + } + + if ( !params.isPaging() && !params.isSkipPaging() ) + { + params.setDefaultPaging(); + } + + return trackedEntityInstanceStore.getTrackedEntityInstances( params ); + } + // TODO lower index on attribute value? - @Override - public Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params ) + public Grid getTrackedEntityInstancesGrid( TrackedEntityInstanceQueryParams params ) { decideAccess( params ); - validate( params ); // --------------------------------------------------------------------- @@ -169,9 +196,9 @@ // --------------------------------------------------------------------- User user = currentUserService.getCurrentUser(); - + if ( user != null && params.isOrganisationUnitMode( OrganisationUnitSelectionMode.ACCESSIBLE ) ) - { + { params.setOrganisationUnits( user.getDataViewOrganisationUnitsWithFallback() ); params.setOrganisationUnitMode( OrganisationUnitSelectionMode.DESCENDANTS ); } @@ -183,7 +210,7 @@ organisationUnit.setLevel( organisationUnitService.getLevelOfOrganisationUnit( organisationUnit.getId() ) ); } } - + if ( !params.isPaging() && !params.isSkipPaging() ) { params.setDefaultPaging(); @@ -234,8 +261,8 @@ { grid.addHeader( new GridHeader( item.getItem().getUid(), item.getItem().getName() ) ); } - - List<Map<String, String>> entities = trackedEntityInstanceStore.getTrackedEntityInstances( params ); + + List<Map<String, String>> entities = trackedEntityInstanceStore.getTrackedEntityInstancesGrid( params ); // --------------------------------------------------------------------- // Grid rows @@ -265,12 +292,12 @@ if ( params.isPaging() ) { int count = 0; - + if ( params.isTotalPages() ) { count = trackedEntityInstanceStore.getTrackedEntityInstanceCount( params ); } - + Pager pager = new Pager( params.getPageWithDefault(), count, params.getPageSizeWithDefault() ); metaData.put( PAGER_META_KEY, pager ); } @@ -302,7 +329,7 @@ throw new IllegalQueryException( "Current user is not authorized to query across all organisation units" ); } } - + @Override public void validate( TrackedEntityInstanceQueryParams params ) throws IllegalQueryException @@ -315,13 +342,13 @@ } User user = currentUserService.getCurrentUser(); - - if ( !params.hasOrganisationUnits() && !( params.isOrganisationUnitMode( ALL ) || params.isOrganisationUnitMode( ACCESSIBLE ) ) ) + + if ( !params.hasOrganisationUnits() && !(params.isOrganisationUnitMode( ALL ) || params.isOrganisationUnitMode( ACCESSIBLE )) ) { violation = "At least one organisation unit must be specified"; } - - if ( params.isOrganisationUnitMode( ACCESSIBLE ) && ( user == null || !user.hasDataViewOrganisationUnitWithFallback() ) ) + + if ( params.isOrganisationUnitMode( ACCESSIBLE ) && (user == null || !user.hasDataViewOrganisationUnitWithFallback()) ) { violation = "Current user must be associated with at least one organisation unit when selection mode is ACCESSIBLE"; } @@ -351,7 +378,7 @@ violation = "Program must be defined when program end date is specified"; } - if ( params.hasEventStatus() && ( !params.hasEventStartDate() || !params.hasEventEndDate() ) ) + if ( params.hasEventStatus() && (!params.hasEventStartDate() || !params.hasEventEndDate()) ) { violation = "Event start and end date must be specified when event status is specified"; } @@ -388,7 +415,7 @@ TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams(); QueryFilter queryFilter = getQueryFilter( query ); - + if ( attribute != null ) { for ( String attr : attribute ) @@ -398,17 +425,17 @@ params.getAttributes().add( it ); } } - + if ( filter != null ) { for ( String filt : filter ) { QueryItem it = getQueryItem( filt ); - + params.getFilters().add( it ); } } - + if ( ou != null ) { for ( String orgUnit : ou ) @@ -467,22 +494,22 @@ { String[] split = item.split( DimensionalObjectUtils.DIMENSION_NAME_SEP ); - if ( split == null || ( split.length % 2 != 1 ) ) + if ( split == null || (split.length % 2 != 1) ) { throw new IllegalQueryException( "Query item or filter is invalid: " + item ); } - + QueryItem queryItem = getItem( split[0] ); - + if ( split.length > 1 ) // Filters specified { for ( int i = 1; i < split.length; i += 2 ) { QueryOperator operator = QueryOperator.fromString( split[i] ); - queryItem.getFilters().add( new QueryFilter( operator, split[i+1] ) ); - } + queryItem.getFilters().add( new QueryFilter( operator, split[i + 1] ) ); + } } - + return queryItem; } @@ -494,10 +521,10 @@ { throw new IllegalQueryException( "Attribute does not exist: " + item ); } - + return new QueryItem( at, null, at.getValueType(), at.getOptionSet() ); } - + /** * Creates a QueryFilter from the given query string. Query is on format * {operator}:{filter-value}. Only the filter-value is mandatory. The EQ @@ -509,7 +536,7 @@ { return null; } - + if ( !query.contains( DimensionalObjectUtils.DIMENSION_NAME_SEP ) ) { return new QueryFilter( QueryOperator.EQ, query ); @@ -517,14 +544,14 @@ else { String[] split = query.split( DimensionalObjectUtils.DIMENSION_NAME_SEP ); - + if ( split == null || split.length != 2 ) { throw new IllegalQueryException( "Query has invalid format: " + query ); } - + QueryOperator op = QueryOperator.fromString( split[0] ); - + return new QueryFilter( op, split[1] ); } } @@ -554,7 +581,7 @@ if ( representativeId != null ) { TrackedEntityInstance representative = trackedEntityInstanceStore.getByUid( representativeId ); - + if ( representative != null ) { instance.setRepresentative( representative ); @@ -566,7 +593,7 @@ if ( relationshipTypeId != null ) { RelationshipType relType = relationshipTypeService.getRelationshipType( relationshipTypeId ); - + if ( relType != null ) { rel.setRelationshipType( relType ); @@ -659,9 +686,9 @@ return false; } - return instance.getRepresentative() == null || !(instance.getRepresentative().getUid().equals( representativeId ) ); + return instance.getRepresentative() == null || !(instance.getRepresentative().getUid().equals( representativeId )); } - + @Override public String validateTrackedEntityInstance( TrackedEntityInstance instance, Program program, I18nFormat format ) { @@ -681,7 +708,7 @@ for ( TrackedEntityAttributeValue attributeValue : instance.getAttributeValues() ) { String valid = trackedEntityInstanceStore.validate( instance, attributeValue, program ); - + if ( valid != null ) { return valid; @@ -691,19 +718,19 @@ return TrackedEntityInstanceService.ERROR_NONE + ""; } - + @Override public ValidationCriteria validateEnrollment( TrackedEntityInstance instance, Program program, I18nFormat format ) { for ( ValidationCriteria criteria : program.getValidationCriteria() ) - { + { for ( TrackedEntityAttributeValue attributeValue : instance.getAttributeValues() ) { if ( attributeValue.getAttribute().getUid().equals( criteria.getProperty() ) ) { String value = attributeValue.getValue(); String type = attributeValue.getAttribute().getValueType(); - + if ( type.equals( TrackedEntityAttribute.TYPE_NUMBER ) ) { int value1 = Integer.parseInt( value ); @@ -721,7 +748,7 @@ Date value1 = format.parseDate( value ); Date value2 = format.parseDate( criteria.getValue() ); int i = value1.compareTo( value2 ); - + if ( i != criteria.getOperator() ) { return criteria; === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceDeletionHandler.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceDeletionHandler.java 2015-06-23 15:59:19 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceDeletionHandler.java 2015-07-03 07:33:54 +0000 @@ -76,7 +76,7 @@ { TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams(); params.addOrganisationUnit( unit ); - Grid grid = instanceService.getTrackedEntityInstances( params ); + Grid grid = instanceService.getTrackedEntityInstancesGrid( params ); return grid.getHeight() == 0 ? null : ERROR; } === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java 2015-06-23 15:59:19 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java 2015-07-03 07:33:54 +0000 @@ -28,30 +28,11 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import static org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers; -import static org.hisp.dhis.system.util.DateUtils.getMediumDateString; -import static org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString; -import static org.hisp.dhis.commons.util.TextUtils.getTokens; -import static org.hisp.dhis.commons.util.TextUtils.removeLastAnd; -import static org.hisp.dhis.commons.util.TextUtils.removeLastComma; -import static org.hisp.dhis.commons.util.TextUtils.removeLastOr; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.CREATED_ID; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.LAST_UPDATED_ID; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.ORG_UNIT_ID; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.TRACKED_ENTITY_ID; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.TRACKED_ENTITY_INSTANCE_ID; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER; -import static org.hisp.dhis.trackedentity.TrackedEntityInstanceService.SEPARATOR; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.Criteria; +import org.hibernate.Query; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hisp.dhis.common.OrganisationUnitSelectionMode; @@ -60,13 +41,13 @@ import org.hisp.dhis.common.QueryOperator; import org.hisp.dhis.common.SetMap; import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore; +import org.hisp.dhis.commons.util.SqlHelper; import org.hisp.dhis.event.EventStatus; import org.hisp.dhis.jdbc.StatementBuilder; import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramInstance; import org.hisp.dhis.program.ProgramStatus; -import org.hisp.dhis.commons.util.SqlHelper; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.trackedentity.TrackedEntityInstance; import org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams; @@ -75,6 +56,19 @@ import org.springframework.jdbc.support.rowset.SqlRowSet; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers; +import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids; +import static org.hisp.dhis.commons.util.TextUtils.*; +import static org.hisp.dhis.system.util.DateUtils.getMediumDateString; +import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.*; +import static org.hisp.dhis.trackedentity.TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER; +import static org.hisp.dhis.trackedentity.TrackedEntityInstanceService.SEPARATOR; + /** * @author Abyot Asalefew Gizaw */ @@ -84,7 +78,7 @@ implements TrackedEntityInstanceStore { private static final Log log = LogFactory.getLog( HibernateTrackedEntityInstanceStore.class ); - + private static final Map<ProgramStatus, Integer> PROGRAM_STATUS_MAP = new HashMap<ProgramStatus, Integer>() { { @@ -110,7 +104,70 @@ // ------------------------------------------------------------------------- @Override - public List<Map<String, String>> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params ) + @SuppressWarnings( "unchecked" ) + public List<TrackedEntityInstance> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params ) + { + String hql = buildTrackedEntityInstanceHql( params ); + Query query = getQuery( hql ); + + if ( params.isPaging() ) + { + query.setFirstResult( params.getOffset() ); + query.setMaxResults( params.getPageSizeWithDefault() ); + } + + return query.list(); + } + + private String buildTrackedEntityInstanceHql( TrackedEntityInstanceQueryParams params ) + { + String hql = "from TrackedEntityInstance tei"; + + if ( params.hasTrackedEntity() || params.hasOrganisationUnits() || params.hasFilters() ) + { + hql += " where "; + } + + if ( params.hasTrackedEntity() ) + { + hql += "tei.trackedEntity.uid='" + params.getTrackedEntity().getUid() + "'"; + hql += params.hasOrganisationUnits() ? " and " : ""; + } + + if ( params.hasOrganisationUnits() ) + { + hql += "tei.organisationUnit.uid in (" + getQuotedCommaDelimitedString( getUids( params.getOrganisationUnits() ) ) + ")"; + } + + if ( params.hasFilters() ) + { + for ( QueryItem queryItem : params.getFilters() ) + { + for ( QueryFilter queryFilter : queryItem.getFilters() ) + { + String filter = queryFilter.getSqlFilter( StringUtils.lowerCase( queryFilter.getFilter() ) ); + + hql += " and exists (from TrackedEntityAttributeValue teav where teav.entityInstance=tei"; + hql += " and teav.attribute.uid='" + queryItem.getItemId() + "'"; + + if ( queryItem.isNumeric() ) + { + hql += " and teav.value " + queryFilter.getSqlOperator() + filter + ")"; + } + else + { + hql += " and lower(teav.value) " + queryFilter.getSqlOperator() + filter + ")"; + } + + } + } + } + + return hql; + } + + @Override + public List<Map<String, String>> getTrackedEntityInstancesGrid( TrackedEntityInstanceQueryParams params ) { SqlHelper hlp = new SqlHelper(); @@ -387,7 +444,7 @@ public String validate( TrackedEntityInstance instance, TrackedEntityAttributeValue attributeValue, Program program ) { TrackedEntityAttribute attribute = attributeValue.getAttribute(); - + try { if ( attribute.isUnique() ) === modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java' --- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java 2015-07-02 07:05:55 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java 2015-07-03 07:33:54 +0000 @@ -677,7 +677,7 @@ params.setOrganisationUnitMode( OrganisationUnitSelectionMode.ALL ); } - Grid instances = teiService.getTrackedEntityInstances( params ); + Grid instances = teiService.getTrackedEntityInstancesGrid( params ); if ( instances.getHeight() == 0 || (instances.getHeight() == 1 && instances.getRow( 0 ).contains( tei.getUid() )) ) === modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java' --- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java 2015-06-29 06:42:08 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java 2015-07-03 07:33:54 +0000 @@ -145,6 +145,7 @@ trackedEntityInstance.setOrgUnit( entityInstance.getOrganisationUnit().getUid() ); trackedEntityInstance.setTrackedEntity( entityInstance.getTrackedEntity().getUid() ); trackedEntityInstance.setCreated( entityInstance.getCreated().toString() ); + trackedEntityInstance.setLastUpdated( entityInstance.getLastUpdated().toString() ); Collection<Relationship> relationships = relationshipService.getRelationshipsForTrackedEntityInstance( entityInstance ); @@ -443,7 +444,7 @@ params.setOrganisationUnitMode( OrganisationUnitSelectionMode.ALL ); } - Grid instances = teiService.getTrackedEntityInstances( params ); + Grid instances = teiService.getTrackedEntityInstancesGrid( params ); if ( instances.getHeight() == 0 || (tei != null && instances.getHeight() == 1 && instances.getRow( 0 ).contains( tei.getUid() )) ) { === modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstance.java' --- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstance.java 2015-02-19 09:18:17 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstance.java 2015-07-03 07:33:54 +0000 @@ -53,6 +53,8 @@ private String created; + private String lastUpdated; + private List<Relationship> relationships = new ArrayList<>(); private List<Attribute> attributes = new ArrayList<>(); @@ -123,6 +125,18 @@ this.created = created; } + @JsonProperty( required = true ) + @JacksonXmlProperty( isAttribute = true ) + public String getLastUpdated() + { + return lastUpdated; + } + + public void setLastUpdated( String lastUpdated ) + { + this.lastUpdated = lastUpdated; + } + @JsonProperty @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 ) public List<Relationship> getRelationships() === modified file 'dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java' --- dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java 2015-07-02 07:05:55 +0000 +++ dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java 2015-07-03 07:33:54 +0000 @@ -317,7 +317,7 @@ TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams(); param.addOrganisationUnit( unit ); - Grid trackedEntityDrid = entityInstanceService.getTrackedEntityInstances( param ); + Grid trackedEntityDrid = entityInstanceService.getTrackedEntityInstancesGrid( param ); List<List<Object>> entityInstanceList = trackedEntityDrid.getRows(); for ( List<Object> entityInstance : entityInstanceList ) @@ -354,7 +354,7 @@ TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams(); param.addOrganisationUnit( unit ); - Grid trackedEntityDrid = entityInstanceService.getTrackedEntityInstances( param ); + Grid trackedEntityDrid = entityInstanceService.getTrackedEntityInstancesGrid( param ); List<List<Object>> entityInstanceList = trackedEntityDrid.getRows(); for ( List<Object> entityInstance : entityInstanceList ) @@ -1747,7 +1747,7 @@ } } - Grid trackedEntityInstanceGrid = entityInstanceService.getTrackedEntityInstances( param ); + Grid trackedEntityInstanceGrid = entityInstanceService.getTrackedEntityInstancesGrid( param ); List<List<Object>> listOfTrackedEntityInstance = trackedEntityInstanceGrid.getRows(); if ( listOfTrackedEntityInstance.size() == 0 ) @@ -1861,7 +1861,7 @@ param.setEventStartDate( fromDate ); param.setEventEndDate( toDate ); - Grid programStageInstanceGrid = entityInstanceService.getTrackedEntityInstances( param ); + Grid programStageInstanceGrid = entityInstanceService.getTrackedEntityInstancesGrid( param ); List<List<Object>> rows = programStageInstanceGrid.getRows(); if ( rows.size() == 0 ) @@ -2191,7 +2191,7 @@ param.setEventStartDate( fromDate ); param.setEventEndDate( toDate ); - Grid programStageInstanceGrid = entityInstanceService.getTrackedEntityInstances( param ); + Grid programStageInstanceGrid = entityInstanceService.getTrackedEntityInstancesGrid( param ); List<List<Object>> listOfListProgramStageInstance = programStageInstanceGrid.getRows(); if ( listOfListProgramStageInstance.size() == 0 ) === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java 2015-06-29 03:35:07 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java 2015-07-03 07:33:54 +0000 @@ -42,6 +42,7 @@ import org.hisp.dhis.dxf2.importsummary.ImportSummary; import org.hisp.dhis.event.EventStatus; import org.hisp.dhis.importexport.ImportStrategy; +import org.hisp.dhis.node.NodeUtils; import org.hisp.dhis.node.types.CollectionNode; import org.hisp.dhis.node.types.RootNode; import org.hisp.dhis.program.ProgramStatus; @@ -100,7 +101,51 @@ // READ // ------------------------------------------------------------------------- - @RequestMapping( value = { "", "/query" }, method = RequestMethod.GET, produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_JAVASCRIPT } ) + @RequestMapping( value = "", method = RequestMethod.GET ) + public @ResponseBody RootNode getTrackedEntityInstances( + @RequestParam( required = false ) String query, + @RequestParam( required = false ) Set<String> attribute, + @RequestParam( required = false ) Set<String> filter, + @RequestParam( required = false ) String ou, + @RequestParam( required = false ) OrganisationUnitSelectionMode ouMode, + @RequestParam( required = false ) String program, + @RequestParam( required = false ) ProgramStatus programStatus, + @RequestParam( required = false ) Boolean followUp, + @RequestParam( required = false ) Date programStartDate, + @RequestParam( required = false ) Date programEndDate, + @RequestParam( required = false ) String trackedEntity, + @RequestParam( required = false ) EventStatus eventStatus, + @RequestParam( required = false ) Date eventStartDate, + @RequestParam( required = false ) Date eventEndDate, + @RequestParam( required = false ) boolean skipMeta, + @RequestParam( required = false ) Integer page, + @RequestParam( required = false ) Integer pageSize, + @RequestParam( required = false ) boolean totalPages, + @RequestParam( required = false ) boolean skipPaging, + HttpServletResponse response ) throws Exception + { + List<String> fields = Lists.newArrayList( contextService.getParameterValues( "fields" ) ); + + if ( fields.isEmpty() ) + { + fields.add( ":all" ); + } + + Set<String> orgUnits = ContextUtils.getQueryParamValues( ou ); + TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, attribute, filter, orgUnits, ouMode, + program, programStatus, followUp, programStartDate, programEndDate, trackedEntity, + eventStatus, eventStartDate, eventEndDate, skipMeta, page, pageSize, totalPages, skipPaging ); + + contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE ); + List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances( instanceService.getTrackedEntityInstances( params ) ); + + RootNode rootNode = NodeUtils.createMetadata(); + rootNode.addChild( fieldFilterService.filter( TrackedEntityInstance.class, trackedEntityInstances, fields ) ); + + return rootNode; + } + + @RequestMapping( value = "/query", method = RequestMethod.GET, produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_JAVASCRIPT } ) public String queryTrackedEntityInstancesJson( @RequestParam( required = false ) String query, @RequestParam( required = false ) Set<String> attribute, @@ -130,14 +175,14 @@ eventStatus, eventStartDate, eventEndDate, skipMeta, page, pageSize, totalPages, skipPaging ); contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE ); - Grid grid = instanceService.getTrackedEntityInstances( params ); + Grid grid = instanceService.getTrackedEntityInstancesGrid( params ); model.addAttribute( "model", grid ); model.addAttribute( "viewClass", "detailed" ); return "grid"; } - @RequestMapping( value = { "", "/query" }, method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_XML ) + @RequestMapping( value = "/query", method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_XML ) public void queryTrackedEntityInstancesXml( @RequestParam( required = false ) String query, @RequestParam( required = false ) Set<String> attribute, @@ -167,7 +212,7 @@ eventStatus, eventStartDate, eventEndDate, skipMeta, page, pageSize, totalPages, skipPaging ); contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_XML, CacheStrategy.NO_CACHE ); - Grid grid = instanceService.getTrackedEntityInstances( params ); + Grid grid = instanceService.getTrackedEntityInstancesGrid( params ); GridUtils.toXml( grid, response.getOutputStream() ); } @@ -201,7 +246,7 @@ eventStatus, eventStartDate, eventEndDate, skipMeta, page, pageSize, totalPages, skipPaging ); contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.NO_CACHE ); - Grid grid = instanceService.getTrackedEntityInstances( params ); + Grid grid = instanceService.getTrackedEntityInstancesGrid( params ); GridUtils.toXls( grid, response.getOutputStream() ); } @@ -235,7 +280,7 @@ eventStatus, eventStartDate, eventEndDate, skipMeta, page, pageSize, totalPages, skipPaging ); contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, CacheStrategy.NO_CACHE ); - Grid grid = instanceService.getTrackedEntityInstances( params ); + Grid grid = instanceService.getTrackedEntityInstancesGrid( params ); GridUtils.toCsv( grid, response.getOutputStream() ); }
_______________________________________________ Mailing list: https://launchpad.net/~dhis2-devs Post to : dhis2-devs@lists.launchpad.net Unsubscribe : https://launchpad.net/~dhis2-devs More help : https://help.launchpad.net/ListHelp