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

import org.hisp.dhis.android.core.arch.db.querybuilders.internal.WhereClauseBuilder;
import org.hisp.dhis.android.core.arch.db.stores.internal.IdentifiableObjectStore;
import org.hisp.dhis.android.core.arch.repositories.children.internal.ChildrenAppender;
import org.hisp.dhis.android.core.arch.repositories.collection.internal.ReadOnlyIdentifiableCollectionRepositoryImpl;
import org.hisp.dhis.android.core.arch.repositories.filters.internal.FilterConnectorFactory;
import org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScope;
import org.hisp.dhis.android.core.common.IdentifiableColumns;
import org.hisp.dhis.android.core.relationship.internal.RelationshipTypeFields;

import java.util.Map;

import javax.inject.Inject;

import dagger.Reusable;

@Reusable
public final class RelationshipTypeCollectionRepository
        extends ReadOnlyIdentifiableCollectionRepositoryImpl<RelationshipType, RelationshipTypeCollectionRepository> {

    @Inject
    RelationshipTypeCollectionRepository(final IdentifiableObjectStore<RelationshipType> store,
                                         final Map<String, ChildrenAppender<RelationshipType>> childrenAppenders,
                                         final RepositoryScope scope) {
        super(store, childrenAppenders, scope, new FilterConnectorFactory<>(scope,
                s -> new RelationshipTypeCollectionRepository(store, childrenAppenders, s)));
    }

    public RelationshipTypeCollectionRepository byConstraint(
            RelationshipEntityType relationshipEntityType,
            RelationshipConstraintType relationshipConstraintType,
            String objectUid) {
        return cf.subQuery(IdentifiableColumns.UID).inTableWhere(
                RelationshipConstraintTableInfo.TABLE_INFO.name(),
                RelationshipConstraintTableInfo.Columns.RELATIONSHIP_TYPE,
                new WhereClauseBuilder()
                        .appendKeyStringValue(
                                RelationshipConstraintTableInfo.Columns.RELATIONSHIP_ENTITY, relationshipEntityType)
                        .appendKeyStringValue(
                                RelationshipConstraintTableInfo.Columns.CONSTRAINT_TYPE, relationshipConstraintType)
                        .appendKeyStringValue(
                                RelationshipConstraintTableInfo.Columns.TRACKED_ENTITY_TYPE, objectUid));
    }

    public RelationshipTypeCollectionRepository withConstraints() {
        return cf.withChild(RelationshipTypeFields.CONSTRAINTS);
    }
}

@@ -1596,24 +1596,61 @@
 ationship;%0A%0A
+import androidx.annotation.NonNull;%0A%0A
 import org.h
@@ -3124,28 +3124,24 @@
 straint(
-%0A           
+@NonNull
  Relatio
@@ -3196,34 +3196,72 @@
     
-RelationshipCons
+                                                 @NonNull S
 tr
-a
 in
-tType
+g
  rel
@@ -3273,49 +3273,14 @@
 ship
-ConstraintType,%0A            String object
+Entity
 Uid)
@@ -3510,17 +3510,18 @@
     
-new Where
+constraint
 Clau
@@ -3534,71 +3534,245 @@
 der(
-)
+relationshipEntityType, relationshipEntityUid));
 %0A    
+%7D%0A%0A
     
-                .appendKeyStringValue(%0A              
+public RelationshipTypeCollectionRepository byConstraint(%0A            @NonNull RelationshipEntityType relationshipEntityType,%0A            @NonNull String relationshipEntityUid,%0A
     
@@ -3771,37 +3771,40 @@
 id,%0A            
-     
+@NonNull
  RelationshipCon
@@ -3815,45 +3815,11 @@
 intT
-ableInfo.Columns.RELATIONSHIP_ENTITY,
+ype
  rel
@@ -3831,19 +3831,25 @@
 ship
-Entity
+Constraint
 Type)
+ %7B
 %0A   
@@ -3857,188 +3857,294 @@
     
-                .appendKeyStringValue(%0A                                RelationshipConstraintTableInfo.Columns.CONSTRAINT_TYPE, relationshipConstraintType)%0A                        
+return cf.subQuery(IdentifiableColumns.UID).inTableWhere(%0A                RelationshipConstraintTableInfo.TABLE_INFO.name(),%0A                RelationshipConstraintTableInfo.Columns.RELATIONSHIP_TYPE,%0A                constraintClauseBuilder(relationshipEntityType, relationshipEntityUid)
 .app
@@ -4162,24 +4162,16 @@
 gValue(%0A
-        
         
@@ -4230,38 +4230,51 @@
 mns.
-TRACKED_ENTITY_TYPE, objectUid
+CONSTRAINT_TYPE, relationshipConstraintType
 ));%0A
@@ -4418,11 +4418,543 @@
 );%0A    %7D
+%0A%0A    private WhereClauseBuilder constraintClauseBuilder(RelationshipEntityType relationshipEntityType,%0A                                                       String relationshipEntityUid) %7B%0A        return new WhereClauseBuilder()%0A                .appendKeyStringValue(%0A                        RelationshipConstraintTableInfo.Columns.RELATIONSHIP_ENTITY, relationshipEntityType)%0A                .appendKeyStringValue(%0A                        RelationshipConstraintTableInfo.Columns.TRACKED_ENTITY_TYPE, relationshipEntityUid);%0A    %7D
 %0A%7D%0A

Divide the filter byConstraint in two methods