/*
 * Copyright (c) 2017, 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 android.support.annotation.Nullable;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.google.auto.value.AutoValue;

@AutoValue
@JsonDeserialize(builder = AutoValue_RelationshipItem.Builder.class)
public abstract class RelationshipItem {

    @Nullable
    @JsonProperty()
    public abstract RelationshipItemTrackedEntityInstance trackedEntityInstance();

    @Nullable
    @JsonProperty()
    public abstract RelationshipItemEnrollment enrollment();

    @Nullable
    @JsonProperty()
    public abstract RelationshipItemEvent event();

    public boolean hasTrackedEntityInstance() {
        return trackedEntityInstance() != null;
    }

    public boolean hasEnrollment() {
        return enrollment() != null;
    }

    public boolean hasEvent() {
        return event() != null;
    }

    public String elementUid() {
        if (hasTrackedEntityInstance()) {
            return trackedEntityInstance().trackedEntityInstance();
        } else if (hasEnrollment()) {
            return enrollment().enrollment();
        } else {
            return event().event();
        }
    }

    public static Builder builder() {
        return new AutoValue_RelationshipItem.Builder();
    }

    @AutoValue.Builder
    @JsonPOJOBuilder(withPrefix = "")
    public abstract static class Builder {
        public abstract Builder trackedEntityInstance(RelationshipItemTrackedEntityInstance trackedEntityInstance);

        public abstract Builder enrollment(RelationshipItemEnrollment enrollment);

        public abstract Builder event(RelationshipItemEvent event);

        protected abstract RelationshipItem autoBuild();

        @SuppressWarnings("PMD.NPathComplexity")
        public RelationshipItem build() {
            RelationshipItem item = autoBuild();
            int teiCount = item.trackedEntityInstance() == null ? 0 : 1;
            int enrollmentCount = item.enrollment() == null ? 0 : 1;
            int eventCount = item.event() == null ? 0 : 1;
            if (teiCount + enrollmentCount + eventCount == 1) {
                return item;
            } else {
                throw new IllegalArgumentException("Item must have either a TEI, enrollment or event");
            }
        }
    }
}

@@ -1615,37 +1615,121 @@
 oid.
-support.annotation.Nullable;%0A
+database.Cursor;%0Aimport android.support.annotation.Nullable;%0A%0Aimport com.fasterxml.jackson.annotation.JsonIgnore;
 %0Aimp
@@ -1927,34 +1927,587 @@
 om.g
-oogle.auto.value.AutoValue
+abrielittner.auto.value.cursor.ColumnAdapter;%0Aimport com.google.auto.value.AutoValue;%0A%0Aimport org.hisp.dhis.android.core.common.Model;%0Aimport org.hisp.dhis.android.core.data.database.DbRelationshipConstraintTypeColumnAdapter;%0Aimport org.hisp.dhis.android.core.data.database.RelationshipItemEnrollmentColumnAdapter;%0Aimport org.hisp.dhis.android.core.data.database.RelationshipItemEventColumnAdapter;%0Aimport org.hisp.dhis.android.core.data.database.RelationshipItemTrackedEntityInstanceColumnAdapter;%0Aimport org.hisp.dhis.android.core.data.database.RelationshipWithUidColumnAdapter
 ;%0A%0A@
@@ -2624,16 +2624,33 @@
 hipItem 
+implements Model 
 %7B%0A%0A    @
@@ -2663,33 +2663,423 @@
 le%0A    @Json
-Property(
+Ignore()%0A    @ColumnAdapter(RelationshipWithUidColumnAdapter.class)%0A    public abstract Relationship relationship();%0A%0A    @Nullable%0A    @JsonIgnore()%0A    @ColumnAdapter(DbRelationshipConstraintTypeColumnAdapter.class)%0A    public abstract RelationshipConstraintType relationshipItemType();%0A%0A    @Nullable%0A    @JsonProperty()%0A    @ColumnAdapter(RelationshipItemTrackedEntityInstanceColumnAdapter.class
 )%0A    public
@@ -3178,32 +3178,98 @@
 @JsonProperty()%0A
+    @ColumnAdapter(RelationshipItemEnrollmentColumnAdapter.class)%0A
     public abstr
@@ -3344,24 +3344,85 @@
 nProperty()%0A
+    @ColumnAdapter(RelationshipItemEventColumnAdapter.class)%0A
     public a
@@ -4111,24 +4111,194 @@
 r();%0A    %7D%0A%0A
+    static RelationshipItem create(Cursor cursor) %7B%0A        return $AutoValue_RelationshipItem.createFromCursor(cursor);%0A    %7D%0A%0A    public abstract Builder toBuilder();%0A%0A
     @AutoVal
@@ -4389,16 +4389,63 @@
 ilder %7B%0A
+%0A        public abstract Builder id(Long id);%0A%0A
         
@@ -4702,24 +4702,202 @@
 nt event);%0A%0A
+        public abstract Builder relationship(Relationship relationship);%0A%0A        public abstract Builder relationshipItemType(RelationshipConstraintType relationshipItemType);%0A%0A
         prot
@@ -5532,9 +5532,8 @@
 %0A    %7D%0A%7D
-%0A

Unify Relationship item