0
votes

I'm trying to compare two Seq strings, which I got using Apache LDAP API with Play! Framework on Scala. After few days I'm out of ideas. Here is the beginning:

@(compares:Seq[Compare] , compares1:Seq[Compare], compareForm: Form[(String, String)])(implicit request:Request[Any])

@import helper._

@main("Admin Tools", request.uri){

I have two interations - how to compare them ?

@for(compare <- compares) {
    <td>@for(group <- compare.memberOf) {
        <li style="list-style-type: none;">@group.replaceAll(",(.*)","").replaceAll("(.*)=","")</li>
    }</td>
}
@for(compare1 <- compares1) {
    <td>@for(group1 <- compare1.memberOf) {
        <li style="list-style-type: none;">@group1.replaceAll(",(.*)","").replaceAll("(.*)=","")</li>
    }</td>
}   
1
What do you mean by "how to compare them"? Are you wanting to see if they are the same length? Have the same values? If there is some logic to be done in your comparison of the two lists, you should do that somewhere other than the view (like in the Controller or some service the Controller delegates to). - Mark S

1 Answers

0
votes

I know it might be one late answer, but I managed to solve this at that time. Code might be a bit dirty, but I hope it will come in handy to someone.

Model part:

def intersection(c1:Seq[String], c2:Seq[String]): Seq[String] = {
    (Set(c1: _*) & Set(c2: _*)).toSeq
}

Controller part:

compareForm.bindFromRequest.fold(
hasErrors = {errors => BadRequest(views.html.compares.index(compareForm, username, User.find(username)))},
success = { ldapName => 
  val leftUser = Compare.findAll(ldapName._1)
  val rightUser = Compare.findAll(ldapName._2)
  val intersection = Compare.intersection(leftUser.memberOf,rightUser.memberOf) 
  val rightLacking: Seq[String] = (leftUser.memberOf.toSet -- intersection.toSet).toList.map(_.toString).sorted      
  val leftLacking: Seq[String] = (rightUser.memberOf.toSet -- intersection.toSet).toList.map(_.toString).sorted
  val bothOk: Seq[String] = (intersection.toSet).toList.sorted
  Ok(views.html.compares.list(leftUser, rightUser, rightLacking, leftLacking, bothOk, compareForm, username, User.find(username)))
}  )

Regards!