<commit_before>/*
 * Copyright 2012-2013 inBloom, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.slc.sli.dal.convert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slc.sli.api.constants.EntityNames;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.MongoEntity;

/**
 * assessment converter that transform assessment superdoc to sli assessment schema
 * 
 * @author <NAME> <EMAIL>
 */

public class AssessmentConverter implements SuperdocConverter {
    
    @Override
    public void subdocToBodyField(Entity entity) {

        if (entity != null && entity.getType().equals(EntityNames.ASSESSMENT)) {
            if ((entity.getEmbeddedData() != null) && !entity.getEmbeddedData().isEmpty()) {
                for (Map.Entry<String, List<Entity>> enbDocList : entity.getEmbeddedData().entrySet()) {
                    List<Map<String, Object>> subDocbody = new ArrayList<Map<String, Object>>();
                    for (Entity subEntity : enbDocList.getValue()) {
                        // remove the assessmentId field
                        subEntity.getBody().remove("assessmentId");
                        subDocbody.add(subEntity.getBody());
                    }
                    entity.getBody().put(enbDocList.getKey(), subDocbody);
                }
                entity.getEmbeddedData().clear();
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public void bodyFieldToSubdoc(Entity entity) {
        if (entity != null && entity.getType().equals(EntityNames.ASSESSMENT)) {
            // convert assessmentitem
            if (entity.getBody().get("assessmentItem") != null) {
                List<Entity> subdocAssessmentItems = new ArrayList<Entity>();
                List<Map<String, Object>> assessmentItems = (List<Map<String, Object>>) entity.getBody().get(
                        "assessmentItem");
                for (Map<String, Object> assessmentItem : assessmentItems) {
                 // TODO generate cat did for assessmentitem
                    MongoEntity subdocAssessmentItem = new MongoEntity("assessmentItem", "assessment_idassessmentItem_id", assessmentItem, null);
                    
                    
                    subdocAssessmentItems.add(subdocAssessmentItem);
                }
                entity.getEmbeddedData().put("assessmentItem", subdocAssessmentItems);
                entity.getBody().remove("assessmentItem");
            }
            // TODO convert objectiveAssessment
            if (entity.getBody().get("objectiveAssessment") != null) {
                // Do something
            }
        }
    }
    
    @Override
    public void subdocToBodyField(Iterable<Entity> entities) {
        if (entities != null) {
            for (Entity entity : entities) {
                subdocToBodyField(entity);
            }
        }
    }
    
    @Override
    public void bodyFieldToSubdoc(Iterable<Entity> entities) {
        // TODO Auto-generated method stub
    }
}<commit_msg>[US5392] assessment converter refactor to add objectiveassessment support<commit_after>/*
 * Copyright 2012-2013 inBloom, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.slc.sli.dal.convert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.slc.sli.api.constants.EntityNames;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.MongoEntity;

/**
 * assessment converter that transform assessment superdoc to sli assessment schema
 * 
 * @author <NAME> <EMAIL>
 */

public class AssessmentConverter implements SuperdocConverter {
    
    @Override
    public void subdocToBodyField(Entity entity) {
        if (entity != null && entity.getType().equals(EntityNames.ASSESSMENT)) {
            subdocsToBody(entity, "assessmentItem", Arrays.asList("assessmentId")); 
            subdocsToBody(entity, "objectiveAssessment", Arrays.asList("assessmentId")); 
            entity.getEmbeddedData().clear();
        }
    }
   
    private Entity subdocsToBody(Entity parent, String subentityType, List<String> removeFields) {
    	if (parent.getEmbeddedData() == null || parent.getEmbeddedData().isEmpty()) {
    		return parent;
    	}
    	
    	List<Entity> subdocs = parent.getEmbeddedData().remove(subentityType);
    	if (subdocs == null || subdocs.isEmpty()) {
    		return parent;
    	}
    	
    	List<Map<String, Object>> subdocBody = new ArrayList<Map<String, Object>>();
    	for (Entity e : subdocs) {
    		for (String field : removeFields) {
    			e.getBody().remove(field);
    		}
    		subdocBody.add(e.getBody());
    	}
    	parent.getBody().put(subentityType, subdocBody);
    	
    	return parent;
    } 
    
    @Override
    public void bodyFieldToSubdoc(Entity entity) {
        if (entity != null && entity.getType().equals(EntityNames.ASSESSMENT)) {
        	bodyToSubdocs(entity, "assessmentItem", "assessmentId");
        	bodyToSubdocs(entity, "objectiveAssessment", "assessmentId");
        }
    }
    
    private Entity bodyToSubdocs(Entity parent, String subentityType, String parentKey) {
    	if (parent.getBody().get(subentityType) != null) {
    		List<Entity> subdocs = new ArrayList<Entity>();
    		@SuppressWarnings("unchecked")
			List<Map<String, Object>> subdocInBody = (List<Map<String, Object>>) parent.getBody().get(subentityType);
    		for (Map<String, Object> inbodyDoc : subdocInBody) {
    			inbodyDoc.put(parentKey, parent.getEntityId());
    			MongoEntity subdoc = new MongoEntity(subentityType, null, inbodyDoc, null);
    			subdocs.add(subdoc);
    		}
    		
    		if (!subdocs.isEmpty()) {
    			parent.getEmbeddedData().put(subentityType, subdocs);
    			parent.getBody().remove(subentityType);
    		}
    	}
    	
    	return parent;
    }
    
    @Override
    public void subdocToBodyField(Iterable<Entity> entities) {
        if (entities != null) {
            for (Entity entity : entities) {
                subdocToBodyField(entity);
            }
        }
    }
    
    @Override
    public void bodyFieldToSubdoc(Iterable<Entity> entities) {
    	if (entities != null) {
    		for (Entity entity : entities) {
    			bodyFieldToSubdoc(entity);
    		}
    	}
    }
    
}
{'fraction_non_alphanumeric': 0.10839445802770986, 'fraction_numerical': 0.0038033143167617496, 'mean_word_length': 2.59521484375, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4503816', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2054, 'n_words': 504}