Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.tools;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javassist.util.proxy.ProxyFactory;
import org.hibernate.Session;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.proxy.HibernateProxy;
/**
* @author Adam Warski (adam at warski dot org)
* @author Hern�n Chanfreau
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class Tools {
public static Map newHashMap() {
return new HashMap();
}
public static Set newHashSet() {
return new HashSet();
}
public static Map newLinkedHashMap() {
return new LinkedHashMap();
}
public static boolean entitiesEqual(SessionImplementor session, String entityName, Object obj1, Object obj2) {
Object id1 = getIdentifier(session, entityName, obj1);
Object id2 = getIdentifier(session, entityName, obj2);
return objectsEqual(id1, id2);
}
public static Object getIdentifier(SessionImplementor session, String entityName, Object obj) {
if (obj == null) {
return null;
}
if (obj instanceof HibernateProxy) {
HibernateProxy hibernateProxy = (HibernateProxy) obj;
return hibernateProxy.getHibernateLazyInitializer().getIdentifier();
}
return session.getEntityPersister(entityName, obj).getIdentifier(obj, session);
}
public static Object getTargetFromProxy(SessionFactoryImplementor sessionFactoryImplementor, HibernateProxy proxy) {
if (!proxy.getHibernateLazyInitializer().isUninitialized()) {
return proxy.getHibernateLazyInitializer().getImplementation();
}
SessionImplementor sessionImplementor = proxy.getHibernateLazyInitializer().getSession();
Session tempSession = sessionImplementor==null
? sessionFactoryImplementor.openTemporarySession()
: sessionImplementor.getFactory().openTemporarySession();
try {
Object target = tempSession.get(
proxy.getHibernateLazyInitializer().getEntityName(),
proxy.getHibernateLazyInitializer().getIdentifier()
);
return target;
}
finally {
tempSession.close();
}
}
/**
* @param clazz Class wrapped with a proxy or not.
* @param Class type.
* @return Returns target class in case it has been wrapped with a proxy. If {@code null} reference is passed,
* method returns {@code null}.
*/
@SuppressWarnings({"unchecked"})
public static Class getTargetClassIfProxied(Class clazz) {
if (clazz == null) {
return null;
} else if (ProxyFactory.isProxyClass(clazz)) {
// Get the source class of Javassist proxy instance.
return (Class) clazz.getSuperclass();
}
return clazz;
}
public static boolean objectsEqual(Object obj1, Object obj2) {
if (obj1 == null) {
return obj2 == null;
}
return obj1.equals(obj2);
}
public static boolean arraysEqual(Object[] array1, Object[] array2) {
if (array1 == null) return array2 == null;
if (array2 == null || array1.length != array2.length) return false;
for (int i = 0; i < array1.length; ++i) {
if (array1[i] != null ? !array1[i].equals(array2[i]) : array2[i] != null) {
return false;
}
}
return true;
}
public static List iteratorToList(Iterator iter) {
List ret = new ArrayList();
while (iter.hasNext()) {
ret.add(iter.next());
}
return ret;
}
public static boolean iteratorsContentEqual(Iterator iter1, Iterator iter2) {
while (iter1.hasNext() && iter2.hasNext()) {
if (!iter1.next().equals(iter2.next())) {
return false;
}
}
//noinspection RedundantIfStatement
if (iter1.hasNext() || iter2.hasNext()) {
return false;
}
return true;
}
/**
* Transforms a list of arbitrary elements to a list of index-element pairs.
* @param list List to transform.
* @return A list of pairs: ((0, element_at_index_0), (1, element_at_index_1), ...)
*/
public static List> listToIndexElementPairList(List list) {
List> ret = new ArrayList>();
Iterator listIter = list.iterator();
for (int i=0; i data, String[] keys) {
Object[] ret = new Object[keys.length];
for (int i = 0; i < keys.length; ++i) {
ret[i] = data.get(keys[i]);
}
return ret;
}
/**
* @param clazz Source class.
* @param propertyName Property name.
* @return Property object or {@code null} if none with expected name has been found.
*/
public static XProperty getProperty(XClass clazz, String propertyName) {
XProperty property = getProperty(clazz, propertyName, "field");
if (property == null) {
property = getProperty(clazz, propertyName, "property");
}
return property;
}
/**
* @param clazz Source class.
* @param propertyName Property name.
* @param accessType Expected access type. Legal values are field and property.
* @return Property object or {@code null} if none with expected name and access type has been found.
*/
public static XProperty getProperty(XClass clazz, String propertyName, String accessType) {
for (XProperty property : clazz.getDeclaredProperties(accessType)) {
if (propertyName.equals(property.getName())) {
return property;
}
}
return null;
}
}