Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a reference to a Lift MetaMapper object by name?
    text
    copied!<p>In modeling an audit table, I've included fields necessary to find the original record being audited (oid: String, className: String). I'd like to programmatically find the MetaMapper for the Mapper class name.</p> <p>For example, if I have:</p> <pre><code>class Foo extends Mapper[Foo] { def getSingleton = Foo } object Foo extends Foo with MetaMapper[Foo] { } </code></pre> <p>Given the String "Foo", how would I get a reference to the object Foo? Ultimately I want a reference to a MetaMapper so that I can do a findAll. In my Hibernate days I might have found a class using Class.byName(className) and then called a static method on that class.</p> <p>Here's what I'm using right now to do this, but it does require maintaining a list of MetaMapper objects and also instantiating classes using MetaMapper#create:</p> <pre><code>case class Audited(name: String, url: Box[String]) def getAudited : Box[Audited] = { // OBJECT_OID and OBJECT_TYPE are for the audited record we are trying to find (OBJECT_OID.is, OBJECT_TYPE.is) match { case (ooid, otype) if List(ooid,otype).forall(s =&gt; StringUtils.isNotBlank(s)) =&gt; { // maintain a list of objects that are metamappers val metas = List(Client) (for { // create a new instance and check its class name meta &lt;- metas.find(meta =&gt; meta.create.getClass.getName == otype) mapper &lt;- meta.find(By(meta.primaryKeyField, ooid)) } yield { val nameFieldNames = List("NAME") val name = mapper.allFields.find(f =&gt; nameFieldNames.contains(f.name)) match { case Some(field) =&gt; tryo(field.is.toString).openOr("") case _ =&gt; mapper.getClass.getName.split(".").last } Full(Audited(name, Empty)) }) openOr Empty } case _ =&gt; Empty } } </code></pre> <p>Which works, but it's ugly IMHO and requires maintenance of a list of supported MetaMappers</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload