Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter records using Boolean Builder Spring Boot

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": "ABC@gmail.com",
        "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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
}

>Solution :

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading