<commit_before>package org.openlmis.core.model.repository;

import android.content.Context;

import com.google.inject.Inject;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.table.TableUtils;

import org.openlmis.core.LMISApp;
import org.openlmis.core.R;
import org.openlmis.core.exceptions.LMISException;
import org.openlmis.core.model.DraftInventory;
import org.openlmis.core.model.DraftLotItem;
import org.openlmis.core.model.Inventory;
import org.openlmis.core.model.Period;
import org.openlmis.core.persistence.DbUtil;
import org.openlmis.core.persistence.GenericDao;
import org.openlmis.core.persistence.LmisSqliteOpenHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

public class InventoryRepository {
    final Context context;
    GenericDao<Inventory> genericDao;
    GenericDao<DraftInventory> draftInventoryGenericDao;
    GenericDao<DraftLotItem> draftLotItemGenericDao;

    private final Comparator<Inventory> inventoryComparator = new Comparator<Inventory>() {
        @Override
        public int compare(Inventory i1, Inventory i2) {
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(i1.getUpdatedAt());
            c2.setTime(i2.getUpdatedAt());
            return c1.get(Calendar.DAY_OF_YEAR) - c2.get(Calendar.DAY_OF_YEAR);
        }
    };

    @Inject
    DbUtil dbUtil;
    @Inject
    private LotRepository lotRepository;

    @Inject
    public InventoryRepository(Context context) {
        genericDao = new GenericDao<>(Inventory.class, context);
        draftInventoryGenericDao = new GenericDao<>(DraftInventory.class, context);
        draftLotItemGenericDao = new GenericDao<>(DraftLotItem.class, context);
        this.context = context;
    }

    public void save(Inventory inventory) {
        try {
            genericDao.create(inventory);
        } catch (LMISException e) {
            e.reportToFabric();
        }
    }



    public List<Inventory> queryPeriodInventory(final Period period) throws LMISException {
        return dbUtil.withDao(Inventory.class, new DbUtil.Operation<Inventory, List<Inventory>>() {
            @Override
            public List<Inventory> operate(Dao<Inventory, String> dao) throws SQLException {
                if (LMISApp.getInstance().getFeatureToggleFor(R.bool.feature_training)) {
                    List<Inventory> inventories = dao.queryBuilder().orderBy("updatedAt", false).where().between("updatedAt", period.getBegin().toDate(), period.getEnd().toDate()).query();
                    TreeSet<Inventory> treeSet = new TreeSet<>(new Comparator<Inventory>() {
                        @Override
                        public int compare(Inventory i1, Inventory i2) {
                            Calendar c1 = Calendar.getInstance();
                            Calendar c2 = Calendar.getInstance();
                            c1.setTime(i1.getUpdatedAt());
                            c2.setTime(i2.getUpdatedAt());
                            return c1.get(Calendar.DAY_OF_YEAR) - c2.get(Calendar.DAY_OF_YEAR);
                        }
                    });
                    treeSet.addAll(inventories);
                    return new ArrayList<>(treeSet);
                }
                return dao.queryBuilder().orderBy("updatedAt", false).where().between("updatedAt", period.getInventoryBegin().toDate(), period.getInventoryEnd().toDate()).query();
            }<commit_msg>YiD && LHX - #1011 select inventory page can show the dates of the same day.<commit_after>package org.openlmis.core.model.repository;

import android.content.Context;

import com.google.inject.Inject;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.table.TableUtils;

import org.openlmis.core.LMISApp;
import org.openlmis.core.R;
import org.openlmis.core.exceptions.LMISException;
import org.openlmis.core.model.DraftInventory;
import org.openlmis.core.model.DraftLotItem;
import org.openlmis.core.model.Inventory;
import org.openlmis.core.model.Period;
import org.openlmis.core.persistence.DbUtil;
import org.openlmis.core.persistence.GenericDao;
import org.openlmis.core.persistence.LmisSqliteOpenHelper;

import java.sql.SQLException;
import java.util.List;

public class InventoryRepository {
    final Context context;
    GenericDao<Inventory> genericDao;
    GenericDao<DraftInventory> draftInventoryGenericDao;
    GenericDao<DraftLotItem> draftLotItemGenericDao;

    @Inject
    DbUtil dbUtil;
    @Inject
    private LotRepository lotRepository;

    @Inject
    public InventoryRepository(Context context) {
        genericDao = new GenericDao<>(Inventory.class, context);
        draftInventoryGenericDao = new GenericDao<>(DraftInventory.class, context);
        draftLotItemGenericDao = new GenericDao<>(DraftLotItem.class, context);
        this.context = context;
    }

    public void save(Inventory inventory) {
        try {
            genericDao.create(inventory);
        } catch (LMISException e) {
            e.reportToFabric();
        }
    }


    public List<Inventory> queryPeriodInventory(final Period period) throws LMISException {
        return dbUtil.withDao(Inventory.class, new DbUtil.Operation<Inventory, List<Inventory>>() {
            @Override
            public List<Inventory> operate(Dao<Inventory, String> dao) throws SQLException {
                if (LMISApp.getInstance().getFeatureToggleFor(R.bool.feature_training)) {
                    return dao.queryBuilder().orderBy("updatedAt", false).where().between("updatedAt", period.getBegin().toDate(), period.getEnd().toDate()).query();
                }
                return dao.queryBuilder().orderBy("updatedAt", false).where().between("updatedAt", period.getInventoryBegin().toDate(), period.getInventoryEnd().toDate()).query();
            }
{'fraction_non_alphanumeric': 0.10802157061004382, 'fraction_numerical': 0.006066734074823054, 'mean_word_length': 3.0374149659863945, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5328175', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1725, 'n_words': 299}