]> source.dussan.org Git - archiva.git/blob
d450c7200f445c2b6260813d5acf0b88a6629b86
[archiva.git] /
1 package org.apache.archiva.redback.struts2.model;
2
3 /*
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import org.apache.archiva.redback.rbac.Role;
23 import org.codehaus.plexus.redback.role.model.ModelApplication;
24 import org.codehaus.plexus.redback.role.model.ModelRole;
25 import org.codehaus.plexus.redback.role.model.ModelTemplate;
26
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;
34 import java.util.Set;
35
36 /**
37  * @todo incredibly ugly population of the table, needs to be more concise
38  */
39 public class ApplicationRoleDetails
40 {
41     private String name;
42
43     private String description;
44
45     private List<String> assignedRoles;
46
47     private List<String> availableRoles;
48
49     private List<ModelTemplate> tableHeader;
50
51     private List<List<RoleTableCell>> table;
52
53     @SuppressWarnings("unchecked")
54     public ApplicationRoleDetails( ModelApplication application, Collection<Role> effectivelyAssignedRoles,
55                                    Collection<Role> allAssignedRoles, List<Role> assignableRoles )
56     {
57         name = application.getId();
58         description = application.getDescription();
59
60         List<ModelTemplate> templates = application.getTemplates();
61         List<ModelRole> roles = application.getRoles();
62
63         tableHeader = new LinkedList<ModelTemplate>( templates );
64
65         computeRoles( roles, assignableRoles, effectivelyAssignedRoles, allAssignedRoles );
66
67         computeTable( gatherResources( templates, assignableRoles ), effectivelyAssignedRoles, allAssignedRoles );
68     }
69
70     public String getName()
71     {
72         return name;
73     }
74
75     public String getDescription()
76     {
77         return description;
78     }
79
80     public List<String> getAssignedRoles()
81     {
82         return assignedRoles;
83     }
84
85     public List<String> getAvailableRoles()
86     {
87         return availableRoles;
88     }
89
90     public List<ModelTemplate> getTableHeader()
91     {
92         return tableHeader;
93     }
94
95     public List<List<RoleTableCell>> getTable()
96     {
97         return table;
98     }
99
100     private void computeRoles( Collection<ModelRole> applicationRoles, Collection<Role> assignableRoles,
101                                Collection<Role> effectivelyAssignedRoles, Collection<Role> allAssignedRoles )
102     {
103         assignedRoles = new ArrayList<String>();
104         availableRoles = new ArrayList<String>();
105         for ( Iterator<ModelRole> i = applicationRoles.iterator(); i.hasNext(); )
106         {
107             ModelRole role =  i.next();
108
109             if ( isInList( role.getName(), allAssignedRoles ) )
110             {
111                 if ( role.isAssignable() )
112                 {
113                     assignedRoles.add( role.getName() );
114                 }
115             }
116             else if ( isInList( role.getName(), effectivelyAssignedRoles ) )
117             {
118                 // nothing
119             }
120             else if ( isInList( role.getName(), assignableRoles ) )
121             {
122                 if ( role.isAssignable() )
123                 {
124                     availableRoles.add( role.getName() );
125                 }
126             }
127         }
128
129         Collections.sort( assignedRoles, String.CASE_INSENSITIVE_ORDER );
130         Collections.sort( availableRoles, String.CASE_INSENSITIVE_ORDER );
131     }
132
133     private Set<String> gatherResources( List<ModelTemplate> applicationTemplates, List<Role> roles )
134     {
135         Set<String> resources = new HashSet<String>();
136         for ( ModelTemplate modelTemplate : applicationTemplates )
137         {
138             for ( Role role : roles )
139             {
140                 String roleName = role.getName();
141                 if ( roleName.startsWith( modelTemplate.getNamePrefix() ) )
142                 {
143                     String delimiter = modelTemplate.getDelimiter();
144                     resources.add( roleName.substring( roleName.indexOf( delimiter ) + delimiter.length() ) );
145                 }
146             }
147         }
148         return resources;
149     }
150
151     private void computeTable( Collection<String> resources, Collection<Role> effectivelyAssignedRoles,
152                                Collection<Role> allAssignedRoles )
153     {
154         table = new LinkedList<List<RoleTableCell>>();
155
156         List<String> resourcesList = new ArrayList<String>( resources );
157         Collections.sort( resourcesList, String.CASE_INSENSITIVE_ORDER );
158
159         for ( String resource : resourcesList )
160         {
161             LinkedList<RoleTableCell> tableRow = new LinkedList<RoleTableCell>();
162
163             RoleTableCell resourceCell = new RoleTableCell();
164             resourceCell.setName( resource );
165             resourceCell.setLabel( true );
166             tableRow.add( resourceCell );
167
168             for ( ModelTemplate modelTemplate : tableHeader )
169             {
170                 RoleTableCell cell = new RoleTableCell();
171
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 );
176
177                 tableRow.add( cell );
178             }
179
180             table.add( tableRow );
181         }
182     }
183
184     private boolean isInList( String roleName, Collection<Role> effectivelyAssignedRoles )
185     {
186         for ( Role role : effectivelyAssignedRoles )
187         {
188             if ( roleName.equals( role.getName() ) )
189             {
190                 return true;
191             }
192         }
193         return false;
194     }
195
196     public class RoleTableCell
197     {
198         private String name;
199
200         private boolean effectivelyAssigned;
201
202         private boolean assigned;
203
204         private boolean label;
205
206         public String getName()
207         {
208             return name;
209         }
210
211         public void setName( String name )
212         {
213             this.name = name;
214         }
215
216         public boolean isEffectivelyAssigned()
217         {
218             return effectivelyAssigned;
219         }
220
221         public void setEffectivelyAssigned( boolean effectivelyAssigned )
222         {
223             this.effectivelyAssigned = effectivelyAssigned;
224         }
225
226         public boolean isAssigned()
227         {
228             return assigned;
229         }
230
231         public void setAssigned( boolean assigned )
232         {
233             this.assigned = assigned;
234         }
235
236         public boolean isLabel()
237         {
238             return label;
239         }
240
241         public void setLabel( boolean label )
242         {
243             this.label = label;
244         }
245     }
246 }