/**
* Copyright (C) 2008 - Abiquo Holdings S.L. All rights reserved.
*
* Please see /opt/abiquo/tomcat/webapps/legal/ on Abiquo server
* or contact contact@abiquo.com for licensing information.
*/
package com.abiquo.server.core.common.persistence;
import javax.persistence.EntityManager;
import org.hibernate.Session;
import com.abiquo.server.core.cloud.VirtualMachine;
import com.abiquo.server.core.enterprise.Enterprise;
import com.abiquo.server.core.enterprise.Scope;
import com.abiquo.server.core.infrastructure.Datacenter;
import com.abiquo.server.core.infrastructure.network.IpPoolManagement;
import com.abiquo.server.core.infrastructure.storage.DiskManagement;
import com.abiquo.server.core.infrastructure.storage.DvdManagement;
import com.abiquo.server.core.infrastructure.storage.VolumeManagement;
/**
* Utility methods to configure JPA.
*
* @author Ignasi Barrera
*/
public class JPAConfiguration
{
private static final String[] NOT_TEMPORAL_FILTERS = {VirtualMachine.NOT_TEMP,
VolumeManagement.NOT_TEMP, IpPoolManagement.NOT_TEMP, DiskManagement.NOT_TEMP,
DvdManagement.NOT_TEMP};
private static final String[] ONLY_TEMPORAL_FILTERS = {VirtualMachine.ONLY_TEMP,
VolumeManagement.ONLY_TEMP, IpPoolManagement.ONLY_TEMP, DiskManagement.ONLY_TEMP,
DvdManagement.ONLY_TEMP};
private static final String[] ONLY_SCOPE_FILTER =
{Enterprise.SCOPE_VIEW, Datacenter.SCOPE_VIEW};
public static EntityManager enableDefaultFilters(final EntityManager em)
{
return enableNotTemporalFilters(em);
}
public static EntityManager enableScopeFilter(final EntityManager em, final int idScope)
{
if (!em.isOpen())
{
throw new IllegalStateException("EntityManager must be open");
}
Session session = (Session) em.getDelegate();
for (String filter : ONLY_SCOPE_FILTER)
{
session.enableFilter(filter).setParameter(Scope.SCOPE_ID, idScope);
}
return em;
}
public static EntityManager disableScopeFilter(final EntityManager em)
{
if (!em.isOpen())
{
throw new IllegalStateException("EntityManager must be open");
}
Session session = (Session) em.getDelegate();
for (String filter : ONLY_SCOPE_FILTER)
{
session.disableFilter(filter);
}
return em;
}
public static EntityManager disableAllFilters(final EntityManager em)
{
if (!em.isOpen())
{
throw new IllegalStateException("EntityManager must be open");
}
Session session = (Session) em.getDelegate();
for (String filter : ONLY_TEMPORAL_FILTERS)
{
session.disableFilter(filter);
}
for (String filter : NOT_TEMPORAL_FILTERS)
{
session.disableFilter(filter);
}
return em;
}
public static EntityManager enableNotTemporalFilters(final EntityManager em)
{
if (!em.isOpen())
{
throw new IllegalStateException("EntityManager must be open");
}
Session session = (Session) em.getDelegate();
for (String filter : ONLY_TEMPORAL_FILTERS)
{
session.disableFilter(filter);
}
for (String filter : NOT_TEMPORAL_FILTERS)
{
session.enableFilter(filter);
}
return em;
}
public static EntityManager enableOnlyTemporalFilters(final EntityManager em)
{
if (!em.isOpen())
{
throw new IllegalStateException("EntityManager must be open");
}
Session session = (Session) em.getDelegate();
for (String filter : NOT_TEMPORAL_FILTERS)
{
session.disableFilter(filter);
}
for (String filter : ONLY_TEMPORAL_FILTERS)
{
session.enableFilter(filter);
}
return em;
}
}