All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.prodevelop.pui9.service.registry.ServiceRegistry Maven / Gradle / Ivy

There is a newer version: 1.18.0
Show newest version
package es.prodevelop.pui9.service.registry;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import es.prodevelop.pui9.model.dao.interfaces.ITableDao;
import es.prodevelop.pui9.model.dao.interfaces.IViewDao;
import es.prodevelop.pui9.model.dao.registry.DaoRegistry;
import es.prodevelop.pui9.model.dto.interfaces.IDto;
import es.prodevelop.pui9.model.dto.interfaces.ITableDto;
import es.prodevelop.pui9.model.dto.interfaces.IViewDto;
import es.prodevelop.pui9.service.interfaces.IService;

/**
 * This is a Registry for all the Services. It brings some useful methods to
 * manage the Services of PUI
 * 
 * @author Marc Gil - [email protected]
 */
@Component
@SuppressWarnings("rawtypes")
public class ServiceRegistry {

	@Autowired
	private DaoRegistry daoRegistry;

	private List> registeredServices;
	private Map, Class> mapServiceFromDto;
	private Map> mapServiceFromModelId;

	private ServiceRegistry() {
		registeredServices = new ArrayList<>();
		mapServiceFromDto = new LinkedHashMap<>();
		mapServiceFromModelId = new LinkedHashMap<>();
	}

	/**
	 * Registers a Service
	 */
	@SuppressWarnings("unchecked")
	public void registerService(Class serviceClass, Class tableDtoPkClass,
			Class tableDtoClass, Class viewDtoClass,
			Class tableDaoClass, Class viewDaoClass) {
		Class serviceIface = (Class) serviceClass.getInterfaces()[0];
		if (registeredServices.contains(serviceIface)) {
			return;
		}

		registeredServices.add(serviceIface);

		Class tableDtoPkIface = (Class) tableDtoPkClass.getInterfaces()[0];
		Class tableDtoIface = (Class) tableDtoClass.getInterfaces()[0];
		Class viewDtoIface = (Class) viewDtoClass.getInterfaces()[0];
		Class tableDaoIface = (Class) tableDaoClass.getInterfaces()[0];
		Class viewDaoIface = (Class) viewDaoClass.getInterfaces()[0];

		daoRegistry.relatedDaos(tableDaoClass, viewDaoClass);
		daoRegistry.relatedDaos(tableDaoIface, viewDaoIface);

		daoRegistry.relatedDtos(tableDtoPkClass, tableDtoClass, viewDtoClass);
		daoRegistry.relatedDtos(tableDtoPkIface, tableDtoIface, viewDtoIface);

		mapServiceFromDto.put(tableDtoPkClass, serviceIface);
		mapServiceFromDto.put(tableDtoPkIface, serviceIface);
		mapServiceFromDto.put(tableDtoClass, serviceIface);
		mapServiceFromDto.put(tableDtoIface, serviceIface);
		mapServiceFromDto.put(viewDtoClass, serviceIface);
		mapServiceFromDto.put(viewDtoIface, serviceIface);
	}

	@SuppressWarnings("unchecked")
	public void registerModelService(String modelId, Class serviceClass) {
		Class serviceIface = (Class) serviceClass.getInterfaces()[0];
		mapServiceFromModelId.put(modelId, serviceIface);
	}

	/**
	 * Get all the registered Services
	 */
	public List> getAllServices() {
		return registeredServices;
	}

	/**
	 * Get the ID related with the given DTO class
	 */
	public Class getServiceFromDto(Class clazz) {
		return mapServiceFromDto.get(clazz);
	}

	/**
	 * Get the Service class represented by the given Model
	 */
	public Class getServiceFromModelId(String modelId) {
		return mapServiceFromModelId.get(modelId);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy