Skip to content

Instantly share code, notes, and snippets.

@JRuumis
Created October 9, 2016 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JRuumis/4219aad70c41ffe90e071bb1a9a095f8 to your computer and use it in GitHub Desktop.
Save JRuumis/4219aad70c41ffe90e071bb1a9a095f8 to your computer and use it in GitHub Desktop.
package rpd.extract
import rpd.load._
import scala.xml.{Elem, Node}
/**
* Created by janis on 01/10/2016.
*/
abstract sealed class SourceExtract {
val xmlRepositoryRootFolder: String
def targets: List[TargetEntity]
}
// todo: review ad-hoc functions. some could be moved to Transformations, some - as lambdas
// Business Layer
// Business Models
case class BusinessModelsExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ParentSourceElements {
val xmlRepositoryFolderName: String = "BusinessModel"
def targets: List[BusinessModel] =
xmlElements map (xml =>
BusinessModel(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
description = AdHocAttribute(name="description", getBusinessModelDescription).value(xml)
)
)
private def getBusinessModelDescription(xml: Node): String = {
val descriptionBlock = xml \ "Description"
if (descriptionBlock.length > 0 && descriptionBlock.head.text.trim != "") descriptionBlock.head.text.trim else "N/A"
}
}
// Logical Tables
case class LogicalTablesExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ParentSourceElements {
val xmlRepositoryFolderName: String = "LogicalTable"
def targets: List[LogicalTable] =
xmlElements map (xml =>
LogicalTable(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
parentId = TransformAttribute(name="subjectAreaRef", Transformations.extractIdFileName).value(xml),
tableType = AdHocAttribute(name="tableType", getTableType).value(xml)
)
)
private def getTableType(xml: Node): String = {
val measureBlock = xml \\ "MeasureDefn"
if (measureBlock.length > 0) "fact" else "dimension"
}
}
// Dimensions
case class DimensionsExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ParentSourceElements {
val xmlRepositoryFolderName: String = "Dimension"
def targets: List[Dimension] =
xmlElements map (xml =>
Dimension(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
isValueBased = SimpleAttribute(name="isValueBased").value(xml),
isRagged = SimpleAttribute(name="isRagged").value(xml),
isSkipped = SimpleAttribute(name="isSkipped").value(xml),
parentId = TransformAttribute(name="subjectAreaRef", Transformations.extractIdFileName).value(xml),
rootLevelId = TransformAttribute(name="defaultRootLevelRef", Transformations.extractIdPostHash).value(xml)
))
}
// Logical Table Sources
case class LogicalTableSourcesExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ParentSourceElements {
val xmlRepositoryFolderName: String = "LogicalTableSource"
def targets: List[LogicalTableSource] =
xmlElements map (xml =>
LogicalTableSource(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
parentId = TransformAttribute(name="logicalTableRef", Transformations.extractIdFileName).value(xml),
filterValue = AdHocAttribute(name="tableType", getFilter).value(xml),
levels = AdHocAttribute(name="tableType", getLevels).value(xml),
fragmentationContent = AdHocAttribute(name="tableType", getFragmentContent).value(xml)
))
def getFilter(xml: Node): String = {
val filterBlock = xml \ "WhereClause" \ "ExprTextDesc"
if (filterBlock.length > 0 && filterBlock.head.text.trim != "") filterBlock.head.text.trim else "N/A"
}
def getLevels(xml: Node): String = {
val levelsBlock = xml \ "GroupBy" \ "ExprTextDesc"
if (levelsBlock.length > 0 && levelsBlock.head.text.trim != "") levelsBlock.head.text.trim else "N/A"
}
def getFragmentContent(xml: Node): String = {
val fragmentBlock = xml \ "FragmentContent" \ "ExprTextDesc"
if (fragmentBlock.length > 0 && fragmentBlock.head.text.trim != "") fragmentBlock.head.text.trim else "N/A"
}
}
// Logical Joins
case class LogicalJoinsExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ParentSourceElements {
val xmlRepositoryFolderName: String = "LogicalComplexJoin"
// "mdsid","name","type"
def targets: List[LogicalJoin] =
xmlElements map (xml =>
LogicalJoin(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
joinType = SimpleAttribute(name="type").value(xml),
logicalTableId1 = TransformAttribute(name="logicalTable1Ref", Transformations.extractIdFileName).value(xml),
logicalColumnId1 = TransformAttribute(name="logicalTable1Ref", Transformations.extractIdPostHash).value(xml),
multiplicity1 = SimpleAttribute(name="multiplicity1").value(xml),
logicalTableId2 = TransformAttribute(name="logicalTable2Ref", Transformations.extractIdFileName).value(xml),
logicalColumnId2 = TransformAttribute(name="logicalTable2Ref", Transformations.extractIdPostHash).value(xml),
multiplicity2 = SimpleAttribute(name="multiplicity2").value(xml)
))
}
// Logical Table Columns
case class LogicalTableColumnsExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ChildSourceElements {
val xmlRepositoryFolderName: String = "LogicalTable"
val childTag = "LogicalColumn"
val parentIdTag = "mdsid"
def targets: List[LogicalColumn] =
xmlElements map (xml =>
LogicalColumn(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
parentId = AdHocAttribute(name="parentId", getParentId).value(xml),
columnType = AdHocAttribute(name="columnType", getColumnType).value(xml),
aggregate = AdHocAttribute(name="aggregate", getAggregate).value(xml),
attributeExpression = AdHocAttribute(name="attributeExpression", getAttributeExpression).value(xml)
))
def getParentId(xml: Node): String = {
val parentBlock = xml \ "parentId"
if (parentBlock.length > 0) parentBlock.head.text else "N/A"
}
def getColumnType(xml: Node): String = {
val measureBlock = xml \ "MeasureDefn"
if (measureBlock.length > 0) "measure" else "attribute"
}
def getAggregate(xml: Node): String = {
val aggregateExpressionText = xml \ "MeasureDefn" \ "AggrRule" \ "Expr" \ "ExprText"
if (aggregateExpressionText.length == 0) "N/A" else aggregateExpressionText.head.text.trim
}
def getAttributeExpression(xml: Node): String = {
val attributeExpressionText = xml \ "AttrDefn" \ "Expr" \ "ExprText"
if (attributeExpressionText.length == 0) "N/A" else attributeExpressionText.head.text.trim
}
}
// Logical Table Source Columns
case class LogicalTableSourceColumnsExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ChildSourceElements {
val xmlRepositoryFolderName: String = "LogicalTableSource"
val childTag = "ColumnMapping"
val parentIdTag = "mdsid"
def targets: List[LogicalTableSourceColumn] =
xmlElements map (xml =>
LogicalTableSourceColumn(
mdsid = AdHocAttribute(name="mdsid", (xml:Node) => (xml \ "Expr" \ "@mdsid").text).value(xml),
name = AdHocAttribute(name="name", (xml:Node) => (xml \ "Expr" \ "@name").text).value(xml),
parentId = AdHocAttribute(name="parentId", getParentId).value(xml),
physicalTableReferences = ColumnReferencesAttribute(name="parentId", getPhysicalTableAncColumnIds).value(xml),
logicalTableReferences = ColumnReferencesAttribute(name="parentId", getLogicalTableAncColumnIds).value(xml)
))
def getParentId(xml: Node): String = {
val parentBlock = xml \ "parentId"
if (parentBlock.length > 0) parentBlock.head.text else "N/A"
}
// todo: should not have two functions as copy/paste
def getPhysicalTableAncColumnIds(xml: Node): List[ColumnReference] = {
val physicalTableReferences = xml \ "Expr" \ "ObjectRefList" \ "RefObject"
if (physicalTableReferences.length > 0)
physicalTableReferences map (ref => ColumnReference(tableReference = Transformations.extractIdFileName(ref.attribute("objectRef").get.text), columnReference = Transformations.extractIdPostHash(ref.attribute("objectRef").get.text) )) toList
else
List(ColumnReference("N/A","N/A"))
}
def getLogicalTableAncColumnIds(xml: Node): List[ColumnReference] = {
val logicalTableReferences = xml \ "LogicalColumnExpr" \ "ObjectRefList" \ "RefObject"
if (logicalTableReferences.length > 0)
logicalTableReferences map (ref => ColumnReference(tableReference = Transformations.extractIdFileName(ref.attribute("objectRef").get.text), columnReference = Transformations.extractIdPostHash(ref.attribute("objectRef").get.text) )) toList
else
List(ColumnReference("N/A","N/A"))
}
}
// Dimension Levels
case class DimensionLevelsExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ChildSourceElements {
val xmlRepositoryFolderName: String = "Dimension"
val childTag = "LogicalLevel"
val parentIdTag = "mdsid"
def targets: List[DimensionLevel] =
xmlElements map (xml =>
DimensionLevel(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
isGrandTotal = SimpleAttribute(name="isGTA").value(xml),
parentId = AdHocAttribute(name="parentId", (xml: Node) => (xml \ "parentId").text ).value(xml),
childLevelIds = AdHocAttributeList(name="childLevels", childLevelIds).value(xml)
))
def childLevelIds(xml: Node): List[String] = {
val childLevels = xml \ "RefChildLevels" \ "RefLogicalLevel"
if (childLevels.length > 0) childLevels map (t => Transformations.extractIdPostHash(t.attribute("logicalLevelRef").get.text)) toList else List()
}
}
// Physical Layer
// Database
case class DatabasesExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ParentSourceElements {
val xmlRepositoryFolderName: String = "Database"
def targets: List[Database] =
xmlElements map (xml =>
Database(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
dbType = SimpleAttribute(name="type").value(xml)
)
)
}
// Schema
case class SchemasExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ParentSourceElements {
val xmlRepositoryFolderName: String = "Schema"
def targets: List[Schema] =
xmlElements map (xml =>
Schema(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
parentId = TransformAttribute(name="aggregate", Transformations.extractIdPostHash).value(xml)
)
)
}
// Physical Table
case class PhysicalTablesExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ParentSourceElements {
val xmlRepositoryFolderName: String = "PhysicalTable"
def targets: List[PhysicalTable] =
xmlElements map (xml =>
PhysicalTable(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
parentId = TransformAttribute(name="containerRef", Transformations.extractIdPostHash).value(xml),
sourceTableId = TransformAttribute(name="sourceTableRef", Transformations.extractIdPostHash).value(xml)
)
)
}
// Physical Column
case class PhysicalColumnsExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ChildSourceElements {
val xmlRepositoryFolderName: String = "PhysicalTable"
val childTag = "PhysicalColumn"
val parentIdTag = "mdsid"
def targets: List[PhysicalColumn] =
xmlElements map (xml =>
PhysicalColumn(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
parentId = AdHocAttribute(name="parentId", (xml: Node) => (xml \ "parentId").text ).value(xml),
dataType = SimpleAttribute(name="dataType").value(xml),
precision = SimpleAttribute(name="precision").value(xml),
description = SimpleAttribute(name="description").value(xml)
)
)
}
// Presentation Layer
// Presentation Catalog
case class PresentationCatalogsExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ParentSourceElements {
val xmlRepositoryFolderName: String = "PresentationCatalog"
def targets: List[PresentationCatalog] =
xmlElements map (xml =>
PresentationCatalog(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
businessModelId = SimpleAttribute(name="subjectAreaRef").value(xml),
description = AdHocAttribute(name="description", (xml: Node) => (xml \ "Description").text ).value(xml)
)
)
}
// Presentation Table
case class PresentationTablesExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ParentSourceElements {
val xmlRepositoryFolderName: String = "PresentationTable"
def targets: List[PresentationTable] =
xmlElements map (xml =>
PresentationTable(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
parentId = SimpleAttribute(name="parentId").value(xml)
)
)
}
// Presentation Table column
case class PresentationTableColumnsExtract(xmlRepositoryRootFolder: String) extends SourceExtract with ChildSourceElements {
val xmlRepositoryFolderName: String = "PresentationTable"
val childTag = "PresentationColumn"
val parentIdTag = "mdsid"
def targets: List[PresentationTableColumn] =
xmlElements map (xml =>
PresentationTableColumn(
mdsid = SimpleAttribute(name="mdsid").value(xml),
name = SimpleAttribute(name="name").value(xml),
parentId = AdHocAttribute(name="parentId", (xml: Node) => (xml \ "parentId").text ).value(xml),
logicalTableId = TransformAttribute(name="logicalColumnRef", Transformations.extractIdFileName).value(xml),
logicalColumnId = TransformAttribute(name="logicalColumnRef", Transformations.extractIdPostHash).value(xml)
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment