Skip to content

Instantly share code, notes, and snippets.

@jakob85
Created November 21, 2013 13:29
Show Gist options
  • Save jakob85/7581579 to your computer and use it in GitHub Desktop.
Save jakob85/7581579 to your computer and use it in GitHub Desktop.
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