Android Open Source - openhds-tablet Download Forms Task






From Project

Back to project page openhds-tablet.

License

The source code is released under:

OPENHDS PLATFORM OPENSOURCE LICENSE AGREEMENT Copyright (c) 2013 University of Southern Maine. All rights reserved. Redistribution and use in source and binary forms, with or without mo...

If you think the Android project openhds-tablet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.openhds.mobile.task;
//from   w  w  w  .  j a  va 2  s  .  c o m
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;
import org.openhds.mobile.database.DatabaseAdapter;
import org.openhds.mobile.model.FormSubmissionRecord;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
import org.openhds.mobile.BadXmlException;

/**
 * AsyncTask that fetches partial forms (and potentially associated validation
 * failure messages for those forms) and stores them in SQL Lite DB
 */
public class DownloadFormsTask extends AbstractHttpTask<Void, Void> {

  private DatabaseAdapter storage;


  public DownloadFormsTask(RequestContext requestCtx, TaskListener listener,
      Context context) {
    super(requestCtx, listener);
    storage = new DatabaseAdapter(context);
  }

  protected EndResult handleResponseData(HttpResponse response) {
    try {
      HttpEntity entity = response.getEntity();
      StringReader reader;
      reader = new StringReader(EntityUtils.toString(entity));
      List<FormSubmissionRecord> records = parseResponseXml(reader);
      for(FormSubmissionRecord rec : records) {
        rec.setFormOwnerId(requestCtx.user);
      }
      
      saveRecords(records);
      return EndResult.SUCCESS;
    } catch (ParseException e) {
      return EndResult.FAILURE;
    } catch (IOException e) {
      return EndResult.FAILURE;
    } catch (BadXmlException e) {
      return EndResult.FAILURE;
    }
  }

  private List<FormSubmissionRecord> parseResponseXml(StringReader reader)
      throws BadXmlException, IOException {
    try {
      XmlPullParser parser = buildXmlPullParser(reader);
      validateStartOfXmlDocument(parser);
      validateDocumentElement(parser);
      return parseSubmissionSet(parser);
    } catch (XmlPullParserException e) {
      throw new BadXmlException("Bad XML Document");
    }
  }

  private XmlPullParser buildXmlPullParser(StringReader reader)
      throws XmlPullParserException {
    XmlPullParser parser = XmlPullParserFactory.newInstance()
        .newPullParser();
    parser.setInput(reader);
    return parser;
  }

  private void validateStartOfXmlDocument(XmlPullParser parser)
      throws XmlPullParserException, BadXmlException, IOException {
    int eventType = parser.getEventType();
    if (eventType != XmlPullParser.START_DOCUMENT) {
      throw new BadXmlException("Start of document");
    }
  }

  private void validateDocumentElement(XmlPullParser parser)
      throws XmlPullParserException, IOException, BadXmlException {
    int eventType = parser.next();
    if (!isStartTag(eventType)
        && !"formSubmissionSet".equals(parser.getName())) {
      throw new BadXmlException("formSubmissionSet");
    }
  }

  private boolean isStartTag(int eventType) {
    return eventType == XmlPullParser.START_TAG;
  }

  private List<FormSubmissionRecord> parseSubmissionSet(XmlPullParser parser)
      throws XmlPullParserException, IOException, BadXmlException {
    List<FormSubmissionRecord> records = new ArrayList<FormSubmissionRecord>();
    int eventType = parser.next();
    if (!isStartTag(eventType) && !"submissions".equals(parser.getName())) {
      throw new BadXmlException("submissions");
    }
    while (!isEndTag(eventType) || !"submissions".equals(parser.getName())) {
      records.add(parseSubmission(parser));
      eventType = parser.next();
    }

    return records;
  }

  private boolean isEndTag(int eventType) {
    return eventType == XmlPullParser.END_TAG;
  }

