2
votes

I'm really new to Scala and I'm try to convert some JSF controller to Scala to do some test. The code is the following:

abstract class BaseListBean[TENTITY <: AnyRef, TID <: AnyRef] extends Serializable {

  @BeanProperty var current:TENTITY = _

  @BeanProperty var lazyDataModel: LazyDataModel[TENTITY] = _

  def initLazyDataModel(): LazyDataModel[TENTITY] = {

    val model:LazyDataModel[TENTITY] = new LazyDataModel[TENTITY]() {

      override def load(first: Int, pageSize: Int, sortField: String, sortOrder: SortOrder, filters: util.Map[String, AnyRef]): util.List[TENTITY] = {

        val paginationInfo:PaginationInfo = new PaginationInfo()
        paginationInfo.setFromRecord(first)
        paginationInfo.setPageSize(pageSize)
        paginationInfo.setSortBy(Array(sortField))
        paginationInfo.setDirections(if (SortOrder.ASCENDING == sortOrder) Array(Direction.Asc) else Array(Direction.Desc))

        val advisoryPaginatedResult:PaginatedResult[TENTITY] = getBaseCRUD.read(paginationInfo)
        setRowCount(advisoryPaginatedResult.getTotalItems.toInt)

        advisoryPaginatedResult getItems
      }

      override def getRowKey(obj: TENTITY): TID = getEntityKey(obj)

    }

    return model;

  }



  def init() = {
    setLazyDataModel(initLazyDataModel())

  }

  def getEntityKey(obj: TENTITY) : TID
  def getBaseCRUD() : BaseCRUD[TENTITY, TID]
  def customInit() = {

  }

}

The error on compile time is:

Error:(24, 34) type mismatch;

found : org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY]

required: org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY]

val model:LazyDataModel[TENTITY] = new LazyDataModel[TENTITY]() {

I search for a solution but the problem still remaign. I try to reproduce with simpler code like this:

abstract class ExampleGenerics[T <: AnyRef, V <: AnyRef] {

  @BeanProperty var list:util.AbstractMap[T, V] = _

  def initList() : util.AbstractMap[T, V] = {
    val model:util.AbstractMap[T, V] = new util.AbstractMap[T, V]() {
      override def entrySet(): util.Set[Entry[T, V]] = {
        return null;
      }
    }
    return model
  }

  def init() = {
    setList(initList());
  }

}


    

But this will work. Someone can help? Thanks

1

1 Answers

0
votes

I found the problem:

This is the head of LazyDataModel<T>:

public abstract class LazyDataModel<T> extends DataModel<T> implements SelectableDataModel<T>, Serializable { /*...*/ }

The class inherit from DataModel<T>.

In my pom.xml I have

    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>javax.faces-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>

and also

<dependency>
    <groupId>javaee</groupId>
    <artifactId>javaee-api</artifactId>
    <version>5</version>
    <scope>provided</scope>
</dependency>

The class DataModel<T> is in the jsf 2 packages but also in javaee-api there is DataModel class (without generic). So the compiler go wrong. The resolution is to modify the pom with the following dependencies:

<dependency>
    <groupId>javax.ejb</groupId>
    <artifactId>ejb-api</artifactId>
    <version>3.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>javax.faces-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
</dependency>

I can't use jee6 api dependencies cause I'm using jee5 compatible app server with JSF2.0 upgrade.