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 */ 016package org.ametys.plugins.core.impl.group.directory; 017 018import java.util.Comparator; 019import java.util.HashMap; 020import java.util.HashSet; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 024import java.util.function.Predicate; 025import java.util.stream.Collectors; 026import java.util.stream.Stream; 027 028import org.apache.commons.lang3.StringUtils; 029 030import org.ametys.core.group.Group; 031import org.ametys.core.group.GroupIdentity; 032import org.ametys.core.group.directory.GroupDirectory; 033import org.ametys.core.user.UserIdentity; 034import org.ametys.core.util.SizeUtils.ExcludeFromSizeCalculation; 035import org.ametys.runtime.i18n.I18nizableText; 036import org.ametys.runtime.plugin.component.AbstractLogEnabled; 037/** 038 * Static implementation of a {@link GroupDirectory} 039 */ 040public class StaticGroupDirectory extends AbstractLogEnabled implements GroupDirectory 041{ 042 private static final String __PARAMETER_GROUPS_DEFINITION = "runtime.groups.static.group"; 043 044 private String _id; 045 private I18nizableText _label; 046 private String _gdModelId; 047 private Map<String, Object> _params; 048 049 private Map<String, Group> _groups; 050 051 @Override 052 public String getId() 053 { 054 return _id; 055 } 056 057 @Override 058 public I18nizableText getLabel() 059 { 060 return _label; 061 } 062 063 @Override 064 public void setId(String id) 065 { 066 _id = id; 067 } 068 069 @Override 070 public void setLabel(I18nizableText label) 071 { 072 _label = label; 073 } 074 075 @Override 076 public String getGroupDirectoryModelId() 077 { 078 return _gdModelId; 079 } 080 081 @Override 082 public Map<String, Object> getParameterValues() 083 { 084 return _params; 085 } 086 087 @Override 088 public void init(String groupDirectoryModelId, Map<String, Object> paramValues) throws Exception 089 { 090 _gdModelId = groupDirectoryModelId; 091 _groups = new HashMap<>(); 092 _params = paramValues; 093 String groupsAsText = (String) paramValues.get(__PARAMETER_GROUPS_DEFINITION); 094 for (String groupLine : groupsAsText.split("\\n")) 095 { 096 if (StringUtils.isNotEmpty(groupLine)) 097 { 098 String[] groupSplited = groupLine.split(":"); 099 if (groupSplited.length == 0) 100 { 101 getLogger().error("Error while reading StaticGroupDirectory groups, cannot create a group with the data from line {}", groupLine); 102 } 103 else 104 { 105 String groupId = groupSplited[0]; 106 String label = groupSplited[groupSplited.length > 1 ? 1 : 0]; 107 Set<UserIdentity> users = new HashSet<>(); 108 for (int i = 2; i < groupSplited.length; i++) 109 { 110 String userLogin = groupSplited[i].indexOf("#") > -1 ? StringUtils.substringBefore(groupSplited[i], "#") : groupSplited[i]; 111 String userPopulation = groupSplited[i].indexOf("#") > -1 ? StringUtils.substringAfter(groupSplited[i], "#") : null; 112 users.add(new UserIdentity(userLogin, userPopulation)); 113 } 114 _groups.put(groupId, new StaticGroup(groupId, _id, label, this, users)); 115 } 116 } 117 } 118 119 } 120 121 @Override 122 public Group getGroup(String groupID) 123 { 124 return _groups.getOrDefault(groupID, null); 125 } 126 127 @Override 128 public Set<Group> getGroups() 129 { 130 return new HashSet<>(_groups.values()); 131 } 132 133 @Override 134 public Set<String> getUserGroups(UserIdentity userIdentity) 135 { 136 return _groups.values().stream() 137 .filter(group -> group.getUsers().contains(userIdentity)) 138 .map(Group::getIdentity) 139 .map(GroupIdentity::getId) 140 .collect(Collectors.toSet()); 141 } 142 143 @Override 144 public List<Group> getGroups(int count, int offset, Map parameters) 145 { 146 String pattern = (String) parameters.get("pattern"); 147 boolean sort = (boolean) parameters.getOrDefault("sort", false); 148 Predicate<Group> matchesPattern = group -> 149 StringUtils.isEmpty(pattern) 150 || group.getLabel().toLowerCase().indexOf(pattern.toLowerCase()) != -1 151 || group.getIdentity() != null && group.getIdentity().getId().toLowerCase().indexOf(pattern.toLowerCase()) != -1; 152 153 Stream<Group> groupsStream = _groups.values().stream() 154 .filter(matchesPattern); 155 if (sort) 156 { 157 groupsStream = groupsStream.sorted(Comparator.comparing(Group::getLabel, String.CASE_INSENSITIVE_ORDER)); 158 } 159 return groupsStream 160 .skip(offset) 161 .limit(count) 162 .collect(Collectors.toList()); 163 } 164 165 private static final class StaticGroup implements Group 166 { 167 private GroupIdentity _identity; 168 private String _label; 169 private Set<UserIdentity> _users; 170 171 @ExcludeFromSizeCalculation 172 private GroupDirectory _groupDirectory; 173 174 StaticGroup(String id, String directoryId, String label, GroupDirectory groupDirectory, Set<UserIdentity> users) 175 { 176 _identity = new GroupIdentity(id, directoryId); 177 _label = label; 178 _groupDirectory = groupDirectory; 179 _users = users; 180 } 181 182 public GroupIdentity getIdentity() 183 { 184 return _identity; 185 } 186 187 public String getLabel() 188 { 189 return _label; 190 } 191 192 public GroupDirectory getGroupDirectory() 193 { 194 return _groupDirectory; 195 } 196 197 public Set<UserIdentity> getUsers() 198 { 199 return _users; 200 } 201 202 } 203}