001// Copyright 2008-2013 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.internal.hibernate; 016 017import org.apache.tapestry5.ValueEncoder; 018import org.apache.tapestry5.ioc.internal.util.InternalUtils; 019import org.apache.tapestry5.ioc.services.PropertyAccess; 020import org.apache.tapestry5.ioc.services.PropertyAdapter; 021import org.apache.tapestry5.ioc.services.TypeCoercer; 022import org.apache.tapestry5.ioc.util.ExceptionUtils; 023import org.hibernate.Session; 024import org.hibernate.mapping.PersistentClass; 025import org.hibernate.mapping.Property; 026import org.slf4j.Logger; 027 028import java.io.Serializable; 029 030public final class HibernateEntityValueEncoder<E> implements ValueEncoder<E> 031{ 032 private final Class<E> entityClass; 033 034 private final Session session; 035 036 private final TypeCoercer typeCoercer; 037 038 private final PropertyAdapter propertyAdapter; 039 040 private final Logger logger; 041 042 public HibernateEntityValueEncoder(Class<E> entityClass, PersistentClass persistentClass, Session session, 043 PropertyAccess propertyAccess, TypeCoercer typeCoercer, Logger logger) 044 { 045 this.entityClass = entityClass; 046 this.session = session; 047 this.typeCoercer = typeCoercer; 048 this.logger = logger; 049 050 Property property = persistentClass.getIdentifierProperty(); 051 052 propertyAdapter = propertyAccess.getAdapter(this.entityClass).getPropertyAdapter(property.getName()); 053 } 054 055 public String toClient(E value) 056 { 057 if (value == null) 058 return null; 059 060 Object id = propertyAdapter.get(value); 061 062 if (id == null) 063 { 064 return null; 065 } 066 067 return typeCoercer.coerce(id, String.class); 068 } 069 070 @SuppressWarnings("unchecked") 071 public E toValue(String clientValue) 072 { 073 if (InternalUtils.isBlank(clientValue)) 074 return null; 075 076 Object id = null; 077 078 try 079 { 080 081 id = typeCoercer.coerce(clientValue, propertyAdapter.getType()); 082 } catch (Exception ex) 083 { 084 throw new RuntimeException(String.format( 085 "Exception converting '%s' to instance of %s (id type for entity %s): %s", clientValue, 086 propertyAdapter.getType().getName(), entityClass.getName(), ExceptionUtils.toMessage(ex)), ex); 087 } 088 089 Serializable ser = (Serializable) id; 090 091 E result = (E) session.get(entityClass, ser); 092 093 if (result == null) 094 { 095 // We don't identify the entity type in the message because the logger is based on the 096 // entity type. 097 logger.error(String.format("Unable to convert client value '%s' into an entity instance.", clientValue)); 098 } 099 100 return result; 101 } 102 103}