50 lines
1.7 KiB
Java
50 lines
1.7 KiB
Java
package org.gcube.accounting.utility.postgresql;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import org.gcube.documentstore.persistence.PersistenceBackendConfiguration;
|
|
import org.gcube.documentstore.records.AggregatedRecord;
|
|
import org.gcube.documentstore.records.Record;
|
|
|
|
public class RecordToDBMapping {
|
|
|
|
public static String getRecordTypeByClass(Class<? extends Record> clz) {
|
|
try {
|
|
Record r = clz.newInstance();
|
|
return r.getRecordType();
|
|
}catch (Exception e) {
|
|
String type = clz.getSimpleName();
|
|
type = type.replace("Abstract", "");
|
|
type = type.replace("Aggregated", "");
|
|
return type;
|
|
}
|
|
}
|
|
|
|
protected final static Map<String, RecordToDBFields> classToRecordToDBMapper;
|
|
protected final static Map<String, RecordToDBConnection> recordToDBInfo;
|
|
|
|
static {
|
|
classToRecordToDBMapper = new HashMap<>();
|
|
recordToDBInfo= new HashMap<>();
|
|
}
|
|
|
|
public static synchronized void addRecordToDB(Class<? extends AggregatedRecord<?, ?>> clz, PersistenceBackendConfiguration configuration) throws Exception {
|
|
String type = getRecordTypeByClass(clz);
|
|
RecordToDBFields recordToDBMapper = new RecordToDBFields(type, clz);
|
|
classToRecordToDBMapper.put(type, recordToDBMapper);
|
|
RecordToDBConnection recordDBInfo = new RecordToDBConnection(type, clz, configuration);
|
|
recordToDBInfo.put(type, recordDBInfo);
|
|
}
|
|
|
|
public static synchronized RecordToDBFields getRecordToDB(Class<? extends Record> clz) throws Exception {
|
|
String type = getRecordTypeByClass(clz);
|
|
return classToRecordToDBMapper.get(type);
|
|
}
|
|
|
|
public static synchronized RecordToDBConnection getRecordDBInfo(Class<? extends Record> clz) throws Exception {
|
|
String type = getRecordTypeByClass(clz);
|
|
return recordToDBInfo.get(type);
|
|
}
|
|
}
|