<commit_before>/*
 * Copyright (c) 2005-2011 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.androidclient.main;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import org.mifos.androidclient.R;
import org.mifos.androidclient.entities.simple.AbstractCustomer;
import org.mifos.androidclient.entities.simple.Center;
import org.mifos.androidclient.entities.simple.CustomersData;
import org.mifos.androidclient.entities.simple.Group;
import org.mifos.androidclient.net.services.CustomerService;
import org.mifos.androidclient.templates.DownloaderActivity;
import org.mifos.androidclient.templates.ServiceConnectivityTask;
import org.mifos.androidclient.util.ApplicationConstants;
import org.mifos.androidclient.util.listadapters.SimpleListAdapter;
import org.mifos.androidclient.util.listadapters.SimpleListItem;
import org.springframework.web.client.RestClientException;

import java.util.ArrayList;

public class CentersListActivity extends DownloaderActivity
        implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private ListView mCentersList;
    private CustomersListTask mCustomersListTask;
    private CustomersData mCustomersData;
    private CustomerService mCustomerService;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.centers_list);

        if (bundle != null && bundle.containsKey(CustomersData.BUNDLE_KEY)) {
            mCustomersData = (CustomersData)bundle.getSerializable(CustomersData.BUNDLE_KEY);
        }
        mCentersList = (ListView)findViewById(R.id.centers_list);
        mCustomerService = new CustomerService(this);
    }

    @Override
    protected void onSessionActive() {
        if (mCustomersData == null) {
            runClientsListTask();
        } else {
            repopulateCustomersList(mCustomersData);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(CustomersData.BUNDLE_KEY, mCustomersData);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
        Center center = (Center)adapterView.getAdapter().getItem(position);
        Intent intent = new Intent().setClass(this, CustomersListActivity.class);
        intent.putExtra(Group.BUNDLE_KEY, new ArrayList<Group>(center.getGroups()));
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long rowId) {
        Center center = (Center)adapterView.getAdapter().getItem(position);
        Intent intent = new Intent().setClass(this, CustomerDetailsActivity.class);
        intent.putExtra(AbstractCustomer.BUNDLE_KEY, center);
        startActivity(intent);
        return true;
    }

    /**
     * Refreshes contents of the centers list.
     */
    private void repopulateCustomersList(CustomersData data) {
        mCentersList.setAdapter(new SimpleListAdapter(
                this,
                new ArrayList<SimpleListItem>(data.getCenters())
        ));
        mCentersList.setOnItemClickListener(this);
        mCentersList.setOnItemLongClickListener(this);
    }

    /**
     * Checks if the task is not currently running and launches it
     * otherwise.
     */
    private void runClientsListTask() {
        if (mCustomersListTask == null || mCustomersListTask.getStatus() != AsyncTask.Status.RUNNING) {
            mCustomersListTask = new CustomersListTask(
                    this,
                    getString(R.string.dialog_getting_customer_data),
                    getString(R.string.dialog_loading_message)
            );
            mCustomersListTask.execute((Void[])null);
        }
    }

    /**
     * Downloads the list of loan officer's customers from the server. Refreshes
     * the list of centers upon successful download.
     */
    private class CustomersListTask extends ServiceConnectivityTask<Void, Void, CustomersData> {

        public CustomersListTask(Context context, String progressTitle, String progressMessage) {
            super(context, progressTitle, progressMessage);
        }

        @Override
        protected CustomersData doInBackgroundBody(Void... params) throws RestClientException, IllegalArgumentException {
            CustomersData result = null;
            if (mCustomerService != null) {<commit_msg>Null pointer exception bug fix.<commit_after>/*
 * Copyright (c) 2005-2011 <NAME>
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.androidclient.main;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import org.mifos.androidclient.R;
import org.mifos.androidclient.entities.simple.AbstractCustomer;
import org.mifos.androidclient.entities.simple.Center;
import org.mifos.androidclient.entities.simple.CustomersData;
import org.mifos.androidclient.entities.simple.Group;
import org.mifos.androidclient.net.services.CustomerService;
import org.mifos.androidclient.templates.DownloaderActivity;
import org.mifos.androidclient.templates.ServiceConnectivityTask;
import org.mifos.androidclient.util.ApplicationConstants;
import org.mifos.androidclient.util.listadapters.SimpleListAdapter;
import org.mifos.androidclient.util.listadapters.SimpleListItem;
import org.springframework.web.client.RestClientException;

import java.util.ArrayList;

public class CentersListActivity extends DownloaderActivity
        implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private ListView mCentersList;
    private CustomersListTask mCustomersListTask;
    private CustomersData mCustomersData;
    private CustomerService mCustomerService;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.centers_list);

        if (bundle != null && bundle.containsKey(CustomersData.BUNDLE_KEY)) {
            mCustomersData = (CustomersData)bundle.getSerializable(CustomersData.BUNDLE_KEY);
        }
        mCentersList = (ListView)findViewById(R.id.centers_list);
        mCustomerService = new CustomerService(this);
    }

    @Override
    protected void onSessionActive() {
        if (mCustomersData == null) {
            runClientsListTask();
        } else {
            repopulateCustomersList(mCustomersData);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(CustomersData.BUNDLE_KEY, mCustomersData);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
        Center center = (Center)adapterView.getAdapter().getItem(position);
        Intent intent = new Intent().setClass(this, CustomersListActivity.class);
        intent.putExtra(Group.BUNDLE_KEY, new ArrayList<Group>(center.getGroups()));
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long rowId) {
        Center center = (Center)adapterView.getAdapter().getItem(position);
        Intent intent = new Intent().setClass(this, CustomerDetailsActivity.class);
        intent.putExtra(AbstractCustomer.BUNDLE_KEY, center);
        startActivity(intent);
        return true;
    }

    /**
     * Refreshes contents of the centers list.
     */
    private void repopulateCustomersList(CustomersData data) {
        if (data.getCenters() != null) {
            mCentersList.setAdapter(new SimpleListAdapter(
                    this,
                    new ArrayList<SimpleListItem>(data.getCenters())
            ));
            mCentersList.setOnItemClickListener(this);
            mCentersList.setOnItemLongClickListener(this);
        }
    }

    /**
     * Checks if the task is not currently running and launches it
     * otherwise.
     */
    private void runClientsListTask() {
        if (mCustomersListTask == null || mCustomersListTask.getStatus() != AsyncTask.Status.RUNNING) {
            mCustomersListTask = new CustomersListTask(
                    this,
                    getString(R.string.dialog_getting_customer_data),
                    getString(R.string.dialog_loading_message)
            );
            mCustomersListTask.execute((Void[])null);
        }
    }

    /**
     * Downloads the list of loan officer's customers from the server. Refreshes
     * the list of centers upon successful download.
     */
    private class CustomersListTask extends ServiceConnectivityTask<Void, Void, CustomersData> {

        public CustomersListTask(Context context, String progressTitle, String progressMessage) {
            super(context, progressTitle, progressMessage);
        }

        @Override
        protected CustomersData doInBackgroundBody(Void... params) throws RestClientException, IllegalArgumentException {
            CustomersData result = null;
            if (mCustomerService != null) {
{'fraction_non_alphanumeric': 0.08573292330630883, 'fraction_numerical': 0.002609262883235486, 'mean_word_length': 3.3821968150265413, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6984616', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2823, 'n_words': 685}