private static void postTrackedEntityInstance(
      TrackedEntityInstance trackedEntityInstance, DhisApi dhisApi) throws APIException {
    try {
      Response response = dhisApi.postTrackedEntityInstance(trackedEntityInstance);
      if (response.getStatus() == 200) {
        ImportSummary importSummary = getImportSummary(response);
        handleImportSummary(
            importSummary, FailedItem.TRACKEDENTITYINSTANCE, trackedEntityInstance.getLocalId());
        if (ImportSummary.SUCCESS.equals(importSummary.getStatus())
            || ImportSummary.OK.equals(importSummary.getStatus())) {

          // change state and save trackedentityinstance

          // trackedEntityInstance.setState(State.SYNCED);
          trackedEntityInstance.setFromServer(true);
          trackedEntityInstance.save();

          clearFailedItem(FailedItem.TRACKEDENTITYINSTANCE, trackedEntityInstance.getLocalId());
          UpdateTrackedEntityInstanceTimestamp(trackedEntityInstance, dhisApi);
        }
      }
    } catch (APIException apiException) {
      NetworkUtils.handleTrackedEntityInstanceSendException(apiException, trackedEntityInstance);
    }
  }
 static void sendTrackedEntityInstanceChanges(
     DhisApi dhisApi, TrackedEntityInstance trackedEntityInstance, boolean sendEnrollments)
     throws APIException {
   if (trackedEntityInstance == null) {
     return;
   }
   if (trackedEntityInstance.getCreated() == null) {
     postTrackedEntityInstance(trackedEntityInstance, dhisApi);
   } else {
     putTrackedEntityInstance(trackedEntityInstance, dhisApi);
   }
   if (sendEnrollments) {
     List<Enrollment> enrollments = TrackerController.getEnrollments(trackedEntityInstance);
     sendEnrollmentChanges(dhisApi, enrollments, sendEnrollments);
   }
 }
  private static void UpdateTrackedEntityInstanceTimestamp(
      TrackedEntityInstance trackedEntityInstance, DhisApi dhisApi) throws APIException {
    try {
      final Map<String, String> QUERY_PARAMS = new HashMap<>();
      QUERY_PARAMS.put("fields", "created,lastUpdated");
      TrackedEntityInstance updatedTrackedEntityInstance =
          dhisApi.getTrackedEntityInstance(
              trackedEntityInstance.getTrackedEntityInstance(), QUERY_PARAMS);

      // merging updated timestamp to local trackedentityinstance model
      trackedEntityInstance.setCreated(updatedTrackedEntityInstance.getCreated());
      trackedEntityInstance.setLastUpdated(updatedTrackedEntityInstance.getLastUpdated());
      trackedEntityInstance.save();
    } catch (APIException apiException) {
      NetworkUtils.handleApiException(apiException);
    }
  }