KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > pretty > Printer


1 //$Id: Printer.java,v 1.10 2005/02/19 12:58:23 oneovthafew Exp $
2
package org.hibernate.pretty;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
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 /**
21  * Renders entities to a nicely readable string.
22  * @author Gavin King
23  */

24 public final class Printer {
25
26     private SessionFactoryImplementor factory;
27     private static final Log log = LogFactory.getLog(Printer.class);
28
29     /**
30      * @param entity an actual entity object, not a proxy!
31      */

32     public String JavaDoc toString(Object JavaDoc entity, EntityMode entityMode) throws HibernateException {
33
34         // todo : this call will not work for anything other than pojos!
35
ClassMetadata cm = factory.getClassMetadata( entity.getClass() );
36
37         if ( cm==null ) return entity.getClass().getName();
38
39         Map JavaDoc result = new HashMap JavaDoc();
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 JavaDoc[] names = cm.getPropertyNames();
50         Object JavaDoc[] values = cm.getPropertyValues( entity, entityMode );
51         for ( int i=0; i<types.length; i++ ) {
52             if ( !names[i].startsWith("_") ) {
53                 String JavaDoc 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 JavaDoc toString(Type[] types, Object JavaDoc[] values) throws HibernateException {
63         List JavaDoc list = new ArrayList JavaDoc( 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 JavaDoc toString(Map JavaDoc namedTypedValues) throws HibernateException {
71         Map JavaDoc result = new HashMap JavaDoc();
72         Iterator JavaDoc iter = namedTypedValues.entrySet().iterator();
73         while ( iter.hasNext() ) {
74             Map.Entry JavaDoc me = (Map.Entry JavaDoc) 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 JavaDoc 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