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