Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/data/ejb/disguiserecord/DisguiseBucketRecordData.java


1   /*
2    * DisguiseBucketRecordData.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:28 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.data.ejb.disguiserecord;
12  
13  import java.util.Enumeration;
14  import java.util.Vector;
15  
16  /**
17   * DisguiseBucketRecordData is a class that encapsulates
18   * data for the initialization of PreIdxService.
19   *
20   *
21   * @author  Wei Tang
22   * @version 1.0, 05/11/99
23   *
24   * @see     com.flexstor.
25   * @see     com.flexstor.
26   *
27   * @since   FLEXSTOR.db 3.0
28   *
29   * Revision History   Description of Change
30   * ----------------   ---------------------------------------------------
31   * WT     05/11/99   Creation
32   */
33  
34  public class DisguiseBucketRecordData
35  extends GenericBucketRecordData
36  {
37     protected Vector vChildBucketsThisLevel = null;
38     protected Vector vElementsThisLevel = null;
39      static final long serialVersionUID = -3323247013907637313L;
40  
41     public DisguiseBucketRecordData()
42     {
43        super();
44        //more initializations
45     }
46  
47     public DisguiseElementRecordData[] getElements()
48     {
49        if( vElementsThisLevel != null && vElementsThisLevel.size() > 0 )
50        {
51           DisguiseElementRecordData[] elements = new DisguiseElementRecordData[ vElementsThisLevel.size() ];
52           vElementsThisLevel.copyInto( elements );
53           return elements;
54        }
55        return null;
56     }
57  
58     public Vector getElementsAsVector()
59     {
60        return vElementsThisLevel;
61     }
62  
63     public boolean setElements(DisguiseElementRecordData[] elements)
64     {
65        if ( elements != null && elements.length > 0 )
66        {
67           vElementsThisLevel = new Vector();
68           for( int i = 0; i < elements.length; i++ )
69              vElementsThisLevel.addElement( elements[i] );
70  
71           return true;
72        }
73        return false;
74     }
75  
76     public void addBucketChild(DisguiseBucketRecordData child)
77     {
78        if ( vChildBucketsThisLevel == null)
79           vChildBucketsThisLevel = new Vector();
80           
81        vChildBucketsThisLevel.addElement( child );
82     }
83  
84     public void addElement(DisguiseElementRecordData element)
85     {
86        if ( vElementsThisLevel == null)
87           vElementsThisLevel = new Vector();
88           
89        vElementsThisLevel.addElement( element );
90     }
91  
92     /*
93     *return only the children at this level
94     *
95     */
96     public DisguiseBucketRecordData[] getBuckets()
97     {
98        if( vChildBucketsThisLevel != null && vChildBucketsThisLevel.size() > 0 )
99        {
100          DisguiseBucketRecordData[] children = new DisguiseBucketRecordData[ vChildBucketsThisLevel.size() ];
101          vChildBucketsThisLevel.copyInto( children );
102          return children;
103       }
104       return null;
105    }
106 
107    public Vector getBucketsAsVector()
108    {
109       return vChildBucketsThisLevel;
110    }
111 
112    public boolean setBuckets(DisguiseBucketRecordData[] buckets)
113    {
114       if ( buckets != null && buckets.length > 0 )
115       {
116          vChildBucketsThisLevel = new Vector();
117          for( int i = 0; i < buckets.length; i++ )
118             vChildBucketsThisLevel.addElement( buckets[i] );
119 
120          return true;
121       }
122       return false;
123    }
124    
125    public void deleteTempAssets()
126    {
127       DisguiseElementRecordData[] elements = this.getElements();
128       if ( elements != null )
129       {
130          for ( int i = 0; i < elements.length; i++ )
131          {
132             DisguiseAssetRecordData parentAsset = elements[i].getParentAsset();
133             parentAsset.deleteTempChildren();
134          }
135       }
136       
137       DisguiseBucketRecordData[] childBuckets = this.getBuckets();
138       if ( childBuckets != null )
139       {
140          for ( int i = 0; i < childBuckets.length; i++ )
141             childBuckets[i].deleteTempAssets();
142       }
143    }
144    
145    /**
146    * Get the assets that matches the role, type and temp/parent flag specified
147    */
148    public void getAssets( Vector vAssets, String sRole, String sType, String sFlag )
149    {
150       getAssets( null, vAssets, sRole, sType, sFlag );
151    }
152    
153    /**
154    * Get the assets that matches the role, type and temp/parent flag specified
155    */
156    void getAssets( Vector vTraversalPath, Vector vAssets, String sRole, String sType, String sFlag )
157    {
158       if ( vTraversalPath != null )
159          vTraversalPath.addElement( this );
160 
161       // Do elements first
162       if ( vElementsThisLevel != null )
163       {
164          for ( int i = 0; i < vElementsThisLevel.size(); i++ )
165          {
166             ((DisguiseElementRecordData)vElementsThisLevel.elementAt(i)).getAssets( vTraversalPath, vAssets, sRole, sType, sFlag );
167          }
168       }
169       // Then do child buckets
170       if ( vChildBucketsThisLevel != null )
171       {
172          for ( int i = 0; i < vChildBucketsThisLevel.size(); i++ )
173          {
174             ((DisguiseBucketRecordData)vChildBucketsThisLevel.elementAt(i)).getAssets( vTraversalPath, vAssets, sRole, sType, sFlag );
175          }
176       }
177 
178       // Remove the traveral item corresponding to this bucket.
179       if ( vTraversalPath != null )
180          vTraversalPath.removeElement( vTraversalPath.lastElement() );
181    }
182 
183    /**
184    * Get all primary assets
185    */
186    public void getPrimaryAssets( Vector vAssets )
187    {
188       // Do elements first
189       if ( vElementsThisLevel != null )
190          for ( int i = 0; i < vElementsThisLevel.size(); i++ )
191             ((DisguiseElementRecordData)vElementsThisLevel.elementAt(i)).getPrimaryAssets( vAssets );
192 
193       // Then do child buckets
194       if ( vChildBucketsThisLevel != null )
195          for ( int i = 0; i < vChildBucketsThisLevel.size(); i++ )
196             ((DisguiseBucketRecordData)vChildBucketsThisLevel.elementAt(i)).getPrimaryAssets( vAssets );
197    }
198 
199    /**
200    * Delete all primary assets which role and type are not equal to the arguments
201    * If there are no elements and child buckets left in this bucket, then return
202    * true so the caller can remove this bucket from the list.
203    */
204    public boolean removePrimaryAssetsNotInRoleType( String sRole, String sType )
205    {
206       boolean bNoElementsLeft = true;
207       boolean bNoChildBucketsLeft = true;
208       if ( vElementsThisLevel != null )
209       {
210          Vector clonedElements = (Vector) vElementsThisLevel.clone();
211          for ( Enumeration e = clonedElements.elements(); e.hasMoreElements(); )
212          {
213             DisguiseElementRecordData element = (DisguiseElementRecordData) e.nextElement();
214             if ( element.removePrimaryAssetsNotInRoleType( sRole, sType ) )
215                vElementsThisLevel.removeElement( element );
216             else
217                bNoElementsLeft = false; //If there is at least one element, do not attempt to delete this bucket
218          }
219       }
220       if ( vChildBucketsThisLevel != null )
221       {
222          Vector vClonedChildBuckets = (Vector) vChildBucketsThisLevel.clone();
223          for ( Enumeration e = vClonedChildBuckets.elements(); e.hasMoreElements();  )
224          {
225             DisguiseBucketRecordData childBucket = (DisguiseBucketRecordData)e.nextElement();
226             if ( childBucket.removePrimaryAssetsNotInRoleType( sRole, sType ) )
227                vChildBucketsThisLevel.removeElement( childBucket );
228             else
229                bNoChildBucketsLeft = false; //If there is at least one child bucket, do not attemp to delete this bucket
230          }
231       }
232       if ( bNoElementsLeft && bNoChildBucketsLeft )
233          return true;
234       else
235          return false;
236    }
237    
238    /**
239    * Delete the assets that matches the role, type and temp/parent flag specified.
240    * This method will return true only if there were elements and/or buckets at this
241    * level and all of them were deleted. If no elements or buckets were present in
242    * this bucket, it will not be deleted. This is to allow to import buckets which
243    * were meant to be empty.
244    */
245    public boolean deleteAssets( String sRole, String sType, String sFlag )
246    {
247       boolean bDeleteBucket = false;
248       // Do elements first
249       if ( vElementsThisLevel != null )
250       {
251          DisguiseElementRecordData element = null;
252          // Create a shallow clone for scanning and to delete items from the original vector
253          Vector vElementClone = (Vector) vElementsThisLevel.clone();
254          for ( Enumeration e = vElementClone.elements(); e.hasMoreElements(); )
255          {
256             element = (DisguiseElementRecordData)e.nextElement();
257             // This will remove elements only if it had assets and parent was deleted.
258             // The element.deleteAssets() will prevent the removal of elements which
259             // didn't contain assets (allow for import of empty elements).
260             if ( element.deleteAssets( sRole, sType, sFlag ) )
261                vElementsThisLevel.removeElement( element );
262          }
263          // If there are no more elements at this level set the boolean bDeleteBuckets
264          // to true to indicate that this bucket might get deleted if there are no child
265          // buckets or if they are all deleted in the next section below.
266          if ( vElementsThisLevel.size() == 0 )
267             bDeleteBucket = true;
268       }
269       // Then do child buckets
270       if ( vChildBucketsThisLevel != null )
271       {
272          DisguiseBucketRecordData bucket = null;
273          // Create a shallow clone for scanning and to delete items from the original vector
274          Vector vBucketClone = (Vector) vChildBucketsThisLevel.clone();
275          for ( Enumeration e = vBucketClone.elements(); e.hasMoreElements(); )
276          {
277             bucket = (DisguiseBucketRecordData)e.nextElement();
278             if ( bucket.deleteAssets( sRole, sType, sFlag ) )
279                vChildBucketsThisLevel.removeElement( bucket );
280          }
281          if ( vChildBucketsThisLevel.size() == 0 )
282             bDeleteBucket = true;
283          else
284             bDeleteBucket = false;
285       }
286       return bDeleteBucket;
287    }
288    
289    /**
290    * Will delete assets that meet the criteria of the argument asset.
291    *
292    * @param assetToDelete
293    * @param nAssetsDeleted keeps a count of the assets deleted so far, at the end of the
294    *        method, this value will be updated with the number of assets (including children
295    *        and itself) deleted.
296    * @return true if this bucket should be deleted.
297    */
298    public boolean deleteAsset( DisguiseAssetRecordData assetToDelete, int[] nAssetsDeleted )
299    {
300       boolean bDeleteBucket = false;
301       // Do elements first
302       if ( vElementsThisLevel != null )
303       {
304          DisguiseElementRecordData element = null;
305          // Create a shallow clone for scanning and to delete items from the original vector
306          Vector vElementClone = (Vector) vElementsThisLevel.clone();
307          for ( Enumeration e = vElementClone.elements(); e.hasMoreElements(); )
308          {
309             element = (DisguiseElementRecordData)e.nextElement();
310             // This will remove elements only if it had assets and parent was deleted.
311             // The element.deleteAssets() will prevent the removal of elements which
312             // didn't contain assets (allow for import of empty elements).
313             if ( element.deleteAsset( assetToDelete, nAssetsDeleted ) )
314                vElementsThisLevel.removeElement( element );
315          }
316          // If there are no more elements at this level set the boolean bDeleteBuckets
317          // to true to indicate that this bucket might get deleted if there are no child
318          // buckets or if they are all deleted in the next section below.
319          if ( vElementsThisLevel.size() == 0 )
320             bDeleteBucket = true;
321       }
322       // Then do child buckets
323       if ( vChildBucketsThisLevel != null )
324       {
325          DisguiseBucketRecordData bucket = null;
326          // Create a shallow clone for scanning and to delete items from the original vector
327          Vector vBucketClone = (Vector) vChildBucketsThisLevel.clone();
328          for ( Enumeration e = vBucketClone.elements(); e.hasMoreElements(); )
329          {
330             bucket = (DisguiseBucketRecordData)e.nextElement();
331             if ( bucket.deleteAsset( assetToDelete, nAssetsDeleted ) )
332                vChildBucketsThisLevel.removeElement( bucket );
333          }
334          if ( vChildBucketsThisLevel.size() == 0 )
335             bDeleteBucket = true;
336          else
337             bDeleteBucket = false;
338       }
339       return bDeleteBucket;
340    }
341 
342    /**
343    * Delete all elements that are represented by the argument element.
344    * This call is recursive; it will parse all the tree structure until the desired element is found;
345    * if multiple instances of this element are found, all of them will be removed.
346    * @return The number of elements deleted, or zero for none
347    */
348    public void deleteElement( DisguiseElementRecordData elementToDelete, int[] nElementsDeleted )
349    {
350       if ( vElementsThisLevel != null )
351       {
352          // Runs until all elements equal to elementToDelete are removed
353          while ( vElementsThisLevel.removeElement(elementToDelete) ) 
354             nElementsDeleted[0]++;
355       }
356       
357       if ( vChildBucketsThisLevel != null )
358       {
359          DisguiseBucketRecordData bucket = null;
360          for ( Enumeration e = vChildBucketsThisLevel.elements(); e.hasMoreElements(); )
361          {
362             bucket = (DisguiseBucketRecordData)e.nextElement();
363             bucket.deleteElement( elementToDelete, nElementsDeleted );
364          }
365       }
366    }
367    
368    /**
369    * Delete all buckets that are represented by the argument bucket.
370    * This call is recursive; it will parse all the tree structure until the desired bucket is found;
371    * if multiple instances of this bucket are found, all of them will be removed.
372    * @return The number of buckets deleted, or zero for none
373    */
374    public void deleteBucket( DisguiseBucketRecordData bucketToDelete, int[] nBucketsDeleted )
375    {
376       if ( vChildBucketsThisLevel != null )
377       {
378          if ( vChildBucketsThisLevel.removeElement( bucketToDelete ) )
379             nBucketsDeleted[0]++;
380          else
381          {
382             DisguiseBucketRecordData bucket = null;
383             for ( Enumeration e = vChildBucketsThisLevel.elements(); e.hasMoreElements(); )
384             {
385                bucket = (DisguiseBucketRecordData)e.nextElement();
386                bucket.deleteBucket( bucketToDelete, nBucketsDeleted );
387             }
388          }
389       }
390    }
391    
392    /**
393    * Clones the bucket. The bFullClone argument determines if this clone will
394    * include a clone copy of all children of this bucket, or not. This provides
395    * for creating lite clones of the object.
396    */
397    public synchronized Object cloneBucket( boolean bFullClone)
398    {
399       DisguiseBucketRecordData clonedObject = new DisguiseBucketRecordData();
400       super.cloneRecord( clonedObject );
401       if ( bFullClone )
402       {
403          if ( vChildBucketsThisLevel != null )
404          {
405             for ( int i = 0; i < vChildBucketsThisLevel.size(); i++ )
406             {
407                DisguiseBucketRecordData aBucket = (DisguiseBucketRecordData) vChildBucketsThisLevel.elementAt(i);
408                clonedObject.addBucketChild( (DisguiseBucketRecordData) aBucket.cloneBucket(true) );
409             }
410          }
411          if ( vElementsThisLevel != null )
412          {
413             for ( int i = 0; i < vElementsThisLevel.size(); i++ )
414             {
415                DisguiseElementRecordData anElement = (DisguiseElementRecordData) vElementsThisLevel.elementAt(i);
416                clonedObject.addElement( (DisguiseElementRecordData) anElement.cloneElement(true) );
417             }
418          }
419       }
420       return clonedObject;
421    }
422 }