Package org.hisp.dhis

Class BaseDhis2

java.lang.Object
org.hisp.dhis.BaseDhis2
Direct Known Subclasses:
Dhis2

public class BaseDhis2 extends Object
Author:
Lars Helge Overland
  • Field Details

    • FIELDS_PARAM

      protected static final String FIELDS_PARAM
      See Also:
    • SKIP_SHARING_PARAM

      protected static final String SKIP_SHARING_PARAM
      See Also:
    • ID_FIELDS

      protected static final String ID_FIELDS
      See Also:
    • NAME_FIELDS

      protected static final String NAME_FIELDS
    • OPTION_SET_FIELDS

      protected static final String OPTION_SET_FIELDS
      Option set fields.
    • DATA_ELEMENT_FIELDS

      protected static final String DATA_ELEMENT_FIELDS
      Data element fields.
    • DATA_ELEMENT_GROUP_SET_FIELDS

      protected static final String DATA_ELEMENT_GROUP_SET_FIELDS
      Data element group set fields.
    • CATEGORY_OPTION_FIELDS

      protected static final String CATEGORY_OPTION_FIELDS
      Category option fields.
    • CATEGORY_OPTION_COMBO_FIELDS

      protected static final String CATEGORY_OPTION_COMBO_FIELDS
      Category option combo fields.
    • CATEGORY_OPTION_GROUP_FIELDS

      protected static final String CATEGORY_OPTION_GROUP_FIELDS
      Category option group fields.
    • CATEGORY_OPTION_GROUP_SET_FIELDS

      protected static final String CATEGORY_OPTION_GROUP_SET_FIELDS
      Category option group set fields.
    • CATEGORY_COMBO_FIELDS

      protected static final String CATEGORY_COMBO_FIELDS
      Category combo fields.
    • CATEGORY_FIELDS

      protected static final String CATEGORY_FIELDS
      Category fields.
    • INDICATOR_TYPE_FIELDS

      protected static final String INDICATOR_TYPE_FIELDS
      Indicator type fields.
    • INDICATOR_FIELDS

      protected static final String INDICATOR_FIELDS
      Indicator fields.
    • INDICATOR_GROUP_SET_FIELDS

      protected static final String INDICATOR_GROUP_SET_FIELDS
      Indicator group set fields.
    • DATA_SET_FIELDS

      protected static final String DATA_SET_FIELDS
      Data set fields.
    • ORG_UNIT_FIELDS

      protected static final String ORG_UNIT_FIELDS
      Org unit fields.
    • ORG_UNIT_GROUP_SET_FIELDS

      protected static final String ORG_UNIT_GROUP_SET_FIELDS
      Org unit group set fields.
    • TRACKED_ENTITY_ATTRIBUTE_FIELDS

      protected static final String TRACKED_ENTITY_ATTRIBUTE_FIELDS
      Tracked entity attribute fields.
    • PROGRAM_STAGE_DATA_ELEMENT_FIELDS

      protected static final String PROGRAM_STAGE_DATA_ELEMENT_FIELDS
      Program stage data element fields.
    • TRACKED_ENTITY_TYPE_FIELDS

      protected static final String TRACKED_ENTITY_TYPE_FIELDS
      Tracked entity type fields.
    • PROGRAM_FIELDS

      protected static final String PROGRAM_FIELDS
      Program fields.
    • DASHBOARD_FIELDS

      protected static final String DASHBOARD_FIELDS
      Data element group set fields.
    • ME_FIELDS

      protected static final String ME_FIELDS
      Me / current user fields.
    • DATA_SET_PARAM

      protected static final String DATA_SET_PARAM
      Validation Rules fields.
      See Also:
    • VALIDATION_SIDE_FIELDS

      protected static final String VALIDATION_SIDE_FIELDS
      See Also:
    • VALIDATION_RULE_FIELDS

      protected static final String VALIDATION_RULE_FIELDS
    • DATA_SET_VALIDATION_FIELDS

      protected static final String DATA_SET_VALIDATION_FIELDS
    • config

      protected final Dhis2Config config
    • objectMapper

      protected final com.fasterxml.jackson.databind.ObjectMapper objectMapper
    • httpClient

      protected final org.apache.hc.client5.http.impl.classic.CloseableHttpClient httpClient
  • Constructor Details

  • Method Details

    • getObject

      protected <T> T getObject(org.apache.hc.core5.net.URIBuilder uriBuilder, Query query, Class<T> type)
      Retrieves an object using HTTP GET.
      Type Parameters:
      T - type.
      Parameters:
      uriBuilder - the URI builder.
      query - the Query filters to apply.
      type - the class type of the object.
      Returns:
      the object.
    • objectExists

      protected boolean objectExists(org.apache.hc.core5.net.URIBuilder uriBuilder)
      Indicates whether an object exists using HTTP HEAD.
      Parameters:
      uriBuilder - the URI builder.
      Returns:
      true if the object exists.
    • getObjectQuery

      protected URI getObjectQuery(org.apache.hc.core5.net.URIBuilder uriBuilder, Query query)
      Returns a URI based on the given query.
      Parameters:
      uriBuilder - the URI builder.
      query - the Query filters to apply.
      Returns:
      a URI.
    • getDataValueSetResponse

      protected <T> T getDataValueSetResponse(org.apache.hc.core5.net.URIBuilder uriBuilder, DataValueSetQuery query, Class<T> type)
      Retrieves a dataValueSet object using HTTP GET.
      Type Parameters:
      T - type.
      Parameters:
      uriBuilder - the URI builder.
      query - the DataValueSetQuery filters to apply.
      type - the class type of the object.
      Returns:
      the object.
    • getDataSetValidationResponse

      protected Validation getDataSetValidationResponse(org.apache.hc.core5.net.URIBuilder uriBuilder, DataSetValidationQuery query)
      Retrieves a Validation object using HTTP GET.
      Parameters:
      uriBuilder - the URI builder.
      query - the DataSetValidationQuery filters to apply.
      Returns:
      the object.
    • getDataValueFileResponse

      protected String getDataValueFileResponse(org.apache.hc.core5.net.URIBuilder uriBuilder, DataValueQuery query)
      Retrieves a dataValue object using HTTP GET.
      Parameters:
      uriBuilder - the URI builder.
      query - the DataValueSetQuery filters to apply.
      Returns:
      the object.
    • getAnalyticsResponse

      protected <T> T getAnalyticsResponse(org.apache.hc.core5.net.URIBuilder uriBuilder, AnalyticsQuery query, Class<T> type)
      Retrieves an analytics object using HTTP GET.
      Type Parameters:
      T - type.
      Parameters:
      uriBuilder - the URI builder.
      query - the AnalyticsQuery filters to apply.
      type - the class type of the object.
      Returns:
      the object.
    • getAnalyticsQuery

      protected URI getAnalyticsQuery(org.apache.hc.core5.net.URIBuilder uriBuilder, AnalyticsQuery query)
      Returns a URI based on the given analytics query.
      Parameters:
      uriBuilder - the URI builder.
      query - the AnalyticsQuery filters to apply.
      Returns:
      a URI.
    • getDataValueSetImportQuery

      protected URI getDataValueSetImportQuery(org.apache.hc.core5.net.URIBuilder uriBuilder, DataValueSetImportOptions options)
      Returns a URI based on the given data value set import options.
      Parameters:
      uriBuilder - the URI builder.
      options - the DataValueSetImportOptions to apply.
      Returns:
      a URI.
    • saveCompleteDataSetRegistrations

      protected CompleteDataSetRegistrationResponse saveCompleteDataSetRegistrations(org.apache.hc.core5.http.HttpEntity entity, CompleteDataSetRegistrationImportOptions options)
      Save complete data set registrations according to complete data set registration import options.
      Parameters:
      entity - the POST body HttpEntity.
      options - the CompleteDataSetRegistrationImportOptions import options.
      Returns:
      POST response body CompleteDataSetRegistrationResponse.
    • getEventsResponse

      protected EventsResult getEventsResponse(org.apache.hc.core5.net.URIBuilder uriBuilder, EventsQuery query)
      Retrieves events using HTTP GET.
      Parameters:
      uriBuilder - the URI builder.
      query - the EventsQuery.
      Returns:
      the Events object.
    • getEventsQuery

      protected URI getEventsQuery(org.apache.hc.core5.net.URIBuilder uriBuilder, EventsQuery query)
      Returns a URI based on the given events query.
      Parameters:
      uriBuilder - the URI builder.
      query - the EventsQuery.
      Returns:
      a URI.
    • getDataValueSetQuery

      protected URI getDataValueSetQuery(org.apache.hc.core5.net.URIBuilder uriBuilder, DataValueSetQuery query)
      Returns a URI based on the given dataValueSet query.
      Parameters:
      uriBuilder - the URI builder.
      query - the DataValueSetQuery.
      Returns:
      a URI.
    • getDataValueQuery

      protected URI getDataValueQuery(org.apache.hc.core5.net.URIBuilder uriBuilder, DataValueQuery query)
      Returns a URI based on the given dataValue query.
      Parameters:
      uriBuilder - the URI builder.
      query - the DataValueQuery.
      Returns:
      a URI.
    • getDataSetValidationQuery

      protected URI getDataSetValidationQuery(org.apache.hc.core5.net.URIBuilder uriBuilder, DataSetValidationQuery query)
      Returns a URI based on the given dataSetValidation query.
      Parameters:
      uriBuilder - the URI builder.
      query - the DataSetValidationQuery.
      Returns:
      a URI.
    • getCompleteDataSetRegistrationQuery

      protected URI getCompleteDataSetRegistrationQuery(org.apache.hc.core5.net.URIBuilder uriBuilder, CompleteDataSetRegistrationQuery query)
      Returns a URI based on the given complete dataset registration query.
      Parameters:
      uriBuilder - the URI builder.
      query - the CompleteDataSetRegistrationQuery.
      Returns:
      a URI.
    • getObject

      protected <T> T getObject(String path, String id, Class<T> type)
      Retrieves an object.
      Type Parameters:
      T - type.
      Parameters:
      path - the URL path.
      id - the object identifier.
      type - the class type of the object.
      Returns:
      the object.
      Throws:
      Dhis2ClientException - if access was denied or resource was not found.
    • executeJsonPostPutRequest

      protected <T extends BaseHttpResponse> T executeJsonPostPutRequest(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request, Object object, Class<T> type)
      Executes the given HttpUriRequestBase request, which may be a POST or PUT request.
      Type Parameters:
      T - class.
      Parameters:
      request - the request.
      object - the object to pass as JSON in the request body.
      type - the class type for the response entity.
      Returns:
      a Response.
      Throws:
      Dhis2ClientException - if access denied or resource not found.
    • executeRequest

      protected Response executeRequest(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request)
      Executes the given request without attempting to parse a response body and returns a response message.
      Parameters:
      request - the request.
      Returns:
      a response message.
      Throws:
      Dhis2ClientException - if access denied or resource not found.
    • getPostRequest

      protected org.apache.hc.client5.http.classic.methods.HttpPost getPostRequest(URI url, org.apache.hc.core5.http.HttpEntity entity)
      Returns a HTTP post request with JSON content type for the given URL and entity.
      Parameters:
      url - the URI.
      entity - the HttpEntity.
      Returns:
      a HttpPost request.
    • getObjectFromUrl

      protected <T> T getObjectFromUrl(URI url, Class<T> type)
      Retrieves an object using HTTP GET.
      Type Parameters:
      T - type.
      Parameters:
      url - the fully qualified URL.
      type - the class type of the object.
      Returns:
      the object.
      Throws:
      Dhis2ClientException - if access denied or resource not found.
    • getJsonHttpResponse

      protected org.apache.hc.client5.http.impl.classic.CloseableHttpResponse getJsonHttpResponse(URI url)
      Gets a CloseableHttpResponse for the given URL.
      Parameters:
      url - the URL.
      Returns:
      a CloseableHttpResponse.
    • readValue

      protected <T> T readValue(String content, Class<T> type) throws IOException
      Deserializes the given JSON content to an object of the given type.
      Type Parameters:
      T - type.
      Parameters:
      content - the JSON content.
      type - the object type.
      Returns:
      an object.
      Throws:
      IOException
    • writeToFile

      protected void writeToFile(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse response, File file)
      Write the given HttpResponse to the given File.
      Parameters:
      response - the response.
      file - the file to write the response to.
      Throws:
      Dhis2ClientException - if the write operation failed.
    • withAuth

      protected <T extends org.apache.hc.client5.http.classic.methods.HttpUriRequestBase> T withAuth(T request)
      Adds authentication to the given request.
      Type Parameters:
      T - class.
      Parameters:
      request - the HttpUriRequestBase.
      Returns:
      the request.
    • toJsonString

      protected String toJsonString(Object object)
      Serializes the given object to a JSON string.
      Parameters:
      object - the object to serialize.
      Returns:
      a JSON string representation of the object.
      Throws:
      Dhis2ClientException - if the serialization failed.
    • newDhis2ClientException

      protected Dhis2ClientException newDhis2ClientException(IOException ex)
      Returns a Dhis2ClientException based on the given exception.
      Parameters:
      ex - the exception.
      Returns:
      a Dhis2ClientException.
    • saveMetadataObject

      protected ObjectResponse saveMetadataObject(String path, IdentifiableObject object)
      Saves a metadata object using HTTP POST.
      Parameters:
      path - the URL path relative to the API end point.
      object - the object to save.
      Returns:
      ObjectResponse holding information about the operation.
      Throws:
      Dhis2ClientException - if the save operation failed due to client side error.
    • saveObject

      protected <T extends BaseHttpResponse> T saveObject(String path, Object object, Class<T> type)
      Saves an object using HTTP POST.
      Type Parameters:
      T - class.
      Parameters:
      path - the URL path relative to the API end point.
      object - the object to save.
      type - the class type for the response entity.
      Returns:
      object holding information about the operation.
      Throws:
      Dhis2ClientException - if the save operation failed due to client side error.
    • saveObject

      protected <T extends BaseHttpResponse> T saveObject(org.apache.hc.core5.net.URIBuilder uriBuilder, Object object, Class<T> type)
      Saves an object using HTTP POST.
      Type Parameters:
      T - class.
      Parameters:
      uriBuilder - the URI builder.
      object - the object to save.
      type - the class type for the response entity.
      Returns:
      object holding information about the operation.
      Throws:
      Dhis2ClientException - if the save operation failed due to client side error.
    • saveMetadataObjects

      protected ObjectsResponse saveMetadataObjects(Dhis2Objects objects)
      Saves or updates metadata objects.
      Parameters:
      objects - the Dhis2Objects.
      Returns:
      ObjectsResponse holding information about the operation.
    • updateMetadataObject

      protected ObjectResponse updateMetadataObject(String path, IdentifiableObject object)
      Updates an object using HTTP PUT.
      Parameters:
      path - the URL path relative to the API end point.
      object - the object to save.
      Returns:
      ObjectResponse holding information about the operation.
    • updateObject

      protected <T extends BaseHttpResponse> T updateObject(String path, Map<String,String> params, Object object, Class<T> type)
      Updates an object using HTTP PUT.
      Type Parameters:
      T - class.
      Parameters:
      path - the URL path relative to the API end point.
      params - the map of query parameter names and values to include in the URL.
      object - the object to save.
      type - the class type for the response entity.
      Returns:
      object holding information about the operation.
    • removeMetadataObject

      protected ObjectResponse removeMetadataObject(String path)
      Removes an object using HTTP DELETE.
      Parameters:
      path - the URL path relative to the API end point.
      Returns:
      ObjectResponse holding information about the operation.
    • removeObject

      protected <T extends BaseHttpResponse> T removeObject(String path, Class<T> type)
      Removes an object using HTTP DELETE.
      Type Parameters:
      T - class.
      Parameters:
      path - the URL path relative to the API end point.
      type - the class type for the response entity.
      Returns:
      object holding information about the operation.
    • getObject

      protected <T> T getObject(String path, Class<T> type)
      Retrieves an object using HTTP GET.
      Type Parameters:
      T - type.
      Parameters:
      path - the URL path relative to the API end point.
      type - the class type of the object.
      Returns:
      the object.
    • toNameValuePairs

      protected List<org.apache.hc.core5.http.NameValuePair> toNameValuePairs(Map<String,String> params)
      Converts the given map of query parameters to a list of name value pairs.
      Parameters:
      params - the Map with query parameter names and values.
      Returns:
      a list of NameValuePair.
    • addToCollection

      protected Response addToCollection(String path, String id, String collection, String item)
      Adds the item to the collection of the entity with the given identifier.
      Parameters:
      path - the object path in plural.
      id - the object identifier.
      collection - the collection path.
      item - the item identifier.
      Returns:
      a Response holding information about the operation.