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> 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> clz; protected final String typeName; protected final String tableName; protected final Map tableFieldToUsageRecordField; protected final Map usageRecordFieldToTableField; public RecordToDBMapper(Class> 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> 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 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) { String ret = tableFieldToUsageRecordField.get(tableField); if(ret==null && usageRecordFieldToTableField.keySet().contains(tableField)) { ret = tableField; } return ret; } public String getTypeName() { return typeName; } public String getTableName() { return tableName; } }