/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package gov.hhs.fha.nhinc.subscription.dte; //import gov.hhs.fha.nhinc.NHINCLib.NullChecker; import gov.hhs.fha.nhinc.nhinclib.NullChecker; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBElement; import oasis.names.tc.ebxml_regrep.xsd.rim._3.*; import org.oasis_open.docs.wsn.b_2.Subscribe; import org.w3c.dom.Element; /** * * @author rayj */ public class AdhocQueryHelper { private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(AdhocQueryHelper.class); public static AdhocQueryType getAdhocQuery(Subscribe nhinSubscribe) { AdhocQueryType adhocQuery = null; log.info("begin getAdhocQuery"); List any = nhinSubscribe.getAny(); log.info("found " + any.size() + " any item(s)"); for (Object anyItem : any) { log.info("anyItem=" + anyItem); if (anyItem instanceof oasis.names.tc.ebxml_regrep.xsd.rim._3.AdhocQueryType) { adhocQuery = (AdhocQueryType) anyItem; } if (anyItem instanceof Element) { Element element = (Element) anyItem; log.info("element.getNodeName()=" + element.getNodeName()); Object o = (JAXBElement) nhinSubscribe.getAny(); // Object o = (JAXBElement) anyItem; } if (anyItem instanceof JAXBElement) { log.info("jaxbelement.getValue=" + ((JAXBElement) anyItem).getValue()); if (((JAXBElement) anyItem).getValue() instanceof AdhocQueryType) { adhocQuery = (AdhocQueryType) ((JAXBElement) anyItem).getValue(); } else { log.warn("unhandled anyitem jaxbelement value " + ((JAXBElement) anyItem).getValue()); } } else { log.warn("unhandled anyitem " + anyItem); } } log.info("end getAdhocQuery"); return adhocQuery; } public static List getAllSlots(Subscribe nhinSubscribe) { AdhocQueryType adhocQuery = getAdhocQuery(nhinSubscribe); if ((adhocQuery == null) || (adhocQuery.getSlot() == null)) { return new ArrayList(); } return adhocQuery.getSlot(); } public static List findSlotValues(Subscribe nhinSubscribe, String slotName) { log.info("begin findSlotValue"); List allSlots = getAllSlots(nhinSubscribe); List matchingSlotValues = findSlotValues(allSlots, slotName); log.info("total slotValues found " + matchingSlotValues.size()); log.info("end findSlotValue"); return matchingSlotValues; } public static List findSlotValues(List slots, String slotName) { log.info("begin findSlotValue"); List matchingSlotValues = new ArrayList(); for (SlotType1 slot : slots) { if (slot.getName().contentEquals(slotName)) { if (slot.getValueList() != null) { List slotValues = slot.getValueList().getValue(); for (String slotValue : slotValues) { if (NullChecker.isNotNullish(slotValue)) { log.info("adding slotValue " + slotValue); matchingSlotValues.add(slotValue); } } } } } log.info("total slotValues found " + matchingSlotValues.size()); log.info("end findSlotValue"); return matchingSlotValues; } public static String findSlotValue(Subscribe nhinSubscribe, String slotName) throws MultipleSlotValuesFoundException { List slotValues = findSlotValues(nhinSubscribe, slotName); return findSingleSlotValue(slotValues); } public static String findSlotValue(List slots, String slotName) throws MultipleSlotValuesFoundException { List slotValues = findSlotValues(slots, slotName); return findSingleSlotValue(slotValues); } private static String findSingleSlotValue(List slotValues ) throws MultipleSlotValuesFoundException { String slotValue=null; if ((slotValues == null) || (slotValues.size() == 0)) { slotValue = null; } else if (slotValues.size() > 1) { throw new MultipleSlotValuesFoundException(); } else { slotValue = slotValues.get(0); } return slotValue; } }