com.seajas.search.profiler.service.taxonomy.TaxonomyService.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.profiler.service.taxonomy.TaxonomyService.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * 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 <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.profiler.service.taxonomy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seajas.search.bridge.jms.integration.MessageConstants;
import com.seajas.search.bridge.profiler.model.taxonomy.TaxonomyNode;
import com.seajas.search.bridge.profiler.model.taxonomy.TaxonomyTranslation;
import com.seajas.search.profiler.jms.service.InjectionService;
import com.seajas.search.profiler.service.profiler.dao.TaxonomyDAO;

/**
 * Taxonomy service.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@Service
public class TaxonomyService {
    /**
     * The logger.
     */
    private static final Logger logger = LoggerFactory.getLogger(TaxonomyService.class);

    /**
     * The injection service.
     */
    @Autowired
    private InjectionService injectionService;

    /**
     * The taxonomy DAO.
     */
    @Autowired
    private TaxonomyDAO taxonomyDAO;

    /**
     * Retrieve all taxonomy nodes.
     * 
     * @return List<TaxonomyNode>
     */
    public List<TaxonomyNode> getAllTaxonomyNodes() {
        return taxonomyDAO.findAllNodesFlat();
    }

    /**
     * Request a list of matches and their identifiers from all anonymous and collection-related entries.
     * 
     * @param collection
     * @param match
     * @return List<Integer>
     */
    public List<Integer> requestIdentifiers(final String collection, final String match) {
        List<Integer> result = new ArrayList<Integer>();

        // Retrieve the node

        TaxonomyNode node = taxonomyDAO.findNodeByMatch(collection, match, true);

        // Create a new node if one doesn't already exist

        if (node == null) {
            if (logger.isDebugEnabled())
                logger.debug("No taxonomy nodes found for given match '" + match + "' on collection '" + collection
                        + "'. Creating a new node.");

            Integer newNodeId = taxonomyDAO.addNode(match, match, collection, TaxonomyDAO.IDENTIFIER_UNASSIGNED,
                    false);

            if ((node = taxonomyDAO.findNodeById(newNodeId)) == null) {
                logger.error("Could not retrieve the newly created taxonomy node for given match '" + match
                        + "' on collection '" + collection + "'. Returning an empty list.");

                return result;
            }
        }

        // Add all identifiers to the list

        do {
            result.add(node.getId());
        } while ((node = node.getParent()) != null);

        return result;
    }

    /**
     * Request the parent IDs which match the given identifier (inclusive).
     * 
     * @param id
     * @return List<Integer>
     */
    public List<Integer> getTaxonomyParentIds(final Integer id) {
        // Retrieve the node

        TaxonomyNode node = taxonomyDAO.findNodeById(id);

        // Create a new node if one doesn't already exist

        if (node == null)
            return Arrays.asList(new Integer[] { id });
        else {
            List<Integer> result = new ArrayList<Integer>();

            do {
                result.add(node.getId());
            } while (node.getParentId() != null && (node = taxonomyDAO.findNodeById(node.getParentId())) != null);

            Collections.reverse(result);

            return result;
        }
    }

    /**
     * Retrieve a taxonomy node by its ID.
     * 
     * @param id
     * @return TaxonomyNode
     */
    public TaxonomyNode getTaxonomyNode(final Integer id) {
        return taxonomyDAO.findNodeById(id);
    }

    /**
     * Retrieve a taxonomy node by its match and collection.
     * 
     * @param match
     * @param collection
     * @return TaxonomyNode
     */
    public TaxonomyNode getTaxonomyNodeByMatch(final String match, final String collection) {
        return taxonomyDAO.findNodeByMatch(collection, match, true);
    }

    /**
     * Return the taxonomy children which are direct descendants of the given node.
     * 
     * @param id
     * @return List<TaxonomyNode>
     */
    public List<TaxonomyNode> getTaxonomyChildrenNodes(final Integer id) {
        return taxonomyDAO.findChildrenNodesById(id);
    }

    /**
     * Create a new taxonomy node under the given parent.
     * 
     * @param nodeName
     * @param parentId
     * @return Integer
     */
    public Integer addTaxonomyNode(final String nodeName, final Integer parentId) {
        Integer nodeId = taxonomyDAO.addNode(nodeName, null, null, parentId, false);

        if (nodeId != null) {
            // Retrieve and then inject the created ('modified') node

            TaxonomyNode createdNode = taxonomyDAO.findNodeById(nodeId);

            injectionService.replicate(createdNode, MessageConstants.REPLICATION_ACTION_UPDATE_TAXONOMY);
        }

        return nodeId;
    }

    /**
     * Update an existing taxonomy node using the given values.
     * 
     * @param id
     * @param name
     * @param match
     * @param collection
     * @param isHidden
     * @param translations
     * @return boolean
     */
    public boolean updateTaxonomyNode(final Integer id, final String name, final String match,
            final String collection, final Boolean isHidden, final List<TaxonomyTranslation> translations) {
        if (taxonomyDAO.modifyNode(id, name, match, collection, isHidden, translations)) {
            // Retrieve and then inject the modified node

            TaxonomyNode updatedNode = taxonomyDAO.findNodeById(id);

            injectionService.replicate(updatedNode, MessageConstants.REPLICATION_ACTION_UPDATE_TAXONOMY);

            return true;
        }

        return false;
    }

    /**
     * Move an existing taxonomy node to a new parent.
     * 
     * @param id
     * @param parentId
     */
    public void moveTaxonomyNode(final Integer id, final Integer parentId) {
        if (taxonomyDAO.moveNode(id, parentId)) {
            // Retrieve and then inject the modified node

            TaxonomyNode movedNode = taxonomyDAO.findNodeById(id);

            injectionService.replicate(movedNode, MessageConstants.REPLICATION_ACTION_UPDATE_TAXONOMY);
        }
    }

    /**
     * Remove a given taxonomy node.
     * 
     * @param id
     */
    public void removeTaxonomyNode(final Integer id) {
        if (taxonomyDAO.deleteNode(id))
            injectionService.replicate(id, MessageConstants.REPLICATION_ACTION_DELETE_TAXONOMY);
    }
}