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