/*
 * Copyright (c) 2004-2019, 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.common;

import org.hisp.dhis.android.core.arch.db.TableInfo;
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.arch.db.executors.CursorExecutorImpl;
import org.hisp.dhis.android.core.arch.db.stores.LinkModelChildStore;
import org.hisp.dhis.android.core.arch.db.stores.LinkModelChildStoreImpl;
import org.hisp.dhis.android.core.arch.db.stores.SingleParentChildStore;
import org.hisp.dhis.android.core.arch.db.stores.SingleParentChildStoreImpl;
import org.hisp.dhis.android.core.arch.db.tableinfos.LinkTableChildProjection;
import org.hisp.dhis.android.core.arch.db.tableinfos.SingleParentChildProjection;
import org.hisp.dhis.android.core.data.database.DatabaseAdapter;

@SuppressWarnings("PMD.UseVarargs")
public final class StoreFactory {

    private StoreFactory() {}

    public static <I extends Model & ObjectWithUidInterface> IdentifiableObjectStore<I>
    objectWithUidStore(DatabaseAdapter databaseAdapter, TableInfo tableInfo, StatementBinder<I> binder,
                       CursorModelFactory<I> modelFactory) {
        SQLStatementBuilder statementBuilder =
                new SQLStatementBuilder(tableInfo.name(), tableInfo.columns().all(), new String[]{});
        SQLStatementWrapper statements = new SQLStatementWrapper(statementBuilder, databaseAdapter);
        return new IdentifiableObjectStoreImpl<>(databaseAdapter, statements, statementBuilder, binder, modelFactory);
    }

    public static <I extends Model> ObjectStore<I> objectStore(DatabaseAdapter databaseAdapter,
                                                           TableInfo tableInfo,
                                                           StatementBinder<I> binder,
                                                           CursorModelFactory<I> modelFactory) {
        SQLStatementBuilder statementBuilder =
                new SQLStatementBuilder(tableInfo.name(), tableInfo.columns().all(), new String[]{});
        return new ObjectStoreImpl<>(databaseAdapter, databaseAdapter.compileStatement(statementBuilder.insert()),
                statementBuilder, binder, modelFactory);
    }

    @Deprecated
    public static <I extends Model> ObjectWithoutUidStore<I> objectWithoutUidStore(
            DatabaseAdapter databaseAdapter, String tableName, BaseModel.Columns columns, StatementBinder<I> binder,
            WhereStatementBinder<I> whereBinder, CursorModelFactory<I> modelFactory) {
        SQLStatementBuilder statementBuilder = new SQLStatementBuilder(tableName, columns.all(), columns.whereUpdate());
        return new ObjectWithoutUidStoreImpl<>(databaseAdapter,
                databaseAdapter.compileStatement(statementBuilder.insert()),
                databaseAdapter.compileStatement(statementBuilder.updateWhere()),
                statementBuilder, binder, whereBinder, modelFactory);
    }

    public static <I extends Model> ObjectWithoutUidStore<I> objectWithoutUidStore(
            DatabaseAdapter databaseAdapter, TableInfo tableInfo, StatementBinder<I> binder,
            WhereStatementBinder<I> whereBinder, CursorModelFactory<I> modelFactory) {
        return objectWithoutUidStore(databaseAdapter, tableInfo.name(), tableInfo.columns(), binder, whereBinder,
                modelFactory);
    }

    @Deprecated
    public static <I extends Model> LinkModelStore<I> linkModelStore(
            DatabaseAdapter databaseAdapter, String tableName, BaseModel.Columns columns,
            String masterColumn, StatementBinder<I> binder, CursorModelFactory<I> modelFactory) {
        SQLStatementBuilder statementBuilder = new SQLStatementBuilder(tableName, columns.all(), columns.whereUpdate());
        return new LinkModelStoreImpl<>(databaseAdapter, databaseAdapter.compileStatement(statementBuilder.insert()),
                statementBuilder, masterColumn, binder, modelFactory);
    }

    public static <I extends Model> LinkModelStore<I> linkModelStore(
            DatabaseAdapter databaseAdapter, TableInfo tableInfo, String masterColumn, StatementBinder<I> binder,
            CursorModelFactory<I> modelFactory) {
        SQLStatementBuilder statementBuilder = new SQLStatementBuilder(tableInfo.name(), tableInfo.columns().all(),
                tableInfo.columns().whereUpdate());
        return new LinkModelStoreImpl<>(databaseAdapter, databaseAdapter.compileStatement(statementBuilder.insert()),
                statementBuilder, masterColumn, binder, modelFactory);
    }

    public static <P extends ObjectWithUidInterface,
            C extends ObjectWithUidInterface> LinkModelChildStore<P, C> linkModelChildStore(
                    DatabaseAdapter databaseAdapter,
                    TableInfo linkTableInfo,
                    LinkTableChildProjection linkTableChildProjection,
                    CursorModelFactory<C> childFactory) {
        return new LinkModelChildStoreImpl<>(
                linkTableChildProjection,
                databaseAdapter,
                new SQLStatementBuilder(linkTableInfo),
                new CursorExecutorImpl<>(childFactory));
    }

    public static <P extends ObjectWithUidInterface, C> SingleParentChildStore<P, C> singleParentChildStore(
                    DatabaseAdapter databaseAdapter,
                    SingleParentChildProjection childProjection,
                    CursorModelFactory<C> childFactory) {
        return new SingleParentChildStoreImpl<>(
                childProjection,
                databaseAdapter,
                new SQLStatementBuilder(childProjection.childTableInfo),
                new CursorExecutorImpl<>(childFactory));
    }
}
@@ -2384,44 +2384,8 @@
 r;%0A%0A
-@SuppressWarnings(%22PMD.UseVarargs%22)%0A
 publ
@@ -2533,20 +2533,16 @@
 Store%3CI%3E
-%0A   
  objectW
@@ -2549,24 +2549,37 @@
 ithUidStore(
+%0A            
 DatabaseAdap
@@ -2643,35 +2643,24 @@
 %3CI%3E binder,%0A
-           
             
@@ -3760,32 +3760,16 @@
 %0A    %7D%0A%0A
-    @Deprecated%0A
     publ
@@ -3893,51 +3893,27 @@
 er, 
-String tableName, BaseModel.Columns columns
+TableInfo tableInfo
 , St
@@ -4062,32 +4062,48 @@
 atementBuilder =
+%0A               
  new SQLStatemen
@@ -4116,37 +4116,56 @@
 er(table
-Name, 
+Info.name(), tableInfo.
 columns
+()
 .all(), 
 columns.
@@ -4148,39 +4148,51 @@
 olumns().all(), 
+tableInfo.
 columns
+()
 .whereUpdate());
@@ -4496,424 +4496,8 @@
  %7D%0A%0A
-    public static %3CI extends Model%3E ObjectWithoutUidStore%3CI%3E objectWithoutUidStore(%0A            DatabaseAdapter databaseAdapter, TableInfo tableInfo, StatementBinder%3CI%3E binder,%0A            WhereStatementBinder%3CI%3E whereBinder, CursorModelFactory%3CI%3E modelFactory) %7B%0A        return objectWithoutUidStore(databaseAdapter, tableInfo.name(), tableInfo.columns(), binder, whereBinder,%0A                modelFactory);%0A    %7D%0A%0A
     

Remove old method for objectWithoutUidStore creation