001/*
002 *  Copyright 2012 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.repository.jcr;
017
018import javax.jcr.Node;
019import javax.jcr.RepositoryException;
020
021import org.apache.jackrabbit.JcrConstants;
022
023/**
024 * Provides helper methods on node types.
025 */
026public final class NodeTypeHelper
027{
028    
029    private NodeTypeHelper()
030    {
031        // Hides the default constructor.
032    }
033    
034    /**
035     * Get the real nodetype name of a given node (even if it's a frozen node).
036     * @param node the node.
037     * @return the real node's nodetype name.
038     * @throws RepositoryException if an error occurs.
039     */
040    public static String getNodeTypeName(Node node) throws RepositoryException
041    {
042        String nodeTypeName = node.getPrimaryNodeType().getName();
043        
044        String realNodeTypeName = nodeTypeName;
045        
046        if (JcrConstants.NT_FROZENNODE.equals(nodeTypeName))
047        {
048            realNodeTypeName = node.getProperty(JcrConstants.JCR_FROZENPRIMARYTYPE).getString();
049        }
050        
051        return realNodeTypeName;
052    }
053    
054    /**
055     * Test the real nodetype name of a given node (even if it's a frozen node).
056     * @param node the node to test.
057     * @param nodeTypeName the node type to test.
058     * @return true if the node is of the given nodetype, false otherwise.
059     * @throws RepositoryException if an error occurs.
060     */
061    public static boolean isNodeType(Node node, String nodeTypeName) throws RepositoryException
062    {
063        if (node.isNodeType(nodeTypeName))
064        {
065            return true;
066        }
067        
068        String theNodeTypeName = node.getPrimaryNodeType().getName();
069        
070        if (JcrConstants.NT_FROZENNODE.equals(theNodeTypeName))
071        {
072            String realNodeTypeName = node.getProperty(JcrConstants.JCR_FROZENPRIMARYTYPE).getString();
073            
074            return nodeTypeName.equals(realNodeTypeName);
075        }
076        
077        return false;
078    }
079    
080}