Skip to main content

Get Mine

28 Desember 2023

getMine merupakan func yang bisa digunakan dengan implementasikan OwnershipInterface di class service. Sesuai nama func nya, berfungsi untuk handle spec berdasarkan user yang login / aktif user.

How To

  1. Implementasikan Class OwnershipInterface pada class Service

    @Service
    public class TestService<Test, TestDto, TestRepository> implements OwnershipInterface {
    // Code Service
    }
  2. Implementasikan func yang diwajibkan oleh OwnershipInterface

    @Override
    public Specification getMine() {
    return null;
    }
  3. Silakan buat Specification yang menyesuaikan agar muncul data yang hanya muncul pada user yang aktif. Contoh :

    @Override
    public Specification getMine() {
    return Specification.where((root, criteriaQuery, criteriaBuilder) ->
    criteriaBuilder.equal(root.get("reviewerEmail"), getCurrentUser()));
    }
    @Override
    public Specification getMine() {
    List<String> instanceIds = camundaProcessService.getTaskByAssignee("procDefKey", getCurrentuser())
    .stream().map(Task::getProcessInstanceId).collect(Collectors.toList());
    return Specification.where((root, criteriaQuery, criteriaBuilder) ->
    criteriaBuilder.in(root.get("processInstanceId")).value(instanceIds));
    }

    root disini merupakan root dari Entity yang terdeklarasi pada class Service. Di contoh ini ialah class Test

Kesimpulan

Untuk menampilkan data yang sesuai dengan user yang login atau aktif user, bisa menggunakan OwnershipInterface, sehingga tidak perlu oper-oper data user login ke class Specification