package com.rehivetech.beeeon.asynctask;

import android.content.Context;

import com.rehivetech.beeeon.controller.Controller;
import com.rehivetech.beeeon.household.adapter.Adapter;

/**
 * Reloads specified data from server.
 */
public class ReloadAdapterDataTask extends CallbackTask<String> {

	private final boolean mForceReload;

	private ReloadWhat mWhat;

	public enum ReloadWhat {
		ADAPTERS_AND_ACTIVE_ADAPTER,
		LOCATIONS,
		FACILITIES,
		UNINITIALIZED_FACILITIES,
		USERS,
		WATCHDOGS,
	}

	public ReloadAdapterDataTask(Context context, boolean forceReload, ReloadWhat what) {
		super(context);

		mForceReload = forceReload;
		mWhat = what;
	}

	@Override
	protected Boolean doInBackground(String adapterId) {
		Controller controller = Controller.getInstance(mContext);

		if (mWhat == ReloadWhat.ADAPTERS_AND_ACTIVE_ADAPTER) {
			controller.getAdaptersModel().reloadAdapters(mForceReload);

			Adapter active = controller.getActiveAdapter();
			if (active == null)
				return true;

			// We need to update also facilities
			adapterId = active.getId();
			mWhat = ReloadWhat.FACILITIES;
		}

		if (mWhat == ReloadWhat.LOCATIONS || mWhat == ReloadWhat.FACILITIES) {
			controller.getLocationsModel().reloadLocationsByAdapter(adapterId, mForceReload);
		}

		if (mWhat == ReloadWhat.FACILITIES) {
			controller.getFacilitiesModel().reloadFacilitiesByAdapter(adapterId, mForceReload);
		}

		if (mWhat == ReloadWhat.UNINITIALIZED_FACILITIES) {
			controller.getUninitializedFacilitiesModel().reloadUninitializedFacilitiesByAdapter(adapterId, mForceReload);
		}

		if (mWhat == ReloadWhat.USERS) {
			controller.getUsersModel().reloadUsersByAdapter(adapterId, mForceReload);
		}

		if (mWhat == ReloadWhat.WATCHDOGS) {
			controller.getWatchDogsModel().reloadWatchDogsByAdapter(adapterId, mForceReload);
		}

		return true;
	}

}

@@ -777,16 +777,59 @@
 text);%0A%0A
+%09%09controller.beginPersistentConnection();%0A%0A
 %09%09if (mW
@@ -1856,16 +1856,57 @@
 );%0A%09%09%7D%0A%0A
+%09%09controller.endPersistentConnection();%0A%0A
 %09%09return

Use persistent connection in ReloadAdapterDataTask