package org.slc.sli.ingestion;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * Describes an entity that has been staged by ingestion.
 *
 * @author dduran
 *
 */
public class IngestionStagedEntity implements Serializable {

    private static final long serialVersionUID = 2248791042291917912L;

    private final String collectionNameAsStaged;

    private final EdfiEntity edfiEntity;

    public IngestionStagedEntity(String parsedCollectionName, EdfiEntity edfiEntity) {
        this.collectionNameAsStaged = parsedCollectionName;
        this.edfiEntity = edfiEntity;
    }

    /**
     * The name of the collection as it has been parsed/staged by ingestion.
     * It may or may not be equal to the corresponding EdFi entity name.
     *
     * @return
     */
    public String getCollectionNameAsStaged() {
        return collectionNameAsStaged;
    }

    /**
     * The corresponding Edfi entity.
     *
     * @return
     */
    public EdfiEntity getEdfiEntity() {
        return edfiEntity;
    }

    @Override
    public String toString() {
        return "{ collectionNameAsStaged: \"" + collectionNameAsStaged + "\", edfiEntity: \"" + edfiEntity
                + "\"}";
    }

    /**
     * Removes entities which have parent entities in the same set
     *
     * @param impure
     *            set of edfi entities to purify
     * @return purifed set which only contains entities which have no dependencies in the provided
     *         set
     */
    public static Set<IngestionStagedEntity> cleanse(Set<IngestionStagedEntity> impure) {
        Set<IngestionStagedEntity> pure = new HashSet<IngestionStagedEntity>(impure);

        for (IngestionStagedEntity outer : impure) {
            if (outer.getEdfiEntity() != null) {
                for (IngestionStagedEntity inner : impure) {
                    if (outer != inner && inner.getEdfiEntity() != null
                            && outer.getEdfiEntity().getNeededEntities().contains(inner.getEdfiEntity())) {
                        pure.remove(outer);
                        break;
                    }
                }
            }
        }

        return pure;
    }

}

@@ -2193,10 +2193,957 @@
 %0A    %7D%0A%0A
+    @Override%0A    public int hashCode() %7B%0A        final int prime = 31;%0A        int result = 1;%0A        result = prime * result + ((collectionNameAsStaged == null) ? 0 : collectionNameAsStaged.hashCode());%0A        result = prime * result + ((edfiEntity == null) ? 0 : edfiEntity.hashCode());%0A        return result;%0A    %7D%0A%0A    @Override%0A    public boolean equals(Object obj) %7B%0A        if (this == obj)%0A            return true;%0A        if (obj == null)%0A            return false;%0A        if (getClass() != obj.getClass())%0A            return false;%0A        IngestionStagedEntity other = (IngestionStagedEntity) obj;%0A        if (collectionNameAsStaged == null) %7B%0A            if (other.collectionNameAsStaged != null)%0A                return false;%0A        %7D else if (!collectionNameAsStaged.equals(other.collectionNameAsStaged))%0A            return false;%0A        if (edfiEntity != other.edfiEntity)%0A            return false;%0A        return true;%0A    %7D%0A%0A
 %7D%0A

define hashCode() and equals() for this class. it is used in collections and implements Serializable!