2021-03-16 15:33:34 +01:00
|
|
|
package org.gcube.accounting.analytics.persistence.postgresql;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.gcube.documentstore.records.AggregatedRecord;
|
|
|
|
import org.gcube.documentstore.records.Record;
|
|
|
|
|
|
|
|
public class RecordToDBMapper {
|
|
|
|
|
|
|
|
protected static String getRecordTypeByClass(Class<? extends AggregatedRecord<?, ?>> 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 static String getKey(String fieldName) {
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
int lenght = fieldName.length();
|
|
|
|
boolean lastLowerCase = true;
|
|
|
|
for (int i=0; i<lenght; i++) {
|
|
|
|
Character ch = fieldName.charAt(i); /*traversing String one by one*/
|
|
|
|
if (Character.isUpperCase(ch)) {
|
|
|
|
if(lastLowerCase) {
|
|
|
|
stringBuffer.append("_");
|
|
|
|
}
|
|
|
|
lastLowerCase = false;
|
|
|
|
}else {
|
|
|
|
lastLowerCase = true;
|
|
|
|
}
|
|
|
|
stringBuffer.append(Character.toLowerCase(ch));
|
|
|
|
}
|
|
|
|
return stringBuffer.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final Class<? extends AggregatedRecord<?, ?>> clz;
|
|
|
|
|
|
|
|
protected final String typeName;
|
|
|
|
protected final String tableName;
|
|
|
|
|
|
|
|
protected final Map<String, String> tableFieldToUsageRecordField;
|
|
|
|
protected final Map<String, String> usageRecordFieldToTableField;
|
|
|
|
|
|
|
|
public RecordToDBMapper(Class<? extends AggregatedRecord<?, ?>> clz) throws Exception {
|
|
|
|
this.clz = clz;
|
|
|
|
this.typeName = getRecordTypeByClass(clz);
|
|
|
|
this.tableName = typeName.toLowerCase();
|
|
|
|
this.tableFieldToUsageRecordField = new HashMap<>();
|
|
|
|
this.usageRecordFieldToTableField = new HashMap<>();
|
|
|
|
mapFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
public RecordToDBMapper(String typeName, Class<? extends AggregatedRecord<?, ?>> clz) throws Exception {
|
|
|
|
this.clz = clz;
|
|
|
|
this.typeName = typeName;
|
|
|
|
this.tableName = typeName.toLowerCase();
|
|
|
|
this.tableFieldToUsageRecordField = new HashMap<>();
|
|
|
|
this.usageRecordFieldToTableField = new HashMap<>();
|
|
|
|
mapFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void mapFields() throws Exception {
|
|
|
|
Set<String> requiredFields = clz.newInstance().getRequiredFields();
|
|
|
|
for(String usageRecordField : requiredFields) {
|
|
|
|
String dbField = getKey(usageRecordField);
|
|
|
|
tableFieldToUsageRecordField.put(dbField, usageRecordField);
|
|
|
|
usageRecordFieldToTableField.put(usageRecordField, dbField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getTableField(String usageRecordField) {
|
|
|
|
return usageRecordFieldToTableField.get(usageRecordField);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUsageRecordField(String tableField) {
|
2021-03-17 16:08:58 +01:00
|
|
|
String ret = tableFieldToUsageRecordField.get(tableField);
|
|
|
|
if(ret==null && usageRecordFieldToTableField.keySet().contains(tableField)) {
|
|
|
|
ret = tableField;
|
|
|
|
}
|
|
|
|
return ret;
|
2021-03-16 15:33:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getTypeName() {
|
|
|
|
return typeName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getTableName() {
|
|
|
|
return tableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|