<commit_before>package org.slc.sli.api.config;

import java.util.List;

import org.slc.sli.api.service.AssociationService;
import org.slc.sli.api.service.Treatment;
import org.slc.sli.api.service.Validator;
import org.slc.sli.dal.repository.EntityRepository;

/**
 * Definition of an association resource
 * 
 * @author nbrown
 * 
 */
public class AssociationDefinition extends EntityDefinition {
    private final EntityDefinition sourceEntity;
    private final EntityDefinition targetEntity;
    
    public AssociationDefinition(String type, String collectionName, String resourceName, List<Treatment> treatments,
            List<Validator> validators, EntityRepository repo, EntityDefinition sourceEntity,
            EntityDefinition targetEntity, String sourceIdKey) {
        super(type, resourceName, new BasicAssocService(collectionName, treatments, validators, repo, sourceEntity,
                sourceIdKey));
        this.sourceEntity = sourceEntity;
        this.targetEntity = targetEntity;
    }
    
    /**
     * The source of the association
     * 
     * @return
     */
    public EntityDefinition getSourceEntity() {
        return sourceEntity;
    }
    
    /**
     * The target of the association
     * 
     * @return
     */
    public EntityDefinition getTargetEntity() {
        return targetEntity;
    }
    
    @Override
    public AssociationService getService() {
        return (AssociationService) super.getService();
    }
    
    /**
     * Create a builder for an entity definition with the same collection and resource name
     * 
     * @param entityName
     *            the collection/resource name
     * @return the builder to create the entity definition
     */
    public static AssocBuilder makeAssoc(String entityName) {
        return new AssocBuilder(entityName);
    }
    
    /**
     * Create a builder for an entity definition
     * 
     * @param collectionName
     *            the name of the entity in the db
     * @param resourceName
     *            the name of the entity in the ReST uri
     * @return the builder to create the entity definition
     */
    public static AssocBuilder makeAssoc(String type, String collectionName, String resourceName) {
        return new AssocBuilder(type);
    }
    
    public static class AssocBuilder extends EntityDefinition.Builder {
        private EntityDefinition sourceEntity;
        private EntityDefinition targetEntity;
        private String sourceIdKey;
        
        /**
         * Create a builder for an association definition
         * 
         * @param collectionName
         *            the name of the association in the db
         * @param resourceName
         *            the name of the association in the ReST uri
         */
        public AssocBuilder(String type) {
            super(type);
        }
        
        /**
         * Sets the source definition
         * 
         * @param source
         *            the source of the association
         * @return the builder
         */
        public AssocBuilder from(EntityDefinition source) {
            this.sourceEntity = source;
            if (sourceIdKey == null) {
                sourceIdKey = source.getType() + "Id";
            }
            return this;
        }
        
        /**
         * Sets the target definition
         * 
         * @param target
         *            the target definition
         * @return the builder
         */
        public AssocBuilder to(EntityDefinition target) {
            this.targetEntity = target;
            return this;
        }
        
        /**
         * Sets the key for the source id
         * 
         * @param sourceID
         * @return
         */
        public AssocBuilder indexAs(String sourceID) {
            this.sourceIdKey = sourceID;
            return this;
        }
        
        @Override
        public AssocBuilder withTreatments(Treatment... treatments) {
            super.withTreatments(treatments);
            return this;
        }
        
        @Override
        public AssocBuilder withValidators(Validator... validators) {
            super.withValidators(validators);
            return this;
        }
        
        @Override
        public AssocBuilder storeAs(String collectionName) {
            super.storeAs(collectionName);
            return this;
        }
        
        @Override
        public AssocBuilder storeIn(EntityRepository repo) {
            super.storeIn(repo);
            return this;
        }
        
        @Override
        public AssocBuilder exposeAs(String resourceName) {
            super.exposeAs(resourceName);
            return this;
        }
        
        @Override
        public AssociationDefinition build() {
            return new AssociationDefinition(getType(), getCollectionName(), getResourceName(), getTreatments(),
                    getValidators(), getRepo(), sourceEntity, targetEntity, sourceIdKey);
        }
        
    }
}<commit_msg>A little refactoring IN-PROGRESS - task TA647: Add associations to service layer  https://rally1.rallydev.com/slm/detail/tk/4920074625<commit_after>package org.slc.sli.api.config;

