001/* 002 * Copyright 2015 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.group; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.HashSet; 021import java.util.List; 022import java.util.Map; 023import java.util.stream.Collectors; 024 025import org.apache.avalon.framework.parameters.Parameters; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.cocoon.acting.ServiceableAction; 029import org.apache.cocoon.environment.ObjectModelHelper; 030import org.apache.cocoon.environment.Redirector; 031import org.apache.cocoon.environment.Request; 032import org.apache.cocoon.environment.SourceResolver; 033import org.apache.commons.lang.StringUtils; 034 035import org.ametys.core.cocoon.JSonReader; 036import org.ametys.core.group.Group; 037import org.ametys.core.group.GroupDirectoryContextHelper; 038import org.ametys.core.group.GroupDirectoryDAO; 039import org.ametys.core.group.directory.GroupDirectory; 040 041/** 042 * Get groups 043 * 044 */ 045public class GroupSearchAction extends ServiceableAction 046{ 047 private static final int _DEFAULT_COUNT_VALUE = 100; 048 private static final int _DEFAULT_OFFSET_VALUE = 0; 049 050 private GroupDirectoryDAO _groupDirectoryDAO; 051 private GroupDirectoryContextHelper _directoryContextHelper; 052 private GroupHelper _groupHelper; 053 054 @Override 055 public void service(ServiceManager smanager) throws ServiceException 056 { 057 super.service(smanager); 058 _groupDirectoryDAO = (GroupDirectoryDAO) smanager.lookup(GroupDirectoryDAO.ROLE); 059 _directoryContextHelper = (GroupDirectoryContextHelper) smanager.lookup(GroupDirectoryContextHelper.ROLE); 060 _groupHelper = (GroupHelper) manager.lookup(GroupHelper.ROLE); 061 } 062 063 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 064 { 065 @SuppressWarnings("unchecked") 066 Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 067 068 List<Map<String, Object>> groups = new ArrayList<>(); 069 070 @SuppressWarnings("unchecked") 071 List<String> contexts = (List<String>) jsParameters.get("contexts"); 072 073 if (contexts != null) 074 { 075 _searchGroupsByContext(groups, jsParameters, source, parameters, contexts); 076 } 077 else 078 { 079 String groupDirectoryId = (String) jsParameters.get("groupDirectoryId"); 080 _searchGroupsByDirectory(groups, jsParameters, source, parameters, groupDirectoryId); 081 } 082 083 Map<String, Object> result = new HashMap<>(); 084 result.put("groups", groups); 085 086 Request request = ObjectModelHelper.getRequest(objectModel); 087 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 088 089 return EMPTY_MAP; 090 } 091 092 /** 093 * Get the search parameters 094 * @param source The search pattern 095 * @return the search parameters 096 */ 097 protected Map<String, String> _getSearchParameters (String source) 098 { 099 Map<String, String> params = new HashMap<>(); 100 params.put("pattern", source); 101 return params; 102 } 103 104 private void _searchGroupsByContext(List<Map<String, Object>> groups, Map<String, Object> jsParameters, String source, Parameters parameters, List<String> contexts) 105 { 106 @SuppressWarnings("unchecked") 107 List<String> groupIds = (List) jsParameters.get("id"); 108 109 if (groupIds != null) 110 { 111 for (String groupDirectoryId : _directoryContextHelper.getGroupDirectoriesOnContexts(new HashSet<>(contexts))) 112 { 113 for (String groupId : groupIds) 114 { 115 _addNonNullJsonGroup(groups, groupDirectoryId, groupId); 116 } 117 } 118 } 119 else if (jsParameters.get("value") != null) 120 { 121 String[] values = ((String) jsParameters.get("value")).split(","); 122 for (String value : values) 123 { 124 String groupId = StringUtils.substringBeforeLast(value, "#"); 125 String groupDir = StringUtils.substringAfterLast(value, "#"); 126 _addNonNullJsonGroup(groups, groupDir, groupId); 127 } 128 } 129 else 130 { 131 int count = parameters.getParameterAsInteger("limit", _DEFAULT_COUNT_VALUE); 132 if (count == -1) 133 { 134 count = Integer.MAX_VALUE; 135 } 136 int offset = parameters.getParameterAsInteger("start", _DEFAULT_OFFSET_VALUE); 137 138 for (String groupDirectoryId : _directoryContextHelper.getGroupDirectoriesOnContexts(new HashSet<>(contexts))) 139 { 140 GroupDirectory groupDirectory = _groupDirectoryDAO.getGroupDirectory(groupDirectoryId); 141 groups.addAll(_groupHelper.groups2JSON(groupDirectory.getGroups(count, offset, _getSearchParameters(source)), false)); 142 } 143 } 144 } 145 146 private void _searchGroupsByDirectory(List<Map<String, Object>> groups, Map<String, Object> jsParameters, String source, Parameters parameters, String groupDirectoryId) 147 { 148 @SuppressWarnings("unchecked") 149 List<String> groupIds = (List) jsParameters.get("id"); 150 151 if (groupIds != null) 152 { 153 for (String groupId : groupIds) 154 { 155 _addNonNullJsonGroup(groups, groupDirectoryId, groupId); 156 } 157 } 158 else 159 { 160 int count = parameters.getParameterAsInteger("limit", _DEFAULT_COUNT_VALUE); 161 if (count == -1) 162 { 163 count = Integer.MAX_VALUE; 164 } 165 int offset = parameters.getParameterAsInteger("start", _DEFAULT_OFFSET_VALUE); 166 167 GroupDirectory groupDirectory = _groupDirectoryDAO.getGroupDirectory(groupDirectoryId); 168 groups.addAll(groupDirectory.getGroups(count, offset, _getSearchParameters(source)).stream() 169 .map(group -> _groupHelper.group2JSON(group, false)) 170 .collect(Collectors.toList())); 171 } 172 } 173 174 private void _addNonNullJsonGroup(List<Map<String, Object>> groups, String groupDirectoryId, String id) 175 { 176 GroupDirectory groupDirectory = _groupDirectoryDAO.getGroupDirectory(groupDirectoryId); 177 if (groupDirectory != null) 178 { 179 Group group = groupDirectory.getGroup(id); 180 if (group != null) 181 { 182 Map<String, Object> groupAsJson = _groupHelper.group2JSON(group, false); 183 groups.add(groupAsJson); 184 } 185 else 186 { 187 getLogger().warn(String.format("Group of id '%s' and group directory '%s' does not exist. It may have been removed. Thus, it will not be returned.", id, groupDirectoryId)); 188 } 189 } 190 else 191 { 192 getLogger().warn(String.format("Group directory of id '%s' does not exist. It may have been removed. Thus, its groups will not be returned.", groupDirectoryId)); 193 } 194 } 195}