  private FormSubmissionRecord parseSubmission(XmlPullParser parser)
      throws XmlPullParserException, IOException, BadXmlException {
    int eventType = parser.getEventType();
    if (!isStartTag(eventType)
        && !"formSubmission".equals(parser.getName())) {
      throw new BadXmlException("formSubmission");
    }

    FormSubmissionRecord record = new FormSubmissionRecord();

    while (!isEndTag(eventType)
        || !"formSubmission".equals(parser.getName())) {
      eventType = parser.next();
      if (isStartTag(eventType)
          && "formType".equals(parser.getName())) {
        checkTextPresent(parser);
        record.setFormType(parser.getText());
      } else if (isStartTag(eventType)
          && "formInstanceXml".equals(parser.getName())) {
        checkTextPresent(parser);
        record.setPartialForm(parser.getText());
      } else if (isStartTag(eventType)
          && "formErrors".equals(parser.getName())) {
        parseFormErrors(parser, record);
      } else if (isStartTag(eventType) && "formId".equals(parser.getName())) {
        checkTextPresent(parser);
        record.setFormId(parser.getText());
      } else if (isStartTag(eventType) && "id".equals(parser.getName())) {
        checkTextPresent(parser);
        try {
          record.setRemoteId(Integer.parseInt(parser.getText()));
        } catch(NumberFormatException e) { }
      }
    }

    return record;
  }

  private void checkTextPresent(XmlPullParser parser)
      throws XmlPullParserException, BadXmlException, IOException {
    int eventType = parser.next();
    if (eventType != XmlPullParser.TEXT) {
      throw new BadXmlException("formOwnerId");
    }

  }

  private void parseFormErrors(XmlPullParser parser,
      FormSubmissionRecord record) throws XmlPullParserException,
      IOException, BadXmlException {
    int eventType = parser.next();

    if (isEndTag(eventType)) {
      return; // no form errors
    }

    while (!isEndTag(eventType) || !"formErrors".equals(parser.getName())) {
      if (!isStartTag(eventType) && !"formError".equals(parser.getName())) {
        throw new BadXmlException("formErrors");
      }

      eventType = parser.next();
      if (!"error".equals(parser.getName())) {
        throw new BadXmlException("error");
      }

      eventType = parser.next();
      if (!isTextEvent(eventType)) {
        throw new BadXmlException("No error text");
      }

      record.addErrorMessage(parser.getText());

      parser.next(); // error tag
      parser.next(); // formError tag

      eventType = parser.next();
    }
  }

  private boolean isTextEvent(int eventType) {
    return eventType == XmlPullParser.TEXT;
  }

  private void saveRecords(List<FormSubmissionRecord> records) {
    for (FormSubmissionRecord record : records) {
      storage.saveFormSubmission(record);
    }
  }
}




Java Source Code List

