This project has retired. For details please refer to its Attic page.
RoleModelUtils xref
View Javadoc

1   package org.apache.archiva.redback.role.util;
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.role.model.ModelApplication;
23  import org.apache.archiva.redback.role.model.ModelOperation;
24  import org.apache.archiva.redback.role.model.ModelResource;
25  import org.apache.archiva.redback.role.model.ModelRole;
26  import org.apache.archiva.redback.role.model.ModelTemplate;
27  import org.apache.archiva.redback.role.model.RedbackRoleModel;
28  import org.codehaus.plexus.util.dag.CycleDetectedException;
29  import org.codehaus.plexus.util.dag.DAG;
30  import org.codehaus.plexus.util.dag.TopologicalSorter;
31  
32  import java.util.ArrayList;
33  import java.util.LinkedList;
34  import java.util.List;
35  
36  /**
37   * RoleModelUtils:
38   *
39   * @author: Jesse McConnell <jesse@codehaus.org>
40   *
41   */
42  public class RoleModelUtils
43  {
44  
45  
46      public static List<ModelRole> getRoles( RedbackRoleModel model )
47      {
48          List<ModelRole> roleList = new ArrayList<ModelRole>( );
49  
50          for ( ModelApplication application : model.getApplications() )
51          {
52              roleList.addAll( application.getRoles() );
53          }
54  
55          return roleList;
56      }
57  
58      public static List<ModelTemplate> getTemplates( RedbackRoleModel model )
59      {
60          List<ModelTemplate> templateList = new ArrayList<ModelTemplate>();
61  
62          for ( ModelApplication application : model.getApplications() )
63          {
64              templateList.addAll( application.getTemplates() );
65          }
66  
67          return templateList;
68      }
69  
70      @SuppressWarnings( "unchecked" )
71      public static List<String> getOperationIdList( RedbackRoleModel model )
72      {
73          List<String> operationsIdList = new ArrayList<String>();
74  
75          for ( ModelApplication application : model.getApplications() )
76          {
77              for ( ModelOperation operation : application.getOperations() )
78              {
79                  operationsIdList.add( operation.getId() );
80              }
81          }
82  
83          return operationsIdList;
84      }
85  
86      @SuppressWarnings( "unchecked" )
87      public static List<String> getResourceIdList( RedbackRoleModel model )
88      {
89          List<String> resourceIdList = new ArrayList<String>();
90  
91          for ( ModelApplication application : model.getApplications() )
92          {
93              for ( ModelResource resource : application.getResources() )
94              {
95                  resourceIdList.add( resource.getId() );
96              }
97          }
98  
99          return resourceIdList;
100     }
101 
102     public static List<String> getRoleIdList( RedbackRoleModel model )
103     {
104         List<String> roleIdList = new ArrayList<String>();
105 
106         for ( ModelApplication application : model.getApplications() )
107         {
108             for ( ModelRole role : application.getRoles() )
109             {
110                 roleIdList.add( role.getId() );
111             }
112         }
113 
114         return roleIdList;
115     }
116 
117 
118     public static List<String> getTemplateIdList( RedbackRoleModel model )
119     {
120         List<String> templateIdList = new ArrayList<String>();
121 
122         for ( ModelApplication application : model.getApplications() )
123         {
124             for ( ModelTemplate template : application.getTemplates() )
125             {
126                 templateIdList.add( template.getId() );
127             }
128         }
129 
130         return templateIdList;
131 
132     }
133 
134     /**
135      * WARNING: can return null
136      *
137      * @param model
138      * @param roleId
139      * @return
140      */
141     @SuppressWarnings( "unchecked" )
142     public static ModelRole getModelRole( RedbackRoleModel model, String roleId )
143     {
144         ModelRole mrole = null;
145 
146         for ( ModelApplication application : model.getApplications() )
147         {
148             for ( ModelRole role : application.getRoles() )
149             {
150                 if ( roleId.equals( role.getId() ) )
151                 {
152                     mrole = role;
153                 }
154             }
155         }
156 
157         return mrole;
158     }
159 
160     /**
161      * WARNING: can return null
162      *
163      * @param model
164      * @param templateId
165      * @return
166      */
167     @SuppressWarnings( "unchecked" )
168     public static ModelTemplate getModelTemplate( RedbackRoleModel model, String templateId )
169     {
170         ModelTemplate mtemplate = null;
171 
172         for ( ModelApplication application : model.getApplications() )
173         {
174             for ( ModelTemplate template : application.getTemplates() )
175             {
176                 if ( templateId.equals( template.getId() ) )
177                 {
178                     mtemplate = template;
179                 }
180             }
181         }
182 
183         return mtemplate;
184     }
185 
186     /**
187      * WARNING: can return null
188      *
189      * @param model
190      * @param operationId
191      * @return
192      */
193     @SuppressWarnings( "unchecked" )
194     public static ModelOperation getModelOperation( RedbackRoleModel model, String operationId )
195     {
196         ModelOperation moperation = null;
197 
198         for ( ModelApplication application : model.getApplications() )
199         {
200             for ( ModelOperation operation : application.getOperations() )
201             {
202                 if ( operationId.equals( operation.getId() ) )
203                 {
204                     moperation = operation;
205                 }
206             }
207         }
208 
209         return moperation;
210     }
211 
212     @SuppressWarnings( "unchecked" )
213     public static ModelResource getModelResource( RedbackRoleModel model, String resourceId )
214     {
215         ModelResource mresource = null;
216 
217         for ( ModelApplication application : model.getApplications() )
218         {
219             for ( ModelResource resource : application.getResources() )
220             {
221                 if ( resourceId.equals( resource.getId() ) )
222                 {
223                     mresource = resource;
224                 }
225             }
226         }
227 
228         return mresource;
229     }
230 
231     @SuppressWarnings( "unchecked" )
232     public static DAG generateRoleGraph( RedbackRoleModel model )
233         throws CycleDetectedException
234     {
235         DAG roleGraph = new DAG();
236 
237         for ( ModelApplication application : model.getApplications() )
238         {
239             for ( ModelRole role : application.getRoles() )
240             {
241                 roleGraph.addVertex( role.getId() );
242 
243                 if ( role.getChildRoles() != null )
244                 {
245                     for ( String childRole : role.getChildRoles() )
246                     {
247                         roleGraph.addVertex( childRole );
248 
249                         roleGraph.addEdge( role.getId(), childRole );
250                     }
251                 }
252 
253                 if ( role.getParentRoles() != null )
254                 {
255                     for ( String parentRole : role.getParentRoles() )
256                     {
257                         roleGraph.addVertex( parentRole );
258 
259                         roleGraph.addEdge( parentRole, role.getId() );
260                     }
261                 }
262             }
263         }
264 
265         return roleGraph;
266     }
267 
268     @SuppressWarnings( "unchecked" )
269     public static DAG generateTemplateGraph( RedbackRoleModel model )
270         throws CycleDetectedException
271     {
272         DAG templateGraph = generateRoleGraph( model );
273 
274         for ( ModelApplication application : model.getApplications() )
275         {
276             for ( ModelTemplate template : application.getTemplates() )
277             {
278                 templateGraph.addVertex( template.getId() );
279 
280                 if ( template.getChildRoles() != null )
281                 {
282                     for ( String childRole : template.getChildRoles() )
283                     {
284                         templateGraph.addVertex( childRole );
285 
286                         templateGraph.addEdge( template.getId(), childRole );
287                     }
288                 }
289 
290                 if ( template.getParentRoles() != null )
291                 {
292                     for ( String parentRole : template.getParentRoles() )
293                     {
294                         templateGraph.addVertex( parentRole );
295 
296                         templateGraph.addEdge( parentRole, template.getId() );
297                     }
298                 }
299 
300                 if ( template.getChildTemplates() != null )
301                 {
302                     for ( String childTemplate : template.getChildTemplates() )
303                     {
304                         templateGraph.addVertex( childTemplate );
305 
306                         templateGraph.addEdge( template.getId(), childTemplate );
307                     }
308                 }
309 
310                 if ( template.getParentTemplates() != null )
311                 {
312                     for ( String parentTemplate : template.getParentTemplates() )
313                     {
314                         templateGraph.addVertex( parentTemplate );
315 
316                         templateGraph.addEdge( parentTemplate, template.getId() );
317                     }
318                 }
319             }
320         }
321 
322         return templateGraph;
323     }
324 
325     @SuppressWarnings( "unchecked" )
326     public static List<String> reverseTopologicalSortedRoleList( RedbackRoleModel model )
327         throws CycleDetectedException
328     {
329         LinkedList<String> sortedGraph =
330             (LinkedList<String>) TopologicalSorter.sort( RoleModelUtils.generateRoleGraph( model ) );
331         List<String> resortedGraph = new LinkedList<String>();
332 
333         while ( !sortedGraph.isEmpty() )
334         {
335             resortedGraph.add( sortedGraph.removeLast() );
336         }
337 
338         return resortedGraph;
339     }
340 
341 }