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.
/**
* Copyright 2014 Internet2
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
*/
package edu.internet2.middleware.grouper.hibernate;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import edu.internet2.middleware.grouperClient.collections.MultiKey;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Junction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.resource.transaction.spi.TransactionStatus;
import org.hibernate.type.ByteType;
import org.hibernate.type.CharacterType;
import org.hibernate.type.DateType;
import org.hibernate.type.DoubleType;
import org.hibernate.type.FloatType;
import org.hibernate.type.IntegerType;
import org.hibernate.type.LongType;
import org.hibernate.type.ObjectType;
import org.hibernate.type.ShortType;
import org.hibernate.type.StringType;
import org.hibernate.type.TimestampType;
import org.hibernate.type.TrueFalseType;
import org.hibernate.type.Type;
import edu.internet2.middleware.grouper.Field;
import edu.internet2.middleware.grouper.internal.dao.QueryOptions;
import edu.internet2.middleware.grouper.util.GrouperUtil;
import edu.internet2.middleware.subject.Source;
import edu.internet2.middleware.subject.Subject;
/**
* @author mchyzer
*
*/
public class HibUtils {
/**
* attach bind values to query
* @param query
*/
public static void attachBindValues(Query query, List bindVarNameParams) {
//note, dont call the method bindVarNameParams() so it doesnt lazyload...
if (bindVarNameParams != null) {
for (HibernateParam hibernateParam : bindVarNameParams) {
if (String.class.equals(hibernateParam.getType())) {
query.setString(hibernateParam.getName(), (String)hibernateParam.getValue());
} else if (Timestamp.class.equals(hibernateParam.getType())) {
query.setTimestamp(hibernateParam.getName(), (Date)hibernateParam.getValue());
} else if (Long.class.equals(hibernateParam.getType())) {
if (hibernateParam.getValue() == null) {
query.setBigDecimal(hibernateParam.getName(), null);
} else {
query.setLong(hibernateParam.getName(), (Long)hibernateParam.getValue());
}
} else if (Double.class.equals(hibernateParam.getType())) {
if (hibernateParam.getValue() == null) {
query.setBigDecimal(hibernateParam.getName(), null);
} else {
query.setDouble(hibernateParam.getName(), (Double)hibernateParam.getValue());
}
} else if (Integer.class.equals(hibernateParam.getType())) {
if (hibernateParam.getValue() == null) {
query.setBigDecimal(hibernateParam.getName(), null);
} else {
query.setInteger(hibernateParam.getName(), (Integer)hibernateParam.getValue());
}
} else {
throw new RuntimeException("Invalid bind var type: "
+ hibernateParam );
}
}
}
}
/**
* Whether the type of the class means that it should be handled as a primitive
* query vs a hibernate object query.
* @param clazz is the class to check.
* @return true if so.
*/
static boolean handleAsPrimitive(Class clazz) {
if (clazz.isArray()) {
clazz = clazz.getComponentType();
if (clazz == Object.class) {
return true;
}
}
if (clazz.isPrimitive() || clazz == java.lang.String.class
|| clazz == BigDecimal.class || clazz == Integer.class
|| clazz == java.sql.Date.class || clazz == java.util.Date.class
|| clazz == java.sql.Time.class || clazz == java.sql.Timestamp.class
|| clazz == java.sql.Clob.class || clazz == java.sql.Blob.class
|| clazz == java.sql.Ref.class || clazz == Boolean.class || clazz == Byte.class
|| clazz == Short.class || clazz == Integer.class || clazz == Float.class
|| clazz == Double.class || clazz == Long.class) {
return true;
}
return false;
}
/**
* Convert the params to friendly strings
*
* @param params
* @param types
* @return the string of the params (for logging)
*/
@SuppressWarnings("unchecked")
public static String paramsToString(Object params, Object types) {
//nothing to do if nothing to do
if (params == null && types == null) {
return "null";
}
List
* @param hibernateSession grouper hibernateSession, can be null if not known
* @param object to evict that was just retrieved, can be list or array
* @param onlyEvictIfNotNew true to only evict if this is a nested tx
*/
public static void evict(HibernateSession hibernateSession,
Object object, boolean onlyEvictIfNotNew) {
evict(hibernateSession, object, onlyEvictIfNotNew, true);
}
/**
*
* evict a list of objects from hibernate. do this always for two reasons:
* 1. If you edit an object that is in the hibernate session, and commit, it will
* commit those changes magically. Only objects called session.save(obj) or
* update etc should be committed
* 2. If you select an object, then try to store it back (but have a different
* reference, e.g. if the DTO went through it, then you will get an exception:
* "a different object with the same identifier value was already associated with the session"
*
* @param hibernateSession grouper hibernateSession, can be null if not known
* @param object to evict that was just retrieved, can be list or array
* @param onlyEvictIfNotNew true to only evict if this is a nested tx
* @param evictBeforeFlush if evict before flush (dont do this if iterating through list)
*/
public static void evict(HibernateSession hibernateSession,
Object object, boolean onlyEvictIfNotNew, boolean evictBeforeFlush) {
if (object instanceof Collection) {
HibUtils.evict(hibernateSession, (Collection)object, onlyEvictIfNotNew);
return;
}
//dont worry about it if new and only evicting if not new
if (hibernateSession != null && hibernateSession.isNewHibernateSession() && onlyEvictIfNotNew) {
return;
}
//if array, loop through
if (object != null && object.getClass().isArray()) {
if (evictBeforeFlush) {
hibernateSession.getSession().flush();
}
for (int i=0;i
* evict a list of objects from hibernate. do this always for two reasons:
* 1. If you edit an object that is in the hibernate session, and commit, it will
* commit those changes magically. Only objects called session.save(obj) or
* update etc should be committed
* 2. If you select an object, then try to store it back (but have a different
* reference, e.g. if the DTO went through it, then you will get an exception:
* "a different object with the same identifier value was already associated with the session"
*
* @param hibernateSession grouper hibernateSession
* @param list of objects from hibernate to evict
* @param onlyEvictIfNotNew true to only evict if this is a nested tx
*/
public static void evict(HibernateSession hibernateSession,
Collection