Skip to content

Commit

Permalink
Tags usertype and converter
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Nov 16, 2015
1 parent 9f3a814 commit bb762a5
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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<Tags,String> {
@Override
public String convertToDatabaseColumn(Tags attribute) {
return attribute.toString();
}

@Override
public Tags convertToEntityAttribute(String dbData) {
return new Tags(dbData);
}
}

101 changes: 101 additions & 0 deletions modules/dbsupport/src/main/java/org/jpos/ee/usertype/TagsType.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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());
}
}

0 comments on commit bb762a5

Please # to comment.