com.geekzone.search.tools.Utils.java Source code

Java tutorial

Introduction

Here is the source code for com.geekzone.search.tools.Utils.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.geekzone.search.tools;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static javassist.CtMethod.ConstParameter.string;
import org.apache.commons.codec.digest.DigestUtils;

public class Utils {

    private static final ObjectMapper mapper = new ObjectMapper();

    /**
     * @description Cette fonction permet de calculer le hashCode de l'objet
     * (entit de la BD) en utilisant la chaine de caratre au format JSON
     * reprsentant l'objet.
     * @param obj
     * @return
     * @throws JsonProcessingException 
     */
    public static int getHash(Object obj) throws JsonProcessingException {

        return getDocumentAsString(obj).hashCode();
    }

    /**
     * @description Cette fonction permet de calculer le hashCode de l'objet
     * (entit de la BD) en utilisant la chaine de caratre au format JSON
     * reprsentant l'objet.
     * @param obj
     * @return
     * @throws JsonProcessingException 
     */
    public static String getHashCode(Object obj) throws JsonProcessingException {
        String hashCode = "";
        hashCode = DigestUtils.sha1Hex(getDocumentAsString(obj));
        return hashCode;
    }

    /**
     * @description Cette fonction permet de rcuprer une entit (table de la base de donne)
     *  sous la forme d'une chaine de caractre au format JSON. Elle servira a calculer le
     *  hashCode de l'objet (entitit de la base de donne) et a crer un champ supplmentaire
     *  pour ajouter le hashCode avant de le convertir en byte[].
     * @param obj
     * @return
     * @throws JsonProcessingException 
     * 
     */
    public static String getDocumentAsString(Object obj) throws JsonProcessingException {

        String document = mapper.writeValueAsString(obj);
        return document;
    }

    /**
     * @description Cette fonction permet d'ajouter le champ "hashCode"
     * au document de l'objet (entit de la BD) avant de l'indexer
     * @param obj
     * @return
     * @throws IOException 
     */
    public static String addHashCode(Object obj) throws IOException {
        JsonNode node = mapper.readTree(getDocumentAsString(obj));
        ObjectNode on = (ObjectNode) node;
        on.put("hashCode", getHashCode(obj));
        String finalObject = node.toString();
        return finalObject;
    }

    /**
     * @description Cette fonction permet de rcuprer le document complet
     * de l'objet  indexer sous forme de tableau de byte (type accept 
     * par elasticSearch pour indexer un document JSON obtenu par conversion
     * d'un objet grce une librairie commme Jackson)
     * @param obj
     * @return
     * @throws JsonProcessingException
     * @throws IOException 
     */
    public static byte[] getDocument(Object obj) throws JsonProcessingException, IOException {
        byte[] document = addHashCode(obj).getBytes(StandardCharsets.UTF_8);
        return document;
    }

}