001/* 002 * Copyright 2013 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.queriesdirectory.actions; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022import java.util.Optional; 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; 033import org.apache.commons.lang3.StringUtils; 034 035import org.ametys.core.cocoon.JSonReader; 036import org.ametys.core.group.GroupIdentity; 037import org.ametys.core.group.GroupManager; 038import org.ametys.core.user.CurrentUserProvider; 039import org.ametys.core.user.UserIdentity; 040import org.ametys.plugins.queriesdirectory.Query; 041import org.ametys.plugins.queriesdirectory.Query.QueryProfile; 042import org.ametys.plugins.queriesdirectory.QueryDAO; 043import org.ametys.plugins.queriesdirectory.QueryHelper; 044import org.ametys.plugins.repository.AmetysObjectIterable; 045import org.ametys.plugins.repository.AmetysObjectResolver; 046 047/** 048 * SAX queries 049 * 050 */ 051public class GetQueriesAction extends ServiceableAction 052{ 053 private CurrentUserProvider _userProvider; 054 private AmetysObjectResolver _resolver; 055 private GroupManager _groupManager; 056 private QueryDAO _queryDAO; 057 058 059 @Override 060 public void service(ServiceManager serviceManager) throws ServiceException 061 { 062 super.service(serviceManager); 063 _userProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE); 064 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 065 _groupManager = (GroupManager) serviceManager.lookup(GroupManager.ROLE); 066 _queryDAO = (QueryDAO) serviceManager.lookup(QueryDAO.ROLE); 067 } 068 069 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 070 { 071 @SuppressWarnings("unchecked") 072 Map jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 073 074 String profileId = StringUtils.defaultIfEmpty((String) jsParameters.get("profile"), "read_access"); 075 Optional<String> queryType = Optional.of("queryType") 076 .map(jsParameters::get) 077 .filter(String.class::isInstance) 078 .map(String.class::cast); 079 080 List<Map<String, Object>> nodes = new ArrayList<>(); 081 082 if (!"read_access".equals(profileId) && !"write_access".equals(profileId)) 083 { 084 throw new IllegalArgumentException("Unexpected profile identifier : " + profileId); 085 } 086 087 QueryProfile profile = QueryProfile.valueOf(profileId.toUpperCase()); 088 089 UserIdentity user = _userProvider.getUser(); 090 Set<GroupIdentity> userGroups = _groupManager.getUserGroups(user); 091 092 String xPathQuery; 093 if (_queryDAO.isAdministrator()) 094 { 095 // Get all queries 096 xPathQuery = QueryHelper.getXPathQueryForAdministrator(queryType); 097 } 098 else 099 { 100 xPathQuery = QueryProfile.READ_ACCESS.equals(profile) ? QueryHelper.getXPathQueryForReadAccess(user, userGroups, queryType) : QueryHelper.getXPathQueryForWriteAccess(user, userGroups, queryType); 101 } 102 103 AmetysObjectIterable<Query> queries = _resolver.query(xPathQuery); 104 105 for (Query query : queries) 106 { 107 nodes.add(_queryDAO.getQueryProperties(query)); 108 } 109 110 Map<String, Object> result = new HashMap<>(); 111 result.put("queries", nodes); 112 113 Request request = ObjectModelHelper.getRequest(objectModel); 114 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 115 116 return EMPTY_MAP; 117 } 118 119}