1

I have AuditLog entity, in this entity I have @ManyToOne mapping to another entity AuditAction that owns field AuditActionType which is an enum. I want implement backend filtering using JPA repository. I want to return by REST GET method all AuditLogs that contains AuditActionType passed to query param. AuditActionType are: LOG_IN,LOG_OUT,CREATE_USER,UPDATE_USER,DELETE_USER

Example:

http://localhost:8086/audit/action?action=lo

should return all AuditLogs where their AuditAction contains 'lo'. So It would return all AuditLogs with LOG_IN and LOG_OUT action.

In my JPA repository I created this:

List findByAction_actionIgnoreCaseContaining(AuditActionType action);

but when I run this It gives me compilation error:

sed by: java.lang.IllegalStateException: Unable to ignore case of com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType types, the property 'action' must reference a String.

Please can somebody help?

AuditLog:

package com.cgi.edu.bootcamp.scoringserver.model;

import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "AUDIT_LOG")
public class AuditLog {

    @Id
    @SequenceGenerator(name = "SEQ_audit", sequenceName = "SEQ_audit", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_audit")
    @Column(name = "AL_ID", nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(referencedColumnName="U_ID", name="AL_U_ID")
    private User user;

    @ManyToOne
    @JoinColumn(referencedColumnName="AA_ID", name="AL_ACTION")
    private AuditAction action;

    @Column(name = "AL_DESCRIPTION", length = 255)
    private String description;

    @Column(name = "AL_DATE")
    private LocalDateTime date;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public AuditAction getAction() {
        return action;
    }

    public void setAction(AuditAction action) {
        this.action = action;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDateTime getDate() {
        return date;
    }

    public void setDate(LocalDateTime date) {
        this.date = date;
    }
}

AuditAction:

    package com.cgi.edu.bootcamp.scoringserver.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType;

@Entity
@Table(name = "AUDIT_ACTION")
public class AuditAction {

    @Id
    @SequenceGenerator(name = "SEQ_action", sequenceName = "SEQ_action", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_action")
    @Column(name = "AA_ID", nullable = false)
    private Long id;

    @Enumerated(EnumType.STRING)
    @NotNull(message = "Audit action can not be empty!")
    @Column(name = "AA_NAME", nullable = false, unique = true)
    private AuditActionType action;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public AuditActionType getAction() {
        return action;
    }

    public void setAction(AuditActionType action) {
        this.action = action;
    }
}

AuditLogRepository:

   package com.cgi.edu.bootcamp.scoringserver.dao;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.cgi.edu.bootcamp.scoringserver.model.AuditLog;
import com.cgi.edu.bootcamp.scoringserver.model.User;
import com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType;

@Repository
public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{

    List<AuditLog> findByAction_actionIgnoreCaseContaining(AuditActionType action);

}

AuditLogServiceImpl:

package com.cgi.edu.bootcamp.scoringserver.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cgi.edu.bootcamp.scoringserver.dao.AuditLogRepository;
import com.cgi.edu.bootcamp.scoringserver.exception.ResourceNotFoundException;
import com.cgi.edu.bootcamp.scoringserver.model.AuditLog;
import com.cgi.edu.bootcamp.scoringserver.model.UserGroup;
import com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType;

@Service
@Transactional
public class AuditLogServiceImpl implements AuditLogService {

    @Autowired
    private AuditLogRepository auditRepository;

    @Override
    public List<AuditLog> findByAction(AuditActionType action) {
        return auditRepository.findByAction_actionIgnoreCaseContaining(action);
    }

}

AuditLogRestController:

    package com.cgi.edu.bootcamp.scoringserver.web;

import java.time.LocalDateTime;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cgi.edu.bootcamp.scoringserver.model.AuditLog;
import com.cgi.edu.bootcamp.scoringserver.model.User;
import com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType;
import com.cgi.edu.bootcamp.scoringserver.service.AuditLogService;

@RestController
@RequestMapping("/audit")
public class AuditLogRestController {

    @Autowired
    private AuditLogService auditLogService;

    @GetMapping("/action")
    public ResponseEntity<List<AuditLog>> getLogsByAction(@RequestParam("action") AuditActionType action){
        return ResponseEntity.ok(auditLogService.findByAction(action));
    }

}
Lorelorelore
  • 2,881
  • 6
  • 25
  • 32
Michael
  • 99
  • 1
  • 2
  • 11

2 Answers2

1

Well, if you think about it, how an the the controller convert a String such as 'lo' to enum? So you'd need to start by converting the parameter to a String.

   @GetMapping("/action")
    public ResponseEntity<List<AuditLog>> getLogsByAction(@RequestParam("action") String action){
        return ResponseEntity.ok(auditLogService.findByAction(action));
    }

and then change the service and repository methods accordingly.

@Repository
public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{

    @Query("select a from AuditLog a where a.action like CONCAT('%', :action, '%')")
    List<AuditLog> findByAction(String action);

}
Alan Hay
  • 20,941
  • 2
  • 47
  • 97
  • tried that, now it gives me on whatever string I pass to params status `200OK` and in Body empty array: `[]` – Michael Nov 20 '18 at 18:33
  • Ok, its working partially now, I modified **a.action** to **a.action.action** but its working only with uppercase letter and when I pass "LOGI" it returns empty array because of that enum is stored in db with underscore "LOG_IN". Any ideas? – Michael Nov 21 '18 at 12:40
  • ok, I fixed the uppercase problem with `@Query("select a from AuditLog a where a.action.action like UPPER(CONCAT('%', :action, '%'))")` but I dont know how to manage to get result when I pass for example "logi". Can you help? – Michael Nov 21 '18 at 13:15
0

Change your repository method to:

@Repository
public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{

    List<AuditLog> findByAction(AuditActionType action);

}

If mapping to enum value in your controller method will be successful, you don't need to change case size in the repository.

Centos
  • 218
  • 3
  • 12
  • tried that, `http://localhost:8086/audit/action?action=LO` gives me : `Failed to convert value of type 'java.lang.String' to required type 'com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType'` – Michael Nov 20 '18 at 11:51
  • So in controller change action type to String and use it this way return ResponseEntity.ok(auditLogService.findByAction(AuditActionType.valueOf(action))); Or you can create enum converter as it is described here https://stackoverflow.com/questions/39774427/springs-requestparam-with-enum – Centos Nov 20 '18 at 12:10
  • tried valueOf(), it gives me: `java.lang.IllegalArgumentException: No enum constant com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType.Lo` – Michael Nov 20 '18 at 18:42