-
Notifications
You must be signed in to change notification settings - Fork 92
/
RegionSource.scala
47 lines (37 loc) · 1.54 KB
/
RegionSource.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package au.csiro.data61.magda.spatial
import java.net.URL
import au.csiro.data61.magda.AppConfig
import com.typesafe.config.{ Config, ConfigObject }
import scala.collection.JavaConversions._
case class RegionSource(
name: String,
url: URL,
idProperty: String,
nameProperty: String,
includeIdInName: Boolean,
shapePath: String,
disabled: Boolean,
order: Int)
object RegionSource {
def generateRegionId(regionType: String, id: String) = s"${regionType}/$id".toLowerCase
}
class RegionSources(config: Config) {
val sources = loadFromConfig(config)
private lazy val lookup = sources.groupBy(_.name.toLowerCase).mapValues(_.head)
def forName(name: String): Option[RegionSource] = lookup.get(name.toLowerCase)
private def loadFromConfig(config: Config): Seq[RegionSource] = {
config.root().map {
case (name: String, config: ConfigObject) =>
val regionSourceConfig = config.toConfig()
RegionSource(
name = name,
url = new URL(regionSourceConfig.getString("url")),
idProperty = regionSourceConfig.getString("idField"),
nameProperty = regionSourceConfig.getString("nameField"),
includeIdInName = if (regionSourceConfig.hasPath("includeIdInName")) regionSourceConfig.getBoolean("includeIdInName") else false,
shapePath = regionSourceConfig.getString("shapePath"),
disabled = regionSourceConfig.hasPath("disabled") && regionSourceConfig.getBoolean("disabled"),
order = regionSourceConfig.getInt("order"))
}.toSeq.filterNot(_.disabled)
}
}