/*
* Calendula - An assistant for personal medication management.
* Copyright (C) 2016 CITIUS - USC
*
* Calendula is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
package es.usc.citius.servando.calendula.persistence.typeSerializers;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.field.SqlType;
import com.j256.ormlite.field.types.BaseDataType;
import com.j256.ormlite.support.DatabaseResults;
import java.sql.SQLException;
/**
* Created by joseangel.pineiro on 10/9/14.
*/
public class BooleanArrayPersister extends BaseDataType {
private static final BooleanArrayPersister singleton = new BooleanArrayPersister();
String format = "kk:mm";
public BooleanArrayPersister() {
super(SqlType.STRING, new Class<?>[]{boolean[].class});
}
public static BooleanArrayPersister getSingleton() {
return singleton;
}
@Override
public Object parseDefaultString(FieldType fieldType, String defaultStr) throws SQLException {
return defaultStr;
}
@Override
public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos) throws SQLException {
return results.getString(columnPos);
}
@Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) throws SQLException {
if (sqlArg == null) {
return null;
}
return toArray((String) sqlArg);
}
@Override
public Object javaToSqlArg(FieldType fieldType, Object javaObject) throws SQLException {
if (javaObject == null) {
return null;
}
return toString((boolean[]) javaObject);
}
private boolean[] toArray(String value) {
String[] values = value.split(",");
boolean[] result = new boolean[values.length];
for (int i = 0; i < values.length; i++) {
result[i] = Boolean.parseBoolean(values[i]);
}
return result;
}
private String toString(boolean[] values) {
String result = "";
for (int i = 0; i < values.length; i++) {
result += String.valueOf(values[i]);
if (i < values.length - 1) {
result += ",";
}
}
return result;
}
}