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

com.droidlogix.dbflare.hub.TransactionRestApiConfigurerAdapter Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
package com.droidlogix.dbflare.hub;

import com.dbflare.core.models.IEndpoint;
import com.droidlogix.dbflare.a2e.ApiCore;
import com.droidlogix.dbflare.datahandler.EntityRepository;
import com.droidlogix.dbflare.datahandler.models.IMetadataResult;
import com.droidlogix.dbflare.endpoint.core.IEndpointCore;
import com.droidlogix.dbflare.exceptions.DbFlareGenericException;
import com.droidlogix.dbflare.util.ApiUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author John Pili
 * @since 2016-10-15
 */
public abstract class TransactionRestApiConfigurerAdapter implements ITransactionRestApiConfigurerAdapter, ITransactionRestApi
{
	private EntityManager entityManager;
	private IEndpointCore endpointCore;
	private ObjectMapper objectMapper;

	@Override
	public void setEntityManager(EntityManager entityManager)
	{
		this.entityManager = entityManager;
	}

	@Override
	public void setEndpointCore(IEndpointCore endpointCore)
	{
		this.endpointCore = endpointCore;
	}

	@Override
	public void setObjectMapper(ObjectMapper objectMapper)
	{
		this.objectMapper = objectMapper;
	}

	@Override
	public String zinsert(String api, boolean prettify, boolean ignoreResult, HttpServletRequest request) throws DbFlareGenericException
	{
		try
		{
			IEndpoint result = endpointCore.getEndpointByApi(api);
			if (result != null)
			{
				List tmp = null;
				tmp = new ApiCore(entityManager, endpointCore, objectMapper).processBindingFromRequestToList(result.getQueryResultBinding(), request);
				if (tmp != null)
				{
					if (ignoreResult)
					{
						new EntityRepository(entityManager).create(tmp);
					}
					else
					{
						if (!prettify)
						{
							return objectMapper.writeValueAsString(new EntityRepository(entityManager).create(tmp));
						}
						else
						{
							return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(new EntityRepository(entityManager).create(tmp));
						}
					}
				}
			}
			else
			{
				throw new Exception("API not found");
			}
		}
		catch(Exception exception)
		{
			throw new DbFlareGenericException(exception.getMessage());
		}
		return null;
	}

	@Override
	public String zupdate(String api, boolean prettify, boolean ignoreResult, HttpServletRequest request) throws DbFlareGenericException
	{
		try
		{
			IEndpoint result = endpointCore.getEndpointByApi(api);
			if (result != null)
			{
				Map parameters = new ApiCore(entityManager, endpointCore, objectMapper).processIncomingURLParameters(result, ApiUtil.hashifyRequestParameter(request));

				Object tmp = null;
				tmp = new ApiCore(entityManager, endpointCore, objectMapper).processBindingFromRequest(Class.forName(result.getQueryResultBinding()), request);
				if (tmp != null)
				{
					if (ignoreResult)
					{
						new EntityRepository(entityManager).update(tmp, result.getQueryString(), parameters);
					}
					else
					{
						if (!prettify)
						{
							return objectMapper.writeValueAsString(new EntityRepository(entityManager).update(tmp, result.getQueryString(), parameters));
						}
						else
						{
							return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(new EntityRepository(entityManager).update(tmp, result.getQueryString(), parameters));
						}
					}
				}
			}
			else
			{
				throw new Exception("API not found");
			}
		}
		catch(Exception exception)
		{
			throw new DbFlareGenericException(exception.getMessage(), exception.getCause());
		}
		return null;
	}

	@Override
	public String zdelete(String api, boolean prettify, boolean ignoreResult, HttpServletRequest request) throws DbFlareGenericException
	{
		try
		{
			IEndpoint result = endpointCore.getEndpointByApi(api);
			if (result != null)
			{
				Map parameters = new ApiCore(entityManager, endpointCore, objectMapper).processIncomingURLParameters(result, ApiUtil.hashifyRequestParameter(request));
				if (ignoreResult)
				{
					new EntityRepository(entityManager).delete(result.getQueryString(), parameters);
				}
				else
				{
					if (!prettify)
					{
						return objectMapper.writeValueAsString(new EntityRepository(entityManager).delete(result.getQueryString(), parameters));
					}
					else
					{
						return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(new EntityRepository(entityManager).delete(result.getQueryString(), parameters));
					}
				}
			}
			else
			{
				throw new Exception("API not found");
			}
		}
		catch(Exception exception)
		{
			throw new DbFlareGenericException(exception.getMessage(), exception.getCause());
		}
		return null;
	}

	@Override
	public List extractMetadata(String nativeSqlQuery) throws DbFlareGenericException
	{
		try
		{
			return new EntityRepository(entityManager).extractMetadata(nativeSqlQuery);
		}
		catch(SQLException sqlException)
		{
			throw new DbFlareGenericException(sqlException.getMessage(), sqlException.getCause());
		}
	}
}