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

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 static org.hisp.dhis.android.core.utils.StoreUtils.sqLiteBind;

public final class DataInputPeriodStore extends ObjectWithoutUidStoreImpl<DataInputPeriod> {

    private DataInputPeriodStore(DatabaseAdapter databaseAdapter, SQLiteStatement insertStatement,
                                 SQLiteStatement updateWhereStatement, SQLStatementBuilder builder) {

        super(databaseAdapter, insertStatement, updateWhereStatement,
                builder, BINDER, WHERE_UPDATE_BINDER, FACTORY);
    }

    public static DataInputPeriodStore create(DatabaseAdapter databaseAdapter) {

        SQLStatementBuilder sqlStatementBuilder =
                new SQLStatementBuilder(DataInputPeriodTableInfo.TABLE_INFO.name(),
                        DataInputPeriodTableInfo.TABLE_INFO.columns());

        return new DataInputPeriodStore(databaseAdapter, databaseAdapter.compileStatement(
                sqlStatementBuilder.insert()),
                databaseAdapter.compileStatement(sqlStatementBuilder.updateWhere()),
                sqlStatementBuilder);
    }

    private static final StatementBinder<DataInputPeriod> BINDER =
            new StatementBinder<DataInputPeriod>() {
                @Override
                public void bindToStatement(@NonNull DataInputPeriod dataInputPeriod,
                                            @NonNull SQLiteStatement sqLiteStatement) {
                    sqLiteBind(sqLiteStatement, 1, dataInputPeriod.dataSetUid());
                    sqLiteBind(sqLiteStatement, 2, dataInputPeriod.periodUid());
                    sqLiteBind(sqLiteStatement, 3, dataInputPeriod.openingDate());
                    sqLiteBind(sqLiteStatement, 4, dataInputPeriod.closingDate());
                }
            };

    private static final WhereStatementBinder<DataInputPeriod> WHERE_UPDATE_BINDER =
            new WhereStatementBinder<DataInputPeriod>() {
                @Override
                public void bindToUpdateWhereStatement(@NonNull DataInputPeriod dataInputPeriod,
                                                       @NonNull SQLiteStatement sqLiteStatement) {
                    sqLiteBind(sqLiteStatement, 5, dataInputPeriod.dataSetUid());
                    sqLiteBind(sqLiteStatement, 6, dataInputPeriod.periodUid());
                }
            };

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

@@ -1791,80 +1791,8 @@
 er;%0A
-import org.hisp.dhis.android.core.arch.db.binders.WhereStatementBinder;%0A
 impo
@@ -1893,33 +1893,22 @@
 mon.
-ObjectWithoutUid
+LinkModel
 Store
-Impl
 ;%0Aim
@@ -1947,34 +1947,27 @@
 common.S
-QLStatementBuilder
+toreFactory
 ;%0Aimport
@@ -2141,959 +2141,47 @@
 ore 
-extends ObjectWithoutUidStoreImpl%3CDataInputPeriod%3E %7B%0A%0A    private DataInputPeriodStore(DatabaseAdapter databaseAdapter, SQLiteStatement insertStatement,%0A                                 SQLiteStatement updateWhereStatement, SQLStatementBuilder builder) %7B%0A%0A        super(databaseAdapter, insertStatement, updateWhereStatement,%0A                builder, BINDER, WHERE_UPDATE_BINDER, FACTORY);%0A    %7D%0A%0A    public static DataInputPeriodStore create(DatabaseAdapter databaseAdapter) %7B%0A%0A        SQLStatementBuilder sqlStatementBuilder =%0A                new SQLStatementBuilder(DataInputPeriodTableInfo.TABLE_INFO.name(),%0A                        DataInputPeriodTableInfo.TABLE_INFO.columns());%0A%0A        return new DataInputPeriodStore(databaseAdapter, databaseAdapter.compileStatement(%0A                sqlStatementBuilder.insert()),%0A                databaseAdapter.compileStatement(sqlStatementBuilder.updateWhere()),%0A                sqlStatementBuilder);%0A    
+%7B%0A%0A    private DataInputPeriodStore() %7B
 %7D%0A%0A 
@@ -2891,67 +2891,51 @@
 nal 
-WhereStatementBinder%3CDataInputPeriod%3E WHERE_UPDATE_BINDER =
+CursorModelFactory%3CDataInputPeriod%3E FACTORY
 %0A   
@@ -2946,33 +2946,33 @@
     
+ =
  new 
-WhereStatementBinder
+CursorModelFactory
 %3CDat
@@ -2985,32 +2985,24 @@
 Period%3E() %7B%0A
-        
         @Ove
@@ -3003,32 +3003,24 @@
   @Override%0A
-        
         publ
@@ -3026,233 +3026,72 @@
 lic 
-void bindToUpdateWhereStatement(@NonNull DataInputPeriod dataInputPeriod,%0A                                                       @NonNull SQLiteStatement sqLiteStatement) %7B%0A                    sqLiteBind(sqLiteStatement, 5, d
+DataInputPeriod fromCursor(Cursor cursor) %7B%0A            return D
 ataI
@@ -3097,36 +3097,37 @@
 InputPeriod.
-dataSetUid()
+create(cursor
 );%0A         
@@ -3129,268 +3129,231 @@
     
+%7D%0A
     
-        sqLiteBind(sqLiteStatement, 6, dataInputPeriod.periodUid());%0A                %7D%0A            %7D;%0A%0A    private static final CursorModelFactory%3CDataInputPeriod%3E FACTORY%0A            = new CursorModelFactory%3CDataInputPeriod%3E() %7B%0A        @Override%0A
+%7D;%0A%0A    public static LinkModelStore%3CDataInputPeriod%3E create(DatabaseAdapter databaseAdapter) %7B%0A%0A        return StoreFactory.linkModelStore(databaseAdapter,%0A                DataInputPeriodModel.TABLE,%0A        
         
 publ
@@ -3340,38 +3340,35 @@
                 
-public
+new
  DataInputPeriod
@@ -3371,36 +3371,19 @@
 riod
- fromCursor(Cursor cursor) %7B
+.Columns(),
 %0A   
@@ -3383,38 +3383,35 @@
 (),%0A            
-return
+   
  DataInputPeriod
@@ -3414,40 +3414,82 @@
 riod
-.create(cursor);%0A        %7D
+TableInfo.DATA_SET,%0A                BINDER,%0A                FACTORY);
 %0A    %7D
-;
 %0A%7D%0A

Refactor DataInputPeriodStore to use LinkModelStore