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
44 public final class RightHome
45 {
46
47 private static final int CONSTANT_ERROR_ORDER = -2;
48 private static final int CONSTANT_STEP_ORDER = 1;
49 private static final int CONSTANT_FIRST_ID_ORDER = 1;
50
51
52 private static IRightDAO _dao = (IRightDAO) SpringContextService.getBean( "rightDAO" );
53
54
55
56
57 private RightHome( )
58 {
59 }
60
61
62
63
64
65
66
67 public static 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 public static Right update( Right right )
82 {
83 Right oldRight = findByPrimaryKey( right.getId( ) );
84
85 if ( oldRight == null )
86 {
87 return null;
88 }
89
90
91 if ( ( ( right.getFeatureGroup( ) != null ) &&
92 !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 if ( right.getOrder( ) != oldRight.getOrder( ) )
101 {
102 right.setOrder( changeRightOrder( oldRight, right.getOrder( ) ) );
103 }
104
105 _dao.store( right );
106
107 return right;
108 }
109
110
111
112
113
114
115 public static void remove( String strId )
116 {
117 Right oldRight = findByPrimaryKey( strId );
118
119 if ( oldRight != null )
120 {
121 deleteEntryFromList( oldRight.getFeatureGroup( ), oldRight.getOrder( ) );
122 }
123
124 _dao.delete( strId );
125 }
126
127
128
129
130
131
132
133
134
135
136 public static Right findByPrimaryKey( String strKey )
137 {
138 return _dao.load( strKey );
139 }
140
141
142
143
144
145
146 public static Collection<Right> getRightsList( )
147 {
148 return _dao.selectRightsList( );
149 }
150
151
152
153
154
155
156
157
158 public static Collection<Right> getRightsList( int nLevel )
159 {
160 return _dao.selectRightsList( nLevel );
161 }
162
163
164
165
166
167
168
169
170 public static Collection<Right> getRightsList( String strFeatureGroup )
171 {
172 return _dao.selectRightsList( strFeatureGroup );
173 }
174
175
176
177
178
179
180
181
182 public static int changeRightOrder( Right right, int nNewOrder )
183 {
184 if ( right == null )
185 {
186 return CONSTANT_ERROR_ORDER;
187 }
188
189 if ( nNewOrder < right.getOrder( ) )
190 {
191 for ( Right rightGroup : getRightsList( right.getFeatureGroup( ) ) )
192 {
193 int nRightToUpdateOrder = rightGroup.getOrder( );
194
195 if ( ( nRightToUpdateOrder >= nNewOrder ) && ( nRightToUpdateOrder < right.getOrder( ) ) )
196 {
197 rightGroup.setOrder( nRightToUpdateOrder + CONSTANT_STEP_ORDER );
198 _dao.store( rightGroup );
199 }
200 }
201 }
202 else if ( nNewOrder > right.getOrder( ) )
203 {
204 for ( Right rightGroup : getRightsList( right.getFeatureGroup( ) ) )
205 {
206 int nRightToUpdateOrder = rightGroup.getOrder( );
207
208 if ( ( nRightToUpdateOrder <= nNewOrder ) && ( nRightToUpdateOrder > right.getOrder( ) ) )
209 {
210 rightGroup.setOrder( nRightToUpdateOrder - CONSTANT_STEP_ORDER );
211 _dao.store( rightGroup );
212 }
213 }
214 }
215
216 return nNewOrder;
217 }
218
219
220
221
222
223
224 public static void deleteEntryFromList( String strFeatureGroup, int nOrderId )
225 {
226 for ( Right rightGroup : getRightsList( strFeatureGroup ) )
227 {
228 int nRightToUpdateOrder = rightGroup.getOrder( );
229
230 if ( ( nRightToUpdateOrder > nOrderId ) )
231 {
232 rightGroup.setOrder( nRightToUpdateOrder - CONSTANT_STEP_ORDER );
233 _dao.store( rightGroup );
234 }
235 }
236 }
237
238
239
240
241
242
243 public static void reinitFeatureOrders( String strFeatureGroup )
244 {
245 if ( ( strFeatureGroup == null ) || strFeatureGroup.equals( "" ) )
246 {
247 return;
248 }
249
250 int nOrder = CONSTANT_FIRST_ID_ORDER;
251
252 for ( Right rightGroup : getRightsList( strFeatureGroup ) )
253 {
254 rightGroup.setOrder( nOrder++ );
255 _dao.store( rightGroup );
256 }
257 }
258
259
260
261
262
263
264
265 public static boolean checkFeatureOrders( String strFeatureGroup )
266 {
267 if ( ( strFeatureGroup == null ) || strFeatureGroup.equals( "" ) )
268 {
269 return false;
270 }
271
272 int nOrder = CONSTANT_FIRST_ID_ORDER;
273
274 for ( Right rightGroup : getRightsList( strFeatureGroup ) )
275 {
276 if ( nOrder != rightGroup.getOrder( ) )
277 {
278 return false;
279 }
280
281 nOrder++;
282 }
283
284 return true;
285 }
286 }