org.openhds.mobile.BadXmlException.java
org.openhds.mobile.Converter.java
org.openhds.mobile.FieldWorkerProvider.java
org.openhds.mobile.FormsProviderAPI.java
org.openhds.mobile.InstanceProviderAPI.java
org.openhds.mobile.OpenHDS.java
org.openhds.mobile.Queries.java
org.openhds.mobile.activity.AbstractActivity.java
org.openhds.mobile.activity.FieldWorkerLoginActivity.java
org.openhds.mobile.activity.FilterActivity.java
org.openhds.mobile.activity.FilterFormActivity.java
org.openhds.mobile.activity.FilterLocationActivity.java
org.openhds.mobile.activity.FilterVisitActivity.java
org.openhds.mobile.activity.FormListActivity.java
org.openhds.mobile.activity.FormViewActivity.java
org.openhds.mobile.activity.OpeningActivity.java
org.openhds.mobile.activity.ServerPreferencesActivity.java
org.openhds.mobile.activity.ShowMapActivity.java
org.openhds.mobile.activity.SupervisorLoginActivity.java
org.openhds.mobile.activity.SupervisorMainActivity.java
org.openhds.mobile.activity.SyncDatabaseActivity.java
org.openhds.mobile.activity.UpdateActivity.java
org.openhds.mobile.adapter.AdapterContent.java
org.openhds.mobile.adapter.MapAdapter.java
org.openhds.mobile.database.DatabaseAdapter.java
org.openhds.mobile.database.DeathUpdate.java
org.openhds.mobile.database.ExternalInMigrationUpdate.java
org.openhds.mobile.database.HouseholdUpdate.java
org.openhds.mobile.database.InternalInMigrationUpdate.java
org.openhds.mobile.database.LocationUpdate.java
org.openhds.mobile.database.MembershipUpdate.java
org.openhds.mobile.database.OutMigrationUpdate.java
org.openhds.mobile.database.PregnancyOutcomeUpdate.java
org.openhds.mobile.database.RelationshipUpdate.java
org.openhds.mobile.database.Updatable.java
org.openhds.mobile.database.VisitUpdate.java
org.openhds.mobile.fragment.EventFragment.java
org.openhds.mobile.fragment.ProgressFragment.java
org.openhds.mobile.fragment.SelectionFilterFragment.java
org.openhds.mobile.fragment.SelectionFilterLocFragment.java
org.openhds.mobile.fragment.SelectionFormFragment.java
org.openhds.mobile.fragment.SelectionFragment.java
org.openhds.mobile.fragment.ValueFormFragment.java
org.openhds.mobile.fragment.ValueFragment.java
org.openhds.mobile.fragment.ValueLocFragment.java
org.openhds.mobile.listener.CollectEntitiesListener.java
org.openhds.mobile.listener.OdkFormLoadListener.java
org.openhds.mobile.listener.RetrieveFieldWorkersListener.java
org.openhds.mobile.listener.TaskCompleteListener.java
org.openhds.mobile.listener.ValueSelectedListener.java
org.openhds.mobile.model.Child.java
org.openhds.mobile.model.FieldWorker.java
org.openhds.mobile.model.FilledForm.java
org.openhds.mobile.model.FilledParams.java
org.openhds.mobile.model.FormFiller.java
org.openhds.mobile.model.FormSubmissionRecord.java
org.openhds.mobile.model.FormXmlReader.java
org.openhds.mobile.model.Form.java
org.openhds.mobile.model.HierarchySelection.java
org.openhds.mobile.model.Individual.java
org.openhds.mobile.model.LocationHierarchy.java
org.openhds.mobile.model.LocationVisit.java
org.openhds.mobile.model.Location.java
org.openhds.mobile.model.Membership.java
org.openhds.mobile.model.PregnancyObservationUpdate.java
org.openhds.mobile.model.PregnancyOutcome.java
org.openhds.mobile.model.Relationship.java
org.openhds.mobile.model.Result.java
org.openhds.mobile.model.Round.java
org.openhds.mobile.model.SocialGroup.java
org.openhds.mobile.model.StateMachine.java
org.openhds.mobile.model.Supervisor.java
org.openhds.mobile.model.UpdateEvent.java
org.openhds.mobile.model.UpdateParams.java
org.openhds.mobile.model.UpdateStatus.java
org.openhds.mobile.model.Visit.java
org.openhds.mobile.provider.OpenHDSProvider.java
org.openhds.mobile.task.AbstractHttpTask.java
org.openhds.mobile.task.AuthenticateTask.java
org.openhds.mobile.task.DownloadFormsTask.java
org.openhds.mobile.task.FieldWorkerLoginTask.java
org.openhds.mobile.task.OdkFormLoadTask.java
org.openhds.mobile.task.OdkGeneratedFormLoadTask.java
org.openhds.mobile.task.SupervisorLoginTask.java
org.openhds.mobile.task.SyncEntitiesTask.java
org.openhds.mobile.task.SyncFormsTask.java
org.openhds.mobile.utilities.L.java
org.openhds.mobile.utilities.Logg.java
org.openhds.mobile.utilities.UrlUtils.java