big decimal number rounding

Limit BigDecimal to n decimal places

Limit BigDecimal to n places only

In java, when you are working with currency and other important precision based calculation, you will use Big Decimal. In some cases you might want to round the number to two (2) decimal places only or, lets say, to n, decimal places.

When to use limited decimal places

There are a couple of cases where you need to round or just to limit the number of decimal places. Once example is when you are working on the currency, after conversion, you may get a long decimal and in that case – rounding will be necessary.

Using big decimal functions for rounding the numbers

BigDecimal bill = new BigDecimal("23.9876546");
bill.setScale(2, RoundingMode.DOWN);

The above will be produce 23 this is because of the flag passed to it of RoundingMode.DOWN. There are other important flags that needs to be passed when rounding.

All the flags are here.

postgres not null

Format output of postgres from psql

Format output of postgres – this is like \G in mysql

If you are looking for the \G equilvalent of the mysql in postgres, then here is what you are looking for:

On the psql terminal just do the following

\x auto;

This will prepare whatever you are going to display on the terminal to have the right output

here is an example of out formatted or nice output when you issue the command

infohub=# select * from help;
-[ RECORD 1 ]+--------------------------------------------------------------------------------------------------------------------------------------
id           | 1
date_created | 2018-08-18 09:58:54.774
message      | use \x auto command to get output printed nicely
title        | how to show the select output nicely formatted in psql
member_id    | 1
-[ RECORD 2 ]+--------------------------------------------------------------------------------------------------------------------------------------
id           | 2
date_created | 2018-08-18 10:41:46.979
message      | postgres has a nice output format option as well.
title        | An equivalent of \G nice format in mysql in postgres
member_id    | 1

As it can be seen above, the output is nice and easy to navigate than a bulky columns here and there.

important docker commands

important docker commands

Important Docker Commands from Beginner to Expert level

Important docker commands I will be going over basic and important definitions and commands that is essential while working with docker images and docker containers

What is docker

The official definition of docker can be found here. But in short, you can consider docker as tool to develop and deploy code using the concept of containers

What is container

Important docker commands might not make any sense if we don’t properly define what a container is:
Continue reading important docker commands

spring upload multiple file

spring upload multiple files

Spring upload multiple files with restful API

Spring upload multiple files with restful api is one of the most requires task. If you are working with spring, chances are high that you are working with file as well.

If you are doing registration, you might need to have multiple files to be uploaded to your server. So how to do spring upload multiple files is what I will be showing you.
Continue reading spring upload multiple files

random file from directory

random files from directory

Get Random Files From Directory Using Shell Script

Random files from directory is most sought after operation specially when working with files. You may be creating video from image, analyzing sample data and more.

Shell scripting has a lot to offer, not only random files from directory. The cool thing about shell script is, it is more native and fast.

I am working on generating videos from selected images and I needed to have ability to select random files from the given folder.
Continue reading random files from directory

postgres not null

psql select not working

PSQL Select not working – or other simple commands not working

Working with command line postgres tool psql and having an issue?

Why doesn’t the psql command show the simple select query?

PSQL select not working is a problem that can happen in either of the following scenarios:

  1. Are you on the right database?
    Chances are you might not be on the right database at all and the relation you are trying to find might not be there at all:Check if you are on the right database.
    Run the command \l to see the available databases.How to connect to the postgres database from the list?
    Use the command \c database_name
    Let’s say the database name is inventory then:\c inventory will connect you to the right database
  2. Continue reading psql select not working

postgres not null

postgres not null to null

How to remove NOT NULL constraint in postgres

A NOT NULL constraint is essential tool in postgres database to ensure validation.

But sometimes, you need to remove not null and allow column to be null.

Remove NOT NULL Postgres

custom spring validator

custom spring validator

creating custom spring validator

Spring boot comes with lots of validation out of the box, Min, Max, NotNull. But, those are not enough as custom validations are important and needed.

When to use custom spring validator

When there is specific business logic that needs to be validated, custom validation will be to the rescue.

An example of creating a custom spring validator can be the following scenario. On your application there is a registration page.

Let’s say you have a survey sent out before registration and you want a user to fill out a survey using their email associated with it before registration.

When the user comes to your website for registration, one criteria as validated user is a completion of the survey. Here, you can create this validator and use it on more than one forms.

What do I need for custom spring validator

A basic knowledge of java and spring framework is good enough for this. Also make sure if it is worth creating spring custom validator. This is also the same if you are considering custom spring boot validator

Custom Spring validator example, spring validator tutorial

Let us extend the above example and work on it. We would like to have a unique email to be read from database as custom spring validator.

Let’s call it UniqueEmailValidator. This is will be checked mostly during registration and we can assume it can also used in a couple of other places.

First, we need to create a custom target interface

 * Custom email validation
 * Checks if the email already exists or not.
 * @author Kaleb Woldearegay 
@Target({ElementType.METHOD, ElementType.FIELD})
@Constraint(validatedBy = UniqueEmailValidator.class)
public @interface UniqueEmail {
    String message() default "{Email already exists}";
    Class[] groups() default {};
    Class[] payload() default {};

You can find the full code on github here.

Then we will need a concrete class that implements the interface

 * Custom email validation. Check if the provided email is unique or not.
 * @author Kaleb Woldearegay 
public class UniqueEmailValidator implements ConstraintValidator {

    private MemberService memberService; //this is injected to the class
    public UniqueEmailValidator() {}
    public UniqueEmailValidator(MemberService memberService) {
        this.memberService = memberService;
    public void initialize(UniqueEmail constraint) {
    public boolean isValid(String email, ConstraintValidatorContext context) {
        if (email == null) {
            return false;
        //actual logic of search for the member goes here..
        Optional member = this.memberService.findByEmail(email);
        return !member.isPresent();

You can see the full class here

The important part of the implementation is the isValid one. That should return the boolean outcome.

That is it, now you have the a full-fledged custom spring validator in created.

You can use it using the annotation @UniqueEmailValidator in your controller or wherever you need to validate.

If you have any questions or comments, let me know.

facebook auto post using php

facebook page post from PHP

How to autopost to facebook page using php

Facebook auto post using php, hmm.., I have a post on this topic a while ago, this one.

This post answers your questions of how to post to facebook page using php.

Though some of the initial steps do still hold, that is no more the case today. In order to combat the fake news and other improper usage of the platform, facebook has come up with different approach.
Continue reading facebook page post from PHP

spring boot validation

spring boot validation

Spring boot validation and Custom Validations

Spring boot validation is probably one of the cool features spring has to provide. Not only spring provided validations but also, it allows custom validation as well.

Custom Spring boot validator will allow you to act like the native validator in your application. Not only that, once you create custom spring validator, you can reuse it.

What is spring validator

Spring validator allows validating the data of the request. The validator will handle basic logic of handling validation than re-inventing the wheel.
Continue reading spring boot validation