Monday, July 1, 2019

Tuto Spring Boot Webservice / JPA Repository / Oracle / Maven / Eclipse

This tutorial aims to build a simple web service using spring boot .
Let's start by ddownload our start package from :
https://start.spring.io/

We will call our project solvability <=> Artifact a sub functionnality of an extranet web services portal tn.cnss.extranet.employeur <=>Group
We will also choose maven to manage dependencies, tests and continuous dev/deployement actions




fig 1- Starting springboot package


Now w will import our project in eclipse environnement :
File > Import maven project



fig 2- Import project in eclipse



fig3- Before maven configuration

Once the project was imported, we will configure maven configuration in eclipse by indicating our M2_HOME
by default maven is configured to store dependencies on /user_home/.m2/repository as shown in this image


And after modification:


fig4- After maven changes

Now we will add our unique additional dependency (oracle driver)
Here ive already in my repo oracle 11.2.0.4 driver
For those who don't have oracle driver installed :
 1- Start by downloading the ojdbc (or copy it from sqldeveloper tool if you use it)
2- run this command :
mvn install:install-file -Dfile=<path-to-oracle-driver-file>

Once all is done we add the dependency tag in our pom file

        <dependency>

            <groupId>com.oracle</groupId>

            <artifactId>ojdbc</artifactId>

            <version>11.2.0.4</version>

        </dependency>



fig5- Oracle dependency






fig6- application.properties file


Now let's continue:
We have to setup our /src/main/resources/application.properties file; and put some needed parmaters:
in our case:
spring.datasource.url=jdbc\:oracle\:thin\:@ip.ip.ip.ip:\port\:SID
spring.datasource.username=user_name
spring.datasource.password=user_password
spring.jpa.properties.hibernate.show_sql=true_or_false
spring.jpa.properties.hibernate.use_sql_comments=true_or_false
spring.jpa.properties.hibernate.format_sql=true_or_false
server.port = our_webservice_port_number
logging.level.org.hibernate=ERROR_see_hibernate_logging_level
NB: ERROR is less verbose



Note now our application can start without errors.

Let's continue
Now we will create our RestController (Entry points for remote invokations)
A RestController is a java class annotated with @RestController
(Reference to org.springframework.web.bind.annotation.RestController)

To make our code structured we can create a package controller under our project






fig7- controllers package



And then we create our controller  under the new created  package
In our case we will check the solvability of a customer we will need two params
root number and key (consistency)
we will invoke that method from "APP_URL/check-solvability " with simple http GET request

So we use
@GetMapping annotation

So the behavior of our code  could be
 /**
  * Check if customer is solvable or no
  * @param root
  * @param key
  * @return true or no
  */
 @GetMapping( "/check-solvability/{rootValue}/{keyValue}") 
 public String getCustomerFinancialSituation(@PathVariable String rootValue, @PathVariable String keyValue ){
  //Some logic here 
  //For exemple if we will return true always
   JSONObject output = new JSONObject();
   output.put("result", "ok");
   output.put("value","1");
   return output.toString();
 
 }

Let's instroduce a business layer (it's logic that the business rules are delegated to a seperated class  
And we create our business layer or business package 
and business implementations package

fig8- business package

And we create this interface that will be implemented by the financial dev team 


fig9- business interface

Let's continue
Once interface is created we can proceed to business injection in the main controller, this can be done by these two lines
 @Autowired
 private FinancialBusiness financialBusiness;

fig10- business injection


Lets's continue

Now we create implementation package for business, and a specific implementation for our previously ceated interface Financial business




fig11- business interface implementation


if you want to interoduce repositories cantact me for more advanced example.








Wednesday, June 19, 2019

Remmina login failed for display 0 Centos client / xrdp server on Centos 7

Hello,
When trying to connect my desktop pc (CentOS 7 ) to my new Laptop (CentOS 7) vusing Remmina, i've faced this problem :
Login successfull and then a message window saying:
sending login info to session manager, please wait
login failed for display 0

is shown.

I tried  to change xrdp.ini file by modifying some parameters such as max_sessions or bpp values but the error persists to update my remmina version or to changs session manager params .....

When i used windows mstsc i discovered that is works fine, rdesktop works fins also(but loud)  I was certain that the problem was remmina's one ...


Solution was very simple for Remmina after 2 days   😕😕😕😕😕
i've just changed
color depth combobox value in my case to 24bpp (max_bpp=32 in /etc/xrdp/xrdp.ini) 

Now it works fine and with correct desktop refresh speed





Friday, March 22, 2019

JPA : Hibernate implementation raises exception when retrieving jdbc connection

For some cases, there a need to retrieve jdbc connection to execute some database operations such as batch works; tables loading from big files or others .....

When working with enitity manager

Connection conn = em.unwrap(Session.class).connection();  

raises an exception when working with an hibernate implementation (works fine with eclipselink)
One solution is :

public Connection hibernateConnection() {
  SessionImplementor si = (SessionImplementor) entityManager.unwrap(Session.class);
  try {
   return si.getJdbcConnectionAccess().obtainConnection();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return null;
  }
  
  
 }

Friday, March 1, 2019

JBoss 7 - JSF null value input become 0

this problem occures when binding an input in jsf to a numeric attribute in the bean.
Usually when we are running the app on apache tomcat we addthis jvm paramter in the tomcat VM
-Dorg.apache.el.parser.COERCE_TO_ZERO=false

For those who are running on JBoss Server 7.x
adding a context parameter


<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param> 
in standalone.xml file did not work for me.
One working solution is to create is to add a listener to your web app that adds this parameter dynamically
Here is the code

'Here i'm using WebListener annotation'

  
package com.cnss.ds.web.utils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;


@WebListener
public class AppWebLsistener  implements ServletContextListener {
 @Override
    public void contextInitialized(ServletContextEvent event) {

        System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");
    }

 @Override
 public void contextDestroyed(ServletContextEvent arg0) {
  // TODO Auto-generated method stub
  
 }

}

Tuesday, January 1, 2019

graphicupload :CKEditor plugin for image upload

Default CKEditor plugins allows files upload through a complicated and non intuitive dialog box.

graphic upload is a sample plugin which allow images upload  and insertion in CKEditor
Here is some printscreens



the plugin is available at
https://github.com/wassimjied/graphicupload