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

Monday, October 15, 2018

Display an address on a map without google api

Two months ago i discovered that Geocoding API of google allows 1 map display per day.
Now i'm on a project that needs to display an address marker on a map, since we are in developement phase, i'm not ready to pay 200$ to enable "up to 200$' use of google api.

Here is an alternative using nominatim and openstreetmap.
Long life to opensource.

Be careful, this code supposes that there is one lon,lat forsame address, then you must mention full address to get the waiting for result.


            




The map displayed at the end of the post is printed using the script above :) where mapdiv is an html div inside your page Be careful when importing jquery (do you have another import ? if you are using cms or already integrated temlplate) If you want to display many addresses you have to iterate through xml nodes print console.log(data); just after function(data, status){ .... to see the xml document returned ... If there is no places with the entered address, same thing you can guess it by reading the xml returned. thanks for reading !

Friday, September 29, 2017

Generic method to get oracle sequence nextval using hibernate

To get a sequence nextval in oracle using Hibernate
 
public static BigDecimal getSequenceNextVal(String sequenceName) {
  return (BigDecimal)HibernateUtils.getSession().createSQLQuery("select "+sequenceName+".NEXTVAL as id from dual").addScalar("id").uniqueResult();
 }
where HibernateUtils is the following class
 
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import tn.cnss.referentiel.models.Assure;
import tn.cnss.referentiel.models.Beneficiaire;


public class HibernateUtils {
 private static final SessionFactory sessionFactory;



 static {
  try {
  StandardServiceRegistry standardRegistry = 
  new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
  
  //Annotated  classes to be scanned
  List<Class<?>> classes = new ArrayList<Class<?>>();
  classes.add(YYYYYYYYYYYYY.class);
  classes.add(XXXXXXXXXXXXX.class);
  
  //Attach classes 
  MetadataSources metadataSources = new MetadataSources(standardRegistry);
  for (Class<?> annotatedClass : classes) {
      metadataSources.addAnnotatedClass(annotatedClass);
  }

  sessionFactory = metadataSources.buildMetadata()
      .buildSessionFactory();

  } catch (Throwable th) {

   System.err.println("Enitial SessionFactory creation failed" + th);
   throw new ExceptionInInitializerError(th);

  }
 }
 public static SessionFactory getSessionFactory() {

  return sessionFactory;

 }
 public static Session getSession() throws org.hibernate.HibernateException {
     SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
     Session sess = null;       
     try {         
         sess = sessionFactory.getCurrentSession();  
     } catch (org.hibernate.HibernateException he) {  
         sess = sessionFactory.openSession();     
     }             
     return sess;
  } 
}
    

Friday, September 22, 2017

Simple code to connect to database using hibernate (annotations) and maven

In this paper we will try to write a mavenized java application that will connect to an Oracle database and perform some data manipulation.

First of all, why maven ?
If you have the right jar dependencies whithout maven that's great ! no need to use maven... Personally i've faced a big problem to download and put the correct dependencies (no only packages but versions also!) together into my project.

Then let's talk maven !

1- Create the application with maven


Run this command line :

mvn archetype:generate -DgroupId=com.itillusion.migrators -DartifactId=loader -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


2- Add dependencies to POM

 
 
     org.hibernate
     hibernate-core
     5.2.11.Final
 
 
 
     org.hibernate
     hibernate-tools
     3.2.0.ga
 

And that's all, no need to other dependencies ...
So our new pom.xml become :

 

  4.0.0
  com.itillusion.migrators
  loader
  jar
  1.0-SNAPSHOT
  loader
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
    
    
 
     org.hibernate
     hibernate-core
     5.2.11.Final
 
 
     org.hibernate
     hibernate-tools
     3.2.0.ga
 
     
  


Note that here we use hibernate 5.2.11

3- Build the HibernateUtils class

This class is recommended, it is a Singleton design pattern implementation, and will facilitate our work

 
package com.itillusion.migrators.utils;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import tn.cnss.referentiel.models.Assure;
import tn.cnss.referentiel.models.Beneficiaire;


public class HibernateUtils {
 private static final SessionFactory sessionFactory;



 static {
  try {
  StandardServiceRegistry standardRegistry = 
  new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
  
  //Annotated  classes to be scanned
  List> classes = new ArrayList>();
  classes.add(Assure.class);
  classes.add(Beneficiaire.class);
  
  //Attach classes 
  MetadataSources metadataSources = new MetadataSources(standardRegistry);
  for (Class annotatedClass : classes) {
      metadataSources.addAnnotatedClass(annotatedClass);
  }

  sessionFactory = metadataSources.buildMetadata()
      .buildSessionFactory();

  } catch (Throwable th) {

   System.err.println("Enitial SessionFactory creation failed" + th);
   throw new ExceptionInInitializerError(th);

  }
 }
 public static SessionFactory getSessionFactory() {

  return sessionFactory;

 }
 public static Session getSession() throws org.hibernate.HibernateException {
     SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
     Session sess = null;       
     try {         
         sess = sessionFactory.getCurrentSession();  
     } catch (org.hibernate.HibernateException he) {  
         sess = sessionFactory.openSession();     
     }             
     return sess;
  } 
}
Note in the fragment
 
  classes.add(Assure.class);
  classes.add(Beneficiaire.class);

We have added our annotated classes !
I'm sure that there is a more elegant methods to do this, (iteration on packages using java reflection api) but in my case i just need two tabled to map.

Note also that we conserved our classic hibernate.cfg.xml file. (Surely we can avoid that and use100% java code to confgiure hibernate, but i don't recommand that and it is more logic to store database informations in a separate faile that can change independently from our code)

Here is a simple cfg file:
 



    
        oracle.jdbc.driver.OracleDriver
        PASSWORD
        jdbc:oracle:thin:@xxx.yy.zz.tt:SID
        USERNAME
        DEFAULT_SCHEMA 
        org.hibernate.dialect.Oracle10gDialect
    


to be continued

Tuesday, September 5, 2017

Prestashop error Fatal error: Call to a member function getPageLink() on null in ......file.blockcart.tpl.php

After writing a new controller displaying all products in a shop i got this error message.
don't go so fat, check if you did the same mistake as me
    public function init()
    {
           /parent::init();  //it's required
    }

The controller was written under ps 1.6
I