package org.c4sg.mapper;

import org.c4sg.dao.OrganizationDAO;
import org.c4sg.dto.CreateProjectDTO;
import org.c4sg.dto.JobTitleDTO;
import org.c4sg.dto.ProjectDTO;
import org.c4sg.entity.JobTitle;
import org.c4sg.entity.Project;
import org.c4sg.entity.UserProject;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;
import java.util.List;

@Component
public class ProjectMapper extends ModelMapper{
	@Autowired
	private OrganizationDAO organizationDAO;
	
	public ProjectDTO getProjectDtoFromEntity(Project project){
		getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
		ProjectDTO projectDTO = map(project, ProjectDTO.class);
		projectDTO.setOrganizationId(project.getOrganization().getId().toString());
		projectDTO.setOrganizationName(project.getOrganization().getName());
		return projectDTO;
	}
	
	public ProjectDTO getProjectDtoFromEntity(UserProject userProject){
		Type projectTypeDTO = new TypeToken<ProjectDTO>() {}.getType();
		ProjectDTO projectDTO = map(userProject.getProject(), projectTypeDTO);
		return projectDTO;
	}

	public List<ProjectDTO> getDtosFromEntities(List<Project> projects){
        Type listTypeDTO = new TypeToken<List<ProjectDTO>>() {}.getType();
		return map(projects, listTypeDTO);
	}

	public Project getProjectEntityFromDto(ProjectDTO projectDTO){
		Project project = map(projectDTO, Project.class);
		return project;
	}
	
	public Project getProjectEntityFromCreateProjectDto(CreateProjectDTO createProjectDTO){
		getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
		Project project = map(createProjectDTO, Project.class);
		project.setOrganization(organizationDAO.findOne(createProjectDTO.getOrganizationId()));
		return project;
	}	
	
	public List<JobTitleDTO> getJobTitleDtosFromEntities(List<JobTitle> jobTitles){
        Type listTypeDTO = new TypeToken<List<JobTitleDTO>>() {}.getType();
		return map(jobTitles, listTypeDTO);
	}
}

@@ -528,16 +528,71 @@
 t.Type;%0A
+import java.util.ArrayList;%0Aimport java.util.Iterator;%0A
 import j
@@ -1392,49 +1392,50 @@
 s)%7B%0A
-        Type listTypeDTO = new TypeToken%3C
+%09%09List%3CProjectDTO%3E projectList = new Array
 List
@@ -1450,61 +1450,220 @@
 DTO%3E
-%3E
 ()
- %7B%7D.getType();%0A%09%09return map(projects, listTypeDTO)
+;%0A%09%09Iterator%3CProject%3E projectIter = projects.iterator();%0A%09%09while (projectIter.hasNext()) %7B%0A%09%09%09Project project = projectIter.next();%0A%09%09%09projectList.add(getProjectDtoFromEntity(project));%0A%09%09%7D%0A%09%09return projectList
 ;%0A%09%7D

Populate org ID for project