All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.graphaware.module.es.mapping.json.DocumentRepresentation Maven / Gradle / Ivy

/*
 * Copyright (c) 2013-2016 GraphAware
 *
 * This file is part of the GraphAware Framework.
 *
 * GraphAware Framework is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If not, see .
 */
package com.graphaware.module.es.mapping.json;

import com.graphaware.common.log.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.neo4j.logging.Log;

public class DocumentRepresentation {
    
    private static final Log LOG = LoggerFactory.getLogger(DocumentRepresentation.class);

    private final String index;

    private final String type;

    private final String id;

    private final Map source;
    
    private final ObjectMapper objectMapper = new ObjectMapper();

    public DocumentRepresentation(String index, String type, String id) {
        this.index = index;
        this.type = type;
        this.id = id;
        this.source = new HashMap<>();
    }

    public DocumentRepresentation(String index, String type, String id, Map source) {
        this.index = index;
        this.type = type;
        this.id = id;
        this.source = source;
    }

    public String getIndex() {
        return index;
    }

    public String getType() {
        return type;
    }

    public String getId() {
        return id;
    }

    public Map getSource() {
        return source;
    }
    
    public String getJson() throws DocumentRepresentationException {
        try {
            return objectMapper.writeValueAsString(source);
        } catch (IOException ex) {
            LOG.error("Error while creating json from action: " + source, ex);
            // @// TODO: 24/07/16  Should we really throw the exception here, instead of silently logging and failing 
            throw new DocumentRepresentationException(source, ex);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy