KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > model > ProtectedItemWrapper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.directory.ldapstudio.aciitemeditor.model;
21
22
23 import java.text.ParseException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.naming.NamingException JavaDoc;
32 import javax.naming.directory.Attribute JavaDoc;
33
34 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor;
35 import org.apache.directory.shared.ldap.aci.ACIItemParser;
36 import org.apache.directory.shared.ldap.aci.ItemFirstACIItem;
37 import org.apache.directory.shared.ldap.aci.ProtectedItem;
38 import org.eclipse.osgi.util.NLS;
39
40
41 /**
42  * The ProtectedItemWrapper is used as input for the table viewer.
43  * The protected item values are always stored as raw string value.
44  *
45  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
46  * @version $Rev$, $Date$
47  */

48 public class ProtectedItemWrapper
49 {
50     /** This map contains all possible protected item identifiers */
51     public static final Map JavaDoc<Class JavaDoc, String JavaDoc> classToIdentifierMap;
52     static
53     {
54         Map JavaDoc<Class JavaDoc, String JavaDoc> map = new HashMap JavaDoc<Class JavaDoc, String JavaDoc>();
55         map.put( ProtectedItem.Entry.class, "entry" ); //$NON-NLS-1$
56
map.put( ProtectedItem.AllUserAttributeTypes.class, "allUserAttributeTypes" ); //$NON-NLS-1$
57
map.put( ProtectedItem.AttributeType.class, "attributeType" ); //$NON-NLS-1$
58
map.put( ProtectedItem.AllAttributeValues.class, "allAttributeValues" ); //$NON-NLS-1$
59
map.put( ProtectedItem.AllUserAttributeTypesAndValues.class, "allUserAttributeTypesAndValues" ); //$NON-NLS-1$
60
map.put( ProtectedItem.AttributeValue.class, "attributeValue" ); //$NON-NLS-1$
61
map.put( ProtectedItem.SelfValue.class, "selfValue" ); //$NON-NLS-1$
62
map.put( ProtectedItem.RangeOfValues.class, "rangeOfValues" ); //$NON-NLS-1$
63
map.put( ProtectedItem.MaxValueCount.class, "maxValueCount" ); //$NON-NLS-1$
64
map.put( ProtectedItem.MaxImmSub.class, "maxImmSub" ); //$NON-NLS-1$
65
map.put( ProtectedItem.RestrictedBy.class, "restrictedBy" ); //$NON-NLS-1$
66
map.put( ProtectedItem.Classes.class, "classes" ); //$NON-NLS-1$
67
classToIdentifierMap = Collections.unmodifiableMap( map );
68     }
69
70     /** This map contains all protected item display values */
71     public static final Map JavaDoc<Class JavaDoc, String JavaDoc> classToDisplayMap;
72     static
73     {
74         Map JavaDoc<Class JavaDoc, String JavaDoc> map = new HashMap JavaDoc<Class JavaDoc, String JavaDoc>();
75         map.put( ProtectedItem.Entry.class, Messages.getString( "ProtectedItemWrapper.protectedItem.entry.label" ) ); //$NON-NLS-1$
76
map.put( ProtectedItem.AllUserAttributeTypes.class, Messages
77             .getString( "ProtectedItemWrapper.protectedItem.allUserAttributeTypes.label" ) ); //$NON-NLS-1$
78
map.put( ProtectedItem.AttributeType.class, Messages
79             .getString( "ProtectedItemWrapper.protectedItem.attributeType.label" ) ); //$NON-NLS-1$
80
map.put( ProtectedItem.AllAttributeValues.class, Messages
81             .getString( "ProtectedItemWrapper.protectedItem.allAttributeValues.label" ) ); //$NON-NLS-1$
82
map.put( ProtectedItem.AllUserAttributeTypesAndValues.class, Messages
83             .getString( "ProtectedItemWrapper.protectedItem.allUserAttributeTypesAndValues.label" ) ); //$NON-NLS-1$
84
map.put( ProtectedItem.AttributeValue.class, Messages
85             .getString( "ProtectedItemWrapper.protectedItem.attributeValue.label" ) ); //$NON-NLS-1$
86
map.put( ProtectedItem.SelfValue.class, Messages
87             .getString( "ProtectedItemWrapper.protectedItem.selfValue.label" ) ); //$NON-NLS-1$
88
map.put( ProtectedItem.RangeOfValues.class, Messages
89             .getString( "ProtectedItemWrapper.protectedItem.rangeOfValues.label" ) ); //$NON-NLS-1$
90
map.put( ProtectedItem.MaxValueCount.class, Messages
91             .getString( "ProtectedItemWrapper.protectedItem.maxValueCount.label" ) ); //$NON-NLS-1$
92
map.put( ProtectedItem.MaxImmSub.class, Messages
93             .getString( "ProtectedItemWrapper.protectedItem.maxImmSub.label" ) ); //$NON-NLS-1$
94
map.put( ProtectedItem.RestrictedBy.class, Messages
95             .getString( "ProtectedItemWrapper.protectedItem.restrictedBy.label" ) ); //$NON-NLS-1$
96
map.put( ProtectedItem.Classes.class, Messages.getString( "ProtectedItemWrapper.protectedItem.classes.label" ) ); //$NON-NLS-1$
97
classToDisplayMap = Collections.unmodifiableMap( map );
98     }
99
100     /** A dummy ACI to check syntax of the protectedItemValue */
101     private static final String JavaDoc DUMMY = "{ identificationTag \"id1\", precedence 1, authenticationLevel simple, " //$NON-NLS-1$
102
+ "itemOrUserFirst itemFirst: { protectedItems { #identifier# #values# }, " //$NON-NLS-1$
103
+ "itemPermissions { { userClasses { allUsers }, grantsAndDenials { grantRead } } } } }"; //$NON-NLS-1$
104

105     /** The class of the protected item, never null. */
106     private final Class JavaDoc clazz;
107
108     /** The protected item values, may be empty. */
109     private List JavaDoc<String JavaDoc> values;
110
111     /** The value prefix, prepended to the value. */
112     private String JavaDoc valuePrefix;
113
114     /** The value suffix, appended to the value. */
115     private String JavaDoc valueSuffix;
116
117     /** The value editor, null means no value. */
118     private AbstractDialogStringValueEditor valueEditor;
119
120     /** The multivalued. */
121     private boolean isMultivalued;
122
123
124     /**
125      * Creates a new instance of ProtectedItemWrapper.
126      *
127      * @param clazz the java class of the UserClass
128      * @param isMultivalued the is multivalued
129      * @param valuePrefix the identifier
130      * @param valueSuffix the dislpay name
131      * @param valueEditor the value editor
132      */

133     public ProtectedItemWrapper( Class JavaDoc clazz, boolean isMultivalued, String JavaDoc valuePrefix, String JavaDoc valueSuffix,
134         AbstractDialogStringValueEditor valueEditor )
135     {
136         this.clazz = clazz;
137         this.isMultivalued = isMultivalued;
138         this.valuePrefix = valuePrefix;
139         this.valueSuffix = valueSuffix;
140         this.valueEditor = valueEditor;
141
142         this.values = new ArrayList JavaDoc<String JavaDoc>();
143     }
144
145
146     /**
147      * Creates a new protected item object. Therefore it uses the
148      * dummy ACI, injects the protected item and its value, parses
149      * the ACI and extracts the protected item from the parsed bean.
150      *
151      * @return the parsed protected item
152      *
153      * @throws ParseException if parsing fails
154      */

155     public ProtectedItem getProtectedItem() throws ParseException JavaDoc
156     {
157         String JavaDoc flatValue = getFlatValue();
158         String JavaDoc spec = DUMMY;
159         spec = spec.replaceAll( "#identifier#", getIdentifier() ); //$NON-NLS-1$
160
spec = spec.replaceAll( "#values#", flatValue ); //$NON-NLS-1$
161
ACIItemParser parser = new ACIItemParser( null );
162         ItemFirstACIItem aci = null;
163         try
164         {
165             aci = ( ItemFirstACIItem ) parser.parse( spec );
166         }
167         catch ( ParseException JavaDoc e )
168         {
169
170             String JavaDoc msg = NLS
171                 .bind(
172                     Messages.getString( "ProtectedItemWrapper.error.message" ), new String JavaDoc[] { getIdentifier(), flatValue } ); //$NON-NLS-1$
173
throw new ParseException JavaDoc( msg, 0 );
174         }
175         ProtectedItem item = ( ProtectedItem ) aci.getProtectedItems().iterator().next();
176         return item;
177     }
178
179
180     /**
181      * Sets the protected item.
182      *
183      * @param item the protected item
184      */

185     public void setProtectedItem( ProtectedItem item )
186     {
187         assert item.getClass() == getClazz();
188
189         // first clear values
190
values.clear();
191
192         // switch on userClass type
193
// no value in ProtectedItem.Entry, ProtectedItem.AllUserAttributeTypes and ProtectedItem.AllUserAttributeTypesAndValues
194
if ( item.getClass() == ProtectedItem.AttributeType.class )
195         {
196             ProtectedItem.AttributeType at = ( ProtectedItem.AttributeType ) item;
197             for ( Iterator JavaDoc it = at.iterator(); it.hasNext(); )
198             {
199                 values.add( it.next().toString() );
200             }
201         }
202         else if ( item.getClass() == ProtectedItem.AllAttributeValues.class )
203         {
204             ProtectedItem.AllAttributeValues aav = ( ProtectedItem.AllAttributeValues ) item;
205             for ( Iterator JavaDoc it = aav.iterator(); it.hasNext(); )
206             {
207                 values.add( it.next().toString() );
208             }
209         }
210         else if ( item.getClass() == ProtectedItem.AttributeValue.class )
211         {
212             ProtectedItem.AttributeValue av = ( ProtectedItem.AttributeValue ) item;
213             for ( Iterator JavaDoc it = av.iterator(); it.hasNext(); )
214             {
215                 Attribute JavaDoc attribute = ( Attribute JavaDoc ) it.next();
216                 try
217                 {
218                     values.add( attribute.getID() + "=" + attribute.get() ); //$NON-NLS-1$
219
}
220                 catch ( NamingException JavaDoc e )
221                 {
222                 }
223             }
224         }
225         else if ( item.getClass() == ProtectedItem.SelfValue.class )
226         {
227             ProtectedItem.SelfValue sv = ( ProtectedItem.SelfValue ) item;
228             for ( Iterator JavaDoc it = sv.iterator(); it.hasNext(); )
229             {
230                 values.add( it.next().toString() );
231             }
232         }
233         else if ( item.getClass() == ProtectedItem.RangeOfValues.class )
234         {
235             ProtectedItem.RangeOfValues rov = ( ProtectedItem.RangeOfValues ) item;
236             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
237             rov.getFilter().printToBuffer( buffer );
238             values.add( buffer.toString() );
239         }
240         else if ( item.getClass() == ProtectedItem.MaxValueCount.class )
241         {
242             ProtectedItem.MaxValueCount mvc = ( ProtectedItem.MaxValueCount ) item;
243             for ( Iterator JavaDoc it = mvc.iterator(); it.hasNext(); )
244             {
245                 ProtectedItem.MaxValueCountItem mvci = ( ProtectedItem.MaxValueCountItem ) it.next();
246                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
247                 mvci.printToBuffer( buffer );
248                 values.add( buffer.toString() );
249             }
250         }
251         else if ( item.getClass() == ProtectedItem.MaxImmSub.class )
252         {
253             ProtectedItem.MaxImmSub mis = ( ProtectedItem.MaxImmSub ) item;
254             values.add( Integer.toString( mis.getValue() ) );
255         }
256         else if ( item.getClass() == ProtectedItem.RestrictedBy.class )
257         {
258             ProtectedItem.RestrictedBy rb = ( ProtectedItem.RestrictedBy ) item;
259             for ( Iterator JavaDoc it = rb.iterator(); it.hasNext(); )
260             {
261                 ProtectedItem.RestrictedByItem rbi = ( ProtectedItem.RestrictedByItem ) it.next();
262                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
263                 rbi.printToBuffer( buffer );
264                 values.add( buffer.toString() );
265             }
266         }
267         else if ( item.getClass() == ProtectedItem.Classes.class )
268         {
269             ProtectedItem.Classes classes = ( ProtectedItem.Classes ) item;
270             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
271             classes.getClasses().printRefinementToBuffer( buffer );
272             values.add( buffer.toString() );
273         }
274
275     }
276
277
278     /**
279      * Returns a user-friedly string, displayed in the table.
280      *
281      * @return the string
282      */

283     public String JavaDoc toString()
284     {
285         String JavaDoc flatValue = getFlatValue();
286         if ( flatValue.length() > 0 )
287         {
288             flatValue = flatValue.replace( '\r', ' ' );
289             flatValue = flatValue.replace( '\n', ' ' );
290             flatValue = ": " + flatValue; //$NON-NLS-1$
291
if ( flatValue.length() > 40 )
292             {
293                 String JavaDoc temp = flatValue;
294                 flatValue = temp.substring( 0, 20 );
295                 flatValue = flatValue + "..."; //$NON-NLS-1$
296
flatValue = flatValue + temp.substring( temp.length() - 20, temp.length() );
297             }
298         }
299
300         return getDisplayName() + " " + flatValue; //$NON-NLS-1$
301
}
302
303
304     /**
305      * Returns the flat value.
306      *
307      * @return the flat value
308      */

309     private String JavaDoc getFlatValue()
310     {
311         if ( valueEditor == null || values.isEmpty() )
312         {
313             return ""; //$NON-NLS-1$
314
}
315
316         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
317         if ( isMultivalued() )
318         {
319             sb.append( "{ " ); //$NON-NLS-1$
320
}
321         for ( Iterator JavaDoc<String JavaDoc> it = values.iterator(); it.hasNext(); )
322         {
323             sb.append( valuePrefix );
324             String JavaDoc value = it.next();
325             sb.append( value );
326             sb.append( valueSuffix );
327             if ( it.hasNext() )
328             {
329                 sb.append( ", " ); //$NON-NLS-1$
330
}
331         }
332         if ( isMultivalued() )
333         {
334             sb.append( " }" ); //$NON-NLS-1$
335
}
336         return sb.toString();
337     }
338
339
340     /**
341      * Returns the list of values, may be modified.
342      *
343      * @return the modifyable list of values.
344      */

345     public List JavaDoc<String JavaDoc> getValues()
346     {
347         return values;
348     }
349
350
351     /**
352      * Gets the display name.
353      *
354      * @return the display name
355      */

356     public String JavaDoc getDisplayName()
357     {
358         return classToDisplayMap.get( clazz );
359     }
360
361
362     /**
363      * Gets the identifier.
364      *
365      * @return the identifier
366      */

367     public String JavaDoc getIdentifier()
368     {
369         return classToIdentifierMap.get( clazz );
370     }
371
372
373     /**
374      * Returns the class of the user class.
375      *
376      * @return the class of the user class.
377      */

378     public Class JavaDoc getClazz()
379     {
380         return clazz;
381     }
382
383
384     /**
385      * Checks if is editable.
386      *
387      * @return true, if is editable
388      */

389     public boolean isEditable()
390     {
391         return valueEditor != null;
392     }
393
394
395     /**
396      * Gets the value editor.
397      *
398      * @return the value editor, may be null.
399      */

400     public AbstractDialogStringValueEditor getValueEditor()
401     {
402         return valueEditor;
403     }
404
405
406     /**
407      * Checks if is multivalued.
408      *
409      * @return true, if is multivalued
410      */

411     public boolean isMultivalued()
412     {
413         return isMultivalued;
414     }
415 }
416
Popular Tags