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.data.holder.ModelLessDataHolder; 026import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder; 027import org.ametys.plugins.repository.data.holder.group.impl.ModifiableModelLessComposite; 028 029/** 030 * Helper class which provides methods to manage reactions on a object 031 */ 032public final class ReactionableObjectHelper 033{ 034 private ReactionableObjectHelper() 035 { 036 // Hide the default constructor. 037 } 038 039 /** 040 * Add a user reaction 041 * @param unversionedDataHolder the unversioned data holder 042 * @param user the issuer of reaction 043 * @param reactionType the reaction type 044 */ 045 public static void addReaction(ModifiableModelLessDataHolder unversionedDataHolder, UserIdentity user, ReactionType reactionType) 046 { 047 ModifiableModelLessComposite reactionHolder = _getReactionsHolder(unversionedDataHolder, reactionType); 048 049 UserIdentity[] userArray = _getUsers(reactionHolder); 050 if (!ArrayUtils.contains(userArray, user)) 051 { 052 UserIdentity[] newUserArray = ArrayUtils.add(userArray, user); 053 reactionHolder.setValue("users", newUserArray); 054 } 055 } 056 057 /** 058 * Remove a user reaction 059 * @param unversionedDataHolder the unversioned data holder 060 * @param user the issuer of reaction to remove 061 * @param reactionType the reaction type 062 */ 063 public static void removeReaction(ModifiableModelLessDataHolder unversionedDataHolder, UserIdentity user, ReactionType reactionType) 064 { 065 ModifiableModelLessComposite reactionHolder = _getReactionsHolder(unversionedDataHolder, reactionType); 066 067 UserIdentity[] userArray = _getUsers(reactionHolder); 068 if (ArrayUtils.contains(userArray, user)) 069 { 070 UserIdentity[] newUserArray = ArrayUtils.removeElement(userArray, user); 071 reactionHolder.setValue("users", newUserArray); 072 } 073 } 074 075 /** 076 * Get the issuers of a reaction 077 * @param unversionedDataHolder the unversioned data holder 078 * @param reactionType the reaction type 079 * @return the issuers of a reaction as a List 080 */ 081 public static List<UserIdentity> getReactionUsers(ModifiableModelLessDataHolder unversionedDataHolder, ReactionType reactionType) 082 { 083 ModifiableModelLessComposite reactionHolder = _getReactionsHolder(unversionedDataHolder, reactionType); 084 UserIdentity[] userArray = _getUsers(reactionHolder); 085 return Arrays.asList(userArray); 086 } 087 088 private static ModifiableModelLessComposite _getReactionsHolder(ModifiableModelLessDataHolder unversionedDataHolder, ReactionType reactionType) 089 { 090 return unversionedDataHolder.getComposite(reactionType.name().toLowerCase(), true); 091 } 092 093 private static UserIdentity[] _getUsers(ModelLessDataHolder dataHolder) 094 { 095 if (dataHolder.hasValue("users") && !dataHolder.isMultiple("users")) 096 { 097 UserIdentity user = dataHolder.getValue("users"); 098 return new UserIdentity[] {user}; 099 } 100 else 101 { 102 return dataHolder.getValue("users", new UserIdentity[0]); 103 } 104 } 105}