/** * Copyright (c) 2011, SOCIETIES Consortium (WATERFORD INSTITUTE OF TECHNOLOGY (TSSG), HERIOT-WATT UNIVERSITY (HWU), SOLUTA.NET * (SN), GERMAN AEROSPACE CENTRE (Deutsches Zentrum fuer Luft- und Raumfahrt e.V.) (DLR), Zavod za varnostne tehnologije * informacijske družbe in elektronsko poslovanje (SETCCE), INSTITUTE OF COMMUNICATION AND COMPUTER SYSTEMS (ICCS), LAKE * COMMUNICATIONS (LAKE), INTEL PERFORMANCE LEARNING SOLUTIONS LTD (INTEL), PORTUGAL TELECOM INOVAÇÃO, SA (PTIN), IBM Corp., * INSTITUT TELECOM (ITSUD), AMITEC DIACHYTI EFYIA PLIROFORIKI KAI EPIKINONIES ETERIA PERIORISMENIS EFTHINIS (AMITEC), TELECOM * ITALIA S.p.a.(TI), TRIALOG (TRIALOG), Stiftelsen SINTEF (SINTEF), NEC EUROPE LTD (NEC)) * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following * conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.societies.context.community.db.impl.model.hibernate; import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.usertype.UserType; import org.societies.api.context.model.CtxAssociationIdentifier; import org.societies.api.context.model.CtxIdentifierFactory; import org.societies.api.context.model.MalformedCtxIdentifierException; /** * Describe your class here... * * @author <a href="mailto:nicolas.liampotis@cn.ntua.gr">Nicolas Liampotis</a> (ICCS) * @since 0.5 */ public class CtxAssociationIdentifierType implements UserType { private static final Class<CtxAssociationIdentifier> CTX_ASSOCIATION_ID_CLASS = CtxAssociationIdentifier.class; private static final int[] SQL_TYPES = { Types.VARCHAR }; /* * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object) */ @Override public Object assemble(Serializable cached, Object owner) throws HibernateException { return cached; } /* * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object) */ @Override public Object deepCopy(Object value) throws HibernateException { return value; } /* * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object) */ @Override public Serializable disassemble(Object value) throws HibernateException { return (Serializable) value; } /* * @see org.hibernate.usertype.UserType#equals(java.lang.Object, * java.lang.Object) */ @Override public boolean equals(Object x, Object y) throws HibernateException { if (x == y) return true; if (null == x || null == y) return false; return x.equals(y); } /* * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object) */ @Override public int hashCode(Object x) throws HibernateException { return x.hashCode(); } /* * @see org.hibernate.usertype.UserType#isMutable() */ @Override public boolean isMutable() { return false; } /* * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object) */ @Override public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { final String idStr = (String) Hibernate.STRING.nullSafeGet(rs, names[0]); if (idStr == null) return null; try { return CtxIdentifierFactory.getInstance().fromString(idStr); } catch (MalformedCtxIdentifierException mcie) { // should never happen throw new HibernateException( "Could not create CtxAssociationIdentifier instance from String representation stored in the database: " + mcie.getLocalizedMessage(), mcie); } } /* * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int) */ @Override public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { Hibernate.STRING.nullSafeSet(st, (value != null) ? value.toString() : null, index); } /* * @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object) */ @Override public Object replace(Object original, Object target, Object owner) throws HibernateException { return original; } /* * @see org.hibernate.usertype.UserType#returnedClass() */ @Override public Class<?> returnedClass() { return CtxAssociationIdentifierType.CTX_ASSOCIATION_ID_CLASS; } /* * @see org.hibernate.usertype.UserType#sqlTypes() */ @Override public int[] sqlTypes() { return CtxAssociationIdentifierType.SQL_TYPES; } }