All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pro.taskana.workbasket.rest.assembler.WorkbasketSummaryRepresentationModelAssembler Maven / Gradle / Ivy

There is a newer version: 8.2.0
Show newest version
package pro.taskana.workbasket.rest.assembler;

import static pro.taskana.common.rest.models.TaskanaPagedModelKeys.WORKBASKETS;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.PagedModel.PageMetadata;
import org.springframework.hateoas.server.RepresentationModelAssembler;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import pro.taskana.common.rest.Mapping;
import pro.taskana.common.rest.models.TaskanaPagedModel;
import pro.taskana.common.rest.models.TaskanaPagedModelKeys;
import pro.taskana.resource.rest.PageLinks;
import pro.taskana.workbasket.api.WorkbasketService;
import pro.taskana.workbasket.api.models.WorkbasketSummary;
import pro.taskana.workbasket.internal.models.WorkbasketImpl;
import pro.taskana.workbasket.rest.models.WorkbasketSummaryRepresentationModel;

/**
 * EntityModel assembler for {@link WorkbasketSummaryRepresentationModel}.
 */
@Component
public class WorkbasketSummaryRepresentationModelAssembler implements
    RepresentationModelAssembler {

  private WorkbasketService workbasketService;

  public WorkbasketSummaryRepresentationModelAssembler() {
  }

  @Autowired
  public WorkbasketSummaryRepresentationModelAssembler(WorkbasketService workbasketService) {
    this.workbasketService = workbasketService;
  }

  @NonNull
  @Override
  public WorkbasketSummaryRepresentationModel toModel(
      @NonNull WorkbasketSummary workbasketSummary) {
    return new WorkbasketSummaryRepresentationModel(workbasketSummary);
  }

  public WorkbasketSummary toEntityModel(WorkbasketSummaryRepresentationModel resource) {
    WorkbasketImpl workbasket =
        (WorkbasketImpl) workbasketService.newWorkbasket(resource.getKey(), resource.getDomain());
    workbasket.setId(resource.getWorkbasketId());
    BeanUtils.copyProperties(resource, workbasket);
    return workbasket.asSummary();
  }

  @PageLinks(Mapping.URL_WORKBASKET)
  public TaskanaPagedModel toPageModel(
      List workbasketSummaries, PageMetadata pageMetadata) {
    return workbasketSummaries.stream()
               .map(this::toModel)
               .collect(
                   Collectors.collectingAndThen(
                       Collectors.toList(),
                       list -> new TaskanaPagedModel<>(getKey(), list, pageMetadata)));
  }

  protected TaskanaPagedModelKeys getKey() {
    return WORKBASKETS;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy