Source code: com/flexstor/flexdbserver/services/asset/ImportDataCreateService.java
1 /*
2 * ImportDataCreateService.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.flexdbserver.services.asset;
12
13 import java.util.Date;
14 import java.util.Enumeration;
15 import java.util.HashSet;
16 import java.util.Iterator;
17 import java.util.Set;
18 import java.util.Vector;
19
20 import com.flexstor.common.constants.ActionPropertiesI;
21 import com.flexstor.common.constants.AssetRolesI;
22 import com.flexstor.common.constants.BucketConstantsI;
23 import com.flexstor.common.data.ActionData;
24 import com.flexstor.common.data.ActionResult;
25 import com.flexstor.common.data.AssetRecordData;
26 import com.flexstor.common.data.TraversalInfo;
27 import com.flexstor.common.data.TraversalItem;
28 import com.flexstor.common.data.ejb.disguiserecord.AssetRoleData;
29 import com.flexstor.common.data.ejb.disguiserecord.AudioRoleData;
30 import com.flexstor.common.data.ejb.disguiserecord.DisguiseAssetRecordData;
31 import com.flexstor.common.data.ejb.disguiserecord.DisguiseBucketRecordData;
32 import com.flexstor.common.data.ejb.disguiserecord.DisguiseElementRecordData;
33 import com.flexstor.common.data.ejb.disguiserecord.DisguiseRecordData;
34 import com.flexstor.common.data.ejb.disguiserecord.GenericBucketRecordData;
35 import com.flexstor.common.data.ejb.disguiserecord.HighresRoleData;
36 import com.flexstor.common.data.ejb.disguiserecord.LayoutRoleData;
37 import com.flexstor.common.data.ejb.disguiserecord.LowresRoleData;
38 import com.flexstor.common.data.ejb.disguiserecord.ThumbnailRoleData;
39 import com.flexstor.common.data.ejb.disguiserecord.VideoRoleData;
40 import com.flexstor.common.data.ejb.search.AssetTree;
41 import com.flexstor.common.errorlogger.FlexError;
42 import com.flexstor.common.gateway.InitGateway;
43 import com.flexstor.common.gateway.SearchResultGateway;
44 import com.flexstor.common.gateway.exceptions.CannotPingServerException;
45 import com.flexstor.common.gateway.exceptions.TransactionFailedException;
46 import com.flexstor.common.importprocessor.ImportCtlData;
47 import com.flexstor.common.importprocessor.ImportData;
48 import com.flexstor.common.importprocessor.ImportServices;
49 import com.flexstor.common.io.NativeFileProperties;
50 import com.flexstor.common.io.xfile.FlexXFile;
51 import com.flexstor.common.resources.Resources;
52 import com.flexstor.common.settings.Settings;
53 import com.flexstor.common.util.Checksum;
54 import com.flexstor.common.util.Diagnostic;
55 import com.flexstor.common.util.FlexHashtable;
56 import com.flexstor.common.util.ServerList;
57 import com.flexstor.ejb.bucket.persist.ServerBucketExtendData;
58 import com.flexstor.ejb.disguise.persist.ServerDisguiseExtendData;
59 import com.flexstor.flexdbserver.disguise.DisguiseLoader;
60 import com.flexstor.flexdbserver.disguise.DisguiseLoaderException;
61 import com.flexstor.flexdbserver.importprocessor.CtlFile;
62 import com.flexstor.flexdbserver.services.Service;
63 import com.flexstor.flexdbserver.services.ServiceContext;
64 import com.flexstor.flexdbserver.util.DisguiseAssetRecordPopulator;
65 import com.flexstor.flexdbserver.util.PathBuilder;
66
67 /**
68 *
69 * <P>
70 * ImportDataCreateService <BR>
71 * <BLOCKQUOTE>
72 * Creates a fully structured ImportData (i.e. buckets, elments, assets ...)
73 * object from the disquise (application)
74 * named in the input control (*.ctl) file and the traversal path information from the
75 * ActionData object passed in.<BR>
76 * </BLOCKQUOTE>
77 * </P>
78 *
79 * <P>
80 * Input Data Object <BR>
81 * <BLOCKQUOTE>
82 * com.flexstor.flexdbserver.services.asset.ActionData
83 * </BLOCKQUOTE>
84 * </P>
85 *
86 * <P>
87 * Output Data Object <BR>
88 * <BLOCKQUOTE>
89 * com.flexstor.common.data.ActionResult
90 * </BLOCKQUOTE>
91 * </P>
92 *
93 */
94 public class ImportDataCreateService
95 implements Service
96 {
97 // To get the version number from MKS
98 public final static String IDENTIFIER="$Id: ImportDataCreateService.java,v 1.4 2003/08/11 02:22:28 aleric Exp $";
99
100 private String fileSeparator;
101 private int id = -1;
102 private ActionData refActionData = null;
103 private String sThisService = null;
104 protected DisguiseRecordData refDisguiseRecordData = null;
105 protected ServerDisguiseExtendData refServerExtend = null;
106 protected ImportCtlData refCtlData = null;
107 /***** TEST ONLY *****/
108 protected boolean bTest = false;
109
110 /**
111 * Calls before the service is initialized (before initData is called) to
112 * pass information about the environment in which the service is running.
113 * This environment consists of information about the properties set for the
114 * service in one of these files (services.config, roletype_services.config,
115 * or *.ctl), plus methods to access other information such as an instance
116 * of the service broker to invoke other services, the transaction id for
117 * the service, file separator character and local path for the installation
118 * directory and configuration directory.
119 *
120 * @param context Holds information about the environment in which the service
121 * is running.
122 */
123 public void setServiceContext( ServiceContext context )
124 {
125 fileSeparator = context.getFileSeparator();
126 id = context.getTransactionId();
127 }
128
129 /**
130 *
131 *
132 */
133 public void initData(ActionData refActionData)
134 {
135 //6363=ImportData Create Service
136 sThisService = Resources.get(6363) + " (" + id + ")";
137
138 // Tell about test mode if it is set
139 if (bTest == true)
140 {
141 System.out.println("");
142 System.out.println("!!!! WARNING, WARNING, WARNING -- THIS IS TEST MODE CODE !!!!");
143 System.out.println("");
144 }
145
146 this.refActionData = refActionData;
147
148 } // initData
149
150 /**
151 *
152 *
153 */
154 public ActionResult go()
155 {
156 boolean bResult = true;
157 AssetRecordData record = null;
158
159 ImportData refImportData = new ImportData();
160 refImportData.setTransId( id );
161
162 // Load global properties set in the ActionData object
163 loadGlobalProperties(refImportData);
164
165 // Load the information contained in the *.ctl file
166 loadCtlInfo(refImportData);
167
168 // Set the user id for this transaction
169 String sUserId = refCtlData.getValuePerKey("userid");
170 refImportData.setUserId( sUserId );
171 // Create the Disguise Record for the ImportData object to be returned
172 DisguiseRecordData refDisguiseRecordData = new DisguiseRecordData();
173
174 // Set the toclassify value in DisguiseRecordData
175 refDisguiseRecordData.setToClassify( refCtlData.getBooleanPerKey("toclassify") );
176
177 // Get the AssetRecordData items from ActionData
178 Vector vRecords = refActionData.getRecords();
179
180 AssetTree assetsTree = null;
181
182 // Process each AssetRecordData
183 for (Enumeration e = vRecords.elements(); e.hasMoreElements(); )
184 {
185 record = (AssetRecordData) e.nextElement();
186 DisguiseBucketRecordData refBucket = null;
187
188 // Get the traversal path(s) for this record
189 Vector vAllTraversalPaths = record.getTraversalPathInfo();
190 if ((vAllTraversalPaths == null) || (vAllTraversalPaths.size() == 0))
191 {
192 continue;
193 }
194
195 // Process each traversal path for this record
196 for (int t=0; t<vAllTraversalPaths.size(); t++)
197 {
198 TraversalInfo refTraversalInfo = (TraversalInfo)vAllTraversalPaths.elementAt(t);
199 if ( refServerExtend == null )
200 {
201 int nDisguiseId = refTraversalInfo.getDisguiseId();
202 getDisguise(nDisguiseId);
203 refDisguiseRecordData.setDisguiseId( nDisguiseId );
204
205 // Get the traversal path for all assets (from element down to lowest asset level)
206 assetsTree = getAssetsTree( vRecords, nDisguiseId );
207 }
208 if (refTraversalInfo == null)
209 {
210 break;
211 }
212
213 // Get the tranversal path items for this traversal path
214 Vector vTraversalPath = refTraversalInfo.getTraversalItems();
215 // For each item in the traversal path find the corresponding bucket
216 // in the bucket structure or create and add a new bucket/element
217 for (int i=0; i<vTraversalPath.size(); i++)
218 {
219 // Get the traversal path item
220 TraversalItem refTraversalItem = (TraversalItem)vTraversalPath.elementAt(i);
221 if (refTraversalItem == null)
222 {
223 break;
224 }
225
226 // The first traversal path item should be a top-bucket
227 if (i == 0)
228 {
229 refBucket = null;
230
231 // Get the top-level buckets
232 DisguiseBucketRecordData[] aDisguiseRecordData = refDisguiseRecordData.getBuckets();
233 if ((aDisguiseRecordData != null) && (aDisguiseRecordData.length > 0))
234 {
235 // Find the top-bucket whose Record ID matches the traversal path item
236 for (int j=0; j<aDisguiseRecordData.length; j++)
237 {
238 refBucket = aDisguiseRecordData[j];
239 if (refBucket != null)
240 {
241 if (refBucket.getRecordId() == refTraversalItem.getRecordId())
242 {
243 // Correct bucket found
244 break;
245 }
246
247 else
248 {
249 refBucket = null;
250 }
251 } // refBucket = null if
252 } // for j
253 } // aDisguiseRecordData else
254
255 if (refBucket == null)
256 {
257 // The desired top-bucket was not found, so create it
258 refBucket = new DisguiseBucketRecordData();
259
260 if (refBucket != null)
261 {
262 refBucket.setRecordId(refTraversalItem.getRecordId());
263 refBucket.setBucketStructId(refTraversalItem.getStructureId());
264
265 // Add it to the Disguise record object
266 refDisguiseRecordData.addBucket(refBucket);
267 }
268
269 else
270 {
271 // ERROR
272 bResult = false;
273 break;
274 }
275 }
276 } // i=0 if (TOP-BUCKET)
277
278
279 // Bucket is below top
280 else if (refBucket != null)
281 {
282 // Find this bucket already in the structure or create and place it in the structure
283 GenericBucketRecordData refReturnBucket =
284 addObjectToStructure(refBucket, refTraversalItem);
285 if (refReturnBucket == null)
286 {
287 // ERROR
288 //?????????? is this sufficient?
289 break;
290 }
291
292 // Is the returned bucket a Normal bucket or an Element bucket?
293 if (refReturnBucket instanceof DisguiseBucketRecordData)
294 {
295 // Its a Normal bucket, save it as the parent for the next level
296 refBucket = (DisguiseBucketRecordData)refReturnBucket;
297 }
298
299 else // its an Element bucket, the end of the line
300 {
301 DisguiseElementRecordData refElement = (DisguiseElementRecordData)refReturnBucket;
302
303 if ( refElement.getParentAsset() == null ) // Nothing has been created yet
304 {
305 AssetTree elementNode = assetsTree.getNode(refElement.getRecordId(), BucketConstantsI.ELEMENT_BUCKET);
306 constructAssetLevelStructure( refElement, elementNode );
307 }
308
309 // Create an Asset record and add it to the Element
310 DisguiseAssetRecordData refAsset = getDisguiseAssetRecordData( record.getRecordId(), refElement.getParentAsset() );
311 // If refAsset was not found, this is a new one; set this as the primary parent disregarding
312 // the asset structure previously set; it is unlikely we will step into this code
313 if ( refAsset == null )
314 {
315 refAsset = new DisguiseAssetRecordData();
316 refElement.setParentAsset(refAsset);
317 }
318 setAttributes(refAsset, record, sUserId);
319
320 // Create the child assets
321 Vector vChildRecords = record.getChildrenRecords();
322 if (vChildRecords != null)
323 {
324 for (int k=0; k<vChildRecords.size(); k++)
325 {
326 AssetRecordData refChildRecord = (AssetRecordData)vChildRecords.elementAt(k);
327 DisguiseAssetRecordData refChildAsset = getDisguiseAssetRecordData( refChildRecord.getRecordId(), refElement.getParentAsset() );
328 // If refAsset was not found, this is a new one; set this as a child of the previous asset;
329 // it is unlikely we will step into this code
330 if ( refChildAsset == null )
331 {
332 refChildAsset = new DisguiseAssetRecordData();
333 refAsset.addChildAsset(refChildAsset);
334 }
335 setAttributes(refChildAsset, refChildRecord, sUserId);
336 } // for k
337 } // vChildRecords null if
338
339 // This traversal path is done
340 break;
341 } // Element bucket
342 } // refBucket = null if
343 } // for i
344 } // for t
345 } // end of enumeration for
346
347 // Add the Disguise Record to the ImportData object
348 refImportData.setDisguiseRecordRef(refDisguiseRecordData);
349
350 // Setup the result
351 ActionResult result;
352 result = new ActionResult(bResult);
353 // result.setText(FORK);
354 result.setData(refImportData);
355
356 // send the response object to the calling class
357 return result;
358 } // go
359
360
361 /**
362 *
363 *
364 */
365 protected GenericBucketRecordData addObjectToStructure(DisguiseBucketRecordData refParentBucket,
366 TraversalItem refTraversalItem)
367 {
368 // Search the children of the parent bucket for a match to the traversal item
369 // If found return the bucket as the next parent.
370 // If not found, create a new bucket as a child to the parent and return the new bucket as next parent
371 GenericBucketRecordData refCurrentBucket = null;
372
373 long nRecordId = refTraversalItem.getRecordId();
374 int nBucketId = refTraversalItem.getStructureId();
375 int nType = getType(nBucketId);
376
377 if (nType == BucketConstantsI.NORMAL_BUCKET)
378 {
379 // The traversal item indicates that the object should be a BUCKET
380 // Get the child buckets from the structure, under the parent
381 // Does the correct bucket already exist in the structure?
382 DisguiseBucketRecordData refChildBucket = null;
383 DisguiseBucketRecordData[] aChildBuckets = refParentBucket.getBuckets();
384 if (aChildBuckets != null)
385 {
386 for(int i=0; i<aChildBuckets.length; i++)
387 {
388 refChildBucket = aChildBuckets[i];
389 if (nBucketId != refChildBucket.getBucketStructId())
390 {
391 // ERROR, not correct bucket for this level
392 return null;
393 }
394
395 if (nRecordId == refChildBucket.getRecordId())
396 {
397 // Proper bucket found, so quit
398 refCurrentBucket = refChildBucket;
399 break;
400 }
401
402 else
403 {
404 // Not the right bucket so continue
405 refChildBucket = null;
406 }
407 } // for
408 } // aChildBuckets null if
409
410 // Was the bucket found already in the structure?
411 if (refChildBucket == null)
412 {
413 // Not found, so create a child bucket
414 refChildBucket = new DisguiseBucketRecordData();
415 refChildBucket.setBucketStructId(nBucketId);
416 refChildBucket.setRecordId(nRecordId);
417
418 // Add it to the parent
419 refParentBucket.addBucketChild(refChildBucket);
420
421 // Setup to return the child bucket
422 refCurrentBucket = refChildBucket;
423 } // refChildBucket null if
424 } // nType = NORMAL_BUCKET
425
426 else if (nType == BucketConstantsI.ELEMENT_BUCKET)
427 {
428 // The traversal item indicates that the object should be an ELEMENT
429 DisguiseElementRecordData refElement = null;
430
431 DisguiseElementRecordData[] aElements = refParentBucket.getElements();
432 if (aElements != null)
433 {
434 // Walk thru the existing elements and check for a record id match
435 for (int i=0; i<aElements.length; i++)
436 {
437 refElement = aElements[i];
438 if (refElement == null)
439 {
440 break;
441 }
442
443 if (nBucketId != refElement.getBucketStructId())
444 {
445 // ERROR - The element bucket structure id doesnt match the id in
446 // the same level in the traversal path
447 // refElement = null;
448 // break;
449 return null;
450 }
451
452 if (nRecordId == refElement.getRecordId())
453 {
454 // The element exists, so exit the loop with a non-null refElement
455 refCurrentBucket = (DisguiseElementRecordData)refElement;
456 break;
457 }
458
459 else
460 {
461 // Not the right element, continue. If loop is done, will exit with null refElement
462 refElement = null;
463 }
464 } // for i
465 } // aElements if
466
467 if (refElement == null)
468 {
469 // The element does not exist yet, so create it
470 DisguiseElementRecordData refNewElement = new DisguiseElementRecordData();
471 refNewElement.setBucketStructId(nBucketId);
472 refNewElement.setRecordId(nRecordId);
473
474 // Add the Element to the parent bucket
475 refParentBucket.addElement(refNewElement);
476
477 // Return the Element
478 refCurrentBucket = (DisguiseElementRecordData)refNewElement;
479 }
480 } // nType = ELEMENT_BUCKET
481
482 else
483 {
484 // ERROR - bucket can only be element or bucket type
485 return null;
486 } // nType else
487
488 return refCurrentBucket;
489 } // addObjectToStructure
490
491 /**
492 * Load the .ctl data to the ImportData object.
493 */
494 protected void loadCtlInfo(ImportData refImportData)
495 {
496 String sCtlFile = refActionData.getString( ActionPropertiesI.HOT_DIRECTORY );
497 FlexXFile fCtlFile = new FlexXFile(sCtlFile);
498
499 CtlFile refCtlFile = new CtlFile(fCtlFile);
500
501 refCtlData = refImportData.getCtlDataRef();
502 if (refCtlFile.copyToVector(refCtlData) == false)
503 {
504 new FlexError(FlexError.CRITICAL, sThisService, IDENTIFIER, "Bad Direct.hot entry");
505 }
506 // Close the ctl file to free descriptor
507 refCtlFile.close();
508
509
510 // Add this hot directory's control files path and ctl file name
511 refCtlData.addElement("CONTROLFILE=" + sCtlFile);
512 refCtlData.addElement("CONTROLFILESPATH=" + sCtlFile.substring(0, sCtlFile.lastIndexOf(fileSeparator) + 1));
513
514 // Setup the pre/post services
515 setServices(refImportData, refCtlData);
516 }
517
518 /**
519 *
520 *
521 */
522 protected int getType(int nStructID)
523 {
524 int nType = -1;
525
526 // This is debug code, bypassing the extend data
527 if (bTest == true)
528 {
529 if ((nStructID == 17) || (nStructID == 18))
530 {
531 nType = BucketConstantsI.NORMAL_BUCKET;
532 }
533
534 else if (nStructID == 19)
535 {
536 nType = BucketConstantsI.ELEMENT_BUCKET;
537 }
538
539 else if (nStructID == 20)
540 {
541 nType = BucketConstantsI.ASSET_BUCKET;
542 }
543
544 return nType;
545 } // bTest if
546
547
548 // From the server disguise extend object for this disguise, get the bucket
549 // with the designated ID. From it, obtain the bucket type <NORMAL_BUCKET, ELEMENT_BUCKET>
550 if (refServerExtend == null)
551 {
552 return nType;
553 }
554
555 ServerBucketExtendData refBucketExtendData = refServerExtend.getServerBucketExtendDataObject(nStructID);
556 if (refBucketExtendData != null)
557 {
558 nType = refBucketExtendData.getBucketType();
559 }
560
561 return nType;
562 } // getType
563
564 protected void loadGlobalProperties( ImportData refImportData )
565 {
566 FlexHashtable hProperties = refActionData.getGlobalProperties();
567 if ( hProperties != null )
568 refImportData.setGlobalProperties( hProperties );
569 }
570
571 /**
572 *
573 *
574 */
575 protected void setAttributes(DisguiseAssetRecordData refAsset, AssetRecordData record, String sUserId)
576 {
577 refAsset.setTraversalInfo( (TraversalInfo) record.getTraversalPathInfo().elementAt(0) );
578
579 int nRoleId = refAsset.getAssetRoleId();
580 AssetRoleData role;
581 switch ( nRoleId )
582 {
583 case AssetRolesI.HIGHRES:
584 role = new HighresRoleData();
585 break;
586 case AssetRolesI.LOWRES:
587 role = new LowresRoleData();
588 break;
589 case AssetRolesI.THUMBNAIL:
590 role = new ThumbnailRoleData();
591 break;
592 case AssetRolesI.LAYOUT:
593 role = new LayoutRoleData();
594 break;
595 case AssetRolesI.AUDIO:
596 role = new AudioRoleData();
597 break;
598 case AssetRolesI.VIDEO:
599 role = new VideoRoleData();
600 break;
601 default:
602 role = new HighresRoleData();
603 }
604 refAsset.setAssetRole( role );
605
606
607 long nFileSize = -1;
608 Date dFileCreateDate = null;
609 try
610 {
611 if ( record.getFileSize() != null )
612 nFileSize = record.getFileSize().longValue();
613 else
614 nFileSize = (new FlexXFile(PathBuilder.constructFilePath(refAsset))).length();
615
616 // Set the file date using the native file properties
617 dFileCreateDate = (new NativeFileProperties()).getCreationDate(fileSeparator + refAsset.getLocation()
618 + refAsset.getFileName());
619 }
620 catch ( NullPointerException npe ) {}
621
622 FlexHashtable hProperties = record.getProperties();
623 if ( hProperties != null )
624 {
625 String sKey;
626 for ( Enumeration e = hProperties.keys(); e.hasMoreElements(); )
627 {
628 sKey = (String) e.nextElement();
629 refAsset.addProperty( sKey, hProperties.get(sKey) );
630 }
631 }
632
633 // Get the checksum
634 Checksum refChecksum = new Checksum();
635 long lChecksum = refChecksum.getChecksum(refAsset.getLocation() + refAsset.getFileName());
636
637
638
639 Date dFileModDate = null;
640 String sFileModDate = record.getString(ActionPropertiesI.MODIFICATION_DATE);
641 if ( sFileModDate != null && !sFileModDate.equals("") )
642 dFileModDate = new Date(record.getString(ActionPropertiesI.MODIFICATION_DATE));
643
644 int nAppleTalkVendor;
645 try { nAppleTalkVendor = record.getAppleTalkVendor().intValue(); }
646 catch ( NullPointerException npe ) { nAppleTalkVendor = ServerList.getAppleTalkVendor(record.getServer()); }
647
648 try
649 {
650 DisguiseAssetRecordPopulator assetPopulator = new DisguiseAssetRecordPopulator(refServerExtend.getId());
651 assetPopulator.setAttributes( refAsset,
652 record.getServer(),
653 record.getLocation(),
654 record.getFileName(),
655 sUserId,
656 dFileModDate,
657 dFileCreateDate,
658 lChecksum,
659 nFileSize,
660 0, // The role hasn't been defined at this point
661 nAppleTalkVendor );
662 }
663 catch ( DisguiseLoaderException dle )
664 {
665 // This exception would indicate that there was something extremely wrong:
666 // - The disguise object is not set in the database
667 // - Could not connect to the database
668 // Since this is very unusual to happen, just set the values in the traditional way; just
669 // in case. However this way will not set values in the DisguiseFieldRecordData objects
670 refAsset.setServer( record.getServer() );
671 refAsset.setLocation( record.getLocation() );
672 refAsset.setFileName( record.getFileName() );
673 refAsset.setUserId( sUserId );
674 refAsset.setModTime( dFileModDate );
675 refAsset.setFileDate( dFileCreateDate );
676 refAsset.setFileSize( nFileSize );
677 refAsset.setAppleTalkVendor( nAppleTalkVendor );
678 }
679 } // setAttributes
680
681 /**
682 *
683 *
684 */
685 protected void setServices(ImportData refImportData, ImportCtlData refCtlData)
686 {
687 // Set Pre-services
688 ImportServices refPreServices = refImportData.getPreServicesRef();
689 getAndSaveServices("PRESERVICES", refPreServices, refCtlData);
690
691 // Set Services
692 ImportServices refServices = refImportData.getServicesRef();
693 getAndSaveServices("SERVICES", refServices, refCtlData);
694
695 // Set Post-services
696 ImportServices refPostServices = refImportData.getPostServicesRef();
697 getAndSaveServices("POSTSERVICES", refPostServices, refCtlData);
698
699 } // setServices
700
701
702 /**
703 *
704 *
705 */
706 protected void getAndSaveServices(String sService,
707 ImportServices refServices,
708 ImportCtlData refCtlData)
709 {
710 String asServices[] = refCtlData.getArraySectionData(sService);
711 for (int i=0; i<asServices.length; i++)
712 {
713 int nPos = asServices[i].indexOf("=");
714 if (nPos != -1)
715 {
716 refServices.addService(asServices[i].substring(nPos + 1).trim());
717 }
718 }
719 } // getAndSaveService
720
721 /**
722 *
723 *
724 */
725 public boolean getDisguise(int nDisguiseId)
726 {
727 try
728 {
729 refServerExtend = DisguiseLoader.getDisguise(nDisguiseId);
730 }
731
732 catch (DisguiseLoaderException e)
733 {
734 new FlexError(FlexError.CRITICAL, sThisService, IDENTIFIER, "ServerExtended transaction failed: " + e.getOriginalException().toString());
735 return false;
736 }
737
738 return true;
739 } // getExtendDataDisguise
740
741
742 /**
743 * Initialize the Server Gateway
744 */
745 public String initializeDebugGateway()
746 {
747 // initializeDebugGateway is only called in debug mode.
748 // Set bSave = true, bLoad = false to serialize the data base data
749 // to the debug file. Then set bSave = false, bLoad = true to run with
750 // the data in the debug file.
751 boolean bSave = true;
752 boolean bLoad = false;
753 String sTestDisguise = "Customer Jobs 1.15";
754 String sDebugFile = "c:\\test\\";
755
756 Diagnostic.enableOutput(true);
757 Diagnostic.setTraceLevel(10);
758 Diagnostic.addTraceCategory(Diagnostic.CAT_GATEWAY);
759 Diagnostic.addOutputDevice(System.out);
760
761 // Get the EJB server type, host, and port.
762 String sEJBType = "ORACLE";
763 String sEJBHost = "hoth.flexstor.com";
764 String sEJBPort = "2227";
765 boolean bEnableCache = Settings.getBoolean ( Settings.EJB_CONTEXT_CACHE );
766 // Initalize gateway debugging (object serialization)
767 InitGateway.enableDebugging ( sDebugFile, bSave , bLoad );
768
769 // Initalize the gateway and do a ping.
770 // if (refControlData.isGatewayInitialized() == false)
771 // {
772 try
773 {
774 new InitGateway ( sEJBType, sEJBHost, sEJBPort, bEnableCache );
775 //ObjectHolder.add ( gw );
776 // refControlData.setGatewayInitialized();
777 }
778 catch ( IllegalArgumentException e )
779 {
780 //5966=An invalid EJB Server type was specified in flexstordb.properties. Exiting FlexDBServer Application.
781 //new FlexError(FlexError.FATAL, "APPSERVER/MAIN", IDENTIFIER, 5966);
782 System.out.println( "An invalid EJB Server type was specified in flexstordb.properties. Exiting FlexDBServer Application." );
783 // System.exit(0);
784 }
785 catch ( CannotPingServerException e )
786 {
787 //5967=Cannot establish communications with the EJB Server. Exiting FlexDBServer Application.
788 //new FlexError(FlexError.FATAL, "APPSERVER/MAIN", IDENTIFIER, 5967);
789 System.out.println( "Cannot establish communications with the EJB Server. Exiting FlexDBServer Application." );
790 // System.exit(0);
791 }
792 // }
793 return sTestDisguise;
794 } // initializeDebugGateway
795
796 /**
797 * Get a tree reflecting the traversal path of each element in the traversal path of each asset
798 * in the vRecords Vector. This traversal path only includes assets; it start at the element level
799 * and works its way down to the last child asset in the structure.
800 *
801 * @param vRecords A Vector holding all assets. Their traversal path will be used to construct a
802 * list of element ids.
803 * @param nDisguiseId
804 * @return AssetTree The root element of the tree contains the disguise id, the next level contains
805 * the element bucket and from there downwards are all assets in their hierarchical structure
806 * (primary parent, child, parent, child, etc).
807 */
808 private AssetTree getAssetsTree( Vector vRecords, int nDisguiseId )
809 {
810 // To get the assets tree we have to obtain all element ids in all traversal paths of each asset
811 // Use a Set to prevent duplicate elements
812 Set sElements = new HashSet();
813 Vector vAssetTravPaths, vSingleTravPath;
814 long lElementId;
815
816 for ( Iterator i = vRecords.iterator(); i.hasNext(); )
817 {
818 // Get the traversal path(s) for this record
819 vAssetTravPaths = ((AssetRecordData)i.next()).getTraversalPathInfo();
820 if ( vAssetTravPaths == null || vAssetTravPaths.isEmpty() )
821 continue;
822
823 // Process each traversal path for this record
824 for ( Iterator j = vAssetTravPaths.iterator(); j.hasNext(); )
825 {
826 // Get the tranversal path items for this traversal path
827 vSingleTravPath = ((TraversalInfo)j.next()).getTraversalItems();
828 lElementId = ((TraversalItem)vSingleTravPath.lastElement()).getRecordId();
829 sElements.add( new Long( lElementId ) );
830 }
831 }
832
833 SearchResultGateway searchGateway = new SearchResultGateway();
834 try
835 {
836 searchGateway.connect();
837 // Convert the set of elements into an array of Long, since by compatibility issues with JDK1.1
838 // we cannot use any Collection to the EJB Server
839 Long[] laElements = new Long[ sElements.size() ];
840 sElements.toArray( laElements );
841 return searchGateway.retrieveAssetsTree(laElements, nDisguiseId);
842 }
843 catch ( TransactionFailedException tfe )
844 {
845 return new AssetTree( nDisguiseId, AssetTree.DISGUISE );
846 }
847 finally
848 {
849 searchGateway.dispose();
850 }
851 }
852
853 /**
854 * Using the asset tree, recreate the asset level structure for the specified element, using empty
855 * DisguiseAssetRecordData object.
856 *
857 * @param element The DisguiseElementRecordData to be filled with its asset level structure
858 * @param elementNode The node that holds ids for all assets to be added to the element
859 */
860 private void constructAssetLevelStructure( DisguiseElementRecordData element, AssetTree elementNode )
861 {
862 AssetTree parentAssetNode = (AssetTree) elementNode.getChildren().elementAt(0); // Elements only contain one primary asset
863 DisguiseAssetRecordData asset = new DisguiseAssetRecordData();
864 asset.setRecordId(parentAssetNode.getId());
865 int nStructId = refServerExtend.getBucketId( "ASSET" );
866 asset.setBucketStructId( nStructId );
867 asset.setAssetRoleId(parentAssetNode.getRole());
868 element.setParentAsset(asset);
869
870 // Populate primary asset with its children
871 setAssetChildren( asset, parentAssetNode, nStructId );
872 }
873
874 /**
875 * Populates asset with its children
876 * @param asset
877 * @param assetNode
878 */
879 private void setAssetChildren( DisguiseAssetRecordData asset, AssetTree assetNode, int nStructId )
880 {
881 if ( assetNode.hasChildren() )
882 {
883 Vector vChildren = assetNode.getChildren();
884 AssetTree childNode;
885 DisguiseAssetRecordData childAsset;
886 for ( Iterator i = vChildren.iterator(); i.hasNext(); )
887 {
888 childNode = (AssetTree) i.next();
889 childAsset = new DisguiseAssetRecordData();
890 childAsset.setRecordId(childNode.getId());
891 childAsset.setBucketStructId( nStructId );
892 childAsset.setAssetRoleId(childNode.getRole());
893 asset.addChildAsset(childAsset);
894 setAssetChildren( childAsset, childNode, nStructId ); // recursively set children assets for this child
895 }
896 }
897 }
898
899 /**
900 * Parse through the asset looking for the DisguiseAssetRecordData object that matches the record
901 * id passed in the argument. This method should be called after calling the
902 * <code>constructAssetLevelStructure</code> method.
903 *
904 * @param lAssetId Record id fro the asset
905 * @param asset The DisguiseAssetRecordData object to match against the lAssetId; if it doesn't match
906 * look into the asset's children
907 * @return DisguiseAssetRecordData
908 */
909 private DisguiseAssetRecordData getDisguiseAssetRecordData( long lAssetId, DisguiseAssetRecordData asset )
910 {
911 if ( asset.getRecordId() == lAssetId )
912 return asset;
913
914 if ( asset.hasChildren() )
915 {
916 DisguiseAssetRecordData[] aChildren = asset.getChildAssets();
917 DisguiseAssetRecordData child;
918 for ( int i = 0; i < aChildren.length; i++ )
919 if ( (child = getDisguiseAssetRecordData(lAssetId, aChildren[i]) ) != null )
920 return child;
921 }
922 return null;
923 }
924
925 } // ImportDataCreateService