/*
 * Copyright (c) 2017, University of Oslo
 *
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.hisp.dhis.android.core.user;

import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import android.support.annotation.NonNull;

import org.hisp.dhis.android.core.arch.db.binders.StatementBinder;
import org.hisp.dhis.android.core.arch.db.binders.WhereStatementBinder;
import org.hisp.dhis.android.core.common.CursorModelFactory;
import org.hisp.dhis.android.core.common.ObjectWithoutUidStoreImpl;
import org.hisp.dhis.android.core.common.SQLStatementBuilder;
import org.hisp.dhis.android.core.data.database.DatabaseAdapter;
import org.hisp.dhis.android.core.organisationunit.OrganisationUnit;

import java.util.List;

import static org.hisp.dhis.android.core.utils.StoreUtils.sqLiteBind;

public final class UserOrganisationUnitLinkStore extends ObjectWithoutUidStoreImpl<UserOrganisationUnitLinkModel>
        implements UserOrganisationUnitLinkStoreInterface{

    private UserOrganisationUnitLinkStore(DatabaseAdapter databaseAdapter,
                                          SQLiteStatement insertStatement,
                                          SQLiteStatement updateWhereStatement,
                                          SQLStatementBuilder builder,
                                          StatementBinder<UserOrganisationUnitLinkModel> binder,
                                          WhereStatementBinder<UserOrganisationUnitLinkModel> whereBinder) {
        super(databaseAdapter, insertStatement, updateWhereStatement, builder, binder, whereBinder, FACTORY);
    }

    private static final StatementBinder<UserOrganisationUnitLinkModel> BINDER
            = new StatementBinder<UserOrganisationUnitLinkModel>() {
        @Override
        public void bindToStatement(@NonNull UserOrganisationUnitLinkModel o,
                                    @NonNull SQLiteStatement sqLiteStatement) {
            sqLiteBind(sqLiteStatement, 1, o.user());
            sqLiteBind(sqLiteStatement, 2, o.organisationUnit());
            sqLiteBind(sqLiteStatement, 3, o.organisationUnitScope());
            sqLiteBind(sqLiteStatement, 4, o.root());
        }
    };

    private static final WhereStatementBinder<UserOrganisationUnitLinkModel> WHERE_UPDATE_BINDER
            = new WhereStatementBinder<UserOrganisationUnitLinkModel>() {
        @Override
        public void bindToUpdateWhereStatement(@NonNull UserOrganisationUnitLinkModel o,
                                               @NonNull SQLiteStatement sqLiteStatement) {
            sqLiteBind(sqLiteStatement, 5, o.user());
            sqLiteBind(sqLiteStatement, 6, o.organisationUnit());
            sqLiteBind(sqLiteStatement, 7, o.organisationUnitScope());
            sqLiteBind(sqLiteStatement, 8, o.root());
        }
    };

    private static final CursorModelFactory<UserOrganisationUnitLinkModel> FACTORY
            = new CursorModelFactory<UserOrganisationUnitLinkModel>() {
        @Override
        public UserOrganisationUnitLinkModel fromCursor(Cursor cursor) {
            return UserOrganisationUnitLinkModel.create(cursor);
        }
    };

    public static UserOrganisationUnitLinkStoreInterface create(DatabaseAdapter databaseAdapter) {
        SQLStatementBuilder statementBuilder = new SQLStatementBuilder(
                UserOrganisationUnitLinkModel.TABLE, new UserOrganisationUnitLinkModel.Columns());

        return new UserOrganisationUnitLinkStore(
                databaseAdapter,
                databaseAdapter.compileStatement(statementBuilder.insert()),
                databaseAdapter.compileStatement(statementBuilder.updateWhere()),
                statementBuilder,
                BINDER,
                WHERE_UPDATE_BINDER);
    }

    @Override
    public List<String> queryRootCaptureOrganisationUnitUids() throws RuntimeException {
        return selectStringColumnsWhereClause(UserOrganisationUnitLinkModel.Columns.ORGANISATION_UNIT,
                        UserOrganisationUnitLinkModel.Columns.ROOT + " = 1 " + "AND "
                                + UserOrganisationUnitLinkModel.Columns.ORGANISATION_UNIT_SCOPE + " = '"
                                + OrganisationUnit.Scope.SCOPE_DATA_CAPTURE + "'");
    }
}
@@ -1783,80 +1783,8 @@
 er;%0A
-import org.hisp.dhis.android.core.arch.db.binders.WhereStatementBinder;%0A
 impo
@@ -1881,32 +1881,25 @@
 .common.
-ObjectWithoutUid
+LinkModel
 StoreImp
@@ -2254,24 +2254,17 @@
 nds 
-ObjectWithoutUid
+LinkModel
 Stor
@@ -2357,16 +2357,17 @@
 nterface
+ 
 %7B%0A%0A    p
@@ -2558,43 +2558,26 @@
    S
-QLiteStatement updateWhereStatement
+tring masterColumn
 ,%0A  
@@ -2744,119 +2744,13 @@
 nder
-,%0A                                          WhereStatementBinder%3CUserOrganisationUnitLinkModel%3E whereBinder
 ) %7B%0A
+%0A
     
@@ -2797,53 +2797,32 @@
 nt, 
-updateWhereStatement, builder, binder, whereB
+builder, masterColumn, b
 inde
@@ -3432,640 +3432,8 @@
 %7D;%0A%0A
-    private static final WhereStatementBinder%3CUserOrganisationUnitLinkModel%3E WHERE_UPDATE_BINDER%0A            = new WhereStatementBinder%3CUserOrganisationUnitLinkModel%3E() %7B%0A        @Override%0A        public void bindToUpdateWhereStatement(@NonNull UserOrganisationUnitLinkModel o,%0A                                               @NonNull SQLiteStatement sqLiteStatement) %7B%0A            sqLiteBind(sqLiteStatement, 5, o.user());%0A            sqLiteBind(sqLiteStatement, 6, o.organisationUnit());%0A            sqLiteBind(sqLiteStatement, 7, o.organisationUnitScope());%0A            sqLiteBind(sqLiteStatement, 8, o.root());%0A        %7D%0A    %7D;%0A%0A
     
@@ -4208,72 +4208,50 @@
     
-databaseAdapter.compileStatement(statementBuilder.updateWhere())
+UserOrganisationUnitLinkModel.Columns.USER
 ,%0A  
@@ -4302,45 +4302,8 @@
     
-BINDER,%0A                WHERE_UPDATE_
 BIND

Modify UserOrganisationLinkStore to implement LinkModelStore