Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View DataProvider.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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
package plugins
 
import play.api.{Logger, Plugin, Application}
import scala.concurrent.Future
import models.{Lead, DataResponse}
import exceptions.ServiceException
 
/**
* User: jakobdobrzynski
* Date: 11/4/13
* Time: 4:28 PM
*/
 
trait RegistrableDataProvider {
def id: String
}
 
class DataProviderRegistry[T <: RegistrableDataProvider](label: String) {
private var registry = Map[String, T]()
 
def register(plugin: T) {
if (registry.contains(plugin.id) ) {
throw new RuntimeException("There is already a %s registered with id %s".format(label, plugin.id))
}
 
val p = (plugin.id, plugin)
registry += p
}
 
def unRegister(id: String) {
registry -= id
}
 
def get(id: String): Option[T] = registry.get(id) orElse {
Logger.error("[dataProviders] can't find %s for id %s".format(label, id))
None
}
 
def all() = registry
}
 
/**
* This is a registry for all the registered dataProviders
*/
object DataProviderRegistry {
lazy val dataProviders = new DataProviderRegistry[DataProvider]("dataProvider")
}
 
 
abstract class DataProvider (application: Application) extends Plugin with RegistrableDataProvider {
 
/**
* Registers the dataProvider in the Provider Registry
*/
override def onStart() {
Logger.info("[dataProvider] loaded dataProvider plugin: %s".format(id))
DataProviderRegistry.dataProviders.register(this)
}
 
/**
* Unregisters the dataProvider
*/
override def onStop() {
Logger.info("[dataProvider] unloaded dataProvider plugin: %s".format(id))
DataProviderRegistry.dataProviders.unRegister(id)
}
 
def removeIndex[A](s: Seq[A], n: Int): Seq[A] = s.indices.collect { case i if i != n => s(i) }
 
def provideDataWithKey(key: String) : Future[List[DataResponse]]
 
def addListToLead(list: List[DataResponse], lead: Lead) : Future[Either[ServiceException, Lead]]
 
def getTypeList(list: List[DataResponse]) : List[DataResponse]
 
def removeForLeadAtIndexInList(id: String, index: Int) : Future[Either[ServiceException, Lead]]
def updateForLeadAtIndexInList(id: String, index: Int, s: String) : Future[Either[ServiceException, Lead]]
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.