001/*
002 *  Copyright 2019 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 */
016
017package org.ametys.cms.content.references;
018
019import javax.jcr.NodeIterator;
020import javax.jcr.RepositoryException;
021import javax.jcr.query.InvalidQueryException;
022import javax.jcr.query.Query;
023
024import org.ametys.cms.repository.DefaultContent;
025import org.ametys.plugins.repository.RepositoryConstants;
026import org.ametys.plugins.repository.jcr.JCRAmetysObject;
027
028/**
029 * Helper for outgoing references
030 */
031public final class OutgoingReferencesHelper
032{
033    private OutgoingReferencesHelper()
034    {
035        // Helper class, never to be instantiated.
036    }
037    
038    /**
039     * Get content outgoing references from the content
040     * @param content the content
041     * @return the node iterator of outgoing references
042     * @throws InvalidQueryException if a query error occurred
043     * @throws RepositoryException if a repository error occurred
044     */
045    public static NodeIterator getContentOutgoingReferences(JCRAmetysObject content) throws InvalidQueryException, RepositoryException
046    {
047        StringBuilder queryStr = new StringBuilder("//element(")
048                .append(OutgoingReferencesExtractor.OUTGOING_REFERENCE_TYPE_CONTENT).append(", ")
049                .append(RepositoryConstants.NAMESPACE_PREFIX).append(':').append(DefaultContent.METADATA_OUTGOING_REFERENCE_NODETYPE)
050                .append(')');
051
052        queryStr.append('[').append(RepositoryConstants.NAMESPACE_PREFIX).append(':').append(DefaultContent.METADATA_OUTGOING_REFERENCE_PROPERTY).append("='").append(content.getId()).append("']");
053        @SuppressWarnings("deprecation")
054        Query query = content.getNode().getSession().getWorkspace().getQueryManager().createQuery(queryStr.toString(), Query.XPATH);
055        return query.execute().getNodes();
056    }
057}