/*
 * Created on 9 Dec 2015 ( Time 11:08:58 )
 * Generated by Telosys Tools Generator ( version 2.1.1 )
 */
package com.abm.mainet.common.master.mapper;

import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.stereotype.Component;
import com.abm.mainet.common.domain.LocationMasEntity;
import com.abm.mainet.common.domain.Organisation;
import com.abm.mainet.common.master.dto.DepartmentDTO;
import com.abm.mainet.common.utility.AbstractServiceMapper;



/**
 * Mapping between entity beans and display beans.
 */
@Component
public class DepartmentServiceMapper extends AbstractServiceMapper {

	/**
	 * ModelMapper : bean to bean mapping library.
	 */
	private ModelMapper modelMapper;
	
	/**
	 * Constructor.
	 */
	public DepartmentServiceMapper() {
		modelMapper = new ModelMapper();
		modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
	}

	/**
	 * Mapping from 'DepartmentEntity' to 'Department'
	 * @param departmentEntity
	 */
	public DepartmentDTO mapDepartmentEntityToDepartment(LocationMasEntity departmentEntity) {
		if(departmentEntity == null) {
			return null;
		}

		//--- Generic mapping 
		DepartmentDTO department = map(departmentEntity, DepartmentDTO.class);

		//--- Link mapping ( link to TbOrganisation )
		if(departmentEntity.getOrganisation() != null) {
			department.setOrgid(departmentEntity.getOrganisation().getOrgid());
		}
		return department;
	}
	
	/**
	 * Mapping from 'Department' to 'DepartmentEntity'
	 * @param department
	 * @param departmentEntity
	 */
	public void mapDepartmentToDepartmentEntity(DepartmentDTO department, LocationMasEntity departmentEntity) {
		if(department == null) {
			return;
		}

		//--- Generic mapping 
		map(department, departmentEntity);

		//--- Link mapping ( link : department )
		if( hasLinkToTbOrganisation(department) ) {
			Organisation organisation=new Organisation();
			organisation.setOrgid(department.getOrgid());
			departmentEntity.setOrganisation(organisation);
		} else {
			departmentEntity.setOrganisation(null);
		}

	}
	
	/**
	 * Verify that TbOrganisation id is valid.
	 * @param TbOrganisation TbOrganisation
	 * @return boolean
	 */
	private boolean hasLinkToTbOrganisation(DepartmentDTO department) {
		if(department.getOrgid() != null) {
			return true;
		}
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ModelMapper getModelMapper() {
		return modelMapper;
	}

	protected void setModelMapper(ModelMapper modelMapper) {
		this.modelMapper = modelMapper;
	}

}