1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.business.right;
35
36 import fr.paris.lutece.portal.service.spring.SpringContextService;
37
38 import java.util.Collection;
39
40
41
42
43 public final class RightHome
44 {
45
46 private static final int CONSTANT_ERROR_ORDER = -2;
47 private static final int CONSTANT_STEP_ORDER = 1;
48 private static final int CONSTANT_FIRST_ID_ORDER = 1;
49
50
51 private static IRightDAO _dao = SpringContextService.getBean( "rightDAO" );
52
53
54
55
56 private RightHome( )
57 {
58 }
59
60
61
62
63
64
65
66
67 public static Rightf="../../../../../../fr/paris/lutece/portal/business/right/Right.html#Right">Right create( Right right )
68 {
69 right.setOrder( getRightsList( right.getFeatureGroup( ) ).size( ) + CONSTANT_STEP_ORDER );
70 _dao.insert( right );
71
72 return right;
73 }
74
75
76
77
78
79
80
81
82 public static Rightf="../../../../../../fr/paris/lutece/portal/business/right/Right.html#Right">Right update( Right right )
83 {
84 Right oldRight = findByPrimaryKey( right.getId( ) );
85
86 if ( oldRight == null )
87 {
88 return null;
89 }
90
91
92 if ( ( ( right.getFeatureGroup( ) != null ) && !right.getFeatureGroup( ).equals( oldRight.getFeatureGroup( ) ) )
93 || ( ( right.getFeatureGroup( ) == null ) && ( oldRight.getFeatureGroup( ) != null ) ) )
94 {
95 deleteEntryFromList( oldRight.getFeatureGroup( ), oldRight.getOrder( ) );
96 right.setOrder( getRightsList( right.getFeatureGroup( ) ).size( ) + CONSTANT_STEP_ORDER );
97 }
98
99
100 else
101 if ( right.getOrder( ) != oldRight.getOrder( ) )
102 {
103 right.setOrder( changeRightOrder( oldRight, right.getOrder( ) ) );
104 }
105
106 _dao.store( right );
107
108 return right;
109 }
110
111
112
113
114
115
116
117 public static void remove( String strId )
118 {
119 Right oldRight = findByPrimaryKey( strId );
120
121 if ( oldRight != null )
122 {
123 deleteEntryFromList( oldRight.getFeatureGroup( ), oldRight.getOrder( ) );
124 }
125
126 _dao.delete( strId );
127 }
128
129
130
131
132
133
134
135
136
137
138
139 public static Right findByPrimaryKey( String strKey )
140 {
141 return _dao.load( strKey );
142 }
143
144
145
146
147
148
149 public static Collection<Right> getRightsList( )
150 {
151 return _dao.selectRightsList( );
152 }
153
154
155
156
157
158
159
160
161
162 public static Collection<Right> getRightsList( int nLevel )
163 {
164 return _dao.selectRightsList( nLevel );
165 }
166
167
168
169
170
171
172
173
174 public static Collection<Right> getRightsList( String strFeatureGroup )
175 {
176 return _dao.selectRightsList( strFeatureGroup );
177 }
178
179
180
181
182
183
184
185
186
187
188 public static int changeRightOrder( Right right, int nNewOrder )
189 {
190 if ( right == null )
191 {
192 return CONSTANT_ERROR_ORDER;
193 }
194
195 if ( nNewOrder < right.getOrder( ) )
196 {
197 for ( Right rightGroup : getRightsList( right.getFeatureGroup( ) ) )
198 {
199 int nRightToUpdateOrder = rightGroup.getOrder( );
200
201 if ( ( nRightToUpdateOrder >= nNewOrder ) && ( nRightToUpdateOrder < right.getOrder( ) ) )
202 {
203 rightGroup.setOrder( nRightToUpdateOrder + CONSTANT_STEP_ORDER );
204 _dao.store( rightGroup );
205 }
206 }
207 }
208 else
209 if ( nNewOrder > right.getOrder( ) )
210 {
211 for ( Right rightGroup : getRightsList( right.getFeatureGroup( ) ) )
212 {
213 int nRightToUpdateOrder = rightGroup.getOrder( );
214
215 if ( ( nRightToUpdateOrder <= nNewOrder ) && ( nRightToUpdateOrder > right.getOrder( ) ) )
216 {
217 rightGroup.setOrder( nRightToUpdateOrder - CONSTANT_STEP_ORDER );
218 _dao.store( rightGroup );
219 }
220 }
221 }
222
223 return nNewOrder;
224 }
225
226
227
228
229
230
231
232
233
234 public static void deleteEntryFromList( String strFeatureGroup, int nOrderId )
235 {
236 for ( Right rightGroup : getRightsList( strFeatureGroup ) )
237 {
238 int nRightToUpdateOrder = rightGroup.getOrder( );
239
240 if ( ( nRightToUpdateOrder > nOrderId ) )
241 {
242 rightGroup.setOrder( nRightToUpdateOrder - CONSTANT_STEP_ORDER );
243 _dao.store( rightGroup );
244 }
245 }
246 }
247
248
249
250
251
252
253
254 public static void reinitFeatureOrders( String strFeatureGroup )
255 {
256 if ( ( strFeatureGroup == null ) || strFeatureGroup.equals( "" ) )
257 {
258 return;
259 }
260
261 int nOrder = CONSTANT_FIRST_ID_ORDER;
262
263 for ( Right rightGroup : getRightsList( strFeatureGroup ) )
264 {
265 rightGroup.setOrder( nOrder++ );
266 _dao.store( rightGroup );
267 }
268 }
269
270
271
272
273
274
275
276
277 public static boolean checkFeatureOrders( String strFeatureGroup )
278 {
279 if ( ( strFeatureGroup == null ) || strFeatureGroup.equals( "" ) )
280 {
281 return false;
282 }
283
284 int nOrder = CONSTANT_FIRST_ID_ORDER;
285
286 for ( Right rightGroup : getRightsList( strFeatureGroup ) )
287 {
288 if ( nOrder != rightGroup.getOrder( ) )
289 {
290 return false;
291 }
292
293 nOrder++;
294 }
295
296 return true;
297 }
298
299 public static Collection<Right> getExternalRightList( )
300 {
301 return _dao.selectExternalRightsList( 0 );
302 }
303 }