Source code: com/flexstor/common/gateway/SearchResultGateway.java
1 /*
2 * SearchResultGateway.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:29 $ 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.gateway;
12
13 import java.util.Enumeration;
14 import java.util.Hashtable;
15
16 import com.flexstor.common.data.ActionData;
17 import com.flexstor.common.data.AssetRecordData;
18 import com.flexstor.common.data.RecordData;
19 import com.flexstor.common.data.ejb.search.AssetInstanceId;
20 import com.flexstor.common.data.ejb.search.AssetRoleDataId;
21 import com.flexstor.common.data.ejb.search.AssetRoleFields;
22 import com.flexstor.common.data.ejb.search.AssetTree;
23 import com.flexstor.common.gateway.debug.GatewayDebugOutput;
24 import com.flexstor.common.gateway.exceptions.TransactionFailedException;
25 import com.flexstor.ejb.EjbObject;
26 import com.flexstor.ejb.search.retrieval.SearchResultRetrieval;
27 import com.flexstor.ejb.search.retrieval.SearchResultRetrievalHome;
28
29 /**
30 * Retrieves additional search records.
31 * @author R. Dettmer
32 * @version 3.0
33 */
34 public class SearchResultGateway
35 extends Gateway
36 {
37 // MKS macro expander
38 public final static String IDENTIFIER = "$Id: SearchResultGateway.java,v 1.4 2003/08/11 02:22:29 aleric Exp $";
39
40 private SearchResultRetrieval bean;
41
42 protected String getHomeName ( )
43 {
44 return SEARCH_RESULT_HOME;
45 }
46
47 protected EjbObject getBeanObject ( )
48 {
49 return bean;
50 }
51
52 public void connect ( )
53 throws TransactionFailedException
54 {
55 try
56 {
57 if ( canLoadObject() )
58 return;
59
60 SearchResultRetrievalHome home = (SearchResultRetrievalHome)getHome();
61 GatewayDebugOutput.println ( 3, "Getting SearchResultRetrieval object" );
62 bean = home.create();
63 }
64 catch ( Throwable e )
65 {
66 throw buildException(e, "Connecting to SearchResultRetrieval", IDENTIFIER );
67 }
68 }
69
70 /**
71 * Retrieve additional bucket data from a bucket table
72 *
73 * @param naFieldIds the list of fields to retrieve.
74 * @param nBucketIdList a list of bucket record ids to retrieve data for.
75 * @return a Hashtable (key is bucket id as Integer, value is an array of MultiValueData)
76 * @throws TransactionFailedException Some problem occured with the EJB server
77 * @see com.flexstor.common.data.ejb.search.MultiValueData
78 */
79 public Hashtable retrieveBucketData ( int[] naFieldIds, long[] naBucketIds )
80 throws TransactionFailedException
81 {
82 return retrieveBucketData( naFieldIds, naBucketIds, true );
83 }
84
85 /**
86 * Retrieve additional bucket data from a bucket table
87 *
88 * @param naFieldIds the list of fields to retrieve.
89 * @param nBucketIdList a list of bucket record ids to retrieve data for.
90 * @param bResolveLookups If set to true it will return the value, instead of the id if the
91 * field is a lookup field
92 * @return a Hashtable (key is bucket id as Integer, value is an array of MultiValueData)
93 * @throws TransactionFailedException Some problem occured with the EJB server
94 * @see com.flexstor.common.data.ejb.search.MultiValueData
95 */
96 public Hashtable retrieveBucketData ( int[] naFieldIds, long[] naBucketIds, boolean bResolveLookups )
97 throws TransactionFailedException
98 {
99 try
100 {
101 Object key;
102 Hashtable data;
103 String sFields = arrayToString ( naFieldIds );
104 String sBuckets = arrayToString ( naBucketIds );
105
106 if ( canLoadObject() )
107 {
108 data = new Hashtable();
109 for ( int i = 0; i < naBucketIds.length; i++ )
110 data.put ( new Long(naBucketIds[i]),
111 retrieveObject ( "SearchResultRetrieval_retrieveBucketData_" + naBucketIds[i]) );
112 return data;
113 }
114
115 GatewayDebugOutput.println ( 3, "Retrieving bucket data for buckets " + sBuckets + " fields " + sFields );
116 data = bean.retrieveBucketData ( naFieldIds, naBucketIds, bResolveLookups );
117
118 if ( canSaveObject() )
119 {
120 for ( Enumeration e = data.keys(); e.hasMoreElements(); )
121 {
122 key = e.nextElement();
123 storeObject ( "SearchResultRetrieval_retrieveBucketData_" + key, data.get(key), !e.hasMoreElements() );
124 }
125 }
126
127 return data;
128 }
129 catch ( Throwable e )
130 {
131 throw buildException(e, "Retrieving Bucket Data", IDENTIFIER );
132 }
133 }
134
135 /**
136 * Retrieve an asset skeleton from the database
137 *
138 * @param naFieldIds collection of field ids
139 * @param anAssetIds collection of asset instance ids
140 * @return a Hashtable (key is AssetInstanceId(element id/asset id), value is SkeletonData)
141 * @throws TransactionFailedException Some problem occured with the EJB server
142 * @see com.flexstor.common.data.ejb.search.AssetInstanceId
143 * @see com.flexstor.common.data.ejb.search.AssetSkeletonData
144 */
145 public Hashtable retrieveAssetSkeleton ( int[] naFieldIds, AssetInstanceId[] anAssetIds )
146 throws TransactionFailedException
147 {
148 try
149 {
150 Hashtable data;
151 String sFields = arrayToString ( naFieldIds );
152 AssetInstanceId id;
153
154 if ( canLoadObject() )
155 {
156 data = new Hashtable();
157 for ( int i = 0; i < anAssetIds.length; i++ )
158 data.put ( anAssetIds[i],
159 retrieveObject ( "SearchResultRetrieval_retrieveAssetSkeleton_" + assetInstIdToString(anAssetIds[i])) );
160 return data;
161 }
162
163 String s = "Retrieving asset skeleton data for asset instance ids ";
164 for ( int i = 0; i < anAssetIds.length; i++ )
165 s += assetInstIdToString ( anAssetIds[i] );
166 GatewayDebugOutput.println ( 3, s + " fields " + sFields );
167
168 data = bean.retrieveAssetSkeleton ( naFieldIds, anAssetIds );
169
170 if ( canSaveObject() )
171 {
172 for ( Enumeration e = data.keys(); e.hasMoreElements(); )
173 {
174 id = (AssetInstanceId)e.nextElement();
175 storeObject ( "SearchResultRetrieval_retrieveAssetSkeleton_" + assetInstIdToString(id), data.get(id), !e.hasMoreElements() );
176 }
177 }
178
179 return data;
180 }
181 catch ( Throwable e )
182 {
183 throw buildException(e, "Retrieve Asset Skeleton", IDENTIFIER );
184 }
185 }
186
187 /**
188 * Retrieve the complete asset data from the database
189 *
190 * @param naFieldIds collection of field ids
191 * @param naAssetIds collection of asset ids
192 * @return a Hashtable (key is asset id as Integer, value is MultiValueData[])
193 * @throes TransactionFailedException Some problem occured with the EJB server
194 */
195 public Hashtable retrieveAssetData ( int[] naFieldIds, long[] naAssetIds )
196 throws TransactionFailedException
197 {
198 return retrieveAssetData( naFieldIds, naAssetIds, true );
199 }
200
201 /**
202 * Retrieve the complete asset data from the database
203 *
204 * @param naFieldIds collection of field ids
205 * @param naAssetIds collection of asset ids
206 * @param bResolveLookups If set to true it will return the value, instead of the id if the
207 * field is a lookup field
208 * @return a Hashtable (key is asset id as Integer, value is MultiValueData[])
209 * @throes TransactionFailedException Some problem occured with the EJB server
210 */
211 public Hashtable retrieveAssetData ( int[] naFieldIds, long[] naAssetIds, boolean bResolveLookups )
212 throws TransactionFailedException
213 {
214 try
215 {
216 Hashtable data;
217 Object key;
218 String sFields = arrayToString ( naFieldIds );
219
220 if ( canLoadObject() )
221 {
222 data = new Hashtable();
223 for ( int i = 0; i < naAssetIds.length; i++ )
224 data.put ( new Long(naAssetIds[i]),
225 retrieveObject ( "SearchResultRetrieval_retrieveAssetData_" + naAssetIds[i]) );
226
227 return data;
228 }
229
230 GatewayDebugOutput.println ( 3, "Retrieving asset data for asset ids " + arrayToString(naAssetIds) + " fields " + sFields );
231
232 data = bean.retrieveAssetData ( naFieldIds, naAssetIds, bResolveLookups );
233
234 if ( canSaveObject() )
235 {
236 for ( Enumeration e = data.keys(); e.hasMoreElements(); )
237 {
238 key = e.nextElement();
239 storeObject ( "SearchResultRetrieval_retrieveAssetData_" + key, data.get(key), e.hasMoreElements() );
240 }
241 }
242
243 return data;
244 }
245 catch ( Throwable e )
246 {
247 throw buildException(e, "Retrieve Asset Data", IDENTIFIER );
248 }
249 }
250
251 /**
252 * Retrieve the asset role data from the database
253 *
254 * @param fieldIds collection of AssetRoleFields
255 * @param assetIds collection of AssetRoleDataId
256 * @return a Hashtable
257 * @throws TransactionFailedException Some problem occured with the EJB server
258 */
259 public Hashtable retrieveAssetRoleData ( AssetRoleFields[] fieldIds, AssetRoleDataId[] assetIds )
260 throws TransactionFailedException
261 {
262 return retrieveAssetRoleData( fieldIds, assetIds, true );
263 }
264
265 /**
266 * Retrieve the asset role data from the database
267 *
268 * @param fieldIds collection of AssetRoleFields
269 * @param assetIds collection of AssetRoleDataId
270 * @param bResolveLookups If set to true it will return the value, instead of the id if the
271 * field is a lookup field
272 * @return a Hashtable
273 * @throws TransactionFailedException Some problem occured with the EJB server
274 */
275 public Hashtable retrieveAssetRoleData ( AssetRoleFields[] fieldIds, AssetRoleDataId[] assetIds, boolean bResolveLookups )
276 throws TransactionFailedException
277 {
278 try
279 {
280 long[] naRecordIds = new long[assetIds.length];
281 for (int i = 0; i < assetIds.length; i++)
282 naRecordIds[i] = assetIds[i].getAssetRecordId();
283
284 Hashtable data;
285 Object key;
286 String sRecords = arrayToString ( naRecordIds );
287
288 if ( canLoadObject() )
289 {
290 data = new Hashtable();
291 for ( int i = 0; i < naRecordIds.length; i++ )
292 data.put ( new Long(naRecordIds[i]),
293 retrieveObject ( "SearchResultRetrieval_retrieveAssetRoleData_" + naRecordIds[i]) );
294
295 return data;
296 }
297
298 GatewayDebugOutput.println ( 3, "Retrieving role data for records " + sRecords );
299 data = bean.retrieveAssetRoleData ( fieldIds, assetIds, bResolveLookups );
300
301 if ( canSaveObject() )
302 {
303 for ( Enumeration e = data.keys(); e.hasMoreElements(); )
304 {
305 key = e.nextElement();
306 storeObject ( "SearchResultRetrieval_retrieveAssetRoleData_" + key, data.get(key), !e.hasMoreElements() );
307 }
308 }
309
310 return data;
311 }
312 catch ( Throwable e )
313 {
314 throw buildException(e, "Retrieving Asset Role Data", IDENTIFIER );
315 }
316 }
317
318 /**
319 * Get the list of version ids for the given asset ids.
320 *
321 * @param naAssetIds an array of asset ids to get versions for
322 * @param bGetDisplayAsset indicates if associated display asset information is retrieved
323 * @return a Hashtable of Vectors containing AssetVersionData objects, one vector for each given asset id
324 * @throws TransactionFailedException Some problem occured with the EJB server
325 */
326 public Hashtable retrieveVersionIds ( long[] naAssetIds, boolean bGetDisplayAsset )
327 throws TransactionFailedException
328 {
329 try
330 {
331 Object key;
332 Hashtable data;
333
334 if ( canLoadObject() )
335 {
336 data = new Hashtable();
337 for ( int i = 0; i < naAssetIds.length; i++ )
338 data.put ( new Long(naAssetIds[i]),
339 retrieveObject ( "SearchResultRetrieval_retrieveVersionIds_" + naAssetIds[i] + "_" + bGetDisplayAsset) );
340 return data;
341 }
342
343 GatewayDebugOutput.println ( 3, "Retrieving asset version data for asset ids " + arrayToString(naAssetIds) );
344 data = bean.retrieveVersionIds ( naAssetIds, bGetDisplayAsset );
345
346 if ( canSaveObject() )
347 {
348 for ( Enumeration e = data.keys(); e.hasMoreElements(); )
349 {
350 key = e.nextElement();
351 storeObject ( "SearchResultRetrieval_retrieveVersionIds_" + key + "_" + bGetDisplayAsset,
352 data.get(key), !e.hasMoreElements() );
353 }
354 }
355
356 return data;
357 }
358 catch ( Throwable e )
359 {
360 throw buildException(e, "Retrieving versions ids", IDENTIFIER );
361 }
362 }
363
364 /**
365 * Gets child assets for an asset.
366 *
367 * @param record the AssetRecordData object representing an asset record (contains the
368 * asset id and the traversal path of container element)
369 * @return ActionData containing the resulting asset records
370 * @throws TransactionFailedException Some problem occured with the EJB server
371 */
372 public ActionData getChildren( AssetRecordData record )
373 throws TransactionFailedException
374 {
375 try
376 {
377 if ( canLoadObject() )
378 return (ActionData)retrieveObject (
379 "SearchResultRetrieval_getChildren_" + record.getRecordId() );
380
381 GatewayDebugOutput.println ( 3, "Retrieving asset children for " + record.getRecordId() );
382
383 ActionData data = bean.getChildren( record );
384
385 if ( canSaveObject() )
386 storeObject ( "SearchResultRetrieval_getChildren_" + record.getRecordId(), data );
387
388 return data;
389 }
390 catch ( Throwable e )
391 {
392 throw buildException(e, "Getting Children", IDENTIFIER );
393 }
394 }
395
396 /**
397 * Gets child assets of one role for an asset.
398 *
399 * @param nRole the asset role (Thumbnail, Hires, Lowres,...)
400 * @param record the RecordData object representing an asset record (contains the
401 * asset id and the traversal path of container element)
402 * @return ActionData containing the resulting asset records
403 * @throws TransactionFailedException Some problem occured with the EJB server
404 */
405 public ActionData getChildrenByRole(int nRole, RecordData record)
406 throws TransactionFailedException
407 {
408 try
409 {
410 if ( canLoadObject() )
411 return (ActionData)retrieveObject (
412 "SearchResultRetrieval_getChildrenByRole_" + record.getRecordId() + "_" + nRole );
413
414 GatewayDebugOutput.println ( 3, "Retrieving asset children by role for " + record.getRecordId() );
415
416 ActionData data = bean.getChildrenByAssetRole ( nRole, record );
417
418 if ( canSaveObject() )
419 storeObject ( "SearchResultRetrieval_getChildrenByRole_" + record.getRecordId() + "_" + nRole, data );
420
421 return data;
422 }
423 catch ( Throwable e )
424 {
425 throw buildException(e, "Getting Children by role", IDENTIFIER );
426 }
427 }
428
429
430 /**
431 * This method returns the traversal path (up to the element level) for every
432 * asset in the argument. This traversal path do not contain bucket
433 * information, just element and parent asset (if any).
434 * Transaction Attribute: Required
435 *
436 * @param laElementIds An array of Long objects holding the element ids
437 * @param nDisguiseId Id of disguise where these assets belong
438 * @return AssetTree The root element of the tree contains the disguise id,
439 * the next level contains the element bucket and from there downwards are
440 * all assets in their hierarchical structure (primary parent, child, parent,
441 * child, etc).
442 *
443 * @throws TransactionFailedException Some problem occured with the EJB server
444 */
445 public AssetTree retrieveAssetsTree(Long[] laElementIds, int nDisguiseId)
446 throws TransactionFailedException
447 {
448 try
449 {
450 if ( canLoadObject() )
451 return (AssetTree)retrieveObject ( "SearchResultRetrieval_retrieveAssetsTree" );
452
453 GatewayDebugOutput.println ( 3, "Retrieving assets traversal path" );
454
455 AssetTree tree = bean.retrieveAssetsTree ( laElementIds, nDisguiseId);
456
457 if ( canSaveObject() )
458 storeObject ( "SearchResultRetrieval_retrieveAssetsTree", tree );
459
460 return tree;
461 }
462 catch ( Throwable e )
463 {
464 throw buildException(e, "Getting traversal path for assets", IDENTIFIER );
465 }
466 }
467
468
469 /**
470 * Convert an int array to a string.
471 */
472 private String arrayToString ( int[] l )
473 {
474 String s = "";
475 for ( int i = 0; i < l.length; i++ )
476 {
477 if ( i > 0 )
478 s += ",";
479 s += l[i];
480 }
481 return s;
482 }
483
484 /**
485 * Convert a long array to a string.
486 */
487 private String arrayToString ( long[] l )
488 {
489 String s = "";
490 for ( int i = 0; i < l.length; i++ )
491 {
492 if ( i > 0 )
493 s += ",";
494 s += l[i];
495 }
496 return s;
497 }
498
499
500 /**
501 * Convert a AssetInstanceId to a string.
502 */
503 private String assetInstIdToString ( AssetInstanceId id )
504 {
505 return "[" +id.getElementId() + "," + id.getAssetId() + "]";
506 }
507 } // end of class