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 to 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        return getContentOutgoingReferences(content, ContentContentElementType.OUTGOING_REFERENCE_TYPE);
049    }
050    
051    /**
052     * Get content outgoing references to the Ametys Object
053     * @param ametysObject the Ametys object
054     * @param type the type of relation (content, page, explorer, etc.)
055     * @return the node iterator of outgoing references
056     * @throws InvalidQueryException if a query error occurred
057     * @throws RepositoryException if a repository error occurred
058     */
059    public static NodeIterator getContentOutgoingReferences(JCRAmetysObject ametysObject, String type) throws InvalidQueryException, RepositoryException
060    {
061        StringBuilder queryStr = new StringBuilder("//element(")
062                .append(type)
063                .append(", ")
064                .append(RepositoryConstants.NAMESPACE_PREFIX).append(':').append(DefaultContent.METADATA_OUTGOING_REFERENCE_NODETYPE)
065                .append(')')
066                .append('[')
067                .append(RepositoryConstants.NAMESPACE_PREFIX).append(':').append(DefaultContent.METADATA_OUTGOING_REFERENCE_PROPERTY)
068                .append("='")
069                .append(ametysObject.getId())
070                .append("']");
071        
072        @SuppressWarnings("deprecation")
073        Query query = ametysObject.getNode().getSession().getWorkspace().getQueryManager().createQuery(queryStr.toString(), Query.XPATH);
074        return query.execute().getNodes();
075    }
076}