1 package org.apache.archiva.redback.struts2.model;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.redback.rbac.Role;
23 import org.apache.archiva.redback.role.model.ModelApplication;
24 import org.apache.archiva.redback.role.model.ModelRole;
25 import org.apache.archiva.redback.role.model.ModelTemplate;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.LinkedList;
33 import java.util.List;
37 * @todo incredibly ugly population of the table, needs to be more concise
39 public class ApplicationRoleDetails
43 private String description;
45 private List<String> assignedRoles;
47 private List<String> availableRoles;
49 private List<ModelTemplate> tableHeader;
51 private List<List<RoleTableCell>> table;
53 @SuppressWarnings( "unchecked" )
54 public ApplicationRoleDetails( ModelApplication application, Collection<Role> effectivelyAssignedRoles,
55 Collection<Role> allAssignedRoles, List<Role> assignableRoles )
57 name = application.getId();
58 description = application.getDescription();
60 List<ModelTemplate> templates = application.getTemplates();
61 List<ModelRole> roles = application.getRoles();
63 tableHeader = new LinkedList<ModelTemplate>( templates );
65 computeRoles( roles, assignableRoles, effectivelyAssignedRoles, allAssignedRoles );
67 computeTable( gatherResources( templates, assignableRoles ), effectivelyAssignedRoles, allAssignedRoles );
70 public String getName()
75 public String getDescription()
80 public List<String> getAssignedRoles()
85 public List<String> getAvailableRoles()
87 return availableRoles;
90 public List<ModelTemplate> getTableHeader()
95 public List<List<RoleTableCell>> getTable()
100 private void computeRoles( Collection<ModelRole> applicationRoles, Collection<Role> assignableRoles,
101 Collection<Role> effectivelyAssignedRoles, Collection<Role> allAssignedRoles )
103 assignedRoles = new ArrayList<String>();
104 availableRoles = new ArrayList<String>();
105 for ( Iterator<ModelRole> i = applicationRoles.iterator(); i.hasNext(); )
107 ModelRole role = i.next();
109 if ( isInList( role.getName(), allAssignedRoles ) )
111 if ( role.isAssignable() )
113 assignedRoles.add( role.getName() );
116 else if ( isInList( role.getName(), effectivelyAssignedRoles ) )
120 else if ( isInList( role.getName(), assignableRoles ) )
122 if ( role.isAssignable() )
124 availableRoles.add( role.getName() );
129 Collections.sort( assignedRoles, String.CASE_INSENSITIVE_ORDER );
130 Collections.sort( availableRoles, String.CASE_INSENSITIVE_ORDER );
133 private Set<String> gatherResources( List<ModelTemplate> applicationTemplates, List<Role> roles )
135 Set<String> resources = new HashSet<String>();
136 for ( ModelTemplate modelTemplate : applicationTemplates )
138 for ( Role role : roles )
140 String roleName = role.getName();
141 if ( roleName.startsWith( modelTemplate.getNamePrefix() ) )
143 String delimiter = modelTemplate.getDelimiter();
144 resources.add( roleName.substring( roleName.indexOf( delimiter ) + delimiter.length() ) );
151 private void computeTable( Collection<String> resources, Collection<Role> effectivelyAssignedRoles,
152 Collection<Role> allAssignedRoles )
154 table = new LinkedList<List<RoleTableCell>>();
156 List<String> resourcesList = new ArrayList<String>( resources );
157 Collections.sort( resourcesList, String.CASE_INSENSITIVE_ORDER );
159 for ( String resource : resourcesList )
161 LinkedList<RoleTableCell> tableRow = new LinkedList<RoleTableCell>();
163 RoleTableCell resourceCell = new RoleTableCell();
164 resourceCell.setName( resource );
165 resourceCell.setLabel( true );
166 tableRow.add( resourceCell );
168 for ( ModelTemplate modelTemplate : tableHeader )
170 RoleTableCell cell = new RoleTableCell();
172 cell.setName( modelTemplate.getNamePrefix() + modelTemplate.getDelimiter() + resource );
173 cell.setEffectivelyAssigned( isInList( cell.getName(), effectivelyAssignedRoles ) );
174 cell.setAssigned( isInList( cell.getName(), allAssignedRoles ) );
175 cell.setLabel( false );
177 tableRow.add( cell );
180 table.add( tableRow );
184 private boolean isInList( String roleName, Collection<Role> effectivelyAssignedRoles )
186 for ( Role role : effectivelyAssignedRoles )
188 if ( roleName.equals( role.getName() ) )
196 public class RoleTableCell
200 private boolean effectivelyAssigned;
202 private boolean assigned;
204 private boolean label;
206 public String getName()
211 public void setName( String name )
216 public boolean isEffectivelyAssigned()
218 return effectivelyAssigned;
221 public void setEffectivelyAssigned( boolean effectivelyAssigned )
223 this.effectivelyAssigned = effectivelyAssigned;
226 public boolean isAssigned()
231 public void setAssigned( boolean assigned )
233 this.assigned = assigned;
236 public boolean isLabel()
241 public void setLabel( boolean label )