Home > Software design >  How to filter records using Boolean Builder Spring Boot
How to filter records using Boolean Builder Spring Boot

Time:12-28

JSON of my code:

{
        "webId": 1713,
        "updateDate": "2021-10-05 09:51 AM",
        "createdDate": "2021-10-05 09:51 AM",
        "clientId": 1301,
        "createdBy": null,
        "updatedBy": "ABC",
        "createdById": 1713,
        "updatedById": 1713,
        "email": "[email protected]",
        "externalId": "ABC",
        "userNumber": "DEF",
        "firstName": "ABC",
        "lastName": "XYZ",
        "surName": "",
        "password": "{ZCjP0UXZnP1ai8StklMx07KjKGW681JwVYp6sT3Fi6A=}1f662684556ea6ca39ef11e93ec73de0",
        "status": "ACTIVE",
        "birthday": null,
        "phoneNumber": "",
        "loginCount": null,
        "lastLogin": null,
        "language": null,
        "timeZone": null,
        "profilePicture": null,
        "userRoleModels": [
            {
                "webId": 8202,
                "updateDate": "2021-10-12 03:46 PM",
                "createdDate": "2021-10-12 03:46 PM",
                "clientId": 1301,
                "createdBy": "ABC",
                "updatedBy": "ABC",
                "createdById": 1713,
                "updatedById": 1713,
                "role": "100",
                "roleCodeText": "Admin",
                "userId": 1713
            },
            {
                "webId": 8152,
                "updateDate": "2021-10-12 03:46 PM",
                "createdDate": "2021-10-12 03:46 PM",
                "clientId": 1301,
                "createdBy": "ABC",
                "updatedBy": "ABC",
                "createdById": 1713,
                "updatedById": 1713,
                "role": "200",
                "roleCodeText": "Manager",
                "userId": 1713
            }
        ],
        "userPermissionId": null
    },
    {
        "webId": 8702,
        "updateDate": "2021-08-26 08:45 AM",
        "createdDate": "2021-08-11 12:00 AM",
        "clientId": 1301,
        "createdBy": "Test",
        "updatedBy": "Test",
        "createdById": 8702,
        "updatedById": 8702,
        "email": null,
        "externalId": null,
        "userNumber": null,
        "firstName": "TEST",
        "lastName": "TEST",
        "surName": null,
        "password": "{EwGlmHpntU1ESocNYysUaaXm8bRpAo3j3OmScvUeQlU=}c5b4fc0e67cdc0ab5ab9e1637e52f43c",
        "status": "ACTIVE",
        "birthday": "2021-08-23",
        "phoneNumber": "03041390843",
        "loginCount": null,
        "lastLogin": null,
        "language": "123",
        "timeZone": null,
        "profilePicture": null,
        "userRoleModels": [
            {
                "webId": 18004,
                "updateDate": "2021-09-04 02:26 PM",
                "createdDate": "2021-09-04 02:26 PM",
                "clientId": 1301,
                "createdBy": "TEST",
                "updatedBy": "TEST",
                "createdById": 8702,
                "updatedById": 8702,
                "role": "100",
                "roleCodeText": "Admin",
                "userId": 8702
            }
        ],
        "userPermissionId": null
    }

There are a lot of records but i am attaching only 2 records.

There is an array of userRoleModels which have an element "role". I have to filter all the records using that role but a user can filter multiple roles on one go. Like

URL: http://localhost:8183/api/security/userAccounts?status=ACTIVE&code=100,200

So the result should be ALL THE RECORDS HAVING userRoleModels role=100 and 200

public Page<UserAccount> getAllUserAccounts(String status, List<String> code,
                                            Pageable pageable) {
    BooleanBuilder filter = new BooleanBuilder();

    if (StringUtils.isNotBlank(status)) {
        filter.and(QUserAccount.userAccount.status.eq(UserAccount.UserAccountStatus.valueOf(status)));
    }
    if (!ObjectUtils.isEmpty(code)) {     //this check is not working
        filter.and(QUserAccount.userAccount.userRoles.any().role.contains((Expression<String>) code));         //what should come here
    }

    return userAccountRepository.findAll(filter, pageable);
}

CodePudding user response:

contains is not meant to be used with lists. In this case, you should use in instead. Please see the example.

public Page<UserAccount> getAllUserAccounts(String status, List<String> code,
                                        Pageable pageable) {
    BooleanBuilder filter = new BooleanBuilder();

    if (StringUtils.isNotBlank(status)) {
        filter.and(QUserAccount.userAccount.status.eq(UserAccount.UserAccountStatus.valueOf(status)));
    }
    if (!ObjectUtils.isEmpty(code)) {     //this check is not working
        filter.and(QUserAccount.userAccount.userRoles.any().role.in(code));         
    }

    return userAccountRepository.findAll(filter, pageable);
}
  • Related