001/*
002 *  Copyright 2018 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.cms.repository;
017
018import java.util.Arrays;
019import java.util.List;
020
021import org.apache.commons.lang3.ArrayUtils;
022
023import org.ametys.cms.repository.ReactionableObject.ReactionType;
024import org.ametys.core.user.UserIdentity;
025import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
026
027/**
028 * Helper class which provides methods to manage reactions on a object
029 */
030public final class ReactionableObjectHelper
031{
032    private ReactionableObjectHelper()
033    {
034        // Hide the default constructor.
035    }
036    
037    /**
038     * Add a user reaction
039     * @param unversionedMetadataHolder the unversioned metadata holder
040     * @param user the issuer of reaction
041     * @param reactionType the reaction type
042     */
043    public static void addReaction(ModifiableCompositeMetadata unversionedMetadataHolder, UserIdentity user, ReactionType reactionType)
044    {
045        ModifiableCompositeMetadata reactionHolder = _getReactionsHolder(unversionedMetadataHolder, reactionType);
046        
047        UserIdentity[] userArray = reactionHolder.getUserArray("users", new UserIdentity[0]);
048        if (!ArrayUtils.contains(userArray, user))
049        {
050            UserIdentity[] newUserArray = ArrayUtils.add(userArray, user);
051            reactionHolder.setMetadata("users", newUserArray);
052        }
053    }
054
055    /**
056     * Remove a user reaction
057     * @param unversionedMetadataHolder the unversioned metadata holder
058     * @param user the issuer of reaction to remove
059     * @param reactionType the reaction type
060     */
061    public static void removeReaction(ModifiableCompositeMetadata unversionedMetadataHolder, UserIdentity user, ReactionType reactionType)
062    {
063        ModifiableCompositeMetadata reactionHolder = _getReactionsHolder(unversionedMetadataHolder, reactionType);
064        
065        UserIdentity[] userArray = reactionHolder.getUserArray("users", new UserIdentity[0]);
066        if (ArrayUtils.contains(userArray, user))
067        {
068            UserIdentity[] newUserArray = ArrayUtils.removeElement(userArray, user);
069            reactionHolder.setMetadata("users", newUserArray);
070        }
071    }
072    
073    /**
074     * Get the issuers of a reaction
075     * @param unversionedMetadataHolder the unversioned metadata holder
076     * @param reactionType the reaction type
077     * @return the issuers of a reaction as a List
078     */
079    public static List<UserIdentity> getReactionUsers(ModifiableCompositeMetadata unversionedMetadataHolder, ReactionType reactionType)
080    {
081        ModifiableCompositeMetadata reactionHolder = _getReactionsHolder(unversionedMetadataHolder, reactionType);
082        return Arrays.asList(reactionHolder.getUserArray("users", new UserIdentity[0]));
083    }
084    
085    private static ModifiableCompositeMetadata _getReactionsHolder(ModifiableCompositeMetadata unversionedMetadataHolder, ReactionType reactionType)
086    {
087        return unversionedMetadataHolder.getCompositeMetadata(reactionType.name().toLowerCase(), true);
088    }
089}