1 | package gov.hhs.fha.nhinc.gateway.aggregator.document;
|
---|
2 |
|
---|
3 | import java.util.Date;
|
---|
4 | import java.util.HashSet;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.HashMap;
|
---|
8 |
|
---|
9 | import org.apache.commons.logging.Log;
|
---|
10 | import org.apache.commons.logging.LogFactory;
|
---|
11 |
|
---|
12 | import gov.hhs.fha.nhinc.common.nhinccommon.QualifiedSubjectIdentifierType;
|
---|
13 |
|
---|
14 | import gov.hhs.fha.nhinc.gateway.aggregator.AggregatorException;
|
---|
15 | import gov.hhs.fha.nhinc.gateway.aggregator.GetAggResultsDocQueryRequestType;
|
---|
16 | import gov.hhs.fha.nhinc.gateway.aggregator.GetAggResultsDocQueryResponseType;
|
---|
17 | import gov.hhs.fha.nhinc.gateway.aggregator.model.DocQueryMessageKey;
|
---|
18 | import gov.hhs.fha.nhinc.gateway.aggregator.model.AggTransaction;
|
---|
19 | import gov.hhs.fha.nhinc.gateway.aggregator.model.AggMessageResult;
|
---|
20 |
|
---|
21 | import gov.hhs.fha.nhinc.gateway.aggregator.dao.AggTransactionDao;
|
---|
22 |
|
---|
23 | import gov.hhs.fha.nhinc.gateway.aggregator.StartTransactionDocQueryRequestType;
|
---|
24 | import gov.hhs.fha.nhinc.gateway.aggregator.SetResponseMsgDocQueryRequestType;
|
---|
25 | import gov.hhs.fha.nhinc.gateway.aggregator.dao.AggMessageResultDao;
|
---|
26 | import gov.hhs.fha.nhinc.gateway.aggregator.persistence.GarbageCollectorMgr;
|
---|
27 | import java.io.StringReader;
|
---|
28 | import java.io.StringWriter;
|
---|
29 | import java.util.Set;
|
---|
30 | import oasis.names.tc.ebxml_regrep.xsd.query._3.AdhocQueryResponse;
|
---|
31 |
|
---|
32 | import javax.xml.bind.JAXBContext;
|
---|
33 | import javax.xml.bind.JAXBElement;
|
---|
34 | import javax.xml.bind.Marshaller;
|
---|
35 | import javax.xml.bind.Unmarshaller;
|
---|
36 | import oasis.names.tc.ebxml_regrep.xsd.rim._3.IdentifiableType;
|
---|
37 | import oasis.names.tc.ebxml_regrep.xsd.rim._3.RegistryObjectListType;
|
---|
38 | import oasis.names.tc.ebxml_regrep.xsd.rs._3.RegistryError;
|
---|
39 | import oasis.names.tc.ebxml_regrep.xsd.rs._3.RegistryErrorList;
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * This class is used to handle the tasks surrounding aggregating
|
---|
44 | * responses to document query messages.
|
---|
45 | *
|
---|
46 | * @author Les Westberg
|
---|
47 | */
|
---|
48 | public class DocQueryAggregator
|
---|
49 | {
|
---|
50 | private static Log log = LogFactory.getLog(DocQueryAggregator.class);
|
---|
51 | private static String DOC_QUERY_NAME = "documentquery";
|
---|
52 | private static String DOC_QUERY_RESPONSE = "AdhocQueryResponse";
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Default constructor
|
---|
56 | */
|
---|
57 | public DocQueryAggregator()
|
---|
58 | {
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * This method marshalls the AdhocQueryResponse into an XML string.
|
---|
63 | *
|
---|
64 | * @param oAdhocQueryRsponse The object to be marshalled.
|
---|
65 | * @return The XML representation of the object.
|
---|
66 | */
|
---|
67 | public String marshalAdhocQueryResponse(AdhocQueryResponse oAdhocQueryResponse)
|
---|
68 | {
|
---|
69 | String sXML = "";
|
---|
70 |
|
---|
71 | if (oAdhocQueryResponse != null)
|
---|
72 | {
|
---|
73 | try
|
---|
74 | {
|
---|
75 | JAXBContext jc = JAXBContext.newInstance("oasis.names.tc.ebxml_regrep.xsd.query._3");
|
---|
76 | Marshaller marshaller = jc.createMarshaller();
|
---|
77 | StringWriter swXML = new StringWriter();
|
---|
78 | marshaller.marshal(oAdhocQueryResponse, swXML);
|
---|
79 | sXML = swXML.toString();
|
---|
80 | }
|
---|
81 | catch (Exception e)
|
---|
82 | {
|
---|
83 | log.error("Failed to marshall AdhocQueryResponse to XML: " + e.getMessage());
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | return sXML;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * This method unmarshalls the XML into an AdhocQueryResponse object.
|
---|
92 | *
|
---|
93 | * @param sAdhocQueryResponseXML The XML of the AdhocQueryResponse object to be unmarshalled.
|
---|
94 | * @return The AdhocQueryResponse object.
|
---|
95 | */
|
---|
96 | public AdhocQueryResponse unmarshalAdhocQueryResponse(String sAdhocQueryResponseXML)
|
---|
97 | {
|
---|
98 | AdhocQueryResponse oAdhocQueryResponse = null;
|
---|
99 |
|
---|
100 | if (sAdhocQueryResponseXML != null)
|
---|
101 | {
|
---|
102 | try
|
---|
103 | {
|
---|
104 | JAXBContext jc = JAXBContext.newInstance("oasis.names.tc.ebxml_regrep.xsd.query._3");
|
---|
105 | Unmarshaller unmarshaller = jc.createUnmarshaller();
|
---|
106 | StringReader srAdhocQueryResponseXML = new StringReader(sAdhocQueryResponseXML);
|
---|
107 | oAdhocQueryResponse = (AdhocQueryResponse)unmarshaller.unmarshal(srAdhocQueryResponseXML);
|
---|
108 | }
|
---|
109 | catch (Exception e)
|
---|
110 | {
|
---|
111 | log.error("Failed to marshall AdhocQueryResponse to XML: " + e.getMessage());
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | return oAdhocQueryResponse;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * This method is called from the web service to start a transaction. it
|
---|
120 | * prepares the set of DocQueryMessageKey(s) and then calls the
|
---|
121 | * other startTransaction to do the work.
|
---|
122 | *
|
---|
123 | * @param oRequest The message that was sent to the web service.
|
---|
124 | * @return The transaction Id.
|
---|
125 | */
|
---|
126 | public String startTransaction (StartTransactionDocQueryRequestType oRequest,
|
---|
127 | HashMap<String,String> hAssignAuthHomeCommMap)
|
---|
128 | {
|
---|
129 | String sTransactionId = "";
|
---|
130 |
|
---|
131 | // Based on property settings, spin off the garbage collector thread
|
---|
132 | //-------------------------------------------------------------------
|
---|
133 | GarbageCollectorMgr.runGarbageCollection();
|
---|
134 |
|
---|
135 | if ((oRequest != null) &&
|
---|
136 | (oRequest.getQualifiedPatientIdentifiers() != null) &&
|
---|
137 | (oRequest.getQualifiedPatientIdentifiers().getQualifiedSubjectIdentifier() != null) &&
|
---|
138 | (oRequest.getQualifiedPatientIdentifiers().getQualifiedSubjectIdentifier().size() > 0))
|
---|
139 | {
|
---|
140 | ArrayList<DocQueryMessageKey> olKey = new ArrayList<DocQueryMessageKey>();
|
---|
141 | List<QualifiedSubjectIdentifierType> olIds = oRequest.getQualifiedPatientIdentifiers().getQualifiedSubjectIdentifier();
|
---|
142 |
|
---|
143 | for (QualifiedSubjectIdentifierType oId : olIds)
|
---|
144 | {
|
---|
145 | boolean baFound[] = {false, false, false};
|
---|
146 |
|
---|
147 | DocQueryMessageKey oKey = new DocQueryMessageKey();
|
---|
148 | if (oId.getAssigningAuthorityIdentifier() != null)
|
---|
149 | {
|
---|
150 | oKey.setAssigningAuthority(oId.getAssigningAuthorityIdentifier());
|
---|
151 | baFound[0] = true;
|
---|
152 | }
|
---|
153 | else
|
---|
154 | {
|
---|
155 | oKey.setAssigningAuthority("");
|
---|
156 | }
|
---|
157 |
|
---|
158 | if ((oKey.getAssigningAuthority() != null) &&
|
---|
159 | (oKey.getAssigningAuthority().length() > 0))
|
---|
160 | {
|
---|
161 | if (hAssignAuthHomeCommMap.containsKey(oKey.getAssigningAuthority()))
|
---|
162 | {
|
---|
163 | String sHomeCommunityId = hAssignAuthHomeCommMap.get(oKey.getAssigningAuthority());
|
---|
164 | if ((sHomeCommunityId != null) &&
|
---|
165 | (sHomeCommunityId.length() > 0))
|
---|
166 | {
|
---|
167 | oKey.setHomeCommunityId(sHomeCommunityId);
|
---|
168 | }
|
---|
169 | else
|
---|
170 | {
|
---|
171 | log.warn("There was a mapping for this assigning authority to a home community, " +
|
---|
172 | "but the mapping was to an empty string. " +
|
---|
173 | "The home community ID will be treated as the same as the assigning authority.");
|
---|
174 | oKey.setHomeCommunityId(""); // Assume it is the same as the
|
---|
175 | }
|
---|
176 | }
|
---|
177 | else
|
---|
178 | {
|
---|
179 | log.warn("There was no mapping for this assigning authority to a home community. " +
|
---|
180 | "The home community ID will be treated as the same as the assigning authority.");
|
---|
181 | oKey.setHomeCommunityId(""); // Assume it is the same as the
|
---|
182 | }
|
---|
183 | baFound[1] = true;
|
---|
184 | }
|
---|
185 | else
|
---|
186 | {
|
---|
187 | log.warn("The assigning authority was either null or empty. " +
|
---|
188 | "No mapping to home community ID can be done.");
|
---|
189 | oKey.setHomeCommunityId(""); // Assume it is the same as the
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (oId.getSubjectIdentifier() != null)
|
---|
193 | {
|
---|
194 | oKey.setPatientId(oId.getSubjectIdentifier());
|
---|
195 | baFound[2] = true;
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | oKey.setPatientId("");
|
---|
200 | }
|
---|
201 |
|
---|
202 | // We can only build keys when we have all the appropriate identifiers
|
---|
203 | //--------------------------------------------------------------------
|
---|
204 | if (baFound[0] && baFound[1] && baFound[2])
|
---|
205 | {
|
---|
206 | olKey.add(oKey);
|
---|
207 | }
|
---|
208 | } // for (QualifiedSubjectIdentifierType oId : olIds)
|
---|
209 |
|
---|
210 | if (olKey.size() > 0)
|
---|
211 | {
|
---|
212 | DocQueryMessageKey oaKey[] = olKey.toArray(new DocQueryMessageKey[0]);
|
---|
213 | sTransactionId = startTransaction(oaKey);
|
---|
214 | }
|
---|
215 | } // if ((oRequest != null) && ...
|
---|
216 |
|
---|
217 | return sTransactionId;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * This method sets the response message for the specified message key.
|
---|
222 | *
|
---|
223 | * @param oRequest The message key and the response message.
|
---|
224 | * @return The status of the request, either "SUCCESS" or "FAIL".
|
---|
225 | */
|
---|
226 | public String setResponseMsg(SetResponseMsgDocQueryRequestType oRequest)
|
---|
227 | {
|
---|
228 | String sStatus = "";
|
---|
229 | String sTransactionId = "";
|
---|
230 | String sHomeCommunityId = "";
|
---|
231 | DocQueryMessageKey oKey = new DocQueryMessageKey();
|
---|
232 | String sAdhocQueryResponseXML = "";
|
---|
233 |
|
---|
234 | if (oRequest != null)
|
---|
235 | {
|
---|
236 | // Transaction Id
|
---|
237 | //----------------
|
---|
238 | if ((oRequest.getTransactionId() != null) &&
|
---|
239 | (oRequest.getTransactionId().length() > 0))
|
---|
240 | {
|
---|
241 | sTransactionId = oRequest.getTransactionId();
|
---|
242 | }
|
---|
243 | else
|
---|
244 | {
|
---|
245 | String sErrorMessage = "DocQuery Aggregator - setResponseMsg called with no Transaction Id - this is a required data element.";
|
---|
246 | log.error(sErrorMessage);
|
---|
247 | sStatus = DocumentConstants.FAIL_TEXT;
|
---|
248 | }
|
---|
249 |
|
---|
250 | // HomeCommunityId
|
---|
251 | //-----------------
|
---|
252 | // If the home community is not passed, it will be looked up by the DocQueryMessageKey based
|
---|
253 | // on assigning authority.
|
---|
254 | //-------------------------------------------------------------------------------------------
|
---|
255 | if ((oRequest.getHomeCommunityId() != null) &&
|
---|
256 | (oRequest.getHomeCommunityId().length() > 0))
|
---|
257 | {
|
---|
258 | sHomeCommunityId = oRequest.getHomeCommunityId();
|
---|
259 | }
|
---|
260 | oKey.setHomeCommunityId(sHomeCommunityId);
|
---|
261 |
|
---|
262 | // Assigning Authority
|
---|
263 | //--------------------
|
---|
264 | if ((oRequest.getQualifiedPatientIdentifier() != null) &&
|
---|
265 | (oRequest.getQualifiedPatientIdentifier().getAssigningAuthorityIdentifier() != null) &&
|
---|
266 | (oRequest.getQualifiedPatientIdentifier().getAssigningAuthorityIdentifier().length() > 0))
|
---|
267 | {
|
---|
268 | oKey.setAssigningAuthority(oRequest.getQualifiedPatientIdentifier().getAssigningAuthorityIdentifier());
|
---|
269 | }
|
---|
270 | else
|
---|
271 | {
|
---|
272 | String sErrorMessage = "DocQuery Aggregator - setResponseMsg called with no assigning authority - this is a required data element.";
|
---|
273 | log.error(sErrorMessage);
|
---|
274 | sStatus = DocumentConstants.FAIL_TEXT;
|
---|
275 | }
|
---|
276 |
|
---|
277 | //Patient Id
|
---|
278 | //-----------
|
---|
279 | if ((oRequest.getQualifiedPatientIdentifier() != null) &&
|
---|
280 | (oRequest.getQualifiedPatientIdentifier().getSubjectIdentifier() != null) &&
|
---|
281 | (oRequest.getQualifiedPatientIdentifier().getSubjectIdentifier().length() > 0))
|
---|
282 | {
|
---|
283 | oKey.setPatientId(oRequest.getQualifiedPatientIdentifier().getSubjectIdentifier());
|
---|
284 | }
|
---|
285 | else
|
---|
286 | {
|
---|
287 | String sErrorMessage = "DocQuery Aggregator - setResponseMsg called with no subject identifier - this is a required data element.";
|
---|
288 | log.error(sErrorMessage);
|
---|
289 | sStatus = DocumentConstants.FAIL_TEXT;
|
---|
290 | }
|
---|
291 |
|
---|
292 | // Marshall the AdhocQueryResponse
|
---|
293 | //---------------------------------
|
---|
294 | if (oRequest.getAdhocQueryResponse() != null)
|
---|
295 | {
|
---|
296 | sAdhocQueryResponseXML = marshalAdhocQueryResponse(oRequest.getAdhocQueryResponse());
|
---|
297 | }
|
---|
298 |
|
---|
299 | if (!sStatus.equals(DocumentConstants.FAIL_TEXT))
|
---|
300 | {
|
---|
301 | sStatus = setResponseMsg(sTransactionId, oKey, sAdhocQueryResponseXML);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | else
|
---|
305 | {
|
---|
306 | sStatus = DocumentConstants.FAIL_TEXT;
|
---|
307 | }
|
---|
308 |
|
---|
309 | return sStatus;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * This method returns either a status if it is still waiting for results
|
---|
314 | * to come in, or the set of aggregated results. If the caller passes in
|
---|
315 | * false for the "timed out" parameter, it will only return the results
|
---|
316 | * when all of the expected responses have been recieved. If they have not
|
---|
317 | * all been received, then it will return a status of "Pending" with no
|
---|
318 | * reesults. When all are received, it will send a status of "Complete"
|
---|
319 | * along with the aggregated results. If timedOut is set to true, then
|
---|
320 | * it will pass back the set of aggregated results that was received and
|
---|
321 | * it will place error information in the aggregated results for the ones
|
---|
322 | * that it did not receive. It will also send back a status of "Incomplete"
|
---|
323 | * with the results. If timedOut is set to true, but everything had been
|
---|
324 | * received, then it will send back a status of "Complete" with the
|
---|
325 | * aggregated results.
|
---|
326 | *
|
---|
327 | * @param getAggResultsDocQueryRequest Tells whether we are in a timed out
|
---|
328 | * state or not.
|
---|
329 | * @return Returns results if all responses have been received or if
|
---|
330 | * timedOut is set to true. Returns status only if we are not
|
---|
331 | * timedOut and if not all expected results have been received.
|
---|
332 | */
|
---|
333 | public GetAggResultsDocQueryResponseType getAggResults(GetAggResultsDocQueryRequestType oRequest)
|
---|
334 | {
|
---|
335 | GetAggResultsDocQueryResponseType oResponse = new GetAggResultsDocQueryResponseType();
|
---|
336 |
|
---|
337 | String sTransactionId = "";
|
---|
338 | boolean bTimedOut = false;
|
---|
339 |
|
---|
340 | if ((oRequest != null) &&
|
---|
341 | (oRequest.getTransactionId() != null) &&
|
---|
342 | (oRequest.getTransactionId().trim().length() > 0))
|
---|
343 | {
|
---|
344 | sTransactionId = oRequest.getTransactionId();
|
---|
345 | }
|
---|
346 | else
|
---|
347 | {
|
---|
348 | String sErrorMessage = "";
|
---|
349 | log.error(sErrorMessage);
|
---|
350 | oResponse.setStatus(DocumentConstants.FAIL_TEXT);
|
---|
351 | return oResponse;
|
---|
352 | }
|
---|
353 |
|
---|
354 | bTimedOut = oRequest.isTimedOut();
|
---|
355 | AdhocQueryResponse oAdhocQueryResponse = null;
|
---|
356 |
|
---|
357 | try
|
---|
358 | {
|
---|
359 | oAdhocQueryResponse = getAggResults(sTransactionId, bTimedOut);
|
---|
360 | if (oAdhocQueryResponse == null)
|
---|
361 | {
|
---|
362 | oResponse.setStatus(DocumentConstants.PENDING_TEXT);
|
---|
363 | oResponse.setAdhocQueryResponse(null);
|
---|
364 | }
|
---|
365 | else
|
---|
366 | {
|
---|
367 | oResponse.setStatus(DocumentConstants.COMPLETE_TEXT);
|
---|
368 | oResponse.setAdhocQueryResponse(oAdhocQueryResponse);
|
---|
369 | }
|
---|
370 | }
|
---|
371 | catch (Exception e)
|
---|
372 | {
|
---|
373 | String sErrorMessage = "Failed to retrieve transaction. Message: " + e.getMessage();
|
---|
374 | log.error(sErrorMessage, e);
|
---|
375 | oResponse.setStatus(DocumentConstants.FAIL_TEXT);
|
---|
376 | oResponse.setAdhocQueryResponse(null);
|
---|
377 | }
|
---|
378 |
|
---|
379 | return oResponse;
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * This method starts a transaction using the set of message keys passed in.
|
---|
385 | * It will create a transaction, with one message for each message key. The
|
---|
386 | * entries will be written to the AGGREGATOR.AGG_TRANSACTION and
|
---|
387 | * AGGREGATOR.AGG_MESSAGE_RESULTS tables.
|
---|
388 | *
|
---|
389 | * @param oaMessageKey The set of message keys. There will be one row
|
---|
390 | * written to the AGG_MESSAGE_RESULTS table for each
|
---|
391 | * array item.
|
---|
392 | * @return The transaction Id that was assigned when this transaction was
|
---|
393 | * started.
|
---|
394 | */
|
---|
395 | public String startTransaction (DocQueryMessageKey[] oaMessageKey)
|
---|
396 | {
|
---|
397 | String sTransactionId = "";
|
---|
398 |
|
---|
399 | if ((oaMessageKey == null) ||
|
---|
400 | (oaMessageKey.length <= 0))
|
---|
401 | {
|
---|
402 | return sTransactionId; // Nothing in the record to start
|
---|
403 | // a transaction for.
|
---|
404 | }
|
---|
405 |
|
---|
406 | Date dtNow = new Date();
|
---|
407 |
|
---|
408 | AggTransaction oTrans = new AggTransaction();
|
---|
409 | oTrans.setServiceType(DOC_QUERY_NAME);
|
---|
410 | oTrans.setTransactionStartTime(dtNow);
|
---|
411 |
|
---|
412 | HashSet<AggMessageResult> hMsgResult = new HashSet<AggMessageResult>();
|
---|
413 |
|
---|
414 | for (DocQueryMessageKey oMessageKey : oaMessageKey)
|
---|
415 | {
|
---|
416 | AggMessageResult oMsgResult = new AggMessageResult();
|
---|
417 | oMsgResult.setAggTransaction(oTrans);
|
---|
418 | oMsgResult.setMessageKey(oMessageKey.createXMLMessageKey());
|
---|
419 | oMsgResult.setMessageOutTime(dtNow);
|
---|
420 | oMsgResult.setResponseMessageType(DOC_QUERY_RESPONSE);
|
---|
421 | oMsgResult.setResponseMessage("");
|
---|
422 | oMsgResult.setResponseReceivedTime(null);
|
---|
423 | hMsgResult.add(oMsgResult);
|
---|
424 | }
|
---|
425 | oTrans.setAggMessageResults(hMsgResult);
|
---|
426 |
|
---|
427 | AggTransactionDao oTransDao = new AggTransactionDao();
|
---|
428 | oTransDao.save(oTrans);
|
---|
429 |
|
---|
430 | if ((oTrans != null) &&
|
---|
431 | (oTrans.getTransactionId() != null) &&
|
---|
432 | (oTrans.getTransactionId().length() > 0))
|
---|
433 | {
|
---|
434 | sTransactionId = oTrans.getTransactionId();
|
---|
435 | }
|
---|
436 |
|
---|
437 | return sTransactionId;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * This method retrieves the message result entry in the database and fills in the
|
---|
442 | * response information for that message. It locates the entry based on the
|
---|
443 | * message key and transaction Id based on the information that was passed in.
|
---|
444 | *
|
---|
445 | * @param sTransactionId The transaction Id associated with the message.
|
---|
446 | * @param oKey The information that is used for the message key.
|
---|
447 | * @param sAdhocQueryResponseXML The AdhocQueryResponse in XML form.
|
---|
448 | * @return The status. "SUCCESS" or "FAIL".
|
---|
449 | */
|
---|
450 | public String setResponseMsg(String sTransactionId, DocQueryMessageKey oKey, String sAdhocQueryResponseXML)
|
---|
451 | {
|
---|
452 | String sStatus = DocumentConstants.SUCCESS_TEXT;
|
---|
453 | String sMessageKey = oKey.createXMLMessageKey();
|
---|
454 |
|
---|
455 | AggMessageResultDao oAggMessageResultDao = new AggMessageResultDao();
|
---|
456 | AggMessageResult oMsgResult = null;
|
---|
457 | try
|
---|
458 | {
|
---|
459 | oMsgResult = oAggMessageResultDao.findByMessageKey(sTransactionId, sMessageKey);
|
---|
460 | }
|
---|
461 | catch (Exception e)
|
---|
462 | {
|
---|
463 | String sErrorMessage = "Failed to retrieve AggMessageResult for: TransactionId: " + sTransactionId +
|
---|
464 | ", MessageKey: " + oKey.createXMLMessageKey() + ". Message: " + e.getMessage();
|
---|
465 | log.error(sErrorMessage, e);
|
---|
466 | sStatus = DocumentConstants.FAIL_TEXT;
|
---|
467 | return sStatus; // No reason to proceed....
|
---|
468 | }
|
---|
469 |
|
---|
470 | if (oMsgResult == null)
|
---|
471 | {
|
---|
472 | String sErrorMessage = "Failed to find existing AggMessageResult for: TransactionId: " + sTransactionId +
|
---|
473 | ", MessageKey: " + oKey.createXMLMessageKey() + ". Message response not recorded.";
|
---|
474 | log.error(sErrorMessage);
|
---|
475 | sStatus = DocumentConstants.FAIL_TEXT;
|
---|
476 | return sStatus; // No reason to proceed....
|
---|
477 | }
|
---|
478 |
|
---|
479 | // if we got here - we have the message and we need to fill in the response information...
|
---|
480 | //----------------------------------------------------------------------------------------
|
---|
481 | oMsgResult.setResponseReceivedTime(new Date());
|
---|
482 | oMsgResult.setResponseMessage(sAdhocQueryResponseXML);
|
---|
483 | oAggMessageResultDao.save(oMsgResult);
|
---|
484 |
|
---|
485 | return DocumentConstants.SUCCESS_TEXT;
|
---|
486 | }
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * This method looks through the results to see if all responses have been
|
---|
490 | * received. If they have, then it returns true. Otherwise it returns
|
---|
491 | * false.
|
---|
492 | *
|
---|
493 | * @param oTrans The Transaction with all of the message results.
|
---|
494 | * @return TRUE if all responses have been received, FALSE if not.
|
---|
495 | */
|
---|
496 | private boolean areResultsReady(AggTransaction oTrans)
|
---|
497 | {
|
---|
498 | if (oTrans == null)
|
---|
499 | {
|
---|
500 | return false;
|
---|
501 | }
|
---|
502 |
|
---|
503 | for (AggMessageResult oMsgResult : oTrans.getAggMessageResults())
|
---|
504 | {
|
---|
505 | // If we fall through - we are still waiting for results.
|
---|
506 | //-------------------------------------------------------
|
---|
507 | if ((oMsgResult != null) &&
|
---|
508 | (oMsgResult.getResponseReceivedTime() == null))
|
---|
509 | {
|
---|
510 | return false;
|
---|
511 | }
|
---|
512 | }
|
---|
513 |
|
---|
514 |
|
---|
515 | return true;
|
---|
516 | }
|
---|
517 |
|
---|
518 | /**
|
---|
519 | * This method will create an empty AdhocQueryResponse. One that would
|
---|
520 | * be returned if there were no results.
|
---|
521 | *
|
---|
522 | * @return An AdhocQueryResponse that represents an empty result set.
|
---|
523 | */
|
---|
524 | private AdhocQueryResponse createEmptyResult()
|
---|
525 | {
|
---|
526 | AdhocQueryResponse oAdhocQueryResponse = new AdhocQueryResponse();
|
---|
527 |
|
---|
528 | oAdhocQueryResponse.setStatus(DocumentConstants.XDS_QUERY_RESPONSE_STATUS_SUCCESS);
|
---|
529 | oAdhocQueryResponse.setRegistryObjectList(new RegistryObjectListType());
|
---|
530 |
|
---|
531 | return oAdhocQueryResponse;
|
---|
532 | }
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * This method is used to combine the results together into a single
|
---|
536 | * AdhocQueryResponse. It will extract the items in the RegistryObjectList
|
---|
537 | * from the various systems and place them into the new response.
|
---|
538 | *
|
---|
539 | * @param olMsgResult List of messages to be combined
|
---|
540 | * @return The combined AdhocQueryResponse object.
|
---|
541 | */
|
---|
542 | private AdhocQueryResponse combineResults(Set<AggMessageResult> olMsgResult)
|
---|
543 | {
|
---|
544 | AdhocQueryResponse oAdhocQueryResponse = createEmptyResult();
|
---|
545 | RegistryObjectListType oRegObjList = oAdhocQueryResponse.getRegistryObjectList();
|
---|
546 |
|
---|
547 | for (AggMessageResult oMsgResult : olMsgResult)
|
---|
548 | {
|
---|
549 | // If the message contains a response received time, then it means that we
|
---|
550 | // received the response from the remote system. We need to aggregate the
|
---|
551 | // results.
|
---|
552 | //--------------------------------------------------------------------------
|
---|
553 | if (oMsgResult.getResponseReceivedTime() != null)
|
---|
554 | {
|
---|
555 | // The response may have been empty - if so - there is nothing to aggregate from here.
|
---|
556 | //-------------------------------------------------------------------------------------
|
---|
557 | if ((oMsgResult.getResponseMessage() != null) &&
|
---|
558 | (oMsgResult.getResponseMessage().trim().length() > 0))
|
---|
559 | {
|
---|
560 | AdhocQueryResponse oTempResponse = unmarshalAdhocQueryResponse(oMsgResult.getResponseMessage());
|
---|
561 | if ((oTempResponse != null) &&
|
---|
562 | (oTempResponse.getRegistryObjectList() != null) &&
|
---|
563 | (oTempResponse.getRegistryObjectList().getIdentifiable() != null) &&
|
---|
564 | (oTempResponse.getRegistryObjectList().getIdentifiable().size() > 0))
|
---|
565 | {
|
---|
566 | List<JAXBElement<? extends IdentifiableType>> olNewRegObjs = oTempResponse.getRegistryObjectList().getIdentifiable();
|
---|
567 |
|
---|
568 | for (JAXBElement<? extends IdentifiableType> oJAXBElement : olNewRegObjs)
|
---|
569 | {
|
---|
570 | oRegObjList.getIdentifiable().add(oJAXBElement);
|
---|
571 | } // for (JAXBElement<? extends IdentifiableType> oJAXBElement : olNewRegObjs)
|
---|
572 | } // if ((oTempResponse != null) &&
|
---|
573 |
|
---|
574 | // It is possible that there may be error information in this message
|
---|
575 | // that we need to pull out too...
|
---|
576 | //--------------------------------------------------------------------
|
---|
577 | if ((oTempResponse != null) &&
|
---|
578 | (oTempResponse.getRegistryErrorList() != null) &&
|
---|
579 | (oTempResponse.getRegistryErrorList().getRegistryError() != null) &&
|
---|
580 | (oTempResponse.getRegistryErrorList().getRegistryError().size() > 0))
|
---|
581 | {
|
---|
582 | RegistryErrorList oRegErrors = null;
|
---|
583 | if (oAdhocQueryResponse.getRegistryErrorList() == null)
|
---|
584 | {
|
---|
585 | oRegErrors = new RegistryErrorList();
|
---|
586 | oAdhocQueryResponse.setRegistryErrorList(oRegErrors);
|
---|
587 | }
|
---|
588 | else
|
---|
589 | {
|
---|
590 | oRegErrors = oAdhocQueryResponse.getRegistryErrorList();
|
---|
591 | }
|
---|
592 |
|
---|
593 | List<RegistryError> olRegError = oRegErrors.getRegistryError();
|
---|
594 | for (RegistryError oRegError : oTempResponse.getRegistryErrorList().getRegistryError())
|
---|
595 | {
|
---|
596 | olRegError.add(oRegError);
|
---|
597 | }
|
---|
598 | }
|
---|
599 | } // if ((oMsgResult.getResponseMessage() != null) &&
|
---|
600 | } // if (oMsgResult.getResponseReceivedTime() != null)
|
---|
601 | else // This means that this result never received a response - log an error that this one timed out.
|
---|
602 | {
|
---|
603 | RegistryErrorList oRegErrors = null;
|
---|
604 | if (oAdhocQueryResponse.getRegistryErrorList() == null)
|
---|
605 | {
|
---|
606 | oRegErrors = new RegistryErrorList();
|
---|
607 | oAdhocQueryResponse.setRegistryErrorList(oRegErrors);
|
---|
608 | }
|
---|
609 | else
|
---|
610 | {
|
---|
611 | oRegErrors = oAdhocQueryResponse.getRegistryErrorList();
|
---|
612 | }
|
---|
613 |
|
---|
614 | List<RegistryError> olRegError = oRegErrors.getRegistryError();
|
---|
615 | RegistryError oRegError = new RegistryError();
|
---|
616 | olRegError.add(oRegError);
|
---|
617 | oRegError.setErrorCode(DocumentConstants.XDS_RETRIEVE_ERRORCODE_REGISTRY_ERROR);
|
---|
618 | oRegError.setCodeContext(DocumentConstants.XDS_RETRIEVE_CODECONTEXT_TIMEDOUT_MSG);
|
---|
619 | if ((oMsgResult.getMessageKey() != null) &&
|
---|
620 | (oMsgResult.getMessageKey().length() > 0))
|
---|
621 | {
|
---|
622 | try
|
---|
623 | {
|
---|
624 | DocQueryMessageKey oMessageKey = new DocQueryMessageKey(oMsgResult.getMessageKey());
|
---|
625 | oRegError.setLocation(oMessageKey.getHomeCommunityId());
|
---|
626 | }
|
---|
627 | catch (Exception e)
|
---|
628 | {
|
---|
629 | String sErrorMessage = "Failed to parse message key. Message = " + e.getMessage();
|
---|
630 | log.error(sErrorMessage, e);
|
---|
631 | // do not throw an error - we will log it and move on...
|
---|
632 | }
|
---|
633 | } // if ((oMsgResult.getMessageKey() != null) &&
|
---|
634 | } // else ...
|
---|
635 | } // for (AggMessageResult oMsgResult : olMsgResult)
|
---|
636 |
|
---|
637 |
|
---|
638 | return oAdhocQueryResponse;
|
---|
639 | }
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * This method returns either an aggregated AdhocQueryResponse if all results
|
---|
643 | * have been retrieved, or if the timed out flag has been passed, or it will
|
---|
644 | * return null if the results are not ready and the timedout flag has not
|
---|
645 | * been set.
|
---|
646 | *
|
---|
647 | * @param sTransactionId The transaction ID of the transaction to be aggregated.
|
---|
648 | * @param bTimedOut TRUE if we should stop waiting for results and compile what
|
---|
649 | * is available. FALSE if we should only return them
|
---|
650 | * if all expected results have been received.
|
---|
651 | * @return The aggregated results.
|
---|
652 | */
|
---|
653 | public AdhocQueryResponse getAggResults(String sTransactionId, boolean bTimedOut)
|
---|
654 | throws AggregatorException
|
---|
655 | {
|
---|
656 | AdhocQueryResponse oResponse = null;
|
---|
657 |
|
---|
658 | // Retrieve the records and see if everything is ready...
|
---|
659 | //--------------------------------------------------------
|
---|
660 | AggTransactionDao oAggTransactionDao = new AggTransactionDao();
|
---|
661 | AggTransaction oTrans = oAggTransactionDao.findById(sTransactionId);
|
---|
662 | if (oTrans == null)
|
---|
663 | {
|
---|
664 | String sErrorMessage = "Failed to find an aggregator transaction for TransactionId: " +
|
---|
665 | sTransactionId;
|
---|
666 | log.error(sErrorMessage);
|
---|
667 | throw new AggregatorException(sErrorMessage);
|
---|
668 | }
|
---|
669 |
|
---|
670 | // Make sure this transaction ID is for the right type of transaction.
|
---|
671 | //--------------------------------------------------------------------
|
---|
672 | if ((oTrans.getServiceType() == null) ||
|
---|
673 | ((oTrans.getServiceType() != null) &&
|
---|
674 | (!oTrans.getServiceType().equals(DOC_QUERY_NAME))))
|
---|
675 | {
|
---|
676 | String sErrorMessage = "The specified TransactionId: " + sTransactionId + " is not associated with " +
|
---|
677 | " Document Query messages. It is for: " + oTrans.getServiceType() + ".";
|
---|
678 | log.error(sErrorMessage);
|
---|
679 | throw new AggregatorException(sErrorMessage);
|
---|
680 | }
|
---|
681 |
|
---|
682 | // If we have timed out, or if the results are ready then aggregate them
|
---|
683 | // and return them.
|
---|
684 | //----------------------------------------------------------------------
|
---|
685 | if ((bTimedOut) ||
|
---|
686 | (areResultsReady(oTrans)))
|
---|
687 | {
|
---|
688 | if ((oTrans.getAggMessageResults() != null) &&
|
---|
689 | (oTrans.getAggMessageResults().size() > 0))
|
---|
690 | {
|
---|
691 | oResponse = combineResults(oTrans.getAggMessageResults());
|
---|
692 | }
|
---|
693 | else
|
---|
694 | {
|
---|
695 | // If we got here - it was a transaction with no expected results
|
---|
696 | // create an empty result set and return it.
|
---|
697 | //---------------------------------------------------------------
|
---|
698 | oResponse = createEmptyResult();
|
---|
699 | }
|
---|
700 |
|
---|
701 | // Delete the entries out of the database...
|
---|
702 | //--------------------------------------------
|
---|
703 | oAggTransactionDao.delete(oTrans);
|
---|
704 | oTrans = null;
|
---|
705 | }
|
---|
706 |
|
---|
707 | return oResponse;
|
---|
708 | }
|
---|
709 | }
|
---|