New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync for Centers #717

Merged
merged 1 commit into from Jul 25, 2017
Jump to file or symbol
Failed to load files and symbols.
+2,248 −51
Diff settings

Always

Just for now

@@ -3,6 +3,7 @@
import com.mifos.api.BaseApiManager; import com.mifos.api.BaseApiManager;
import com.mifos.api.GenericResponse; import com.mifos.api.GenericResponse;
import com.mifos.api.local.databasehelper.DatabaseHelperCenter; import com.mifos.api.local.databasehelper.DatabaseHelperCenter;
import com.mifos.objects.accounts.CenterAccounts;
import com.mifos.objects.client.ActivatePayload; import com.mifos.objects.client.ActivatePayload;
import com.mifos.objects.client.Page; import com.mifos.objects.client.Page;
import com.mifos.objects.group.Center; import com.mifos.objects.group.Center;
@@ -18,6 +19,7 @@
import javax.inject.Singleton; import javax.inject.Singleton;
import rx.Observable; import rx.Observable;
import rx.functions.Func1;
/** /**
* This DataManager is for Managing Center API, In which Request is going to Server * This DataManager is for Managing Center API, In which Request is going to Server
@@ -73,6 +75,34 @@ public DataManagerCenter(BaseApiManager baseApiManager,
} }
} }
/**
* This method save the single Center in Database.
*
* @param center Center
* @return Center
*/
public Observable<Center> syncCenterInDatabase(Center center) {
return mDatabaseHelperCenter.saveCenter(center);
}
/**
* This Method Fetching the Center Accounts (Loan, saving, etc Accounts ) from REST API
* and then Saving all Accounts into the Database and then returns the Center Group Accounts
*
* @param centerId Center Id
* @return CenterAccounts
*/
public Observable<CenterAccounts> syncCenterAccounts(final int centerId) {
return mBaseApiManager.getCenterApi().getCenterAccounts(centerId)
.concatMap(new Func1<CenterAccounts, Observable<? extends CenterAccounts>>() {
@Override
public Observable<? extends CenterAccounts> call(CenterAccounts
centerAccounts) {
return mDatabaseHelperCenter.saveCenterAccounts(centerAccounts, centerId);
}
});
}
/** /**
* Method Fetching CollectionSheet of the Center from : * Method Fetching CollectionSheet of the Center from :
* demo.openmf.org/fineract-provider/api/v1/centers/{centerId} * demo.openmf.org/fineract-provider/api/v1/centers/{centerId}
@@ -102,6 +132,36 @@ public DataManagerCenter(BaseApiManager baseApiManager,
} }
} }
/**
* This Method Fetch the Groups that are attached to the Centers.
* @param centerId Center Id
* @return CenterWithAssociations
*/
public Observable<CenterWithAssociations> getCenterWithAssociations(int centerId) {
switch (PrefManager.getUserStatus()) {
case 0:
return mBaseApiManager.getCenterApi().getAllGroupsForCenter(centerId);
case 1:
/**
* Return Groups from DatabaseHelperGroups.
*/
return mDatabaseHelperCenter.getCenterAssociateGroups(centerId);
default:
return Observable.just(new CenterWithAssociations());
}
}
/**
* This Method Request to the DatabaseHelperCenter and DatabaseHelperCenter Read the All
* centers from Center_Table and give the response Page of List of Center
*
* @return Page of Center List
*/
public Observable<Page<Center>> getAllDatabaseCenters() {
return mDatabaseHelperCenter.readAllCenters();
}
public Observable<List<Office>> getOffices() { public Observable<List<Office>> getOffices() {
return mBaseApiManager.getOfficeApi().getAllOffices(); return mBaseApiManager.getOfficeApi().getAllOffices();
} }
@@ -0,0 +1,59 @@
package com.mifos.api.local.database;
import com.mifos.api.local.MifosDatabase;
import com.mifos.objects.accounts.loan.LoanAccount;
import com.mifos.objects.accounts.savings.SavingsAccount;
import com.mifos.objects.group.Center;
import com.raizlabs.android.dbflow.annotation.Migration;
import com.raizlabs.android.dbflow.sql.SQLiteType;
import com.raizlabs.android.dbflow.sql.migration.AlterTableMigration;
/**
* Created by mayankjindal on 17/07/17.
*/
public class MigrationVersion2 {
@Migration(version = MifosDatabase.VERSION, database = MifosDatabase.class)
public static class Migration1 extends AlterTableMigration<LoanAccount> {
public Migration1(Class<LoanAccount> table) {
super(table);
}
@Override
public void onPreMigrate() {
addColumn(SQLiteType.INTEGER, "centerId");
}
}
@Migration(version = MifosDatabase.VERSION, database = MifosDatabase.class)
public static class Migration2 extends AlterTableMigration<SavingsAccount> {
public Migration2(Class<SavingsAccount> table) {
super(table);
}
@Override
public void onPreMigrate() {
addColumn(SQLiteType.INTEGER, "centerId");
}
}
@Migration(version = MifosDatabase.VERSION, database = MifosDatabase.class)
public static class Migration3 extends AlterTableMigration<Center> {
public Migration3(Class<Center> table) {
super(table);
}
@Override
public void onPreMigrate() {
addColumn(SQLiteType.INTEGER, "sync");
addColumn(SQLiteType.INTEGER, "centerDate_centerId");
addColumn(SQLiteType.INTEGER, "centerDate_chargeId");
addColumn(SQLiteType.INTEGER, "centerDate_day");
addColumn(SQLiteType.INTEGER, "centerDate_month");
addColumn(SQLiteType.INTEGER, "centerDate_year");
}
}
}
@@ -3,8 +3,15 @@
import android.os.AsyncTask; import android.os.AsyncTask;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import com.mifos.objects.accounts.CenterAccounts;
import com.mifos.objects.accounts.loan.LoanAccount;
import com.mifos.objects.accounts.savings.SavingsAccount;
import com.mifos.objects.client.Page; import com.mifos.objects.client.Page;
import com.mifos.objects.group.Center; import com.mifos.objects.group.Center;
import com.mifos.objects.group.CenterDate;
import com.mifos.objects.group.CenterWithAssociations;
import com.mifos.objects.group.Group;
import com.mifos.objects.group.Group_Table;
import com.mifos.objects.response.SaveResponse; import com.mifos.objects.response.SaveResponse;
import com.mifos.services.data.CenterPayload; import com.mifos.services.data.CenterPayload;
import com.mifos.services.data.CenterPayload_Table; import com.mifos.services.data.CenterPayload_Table;
@@ -98,6 +105,53 @@ public void call(Subscriber<? super Page<Center>> subscriber) {
}); });
} }
/**
* This Method Fetch the Groups that are attached to the Center.
* @param centerId Center Id
* @return CenterWithAssociations
*/
public Observable<CenterWithAssociations> getCenterAssociateGroups(final int centerId) {
return Observable.defer(new Func0<Observable<CenterWithAssociations>>() {
@Override
public Observable<CenterWithAssociations> call() {
List<Group> groups = SQLite.select()
.from(Group.class)
.where(Group_Table.centerId.eq(centerId))
.queryList();
CenterWithAssociations centerWithAssociations = new CenterWithAssociations();
centerWithAssociations.setGroupMembers(groups);
return Observable.just(centerWithAssociations);
}
});
}
/**
* This Method Saving the Single Center in the Database
*
* @param center
* @return Observable.just(Center)
*/
public Observable<Center> saveCenter(final Center center) {
return Observable.defer(new Func0<Observable<Center>>() {
@Override
public Observable<Center> call() {
if (center.getActivationDate().size() != 0) {
CenterDate centerDate = new CenterDate(center.getId(), 0,
center.getActivationDate().get(0),
center.getActivationDate().get(1),
center.getActivationDate().get(2));
center.setCenterDate(centerDate);
}
center.save();
return Observable.just(center);
}
});
}
/** /**
* This Method for deleting the center payload from the Database according to Id and * This Method for deleting the center payload from the Database according to Id and
* again fetch the center List from the Database CenterPayload_Table * again fetch the center List from the Database CenterPayload_Table
@@ -128,4 +182,42 @@ public void call(Subscriber<? super Page<Center>> subscriber) {
} }
}); });
} }
/**
* This Method write the CenterAccounts in tho DB. According to Schema Defined in Model
*
* @param centerAccounts Model of List of LoanAccount and SavingAccount
* @param centerId Center Id
* @return CenterAccounts
*/
public Observable<CenterAccounts> saveCenterAccounts(final CenterAccounts centerAccounts,
final int centerId) {
return Observable.defer(new Func0<Observable<CenterAccounts>>() {
@Override
public Observable<CenterAccounts> call() {
List<LoanAccount> loanAccounts = centerAccounts.getLoanAccounts();
List<SavingsAccount> savingsAccounts = centerAccounts.getSavingsAccounts();
List<LoanAccount> memberLoanAccounts = centerAccounts.getMemberLoanAccounts();
for (LoanAccount loanAccount : loanAccounts) {
loanAccount.setCenterId(centerId);
loanAccount.save();
}
for (SavingsAccount savingsAccount : savingsAccounts) {
savingsAccount.setCenterId(centerId);
savingsAccount.save();
}
for (LoanAccount memberLoanAccount : memberLoanAccounts) {
memberLoanAccount.setCenterId(centerId);
memberLoanAccount.save();
}
return Observable.just(centerAccounts);
}
});
}
} }
@@ -8,6 +8,7 @@
import com.mifos.api.model.APIEndPoint; import com.mifos.api.model.APIEndPoint;
import com.mifos.api.model.CollectionSheetPayload; import com.mifos.api.model.CollectionSheetPayload;
import com.mifos.api.model.Payload; import com.mifos.api.model.Payload;
import com.mifos.objects.accounts.CenterAccounts;
import com.mifos.objects.client.ActivatePayload; import com.mifos.objects.client.ActivatePayload;
import com.mifos.objects.response.SaveResponse; import com.mifos.objects.response.SaveResponse;
import com.mifos.objects.client.Page; import com.mifos.objects.client.Page;
@@ -39,6 +40,9 @@
@Query("offset") int offset, @Query("offset") int offset,
@Query("limit") int limit); @Query("limit") int limit);
@GET(APIEndPoint.CENTERS + "/{centerId}/accounts")
Observable<CenterAccounts> getCenterAccounts(@Path("centerId") int centerId);
@GET(APIEndPoint.CENTERS + "/{centerId}?associations=groupMembers,collectionMeetingCalendar") @GET(APIEndPoint.CENTERS + "/{centerId}?associations=groupMembers,collectionMeetingCalendar")
Observable<CenterWithAssociations> getCenterWithGroupMembersAndCollectionMeetingCalendar Observable<CenterWithAssociations> getCenterWithGroupMembersAndCollectionMeetingCalendar
(@Path("centerId") int centerId); (@Path("centerId") int centerId);
@@ -6,13 +6,18 @@
package com.mifos.mifosxdroid.adapters; package com.mifos.mifosxdroid.adapters;
import android.content.Context; import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import com.mifos.mifosxdroid.R; import com.mifos.mifosxdroid.R;
import com.mifos.mifosxdroid.core.SelectableAdapter;
import com.mifos.mifosxdroid.views.CircularImageView; import com.mifos.mifosxdroid.views.CircularImageView;
import com.mifos.objects.group.Center; import com.mifos.objects.group.Center;
import com.mifos.utils.Utils; import com.mifos.utils.Utils;
@@ -28,7 +33,7 @@
/** /**
* Created by ishankhanna on 11/03/14. * Created by ishankhanna on 11/03/14.
*/ */
public class CentersListAdapter extends RecyclerView.Adapter<CentersListAdapter.ViewHolder> { public class CentersListAdapter extends SelectableAdapter<RecyclerView.ViewHolder> {
private List<Center> centers; private List<Center> centers;
private Context context; private Context context;
@@ -46,28 +51,38 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
} }
@Override @Override
public void onBindViewHolder(ViewHolder holder, int position) { public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Center center = centers.get(position); if (holder instanceof ViewHolder) {
Center center = centers.get(position);
holder.tvAccountNumber.setText(String.format(context.
getString(R.string.centerList_account_prefix), center.getAccountNo())); ((ViewHolder) holder).tvAccountNumber.setText(String.format(context.
holder.tvCenterId.setText(String.valueOf(center.getId())); getString(R.string.centerList_account_prefix), center.getAccountNo()));
holder.tvCenterName.setText(center.getName()); ((ViewHolder) holder).tvCenterId.setText(String.valueOf(center.getId()));
if (center.getStaffId() != null) { ((ViewHolder) holder).tvCenterName.setText(center.getName());
holder.tvStaffId.setText(String.valueOf(center.getStaffId())); if (center.getStaffId() != null) {
holder.tvStaffName.setText(center.getStaffName()); ((ViewHolder) holder).tvStaffId.setText(String.valueOf(center.getStaffId()));
} else { ((ViewHolder) holder).tvStaffName.setText(center.getStaffName());
holder.tvStaffId.setText(""); } else {
holder.tvStaffName.setText(R.string.no_staff); ((ViewHolder) holder).tvStaffId.setText("");
} ((ViewHolder) holder).tvStaffName.setText(R.string.no_staff);
holder.tvOfficeId.setText(String.valueOf(center.getOfficeId())); }
holder.tvOfficeName.setText(center.getOfficeName()); ((ViewHolder) holder).tvOfficeId.setText(String.valueOf(center.getOfficeId()));
if (center.getActive()) { ((ViewHolder) holder).tvOfficeName.setText(center.getOfficeName());
holder.ivStatusIndicator.setImageDrawable( if (center.getActive()) {
Utils.setCircularBackground(R.color.light_green, context)); ((ViewHolder) holder).ivStatusIndicator.setImageDrawable(
} else { Utils.setCircularBackground(R.color.light_green, context));
holder.ivStatusIndicator.setImageDrawable( } else {
Utils.setCircularBackground(R.color.light_red, context)); ((ViewHolder) holder).ivStatusIndicator.setImageDrawable(
Utils.setCircularBackground(R.color.light_red, context));
}
//Changing the Color of Selected Centers
((ViewHolder) holder).viewSelectedOverlay
.setBackgroundColor(isSelected(position) ? ContextCompat.getColor(context,
R.color.gray_light) : Color.WHITE);
((ViewHolder) holder).ivSyncStatus
.setVisibility(center.isSync() ? View.VISIBLE : View.INVISIBLE);
} }
} }
@@ -121,6 +136,12 @@ public int getItemCount() {
@BindView(R.id.tv_office_id) @BindView(R.id.tv_office_id)
TextView tvOfficeId; TextView tvOfficeId;
@BindView(R.id.linearLayout)
LinearLayout viewSelectedOverlay;
@BindView(R.id.iv_sync_status)
ImageView ivSyncStatus;
public ViewHolder(View v) { public ViewHolder(View v) {
super(v); super(v);
ButterKnife.bind(this, v); ButterKnife.bind(this, v);
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.