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.right;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032import org.apache.commons.lang3.StringUtils;
033
034import org.ametys.core.cocoon.JSonReader;
035import org.ametys.core.right.Right;
036import org.ametys.core.right.RightsExtensionPoint;
037import org.ametys.core.util.I18nUtils;
038
039/**
040 * Get rights
041 *
042 */
043public class GetRightsAction extends ServiceableAction
044{
045    private RightsExtensionPoint _rights;
046    private I18nUtils _i18nUtils;
047    
048    @Override
049    public void service(ServiceManager m) throws ServiceException
050    {
051        super.service(m);
052        _rights = (RightsExtensionPoint) m.lookup(RightsExtensionPoint.ROLE);
053        _i18nUtils = (I18nUtils) m.lookup(I18nUtils.ROLE);
054    }
055    
056    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
057    {
058        @SuppressWarnings("unchecked")
059        Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
060        
061        Map<String, Object> result = new HashMap<>();
062        
063        List<Map<String, Object>> rights = new ArrayList<>();
064        Set<String> rightIds = _rights.getExtensionsIds();
065        
066        String rightQuery = (String) jsParameters.get("query");
067        if (StringUtils.isEmpty(rightQuery))
068        {
069            // Return all rights
070            for (String rightId : rightIds)
071            {
072                rights.add(_rights.getExtension(rightId).toJSON());
073            }
074        }
075        else
076        {
077            // Only return the matching rights
078            rightQuery = StringUtils.stripAccents(rightQuery.toLowerCase());
079            for (String rightId : rightIds)
080            {
081                Right right = _rights.getExtension(rightId);
082                String translatedLabel = _i18nUtils.translate(right.getLabel());
083                String translatedDescription = _i18nUtils.translate(right.getDescription());
084                
085                String rightLabel = null;
086                if (translatedLabel != null)
087                {
088                    rightLabel = StringUtils.stripAccents(translatedLabel.toLowerCase());
089                }
090                String rightDescription = null;
091                if (translatedDescription != null)
092                {
093                    rightDescription = StringUtils.stripAccents(translatedDescription.toLowerCase());
094                }
095                
096                if (rightLabel != null && rightLabel.contains(rightQuery)
097                    || rightDescription != null && rightDescription.contains(rightQuery))
098                {
099                    rights.add(right.toJSON());
100                }
101            }
102        }
103        
104        result.put("rights", rights);
105        
106        Request request = ObjectModelHelper.getRequest(objectModel);
107        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
108
109        return EMPTY_MAP;
110    }
111}