import org.slc.sli.api.service.AssociationService;
import org.slc.sli.api.service.Treatment;
import org.slc.sli.api.service.Validator;
import org.slc.sli.dal.repository.EntityRepository;

/**
 * Definition of an association resource
 * 
 * @author nbrown
 * 
 */
public class AssociationDefinition extends EntityDefinition {
    private final EntityDefinition sourceEntity;
    private final EntityDefinition targetEntity;
    
    public AssociationDefinition(String type, String resourceName, AssociationService service,
            EntityDefinition sourceEntity, EntityDefinition targetEntity, String sourceIdKey) {
        super(type, resourceName, service);
        this.sourceEntity = sourceEntity;
        this.targetEntity = targetEntity;
    }
    
    /**
     * The source of the association
     * 
     * @return
     */
    public EntityDefinition getSourceEntity() {
        return sourceEntity;
    }
    
    /**
     * The target of the association
     * 
     * @return
     */
    public EntityDefinition getTargetEntity() {
        return targetEntity;
    }
    
    @Override
    public AssociationService getService() {
        return (AssociationService) super.getService();
    }
    
    /**
     * Create a builder for an entity definition with the same collection and resource name
     * 
     * @param entityName
     *            the collection/resource name
     * @return the builder to create the entity definition
     */
    public static AssocBuilder makeAssoc(String entityName) {
        return new AssocBuilder(entityName);
    }
    
    /**
     * Create a builder for an entity definition
     * 
     * @param collectionName
     *            the name of the entity in the db
     * @param resourceName
     *            the name of the entity in the ReST uri
     * @return the builder to create the entity definition
     */
    public static AssocBuilder makeAssoc(String type, String collectionName, String resourceName) {
        return new AssocBuilder(type);
    }
    
    public static class AssocBuilder extends EntityDefinition.Builder {
        private EntityDefinition sourceEntity;
        private EntityDefinition targetEntity;
        private String sourceIdKey;
        
        /**
         * Create a builder for an association definition
         * 
         * @param collectionName
         *            the name of the association in the db
         * @param resourceName
         *            the name of the association in the ReST uri
         */
        public AssocBuilder(String type) {
            super(type);
        }
        
        /**
         * Sets the source definition
         * 
         * @param source
         *            the source of the association
         * @return the builder
         */
        public AssocBuilder from(EntityDefinition source) {
            this.sourceEntity = source;
            if (sourceIdKey == null) {
                sourceIdKey = source.getType() + "Id";
            }
            return this;
        }
        
        /**
         * Sets the target definition
         * 
         * @param target
         *            the target definition
         * @return the builder
         */
        public AssocBuilder to(EntityDefinition target) {
            this.targetEntity = target;
            return this;
        }
        
        /**
         * Sets the key for the source id
         * 
         * @param sourceID
         * @return
         */
        public AssocBuilder indexAs(String sourceID) {
            this.sourceIdKey = sourceID;
            return this;
        }
        
        @Override
        public AssocBuilder withTreatments(Treatment... treatments) {
            super.withTreatments(treatments);
            return this;
        }
        
        @Override
        public AssocBuilder withValidators(Validator... validators) {
            super.withValidators(validators);
            return this;
        }
        
        @Override
        public AssocBuilder storeAs(String collectionName) {
            super.storeAs(collectionName);
            return this;
        }
        
        @Override
        public AssocBuilder storeIn(EntityRepository repo) {
            super.storeIn(repo);
            return this;
        }
        
        @Override
        public AssocBuilder exposeAs(String resourceName) {
            super.exposeAs(resourceName);
            return this;
        }
        
        @Override
        public AssociationDefinition build() {
            AssociationService service = new BasicAssocService(getCollectionName(), getTreatments(), getValidators(),
                    getRepo(), sourceEntity, sourceIdKey);
            return new AssociationDefinition(getType(), getResourceName(), service, sourceEntity, targetEntity,
                    sourceIdKey);
        }
        
    }
}
{'fraction_non_alphanumeric': 0.06931773105910354, 'fraction_numerical': 0.001388337961126537, 'mean_word_length': 1.793628808864266, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, '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': '2654938', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2384, 'n_words': 691}