1 /**
2 * This file is part of the equanda project.
3 *
4 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7 *
8 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
9 * ANY KIND, either express or implied. See the License for the specific language governing rights and
10 * limitations under the License.
11 *
12 * Alternatively, the contents of this file may be used under the terms of
13 * either the GNU General Public License Version 2 or later (the "GPL"), or
14 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
15 * in which case the provisions of the GPL or the LGPL are applicable instead
16 * of those above. If you wish to allow use of your version of this file only
17 * under the terms of either the GPL or the LGPL, and not to allow others to
18 * use your version of this file under the terms of the MPL, indicate your
19 * decision by deleting the provisions above and replace them with the notice
20 * and other provisions required by the GPL or the LGPL. If you do not delete
21 * the provisions above, a recipient may use your version of this file under
22 * the terms of any one of the MPL, the GPL or the LGPL.
23 */
24
25 package org.equanda.test.dm.server;
26
27 import org.equanda.persistence.EquandaPersistenceException;
28 import org.equanda.test.dm.client.*;
29 import org.equanda.test.TestExceptionCodes;
30 import org.equanda.validation.EditableRights;
31 import org.equanda.validation.RunnableRights;
32
33 import java.util.Collection;
34 import java.util.ArrayList;
35
36
37 /**
38 * Mediator for EquandaRole. Implementation for functions mergeRights & mergeRestrictions
39 *
40 * @author NetRom team
41 */
42 public class EquandaRoleMediator
43 extends EquandaRoleMediatorBase
44 implements TestExceptionCodes
45 {
46 private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger( getClass() );
47
48 public void mergeRestrictions( String mergeName, String resultName )
49 throws EquandaPersistenceException
50 {
51 //mergeRoles( mergeName, resultName, true );
52 }
53
54 public void mergeRights( String mergeName, String resultName )
55 throws EquandaPersistenceException
56 {
57 //mergeRoles( mergeName, resultName, false );
58 }
59
60 /*
61 private void mixRights( Collection<EquandaRight> col1, Collection<EquandaRight> col2, Collection<EquandaRight> result, boolean restrict )
62 throws EquandaPersistenceException
63 {
64 for ( EquandaRight right1 : col1 )
65 {
66 boolean found = false;
67 for ( EquandaRight right2 : col2 )
68 {
69 // for rights existing in both roles, mix them, depending on the value of restrict
70
71 if ( right1.getIdentifier().equals( right2.getIdentifier() ) )
72 {
73 if ( !right1.getEquandaType().equals( right2.getEquandaType() ) )
74 {
75 throw new EquandaPersistenceException( EPE_SAME_IDENTIFIER_USED_FOR_EDIT_AND_RUN_ACCESS );
76 }
77 if ( right1.getEquandaType().equals( "EDIT" ) )
78 {
79 if ( right1.getEditAccess().equals( right2.getEditAccess() ) )
80 {
81 result.add( copy( right1 ) );
82 break;
83 }
84 }
85 if ( right1.getEquandaType().equals( "RUNN" ) )
86 {
87 if ( right1.getRunAccess().equals( right2.getRunAccess() ) )
88 {
89 result.add( copy( right1 ) );
90 break;
91 }
92 }
93 if ( restrict )
94 {
95 if ( right1.getEquandaType().equals( "EDIT" ) )
96 {
97 if ( right1.getEditAccess().equals( EditableRights.NONE ) ||
98 right2.getEditAccess().equals( EditableRights.NONE ) )
99 {
100 result.add(
101 right1.getEditAccess().equals( EditableRights.NONE )
102 ? copy( right1 )
103 : copy( right2 ) );
104 found = true;
105 break;
106 }
107 if ( right1.getEditAccess().equals( EditableRights.VIEW ) ||
108 right2.getEditAccess().equals( EditableRights.VIEW ) )
109 {
110 result.add(
111 right1.getEditAccess().equals( EditableRights.VIEW )
112 ? copy( right1 )
113 : copy( right2 ) );
114 found = true;
115 break;
116 }
117 // EDITABLE_ACCESS_EDIT is not saved
118 found = true;
119 break;
120 }
121 if ( right1.getEquandaType().equals( "RUNN" ) )
122 {
123 if ( right1.getRunAccess().equals( RunnableRights.NONE ) ||
124 right2.getRunAccess().equals( RunnableRights.NONE ) )
125 {
126 result.add(
127 right1.getRunAccess().equals( RunnableRights.NONE ) ? copy( right1 ) : copy(
128 right2 ) );
129 found = true;
130 break;
131 }
132
133 // RunnableRights.RUN is not saved
134 found = true;
135 break;
136 }
137 }
138 else
139 {
140 if ( right1.getEquandaType().equals( "EDIT" ) )
141 {
142 if ( right1.getEditAccess().equals( EditableRights.EDIT ) ||
143 right2.getEditAccess().equals( EditableRights.EDIT ) )
144 {
145 found = true;
146 break;
147 }
148 if ( right1.getEditAccess().equals( EditableRights.VIEW ) ||
149 right1.getEditAccess().equals( EditableRights.VIEW ) )
150 {
151 result.add(
152 right1.getEditAccess().equals( EditableRights.VIEW )
153 ? copy( right1 )
154 : copy( right2 ) );
155 found = true;
156 break;
157 }
158 result.add( copy( right1 ) );
159 found = true;
160 break;
161 }
162 if ( right1.getEquandaType().equals( "RUNN" ) )
163 {
164 if ( right1.getRunAccess().equals( RunnableRights.RUN ) ||
165 right2.getRunAccess().equals( RunnableRights.RUN ) )
166 {
167 found = true;
168 break;
169 }
170
171 result.add( copy( right1 ) );
172 found = true;
173 break;
174 }
175 }
176 }
177 }
178 // if restrict, copy all restrictions which are in col1 and not in col2
179 if ( restrict )
180 {
181 if ( !found )
182 {
183 result.add( copy( right1 ) );
184 }
185 }
186 }
187
188 // if restrict, copy all restrictions which are in col2 and not in col1
189
190 if ( restrict )
191 {
192 for ( EquandaRight right1 : col2 )
193 {
194 boolean found = false;
195 for ( EquandaRight right2 : col1 )
196 {
197 if ( right1.getIdentifier().equals( right2.getIdentifier() ) )
198 {
199 if ( !right1.getEquandaType().equals( right2.getEquandaType() ) )
200 {
201 throw new EquandaPersistenceException( EPE_SAME_IDENTIFIER_USED_FOR_EDIT_AND_RUN_ACCESS );
202 }
203 found = true;
204 break;
205 }
206 }
207 if ( !found )
208 {
209 result.add( copy( right1 ) );
210 }
211 }
212 } // if restrict
213 }
214
215 private EquandaRight copy( EquandaRight src ) throws EquandaPersistenceException
216 {
217 EquandaRight ret = new EquandaRight();
218 ret.setEquandaType( src.getEquandaType() );
219 ret.setIdentifier( src.getIdentifier() );
220 ret.setRunAccess( src.getRunAccess() );
221 ret.setEditAccess( src.getEditAccess() );
222 return ret;
223 }
224
225 private void mergeRoles( String mergeName, String resultName, boolean restrict )
226 throws EquandaPersistenceException
227 {
228 if ( resultName == null )
229 {
230 log.error( "Cannot merge roles, resultName must be a name for a new role, not null" );
231 throw new EquandaPersistenceException( EPE_RESULTNAME_CANNOT_BE_NULL );
232 }
233 if ( mergeName == null )
234 {
235 log.error( "mergeName must be the name of an existing role, not null" );
236 throw new EquandaPersistenceException( EPE_MERGENAME_MUST_EXIST_CANNOT_BE_NULL );
237 }
238 try
239 {
240 EquandaRoleSelectorEJB sel = EquandaGlobal.getEquandaRoleSelector();
241 EquandaRole mergeVal = sel.selectRole( mergeName );
242 sel.remove();
243 if ( mergeVal == null )
244 {
245 log.error( "role " + mergeName + " not found" );
246 throw new EquandaPersistenceException( EPE_ROLE_NOT_FOUND, mergeName );
247 }
248 Collection<EquandaRight> mergeRights = mergeVal.getRights();
249 Collection<EquandaRight> ourRights = object.getEquanda().getRights();
250
251 ArrayList<EquandaRight> result = new ArrayList<EquandaRight>();
252 mixRights( ourRights, mergeRights, result, restrict );
253 EquandaRole resultProxy = new EquandaRole();
254 resultProxy.setRoleName( resultName );
255 resultProxy.setRights( result );
256 resultProxy.equandaUpdate();
257 }
258 catch ( Exception e )
259 {
260 log.error( "error merging roles", e );
261 throw new EquandaPersistenceException( e );
262 }
263 }
264 */
265 }