You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
5.4 KiB
143 lines
5.4 KiB
package de.superx.bianalysis.metadata; |
|
|
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
import java.util.Optional; |
|
|
|
import org.apache.log4j.Logger; |
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore; |
|
|
|
import de.superx.bianalysis.FaultyMetadataException; |
|
import de.superx.bianalysis.metadata.models.json.MetaDimension; |
|
import de.superx.bianalysis.metadata.models.json.MetaDimensionAttribute; |
|
import de.superx.bianalysis.metadata.models.json.MetaFact; |
|
import de.superx.bianalysis.metadata.models.json.MetaMeasure; |
|
import de.superx.bianalysis.metadata.models.json.MetaMeasureFilter; |
|
import de.superx.bianalysis.metadata.models.json.MetaObject; |
|
|
|
public class MetaImport extends MetaJson { |
|
|
|
public List<MetaFact> facts; |
|
|
|
private static Logger log = Logger.getLogger(MetaImport.class); |
|
|
|
@JsonIgnore |
|
private Map<String, MetaDimensionAttribute> keysForMeasureFilter = new HashMap<>(); |
|
|
|
@JsonIgnore |
|
public List<MetaDimension> conformedDimensions; |
|
|
|
@JsonIgnore |
|
public void setConformedDimensions(List<MetaDimension> conformedDimensions){ |
|
this.conformedDimensions = conformedDimensions; |
|
if(this.conformedDimensions != null) { |
|
for (MetaDimension dim : this.conformedDimensions) { |
|
for (MetaDimensionAttribute attr : dim.getAttributes()) { |
|
attr.setDimension(dim); |
|
keysForMeasureFilter.put(dim.getDimension()+"."+attr.getDimColumn(), attr); |
|
} |
|
} |
|
} |
|
} |
|
|
|
@JsonIgnore |
|
@Override |
|
public void init() { |
|
this.allMetaObj = new ArrayList<MetaObject>(); |
|
for (MetaFact fact : this.facts) { |
|
allMetaObj.add(fact); |
|
for (MetaDimension dim : fact.getDimensions()) { |
|
allMetaObj.add(dim); |
|
if(dim.getRefTo() != null && !dim.getRefTo().isEmpty()) { |
|
MetaDimension conformedDim = findByRef(dim); |
|
dim.setConformedDimension(conformedDim); |
|
} |
|
for (MetaDimensionAttribute attr : dim.getAttributes()) { |
|
allMetaObj.add(attr); |
|
if(attr.getRefTo() != null && !attr.getRefTo().isEmpty()) { |
|
MetaDimension confDimension = dim.getConformedDimension(); |
|
if(confDimension == null) { |
|
throw new FaultyMetadataException( |
|
"The attribute with the id: '" + attr.getId() + "' " + |
|
"references a conformed attribute '" + attr.getRefTo() + "' " + |
|
"but the dimension " + dim.getId() + " is missing a ref_to"); |
|
} |
|
String dimTable = confDimension.getDimension(); |
|
MetaDimensionAttribute refAttribute = findByRefAttr(dimTable, attr); |
|
attr.setConformedDimensionAttribute(refAttribute); |
|
} |
|
keysForMeasureFilter.put(dim.getDimension()+"."+attr.getDimColumn(), attr); |
|
} |
|
} |
|
if(fact.getMeasures() != null) { |
|
for (MetaMeasure measure : fact.getMeasures()) { |
|
allMetaObj.add(measure); |
|
MetaMeasureFilter filter = measure.getFilter(); |
|
if(filter != null) { |
|
if(filter.getDimensionRef() != null && !filter.getDimensionRef().isBlank()) { |
|
MetaDimensionAttribute attr = keysForMeasureFilter.get(filter.getDimensionRef()); |
|
if(attr == null) { |
|
throw new FaultyMetadataException("Could not resolve dimensionRef '" + filter.getDimensionRef() + |
|
"' (" + file.getName() + " -> " + fact.getFacttable() + ")"); |
|
} |
|
filter.setAttribute(attr); |
|
allMetaObj.add(filter); |
|
} else if(filter.getFactColumnRef() != null && !filter.getFactColumnRef().isBlank()) { |
|
allMetaObj.add(filter); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
private MetaDimensionAttribute findByRefAttr(String dimensionTable, MetaDimensionAttribute attribute) { |
|
String attributeColumn = attribute.getRefTo(); |
|
MetaDimensionAttribute confAttr = null; |
|
for (MetaDimension confDim : this.conformedDimensions) { |
|
if(!confDim.getDimension().equals(dimensionTable)) { |
|
continue; |
|
} |
|
for (MetaDimensionAttribute attr : confDim.getAttributes()) { |
|
if(attr.getDimColumn() != null && attr.getDimColumn().equals(attributeColumn)) { |
|
confAttr = attr; |
|
break; |
|
} |
|
} |
|
} |
|
if(confAttr == null) { |
|
throw new FaultyMetadataException( |
|
"Could not resolve attribute reference '" + attributeColumn + "' (" |
|
+ file.getName() + " -> " |
|
+ attribute.getDimension().getFact().getFacttable() + " -> " |
|
+ attribute.getDimension().getRefTo() + ")" |
|
); |
|
} |
|
return confAttr; |
|
} |
|
|
|
|
|
@JsonIgnore |
|
private MetaDimension findByRef(MetaDimension dim) { |
|
String refTo = dim.getRefTo(); |
|
MetaDimension resolvedRefTo = null; |
|
for (MetaDimension dimConf : this.conformedDimensions) { |
|
if (dimConf.getDimension() == null) { |
|
log.error("Missing dimension attribute for " + dimConf.getCaption()); |
|
continue; |
|
} |
|
if (dimConf.getDimension().equals(refTo)) { |
|
resolvedRefTo = dimConf; |
|
break; |
|
} |
|
} |
|
if (resolvedRefTo == null) { |
|
throw new FaultyMetadataException("Could not resolve dimension reference '" + refTo + "' (" + file.getName() + " -> " + dim.getFact().getFacttable() + ")"); |
|
} |
|
return resolvedRefTo; |
|
} |
|
|
|
}
|
|
|