mercredi 28 juillet 2021

*** annotation LOMBOK *** replacing getters and setters methods

 *** annotation LOMBOK ***

in IntelliJ : install the plugin lombok
in project pom.xml : add the dependency
in Entity add those annotations
@Entity
@Data @NoArgsConstructor @AllArgsConstructor @ToString

and we have not need to add Setters, Getters, and Constructors methods

mardi 27 juillet 2021

@ManyToOne and @OneToOne relations with @EmbeddedId

******************************

*** @ManyToOne and @OneToOne relations with @EmbeddedId

******************************


@Entity

public class Bons implements Serializable {


@EmbeddedId

private BonsKey id;

...

}

@Embeddable

public class BonsKey implements Serializable {


@ManyToOne

@JoinColumn(name = "CODE_PRESTATAIRE")

private Prestataires prestataire;

@ManyToOne

@JoinColumn(name = "NUM_SERVICE")

private ServicesDossiers serviceDossier;

...

}

@Entity

public class Communications implements Serializable{


@Id @GeneratedValue

@Column(name="CODE_COMMUNICATION")

private Long codeCommunication;



@ManyToOne

@JoinColumns({

@JoinColumn(name="BON_CODE_PRESTATAIRE", nullable = true),

@JoinColumn(name="BON_NUM_SERVICE",nullable = true)

})

private Bons bonId;

} 

lundi 26 juillet 2021

GIT install Config

1- Download GIT Installer from https://git-scm.com/downloads

2- Configure username and Email

$ git config --global user.name "Mohamed AMINE OSMAN"

$ git config --global user.email "medamine.osman@apus-consulting.tn"

3- Create the first Git Repository

$ git init first_project

$ cd first_project

create file1.txt

$ git add file1.txt

$ git commit -m "this is my first commit"

4- Edit your setting

$ git config --list --show-origin

vendredi 16 juillet 2021

Spring Boot Get Client IP Adress

private final String LOCALHOST_IPV4 = "127.0.0.1";

private final String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";


public String getClientIp(HttpServletRequest request) {

String ipAddress = request.getHeader("X-Forwarded-For");

if(StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {

ipAddress = request.getHeader("Proxy-Client-IP");

}


if(StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {

ipAddress = request.getHeader("WL-Proxy-Client-IP");

}


if(StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {

ipAddress = request.getRemoteAddr();

if(LOCALHOST_IPV4.equals(ipAddress) || LOCALHOST_IPV6.equals(ipAddress)) {

try {

InetAddress inetAddress = InetAddress.getLocalHost();

ipAddress = inetAddress.getHostAddress();

} catch (UnknownHostException e) {

e.printStackTrace();

}

}

}


if(!StringUtils.isEmpty(ipAddress)

&& ipAddress.length() > 15

&& ipAddress.indexOf(",") > 0) {

ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));

}


return ipAddress;

}

@RequestMapping(value="/Index")

public String Index(HttpServletRequest request) {

System.out.println("mon adresse ip:"+getClientIp(request));

}


run console:

mon adresse ip:xxx.xxx.xxx.xxx


dimanche 11 juillet 2021

Date manipulation

 LocalDateTime localDateTime = LocalDateTime.now();

int year = localDateTime.getYear();

int month = localDateTime.getMonthValue();

int day = localDateTime.getDayOfMonth();

int hour = localDateTime.getHour();

int minute = localDateTime.getMinute();

int seconde = localDateTime.getSecond();


System.out.println("local date:" + year + "/" + month + "/" + day + ":" + hour + ":" + minute + ":" + seconde);

local date:2021/7/12:12:18:18

-------------------------------------------------------------------------------------

int year = service.getDateService().getYear()+1900;

int month = service.getDateService().getMonth()+1;

System.out.println("année service:"+year);

System.out.println("mois service:"+month);

System.out.println("mois service:"+String.valueOf(year)+String.format("%02d", month));

//année service:2021

//mois service:7

//mois service:202107

   

--> how to resolve a deprecated getYear() and getMonth():

//Initialize your Date however you like it.

Date date = service.getDateService();

Calendar calendar = new GregorianCalendar();

calendar.setTime(date);

int annee = calendar.get(Calendar.YEAR);

//Add one to month {0 - 11}

int mois = calendar.get(Calendar.MONTH) + 1;

int jours = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println("année:"+annee);

System.out.println("mois:"+mois);

System.out.println("jours:"+jours);

//année:2021

//mois:7

//jours:10

vendredi 2 juillet 2021

Thymeleaf when the value is null

 th:value="${beneficiaireDossier.client} ? ${beneficiaireDossier.client.libelleClient} : ''"

<input class="form-control form-control-sm" type="text" readonly="readonly" th:value="${beneficiaireDossier.client} ? ${beneficiaireDossier.client.libelleClient} : ''"/>


jeudi 24 juin 2021

Recursive relationship in Spring Data and JPA

@Entity

public class Rubriques implements Serializable {


@Id @GeneratedValue(strategy = GenerationType.IDENTITY)

@Column(name="CODE_RUBRIQUE")

private int codeRubrique;

@Column(name="LIBELLE_RUBRIQUE", length = 120)

private String libelleRubrique;


@OneToMany(mappedBy="rubriqueParent")

  private Set<Rubriques> sousRubrique;


  @ManyToOne

  @JoinColumn(name="RUBRIQUE_PARENT")

  private Rubriques rubriqueParent;

   

  @ManyToOne

  @JoinColumn(name="CODE_TYPE_RUBRIQUE")

  private TypesRubrique typeRubrique;