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

com.jwebmp.entityassist.injections.EntityAssistBinder Maven / Gradle / Ivy

There is a newer version: 0.68.0.1
Show newest version
package com.jwebmp.entityassist.injections;

import com.google.inject.Key;
import com.google.inject.Singleton;
import com.google.inject.name.Names;
import com.jwebmp.entityassist.EntityAssistException;
import com.jwebmp.entityassist.services.EntityAssistIDMapping;
import com.jwebmp.guicedinjection.GuiceContext;
import com.jwebmp.guicedinjection.abstractions.GuiceInjectorModule;
import com.jwebmp.guicedinjection.interfaces.IGuiceDefaultBinder;
import com.jwebmp.guicedinjection.pairing.Pair;

import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class EntityAssistBinder
		implements IGuiceDefaultBinder
{
	public static final Key entityIDMappingsKey = Key.get(Map.class, Names.named("EntityAssistIDMap"));

	private final Map, Pair, Class>> entityIDMappings = new ConcurrentHashMap<>();

	@SuppressWarnings("unchecked")
	public static EntityAssistIDMapping lookup(Class dbType, Class fieldType)
	{
		Map, Pair, Class>> map = GuiceContext.get(entityIDMappingsKey);
		for (Map.Entry, Pair, Class>> entry : map.entrySet())
		{
			Class db = entry.getKey();
			Pair, Class> field = entry.getValue();
			if (field.getKey()
			         .equals(dbType) && field.getValue()
			                                 .equals(fieldType))
			{
				return GuiceContext.get(db);
			}
		}
		throw new EntityAssistException("Unable to find an ID mapping for db type [" + dbType + "] and id field type [" + fieldType +
		                                "]. You can create a service for EntityAssistIDMapping to resolve this");
	}

	@Override
	public void onBind(GuiceInjectorModule module)
	{
		module.bind(entityIDMappingsKey)
		      .toProvider(() ->
		                  {
			                  if (entityIDMappings.isEmpty())
			                  {
				                  @NotNull Set loader = GuiceContext.instance()
				                                                                           .getLoader(EntityAssistIDMapping.class, ServiceLoader.load(EntityAssistIDMapping.class));
				                  for (EntityAssistIDMapping mapping : loader)
				                  {
					                  @SuppressWarnings("unchecked")
					                  Pair, Class> pair = new Pair<>(mapping.getDBClassType(), mapping.getObjectClassType());
					                  entityIDMappings.put(mapping.getClass(), pair);
				                  }
			                  }
			                  return entityIDMappings;
		                  })
		      .in(Singleton.class);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy