Data Util

Regular Functionality

<D> List<Long> getAllId(RequestBulkData<D> data)

Return list of Long id, from Bulk request object list. if there is no id field then it will return empty.

For example see the Person Service bulkUpdate method.

personRepository.findAllById(dataUtil().getAllId(data));


<D> List<Long> getAllId(List<D> list)

Return list of Long id, from object list. if there is no id field then it will return empty.


<D, L> List<L> getFieldValues(List<D> list, String name, Class<L> klass)

Sometimes we need list of some field from list of object, for example, we want to query by UUID in list, so first we have to iterate the object list then collect UUID from the list, then query to database. if this is the case then don’t worry. use this method.

Example

getFieldValues(list, "uuid", String.class);


<D> List<D> markAsDeletedFlag(List<D> dataList, Boolean isDeleted)

This method basically used for soft delete, it’s mean, if in your Entity has field called isDeleted and you got List of entity, now you want to True the all isDeleted field, so you can use this method.

Example:

List<Person> list = ....
markAsDeletedFlag(list, true)


<D> Iterable<D> markAsDeleted(Iterable<D> dataList)

Modify your isDeleted field to True from Entity Iterable.

Example:

    public MessageResponse bulkDelete(RequestBulkData<Long> data) {
        Iterable<Person> entities = personRepository.findAllById(data.getData());
        if (dataUtil().isEmpty(entities)) {
            return responseProcessor().error("Content not found");
        }
        dataUtil().markAsDeleted(entities);
        personRepository.saveAll(entities);
        return responseProcessor().response("Deleted");
    }> entities = personRepository.findAllById(data.getData());


<D> Iterable<D> markAsUndeleted(Iterable<D> dataList)

Modify your isDeleted field to False from Entity Iterable. Example:

    public MessageResponse bulkRestore(RequestBulkData<Long> data) {
        Iterable<Person> entities = personRepository.findAllById(data.getData());
        if (dataUtil().isEmpty(entities)) {
            return responseProcessor().error("Content not found");
        }
        dataUtil().markAsUndeleted(entities);
        personRepository.saveAll(entities);
        return responseProcessor().response("Restored");
    }


<D> Boolean isEmpty(Iterable<D> iterable)

Check the Iterable is Empty or not.


<D> Integer size(Iterable<D> iterable)

Get Iterable size.


<D> D getObjectFromListIfFieldValueMatch(List<D> dataList, String fieldName, Object dataObject)

Suppose to we got a DTO List, and we have a entity, now we want to find out DTO from the list if entity id match with DTO id.

Example

List<PersonUpdateDTO> dtoList = ...
Person person = personRepository.findById(10)
PersonUpdateDTO dto = getObjectFromListIfFieldValueMatch(dtoList, "id", person);


<E, D> BulkErrorValidEntities<D, E> merge(Iterable<E> mergeTo, RequestBulkData<D> data)

Validate by Java validation annotation, and return list of valid entity and error dto list.

Example:

        Iterable<Person> entities = personRepository.findAllById(dataUtil().getAllId(data));
        BulkErrorValidEntities<PersonUpdateDTO, Person> bulkData = dataUtil().merge(entities, data);
        if (bulkData.isValidEntities()) {
            personRepository.saveAll(bulkData.getEntities());
        }
        return responseProcessor().response(bulkData, PersonUpdateDTO.class);


<D> D markAsUndeleted(D data)

Mark isDeleted field False from Object


<D> D markAsDeleted(D data)

Mark isDeleted field True from Object

Example:

    public MessageResponse delete(Long id) {
        id = requestProcessor().validateId(id, "Id not found");
        Person entity = dataUtil().validateAndOptionToEntity(personRepository.findById(id), "Content not found");
        dataUtil().markAsDeleted(entity);
        personRepository.save(entity);
        return responseProcessor().response("Deleted");
    }


<D> D updateProperty(D data, Map<String, Object> fieldValue)

Suppose you want to change one or more value of an object.

Example:

updateProperty(data, Map.of("isDeleted", true));


<D> List<D> updateProperty(List<D> dataList, Map<String, Object> fieldValue)

Suppose you want to change one or more value of a List of Object.

Example:

updateProperty(dataList, Map.of("isDeleted", true));


<E> E validateAndOptionToEntity(Optional<E> optional, String message)

Suppose we get data from a repository, we have to check data exist or not, if exist then need the entity, or send the error message.

Example:

    public MessageResponse delete(Long id) {
        id = requestProcessor().validateId(id, "Id not found");
        Person entity = dataUtil().validateAndOptionToEntity(personRepository.findById(id), "Content not found");
        dataUtil().markAsDeleted(entity);
        personRepository.save(entity);
        return responseProcessor().response("Deleted");
    }


Static Method for Remote usage

static DataUtil instance()

Return the instance of a DataUtil.