/*
 * 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.trackedentity;

import org.hisp.dhis.android.core.arch.repositories.children.ChildrenAppender;
import org.hisp.dhis.android.core.arch.repositories.collection.ReadOnlyCollectionRepositoryImpl;
import org.hisp.dhis.android.core.arch.repositories.filters.DateFilterConnector;
import org.hisp.dhis.android.core.arch.repositories.filters.FilterConnectorFactory;
import org.hisp.dhis.android.core.arch.repositories.filters.StringFilterConnector;
import org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScope;

import java.util.Map;

import javax.inject.Inject;

import dagger.Reusable;

@Reusable
public final class TrackedEntityAttributeValueCollectionRepository
        extends ReadOnlyCollectionRepositoryImpl<TrackedEntityAttributeValue,
        TrackedEntityAttributeValueCollectionRepository> {

    @Inject
    TrackedEntityAttributeValueCollectionRepository(
            final TrackedEntityAttributeValueStore store,
            final Map<String, ChildrenAppender<TrackedEntityAttributeValue>> childrenAppenders,
            final RepositoryScope scope) {
        super(store, childrenAppenders, scope, new FilterConnectorFactory<>(scope,
                s -> new TrackedEntityAttributeValueCollectionRepository(store, childrenAppenders, s)));
    }

    public StringFilterConnector<TrackedEntityAttributeValueCollectionRepository> byTrackedEntityAttribute() {
        return cf.string(TrackedEntityAttributeValueTableInfo.Columns.TRACKED_ENTITY_ATTRIBUTE);
    }

    public StringFilterConnector<TrackedEntityAttributeValueCollectionRepository> byValue() {
        return cf.string(TrackedEntityAttributeValueFields.VALUE);
    }

    public DateFilterConnector<TrackedEntityAttributeValueCollectionRepository> byCreated() {
        return cf.date(TrackedEntityAttributeValueFields.CREATED);
    }

    public DateFilterConnector<TrackedEntityAttributeValueCollectionRepository> byLastUpdated() {
        return cf.date(TrackedEntityAttributeValueFields.LAST_UPDATED);
    }

    public StringFilterConnector<TrackedEntityAttributeValueCollectionRepository> byTrackedEntityInstance() {
        return cf.string(TrackedEntityAttributeValueTableInfo.Columns.TRACKED_ENTITY_INSTANCE);
    }
}
@@ -1744,28 +1744,38 @@
 lection.Read
-Only
+WriteWithValue
 CollectionRe
@@ -2110,16 +2110,298 @@
 ryScope;
+%0Aimport org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScopeFilterItem;%0Aimport org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScopeHelper;%0Aimport org.hisp.dhis.android.core.common.DataStatePropagator;%0Aimport org.hisp.dhis.android.core.common.Transformer;
 %0A%0Aimport
@@ -2572,12 +2572,22 @@
 Read
-Only
+WriteWithValue
 Coll
@@ -2676,30 +2676,193 @@
 lueC
-ollectionRepository%3E %7B
+reateProjection, TrackedEntityAttributeValueCollectionRepository%3E %7B%0A%0A    private final TrackedEntityAttributeValueStore store;%0A    private final DataStatePropagator dataStatePropagator;
 %0A%0A  
@@ -3117,16 +3117,192 @@
 pe scope
+,%0A            final Transformer%3CTrackedEntityAttributeValueCreateProjection, TrackedEntityAttributeValue%3E transformer,%0A            final DataStatePropagator dataStatePropagator
 ) %7B%0A    
@@ -3343,16 +3343,29 @@
 , scope,
+ transformer,
  new Fil
@@ -3466,16 +3466,41 @@
 ository(
+%0A                        
 store, c
@@ -3518,18 +3518,1048 @@
 nders, s
-))
+, transformer, dataStatePropagator)));%0A        this.store = store;%0A        this.dataStatePropagator = dataStatePropagator;%0A    %7D%0A%0A    public TrackedEntityAttributeValueObjectRepository value(String trackedEntityAttribute,%0A                                                             String trackedEntityInstance) %7B%0A        RepositoryScope updatedScope = RepositoryScopeHelper.withFilterItem(scope, RepositoryScopeFilterItem.builder()%0A                .key(TrackedEntityAttributeValueTableInfo.Columns.TRACKED_ENTITY_ATTRIBUTE).operator(%22=%22)%0A                .value(%22'%22 + trackedEntityAttribute + %22'%22).build());%0A        updatedScope = RepositoryScopeHelper.withFilterItem(updatedScope, RepositoryScopeFilterItem.builder()%0A                .key(TrackedEntityAttributeValueTableInfo.Columns.TRACKED_ENTITY_INSTANCE).operator(%22=%22)%0A                .value(%22'%22 + trackedEntityInstance + %22'%22).build());%0A%0A        return new TrackedEntityAttributeValueObjectRepository(%0A                store, childrenAppenders, updatedScope, dataStatePropagator
 );%0A    %7D

Convert TrackedEntityAttributeValueCollectionRepository on a read-write repository