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.cart.actions.visibility;
017
018import java.util.HashMap;
019import java.util.LinkedList;
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.plugins.cart.Cart;
035import org.ametys.plugins.cart.Cart.CartProfile;
036import org.ametys.plugins.cart.Cart.Visibility;
037import org.ametys.plugins.repository.AmetysObjectResolver;
038import org.ametys.core.cocoon.JSonReader;
039import org.ametys.core.group.Group;
040import org.ametys.core.group.GroupIdentity;
041import org.ametys.core.group.GroupManager;
042import org.ametys.core.user.CurrentUserProvider;
043import org.ametys.core.user.User;
044import org.ametys.core.user.UserIdentity;
045import org.ametys.core.user.UserManager;
046import org.ametys.core.user.population.UserPopulationDAO;
047
048/**
049 * Action populating a map containing information about the visibility of a {@link Cart}
050 */
051public class GetCartProfileTreeAction extends ServiceableAction
052{
053    /** Ametys resolver */
054    protected AmetysObjectResolver _resolver;
055    /** Group manager */
056    protected GroupManager _groupManager;
057    /** User manager */
058    protected UserManager _userManager;
059    /** The current user provider */
060    protected CurrentUserProvider _userProvider;
061    /** The user population DAO */
062    protected UserPopulationDAO _userPopulationDAO;
063    
064    @Override
065    public void service(ServiceManager smanager) throws ServiceException
066    {
067        super.service(smanager);
068        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
069        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
070        _groupManager = (GroupManager) smanager.lookup(GroupManager.ROLE);
071        _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
072        _userPopulationDAO = (UserPopulationDAO) smanager.lookup(UserPopulationDAO.ROLE);
073    }
074    
075    @Override
076    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
077    {
078        Request request = ObjectModelHelper.getRequest(objectModel);
079        
080        String cartId = request.getParameter("cartId");
081        Cart cart = null;
082        
083        if (StringUtils.isNotEmpty(cartId))
084        {
085            cart = _resolver.resolveById(cartId);
086        }
087        
088        Map<String, Object> results = new HashMap<>();
089        
090        Map<String, Object> info = new HashMap<>();
091        results.put("info", _getCartInfo(cart));
092        
093        if (cart != null)
094        {
095            info.put("cartLabel", cart.getTitle());
096        }
097        
098        List<Object> profiles = new LinkedList<>();
099        results.put("children", profiles);
100        
101        // Read only profile
102        profiles.add(_getProfileInfo(cart, CartProfile.READ_ACCESS));
103        
104        // Read write profile
105        profiles.add(_getProfileInfo(cart, CartProfile.WRITE_ACCESS));
106        
107        request.setAttribute(JSonReader.OBJECT_TO_READ, results);
108        
109        return EMPTY_MAP;
110    }
111
112    private Map<String, Object> _getCartInfo(Cart cart)
113    {
114        Map<String, Object> info = new HashMap<>();
115        
116        if (cart != null)
117        {
118            info.put("cartLabel", cart.getTitle());
119            info.put("cartVisibility", cart.getVisibility().toString());
120            info.put("cartAuthor", cart.getAuthor());
121        }
122        
123        return info;
124    }
125    
126    private Map<String, Object> _getProfileInfo(Cart cart, CartProfile profile)
127    {
128        Map<String, Object> profileMap = new HashMap<>();
129        
130        profileMap.put("type", "profile");
131        profileMap.put("id", profile.toString());
132        profileMap.put("text", profile.getTitle());
133        profileMap.put("description", profile.getDescription());
134        
135        List<Map<String, Object>> entries = new LinkedList<>();
136         
137        if (cart != null)
138        {
139            // Check shared visibility + author is current user
140            boolean addInfo = Visibility.SHARED.equals(cart.getVisibility()) && cart.getAuthor().equals(_userProvider.getUser());
141            
142            if (addInfo)
143            {
144                Set<UserIdentity> users = cart.getGrantedUsers(profile);
145                for (UserIdentity user : users)
146                {
147                    _addUserInfo(entries, user);
148                }
149                
150                Set<GroupIdentity> groups = cart.getGrantedGroups(profile);
151                for (GroupIdentity group : groups)
152                {
153                    _addGroupInfo(entries, group);
154                }
155            }
156        }
157        
158        if (entries.isEmpty())
159        {
160            profileMap.put("leaf", true);
161        }
162        else
163        {
164            profileMap.put("children", entries);
165        }
166        
167        return profileMap;
168    }
169    
170    private void _addUserInfo(List<Map<String, Object>> entries, UserIdentity userIdentity)
171    {
172        String populationId = userIdentity.getPopulationId();
173        User user = _userManager.getUser(populationId, userIdentity.getLogin());
174        
175        Map<String, Object> entry = new HashMap<>();
176        entry.put("type", "user");
177        entry.put("id", userIdentity.getLogin());
178        entry.put("login", userIdentity.getLogin());
179        entry.put("populationId", populationId);
180        entry.put("populationLabel", _userPopulationDAO.getUserPopulation(populationId).getLabel());
181        entry.put("leaf", true);
182        
183        if (user != null)
184        {
185            entry.put("name", user.getName());
186            entry.put("fullName", user.getFullName());
187        }
188        else
189        {
190            entry.put("not-found", true);
191        }
192        
193        entries.add(entry);
194    }
195
196    private void _addGroupInfo(List<Map<String, Object>> entries, GroupIdentity groupIdentity)
197    {
198        Group group = _groupManager.getGroup(groupIdentity.getDirectoryId(), groupIdentity.getId());
199        
200        Map<String, Object> entry = new HashMap<>();
201        entry.put("type", "group");
202        entry.put("id", GroupIdentity.groupIdentityToString(groupIdentity));
203        entry.put("groupId", groupIdentity.getId());
204        entry.put("groupDirectory", groupIdentity.getDirectoryId());
205        entry.put("leaf", true);
206        
207        if (group != null)
208        {
209            entry.put("label", group.getLabel());
210        }
211        else
212        {
213            entry.put("not-found", true);
214        }
215        
216        entries.add(entry);
217    }
218}