Search in sources :

Example 1 with ServerFormDetails

use of org.odk.collect.android.formmanagement.ServerFormDetails in project collect by opendatakit.

the class ServerFormsSynchronizer method synchronize.

public void synchronize() throws FormSourceException {
    List<ServerFormDetails> formList = serverFormsDetailsFetcher.fetchFormDetails();
    List<Form> formsOnDevice = formsRepository.getAll();
    FormDeleter formDeleter = new FormDeleter(formsRepository, instancesRepository);
    formsOnDevice.stream().forEach(form -> {
        if (formList.stream().noneMatch(f -> form.getFormId().equals(f.getFormId()))) {
            formDeleter.delete(form.getDbId());
        }
    });
    boolean downloadException = false;
    for (ServerFormDetails form : formList) {
        if (form.isNotOnDevice() || form.isUpdated()) {
            try {
                formDownloader.downloadForm(form, null, null);
            } catch (FormDownloadException.DownloadingInterrupted e) {
                return;
            } catch (FormDownloadException e) {
                downloadException = true;
            }
        }
    }
    if (downloadException) {
        throw new FormSourceException.FetchError();
    }
}
Also used : ServerFormDetails(org.odk.collect.android.formmanagement.ServerFormDetails) Form(org.odk.collect.forms.Form) FormDeleter(org.odk.collect.android.formmanagement.FormDeleter) FormDownloadException(org.odk.collect.android.formmanagement.FormDownloadException)

Example 2 with ServerFormDetails

use of org.odk.collect.android.formmanagement.ServerFormDetails in project collect by opendatakit.

the class DownloadFormListTask method onPostExecute.

@Override
protected void onPostExecute(Pair<List<ServerFormDetails>, FormSourceException> result) {
    synchronized (this) {
        if (stateListener != null) {
            if (result.first != null) {
                HashMap<String, ServerFormDetails> detailsHashMap = new HashMap<>();
                for (ServerFormDetails details : result.first) {
                    detailsHashMap.put(details.getFormId(), details);
                }
                stateListener.formListDownloadingComplete(detailsHashMap, result.second);
            } else {
                stateListener.formListDownloadingComplete(null, result.second);
            }
        }
    }
}
Also used : ServerFormDetails(org.odk.collect.android.formmanagement.ServerFormDetails) HashMap(java.util.HashMap)

Example 3 with ServerFormDetails

use of org.odk.collect.android.formmanagement.ServerFormDetails in project collect by opendatakit.

the class FormDownloadListActivity method init.

private void init(Bundle savedInstanceState) {
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        if (bundle.containsKey(DISPLAY_ONLY_UPDATED_FORMS)) {
            displayOnlyUpdatedForms = (boolean) bundle.get(DISPLAY_ONLY_UPDATED_FORMS);
        }
        if (bundle.containsKey(ApplicationConstants.BundleKeys.FORM_IDS)) {
            viewModel.setDownloadOnlyMode(true);
            viewModel.setFormIdsToDownload(bundle.getStringArray(ApplicationConstants.BundleKeys.FORM_IDS));
            if (viewModel.getFormIdsToDownload() == null) {
                setReturnResult(false, "Form Ids is null", null);
                finish();
            }
            if (bundle.containsKey(ApplicationConstants.BundleKeys.URL)) {
                viewModel.setUrl(bundle.getString(ApplicationConstants.BundleKeys.URL));
                if (bundle.containsKey(ApplicationConstants.BundleKeys.USERNAME) && bundle.containsKey(ApplicationConstants.BundleKeys.PASSWORD)) {
                    viewModel.setUsername(bundle.getString(ApplicationConstants.BundleKeys.USERNAME));
                    viewModel.setPassword(bundle.getString(ApplicationConstants.BundleKeys.PASSWORD));
                }
            }
        }
    }
    downloadButton = findViewById(R.id.add_button);
    downloadButton.setEnabled(listView.getCheckedItemCount() > 0);
    downloadButton.setOnClickListener(v -> {
        ArrayList<ServerFormDetails> filesToDownload = getFilesToDownload();
        startFormsDownload(filesToDownload);
    });
    toggleButton = findViewById(R.id.toggle_button);
    toggleButton.setEnabled(false);
    toggleButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            downloadButton.setEnabled(toggleChecked(listView));
            toggleButtonLabel(toggleButton, listView);
            viewModel.clearSelectedFormIds();
            if (listView.getCheckedItemCount() == listView.getCount()) {
                for (HashMap<String, String> map : viewModel.getFormList()) {
                    viewModel.addSelectedFormId(map.get(FORMDETAIL_KEY));
                }
            }
        }
    });
    Button refreshButton = findViewById(R.id.refresh_button);
    refreshButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            viewModel.setLoadingCanceled(false);
            viewModel.clearFormList();
            updateAdapter();
            clearChoices();
            downloadFormList();
        }
    });
    if (savedInstanceState != null) {
        // Android should keep track of this, but broken on rotate...
        if (savedInstanceState.containsKey(BUNDLE_SELECTED_COUNT)) {
            downloadButton.setEnabled(savedInstanceState.getInt(BUNDLE_SELECTED_COUNT) > 0);
        }
    }
    filteredFormList.addAll(viewModel.getFormList());
    if (getLastCustomNonConfigurationInstance() instanceof DownloadFormListTask) {
        downloadFormListTask = (DownloadFormListTask) getLastCustomNonConfigurationInstance();
        if (downloadFormListTask.getStatus() == AsyncTask.Status.FINISHED) {
            DialogFragmentUtils.dismissDialog(RefreshFormListDialogFragment.class, getSupportFragmentManager());
            downloadFormsTask = null;
        }
    } else if (getLastCustomNonConfigurationInstance() instanceof DownloadFormsTask) {
        downloadFormsTask = (DownloadFormsTask) getLastCustomNonConfigurationInstance();
        if (downloadFormsTask.getStatus() == AsyncTask.Status.FINISHED) {
            DialogFragmentUtils.dismissDialog(RefreshFormListDialogFragment.class, getSupportFragmentManager());
            downloadFormsTask = null;
        }
    } else if (viewModel.getFormDetailsByFormId().isEmpty() && getLastCustomNonConfigurationInstance() == null && !viewModel.wasLoadingCanceled()) {
        // first time, so get the formlist
        downloadFormList();
    }
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setItemsCanFocus(false);
    sortingOptions = new int[] { R.string.sort_by_name_asc, R.string.sort_by_name_desc };
}
Also used : ServerFormDetails(org.odk.collect.android.formmanagement.ServerFormDetails) HashMap(java.util.HashMap) Button(android.widget.Button) Bundle(android.os.Bundle) DownloadFormsTask(org.odk.collect.android.tasks.DownloadFormsTask) OnClickListener(android.view.View.OnClickListener) DownloadFormListTask(org.odk.collect.android.tasks.DownloadFormListTask) RefreshFormListDialogFragment(org.odk.collect.android.formentry.RefreshFormListDialogFragment) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView)

