001/* 002 * Copyright 2016 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.ui.groupdirectories; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.HashSet; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 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; 033 034import org.ametys.core.cocoon.JSonReader; 035import org.ametys.core.group.GroupDirectoryContextHelper; 036import org.ametys.core.group.GroupDirectoryDAO; 037import org.ametys.core.group.directory.GroupDirectory; 038import org.ametys.core.group.directory.ModifiableGroupDirectory; 039 040/** 041 * Action for generating the modifiable group directories (all of it or by a given context) 042 */ 043public class GetModifiableGroupDirectoriesAction extends ServiceableAction 044{ 045 /** The DAO for group directories */ 046 private GroupDirectoryDAO _groupDirectoryDAO; 047 /** The helper for the associations group directory/context */ 048 private GroupDirectoryContextHelper _directoryContextHelper; 049 050 @Override 051 public void service(ServiceManager smanager) throws ServiceException 052 { 053 super.service(smanager); 054 _groupDirectoryDAO = (GroupDirectoryDAO) smanager.lookup(GroupDirectoryDAO.ROLE); 055 _directoryContextHelper = (GroupDirectoryContextHelper) smanager.lookup(GroupDirectoryContextHelper.ROLE); 056 } 057 058 @Override 059 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 060 { 061 List<Object> groupDirectories = new ArrayList<>(); 062 063 @SuppressWarnings("unchecked") 064 Map jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 065 066 @SuppressWarnings("unchecked") 067 List<String> contexts = (List<String>) jsParameters.get("contexts"); 068 if (contexts == null) 069 { 070 for (GroupDirectory gd : _groupDirectoryDAO.getGroupDirectories()) 071 { 072 if (gd instanceof ModifiableGroupDirectory) 073 { 074 groupDirectories.add(_groupDirectoryDAO.getGroupDirectory2Json(gd)); 075 } 076 } 077 } 078 else 079 { 080 Set<String> groupDirectoryIds = _directoryContextHelper.getGroupDirectoriesOnContexts(new HashSet<>(contexts)); 081 for (String gdId : groupDirectoryIds) 082 { 083 GroupDirectory gd = _groupDirectoryDAO.getGroupDirectory(gdId); 084 if (gd instanceof ModifiableGroupDirectory) 085 { 086 groupDirectories.add(_groupDirectoryDAO.getGroupDirectory2Json(gd)); 087 } 088 } 089 } 090 091 Map<String, Object> result = new HashMap<>(); 092 result.put("groupDirectories", groupDirectories); 093 094 Request request = ObjectModelHelper.getRequest(objectModel); 095 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 096 097 return EMPTY_MAP; 098 } 099 100}