From bb762a501ab309c3540d3061bd7c07d662fcfea9 Mon Sep 17 00:00:00 2001 From: Alejandro Revilla Date: Mon, 16 Nov 2015 12:49:59 -0300 Subject: [PATCH] Tags usertype and converter --- .../org/jpos/ee/converter/TagsConverter.java | 37 +++++++ .../java/org/jpos/ee/usertype/TagsType.java | 101 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 modules/dbsupport/src/main/java/org/jpos/ee/converter/TagsConverter.java create mode 100644 modules/dbsupport/src/main/java/org/jpos/ee/usertype/TagsType.java diff --git a/modules/dbsupport/src/main/java/org/jpos/ee/converter/TagsConverter.java b/modules/dbsupport/src/main/java/org/jpos/ee/converter/TagsConverter.java new file mode 100644 index 0000000000..6c574f0bab --- /dev/null +++ b/modules/dbsupport/src/main/java/org/jpos/ee/converter/TagsConverter.java @@ -0,0 +1,37 @@ +/* + * jPOS Project [http://jpos.org] + * Copyright (C) 2000-2015 Alejandro P. Revilla + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.jpos.ee.converter; + +import org.jpos.util.Tags; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +@Converter(autoApply = true) +public class TagsConverter implements AttributeConverter { + @Override + public String convertToDatabaseColumn(Tags attribute) { + return attribute.toString(); + } + + @Override + public Tags convertToEntityAttribute(String dbData) { + return new Tags(dbData); + } +} + diff --git a/modules/dbsupport/src/main/java/org/jpos/ee/usertype/TagsType.java b/modules/dbsupport/src/main/java/org/jpos/ee/usertype/TagsType.java new file mode 100644 index 0000000000..a170d6c826 --- /dev/null +++ b/modules/dbsupport/src/main/java/org/jpos/ee/usertype/TagsType.java @@ -0,0 +1,101 @@ +/* + * jPOS Project [http://jpos.org] + * Copyright (C) 2000-2015 Alejandro P. Revilla + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.jpos.ee.usertype; + +import org.hibernate.HibernateException; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.usertype.UserType; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; + +import org.jpos.util.Tags; + +public class TagsType implements UserType { + private static int[] TYPES = { Types.VARCHAR }; + + @Override + public int[] sqlTypes() { + return TYPES; + } + + @Override + public Class returnedClass() { + return Tags.class; + } + + @Override + public boolean equals(Object a, Object b) throws HibernateException { + return (a == b) || ((a != null) && (b != null) && (a.equals(b))); + } + + @Override + public int hashCode(Object o) throws HibernateException { + return o.hashCode(); + } + + @Override + public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { + String tags = rs.getString(names[0]); + if (rs.wasNull()) + return null; + return new Tags(tags); + } + + @Override + public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { + if (value==null) { + st.setNull(index, Types.VARCHAR); + } else { + st.setString (index, value.toString()); + } + } + + @Override + public Object deepCopy(Object value) throws HibernateException { + if (value == null) + return null; + return new Tags(value.toString()); + + } + + @Override + public boolean isMutable() { + return true; + } + + @Override + public Serializable disassemble(Object value) throws HibernateException { + return (Serializable) value; + } + + @Override + public Object assemble(Serializable cached, Object owner) throws HibernateException { + return cached; + } + + @Override + public Object replace(Object original, Object target, Object owner) throws HibernateException { + return new Tags(original.toString()); + } +} +