persistence.ContactPersistence.java Source code

Java tutorial

Introduction

Here is the source code for persistence.ContactPersistence.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 persistence;

import com.fasterxml.jackson.databind.ObjectMapper;
import dto.Contact;
import io.searchbox.core.SearchResult;
import java.io.IOException;
import persistence.util.JestConfig;

/**
 *
 * @author cristiano
 */
public class ContactPersistence {

    private static String typeName = "contact";

    public static void createContact(Contact contact) throws IOException {

        ObjectMapper mapper = new ObjectMapper();

        String data = mapper.writeValueAsString(contact);

        JestConfig.indexData(typeName, data);
    }

    public static SearchResult getContacts() throws IOException {

        String query = "{\"query\":{\"bool\":{\"must\":[{\"match_all\":{}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":1000}";

        return JestConfig.searchData(query);

    }

    public static void updateContact(String id, Contact contact) throws IOException {

        ObjectMapper mapper = new ObjectMapper();

        String data = mapper.writeValueAsString(contact);

        JestConfig.updateData(typeName, id, data);
    }

    public static void deleteContact(String id) throws IOException {

        JestConfig.deleteData(typeName, id);
    }
}