1 package org.hibernate.pretty; 3 4 import java.util.ArrayList ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.hibernate.HibernateException; 13 import org.hibernate.EntityMode; 14 import org.hibernate.engine.SessionFactoryImplementor; 15 import org.hibernate.engine.TypedValue; 16 import org.hibernate.intercept.LazyPropertyInitializer; 17 import org.hibernate.metadata.ClassMetadata; 18 import org.hibernate.type.Type; 19 20 24 public final class Printer { 25 26 private SessionFactoryImplementor factory; 27 private static final Log log = LogFactory.getLog(Printer.class); 28 29 32 public String toString(Object entity, EntityMode entityMode) throws HibernateException { 33 34 ClassMetadata cm = factory.getClassMetadata( entity.getClass() ); 36 37 if ( cm==null ) return entity.getClass().getName(); 38 39 Map result = new HashMap (); 40 41 if ( cm.hasIdentifierProperty() ) { 42 result.put( 43 cm.getIdentifierPropertyName(), 44 cm.getIdentifierType().toLoggableString( cm.getIdentifier( entity, entityMode ), factory ) 45 ); 46 } 47 48 Type[] types = cm.getPropertyTypes(); 49 String [] names = cm.getPropertyNames(); 50 Object [] values = cm.getPropertyValues( entity, entityMode ); 51 for ( int i=0; i<types.length; i++ ) { 52 if ( !names[i].startsWith("_") ) { 53 String strValue = values[i]==LazyPropertyInitializer.UNFETCHED_PROPERTY ? 54 values[i].toString() : 55 types[i].toLoggableString( values[i], factory ); 56 result.put( names[i], strValue ); 57 } 58 } 59 return cm.getEntityName() + result.toString(); 60 } 61 62 public String toString(Type[] types, Object [] values) throws HibernateException { 63 List list = new ArrayList ( types.length * 5 ); 64 for ( int i=0; i<types.length; i++ ) { 65 if ( types[i]!=null ) list.add( types[i].toLoggableString( values[i], factory ) ); 66 } 67 return list.toString(); 68 } 69 70 public String toString(Map namedTypedValues) throws HibernateException { 71 Map result = new HashMap (); 72 Iterator iter = namedTypedValues.entrySet().iterator(); 73 while ( iter.hasNext() ) { 74 Map.Entry me = (Map.Entry ) iter.next(); 75 TypedValue tv = (TypedValue) me.getValue(); 76 result.put( me.getKey(), tv.getType().toLoggableString( tv.getValue(), factory ) ); 77 } 78 return result.toString(); 79 } 80 81 public void toString(Iterator iter, EntityMode entityMode) throws HibernateException { 82 if ( !log.isDebugEnabled() || !iter.hasNext() ) return; 83 log.debug("listing entities:"); 84 int i=0; 85 while ( iter.hasNext() ) { 86 if (i++>20) { 87 log.debug("more......"); 88 break; 89 } 90 log.debug( toString( iter.next(), entityMode ) ); 91 } 92 } 93 94 public Printer(SessionFactoryImplementor factory) { 95 this.factory = factory; 96 } 97 98 } 99 | Popular Tags |