Example 4 with ServerFormDetails

use of org.odk.collect.android.formmanagement.ServerFormDetails in project collect by opendatakit.

the class FormDownloadListActivity method formsDownloadingComplete.

@Override
public void formsDownloadingComplete(Map<ServerFormDetails, FormDownloadException> result) {
    if (downloadFormsTask != null) {
        downloadFormsTask.setDownloaderListener(null);
    }
    cleanUpWebCredentials();
    DialogFragmentUtils.dismissDialog(RefreshFormListDialogFragment.class, getSupportFragmentManager());
    Bundle args = new Bundle();
    args.putSerializable(FormsDownloadResultDialog.ARG_RESULT, (Serializable) result);
    DialogFragmentUtils.showIfNotShowing(FormsDownloadResultDialog.class, args, getSupportFragmentManager());
    // Set result to true for forms which were downloaded
    if (viewModel.isDownloadOnlyMode()) {
        for (ServerFormDetails serverFormDetails : result.keySet()) {
            if (result.get(serverFormDetails) == null) {
                if (viewModel.getFormResults().containsKey(serverFormDetails.getFormId())) {
                    viewModel.putFormResult(serverFormDetails.getFormId(), true);
                }
            }
        }
        setReturnResult(true, null, viewModel.getFormResults());
    }
}
Also used : ServerFormDetails(org.odk.collect.android.formmanagement.ServerFormDetails) Bundle(android.os.Bundle)

Example 5 with ServerFormDetails

use of org.odk.collect.android.formmanagement.ServerFormDetails in project collect by opendatakit.

the class FormDownloadListActivity method performDownloadModeDownload.

private void performDownloadModeDownload() {
    for (String formId : viewModel.getFormIdsToDownload()) {
        viewModel.putFormResult(formId, false);
    }
    ArrayList<ServerFormDetails> filesToDownload = new ArrayList<>();
    for (ServerFormDetails serverFormDetails : viewModel.getFormDetailsByFormId().values()) {
        String formId = serverFormDetails.getFormId();
        if (viewModel.getFormResults().containsKey(formId)) {
            filesToDownload.add(serverFormDetails);
        }
    }
    // 2. Select forms and start downloading
    if (!filesToDownload.isEmpty()) {
        startFormsDownload(filesToDownload);
    } else {
        // None of the forms was found
        setReturnResult(false, "Forms not found on server", viewModel.getFormResults());
        finish();
    }
}
Also used : ServerFormDetails(org.odk.collect.android.formmanagement.ServerFormDetails) ArrayList(java.util.ArrayList)

Aggregations

ServerFormDetails (org.odk.collect.android.formmanagement.ServerFormDetails)9 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)3 Bundle (android.os.Bundle)2 FormDownloadException (org.odk.collect.android.formmanagement.FormDownloadException)2 SparseBooleanArray (android.util.SparseBooleanArray)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 AdapterView (android.widget.AdapterView)1 Button (android.widget.Button)1 ListView (android.widget.ListView)1 Pair (androidx.core.util.Pair)1 RefreshFormListDialogFragment (org.odk.collect.android.formentry.RefreshFormListDialogFragment)1 FormDeleter (org.odk.collect.android.formmanagement.FormDeleter)1 FormSourceExceptionMapper (org.odk.collect.android.formmanagement.FormSourceExceptionMapper)1 DownloadFormListTask (org.odk.collect.android.tasks.DownloadFormListTask)1 DownloadFormsTask (org.odk.collect.android.tasks.DownloadFormsTask)1 Form (org.odk.collect.forms.Form)1 FormSourceException (org.odk.collect.forms.FormSourceException)1 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)1