001: //$Id: Printer.java 5785 2005-02-19 12:58:24Z oneovthafew $
002: package org.hibernate.pretty;
003:
004: import java.util.ArrayList;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.hibernate.HibernateException;
013: import org.hibernate.EntityMode;
014: import org.hibernate.engine.SessionFactoryImplementor;
015: import org.hibernate.engine.TypedValue;
016: import org.hibernate.intercept.LazyPropertyInitializer;
017: import org.hibernate.metadata.ClassMetadata;
018: import org.hibernate.type.Type;
019:
020: /**
021: * Renders entities to a nicely readable string.
022: * @author Gavin King
023: */
024: public final class Printer {
025:
026: private SessionFactoryImplementor factory;
027: private static final Log log = LogFactory.getLog(Printer.class);
028:
029: /**
030: * @param entity an actual entity object, not a proxy!
031: */
032: public String toString(Object entity, EntityMode entityMode)
033: throws HibernateException {
034:
035: // todo : this call will not work for anything other than pojos!
036: ClassMetadata cm = factory.getClassMetadata(entity.getClass());
037:
038: if (cm == null)
039: return entity.getClass().getName();
040:
041: Map result = new HashMap();
042:
043: if (cm.hasIdentifierProperty()) {
044: result.put(cm.getIdentifierPropertyName(), cm
045: .getIdentifierType().toLoggableString(
046: cm.getIdentifier(entity, entityMode),
047: factory));
048: }
049:
050: Type[] types = cm.getPropertyTypes();
051: String[] names = cm.getPropertyNames();
052: Object[] values = cm.getPropertyValues(entity, entityMode);
053: for (int i = 0; i < types.length; i++) {
054: if (!names[i].startsWith("_")) {
055: String strValue = values[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY ? values[i]
056: .toString()
057: : types[i].toLoggableString(values[i], factory);
058: result.put(names[i], strValue);
059: }
060: }
061: return cm.getEntityName() + result.toString();
062: }
063:
064: public String toString(Type[] types, Object[] values)
065: throws HibernateException {
066: List list = new ArrayList(types.length * 5);
067: for (int i = 0; i < types.length; i++) {
068: if (types[i] != null)
069: list.add(types[i].toLoggableString(values[i], factory));
070: }
071: return list.toString();
072: }
073:
074: public String toString(Map namedTypedValues)
075: throws HibernateException {
076: Map result = new HashMap();
077: Iterator iter = namedTypedValues.entrySet().iterator();
078: while (iter.hasNext()) {
079: Map.Entry me = (Map.Entry) iter.next();
080: TypedValue tv = (TypedValue) me.getValue();
081: result.put(me.getKey(), tv.getType().toLoggableString(
082: tv.getValue(), factory));
083: }
084: return result.toString();
085: }
086:
087: public void toString(Iterator iter, EntityMode entityMode)
088: throws HibernateException {
089: if (!log.isDebugEnabled() || !iter.hasNext())
090: return;
091: log.debug("listing entities:");
092: int i = 0;
093: while (iter.hasNext()) {
094: if (i++ > 20) {
095: log.debug("more......");
096: break;
097: }
098: log.debug(toString(iter.next(), entityMode));
099: }
100: }
101:
102: public Printer(SessionFactoryImplementor factory) {
103: this.factory = factory;
104: }
105:
106: }
|