Example usage for java.util HashMap putAll

List of usage examples for java.util HashMap putAll

Introduction

In this page you can find the example usage for java.util HashMap putAll.

Prototype

public void putAll(Map<? extends K, ? extends V> m) 

Source Link

Document

Copies all of the mappings from the specified map to this map.

Usage

From source file:org.aselect.server.request.handler.xsaml20.sp.Xsaml20_AssertionConsumer.java

/**
 * Assertion consumer. <br>//from   ww  w  . j  ava 2  s. c om
 * 
 * @param servletRequest
 *            HttpServletRequest.
 * @param servletResponse
 *            HttpServletResponse.
 * @return the request state
 * @throws ASelectException
 *             on failure
 */
@SuppressWarnings("unchecked")
public RequestState process(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ASelectException {
    String sMethod = "process";
    boolean checkAssertionSigning = false;
    Object samlResponseObject = null;
    String auth_proof = null;
    PrintWriter pwOut = null;

    try {
        pwOut = Utils.prepareForHtmlOutput(servletRequest, servletResponse);

        String sReceivedArtifact = servletRequest.getParameter("SAMLart");
        String sReceivedResponse = servletRequest.getParameter("SAMLResponse");
        String sRelayState = servletRequest.getParameter("RelayState");
        _systemLogger.log(Level.INFO, MODULE, sMethod,
                "Received artifact: " + sReceivedArtifact + " RelayState=" + sRelayState);
        if (!(sReceivedArtifact == null || "".equals(sReceivedArtifact))) {
            String sFederationUrl = _sFederationUrl; // default, remove later on, can be null
            if (sRelayState.startsWith("idp=")) {
                sFederationUrl = sRelayState.substring(4);
            } else { // Could be Base64 encoded
                sRelayState = new String(Base64Codec.decode(sRelayState));
                _systemLogger.log(Level.INFO, MODULE, sMethod, "RelayState=" + sRelayState);
                sFederationUrl = Utils.getParameterValueFromUrl(sRelayState, "idp");
            }
            if (!Utils.hasValue(sFederationUrl)) {
                _systemLogger.log(Level.WARNING, MODULE, sMethod,
                        "No idp value found in RelayState (or in <federation_url> config)");
                throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
            }

            _systemLogger.log(Level.INFO, MODULE, sMethod, "FederationUrl=" + sFederationUrl);
            // use metadata
            MetaDataManagerSp metadataManager = MetaDataManagerSp.getHandle();
            String sASelectServerUrl = metadataManager.getLocation(sFederationUrl,
                    ArtifactResolutionService.DEFAULT_ELEMENT_LOCAL_NAME,
                    SAMLConstants.SAML2_SOAP11_BINDING_URI);
            _systemLogger.log(Level.INFO, MODULE, sMethod, "Artifact Resolution at " + sASelectServerUrl);

            if (sASelectServerUrl == null) {
                _systemLogger.log(Level.INFO, MODULE, sMethod, "Artifact NOT found");
                throw new ASelectException(Errors.ERROR_ASELECT_NOT_FOUND);
            }

            SAMLObjectBuilder<Artifact> artifactBuilder = (SAMLObjectBuilder<Artifact>) _oBuilderFactory
                    .getBuilder(Artifact.DEFAULT_ELEMENT_NAME);
            Artifact artifact = artifactBuilder.buildObject();
            artifact.setArtifact(sReceivedArtifact);

            SAMLObjectBuilder<ArtifactResolve> artifactResolveBuilder = (SAMLObjectBuilder<ArtifactResolve>) _oBuilderFactory
                    .getBuilder(ArtifactResolve.DEFAULT_ELEMENT_NAME);
            ArtifactResolve artifactResolve = artifactResolveBuilder.buildObject();

            artifactResolve.setID(SamlTools.generateIdentifier(_systemLogger, MODULE));
            artifactResolve.setVersion(SAMLVersion.VERSION_20);
            artifactResolve.setIssueInstant(new DateTime());

            // We decided that the other side could retrieve public key from metadata
            // by looking up the issuer as an entityID in the metadata
            // So we MUST supply an Issuer (which otherwise would be optional (by SAML standards))
            SAMLObjectBuilder<Issuer> assertionIssuerBuilder = (SAMLObjectBuilder<Issuer>) _oBuilderFactory
                    .getBuilder(Issuer.DEFAULT_ELEMENT_NAME);
            Issuer assertionIssuer = assertionIssuerBuilder.buildObject();

            // 20100312, Bauke: eHerkenning, no assertion issuer format:
            // assertionIssuer.setFormat(NameIDType.ENTITY);
            // 20100311, Bauke: added for eHerkenning: Specific issuer id, independent of the Url
            PartnerData partnerData = MetaDataManagerSp.getHandle().getPartnerDataEntry(sFederationUrl);
            String specialSettings = (partnerData == null) ? null : partnerData.getSpecialSettings();
            if (partnerData != null && partnerData.getLocalIssuer() != null)
                assertionIssuer.setValue(partnerData.getLocalIssuer());
            else
                assertionIssuer.setValue(_sRedirectUrl);
            artifactResolve.setIssuer(assertionIssuer);
            artifactResolve.setArtifact(artifact);

            // Do some logging for testing
            _systemLogger.log(Level.INFO, MODULE, sMethod, "Sign the artifactResolve >======");
            boolean useSha256 = (specialSettings != null && specialSettings.contains("sha256"));
            artifactResolve = (ArtifactResolve) SamlTools.signSamlObject(artifactResolve,
                    useSha256 ? "sha256" : "sha1");
            _systemLogger.log(Level.INFO, MODULE, sMethod, "Signed the artifactResolve ======<");

            // Build the SOAP message
            SoapManager soapManager = null;
            if (isUseBackchannelClientcertificate()) {
                soapManager = new SoapManager(getSslSocketFactory());
            } else {
                soapManager = new SoapManager();
            }
            Envelope envelope = soapManager.buildSOAPMessage(artifactResolve);
            _systemLogger.log(Level.INFO, MODULE, sMethod, "Marshall");
            Element envelopeElem = SamlTools.marshallMessage(envelope);
            _systemLogger.log(Level.INFO, MODULE, sMethod,
                    "Writing SOAP message:\n" + XMLHelper.nodeToString(envelopeElem));
            // XMLHelper.prettyPrintXML(envelopeElem));

            // ------------ Send/Receive the SOAP message
            String sSamlResponse = soapManager.sendSOAP(XMLHelper.nodeToString(envelopeElem),
                    sASelectServerUrl); // x_AssertionConsumer_x
            //byte[] sSamlResponseAsBytes = sSamlResponse.getBytes();
            _systemLogger.log(Level.INFO, MODULE, sMethod,
                    "Received response: " + sSamlResponse + " length=" + sSamlResponse.length());

            // save original, but, for (internal) transport, encode base64 
            auth_proof = new String(
                    org.apache.commons.codec.binary.Base64.encodeBase64(sSamlResponse.getBytes("UTF-8")));

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setNamespaceAware(true);
            // dbFactory.setExpandEntityReferences(false);
            // dbFactory.setIgnoringComments(true);
            DocumentBuilder builder = dbFactory.newDocumentBuilder();

            StringReader stringReader = new StringReader(sSamlResponse);
            InputSource inputSource = new InputSource(stringReader);
            Document docReceivedSoap = builder.parse(inputSource);
            _systemLogger.log(Level.INFO, MODULE, sMethod, "parsed=" + docReceivedSoap.toString());
            Element elementReceivedSoap = docReceivedSoap.getDocumentElement();
            _systemLogger.log(Level.INFO, MODULE, sMethod, "getdoc=" + elementReceivedSoap.toString());

            // Remove all SOAP elements
            Node eltArtifactResponse = SamlTools.getNode(elementReceivedSoap, "ArtifactResponse");

            // Unmarshall to the SAMLmessage
            UnmarshallerFactory factory = Configuration.getUnmarshallerFactory();
            Unmarshaller unmarshaller = factory.getUnmarshaller((Element) eltArtifactResponse);
            ArtifactResponse artifactResponse = (ArtifactResponse) unmarshaller
                    .unmarshall((Element) eltArtifactResponse);

            Issuer issuer = artifactResponse.getIssuer();
            String sIssuer = (issuer == null) ? null : issuer.getValue();
            // If issuer is not present in the response, use sASelectServerUrl value retrieved from metadata
            // else use value from the response
            String artifactResponseIssuer = (sIssuer == null || "".equals(sIssuer)) ? sASelectServerUrl
                    : sIssuer;

            _systemLogger.log(Level.INFO, MODULE, sMethod,
                    "Do artifactResponse signature verification=" + is_bVerifySignature());
            //            if (is_bVerifySignature()) {   // RH, 20121205, o
            if (is_bVerifySignature() || isVerifyArtifactResponseSignature()) { // RH, 20121205, n
                // Check signature of artifactResolve here
                // We get the public key from the metadata
                // Therefore we need a valid Issuer to lookup the entityID in the metadata
                // We get the metadataURL from aselect.xml so we consider this safe and authentic
                if (artifactResponseIssuer == null || "".equals(artifactResponseIssuer)) {
                    _systemLogger.log(Level.SEVERE, MODULE, sMethod,
                            "For signature verification the received message must have an Issuer");
                    throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                }

                PublicKey pkey = metadataManager.getSigningKeyFromMetadata(artifactResponseIssuer);
                if (pkey == null || "".equals(pkey)) {
                    _systemLogger.log(Level.SEVERE, MODULE, sMethod, "No valid public key in metadata");
                    throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                }

                if (SamlTools.checkSignature(artifactResponse, pkey)) {
                    _systemLogger.log(Level.INFO, MODULE, sMethod, "artifactResponse was signed OK");
                } else {
                    _systemLogger.log(Level.SEVERE, MODULE, sMethod, "artifactResponse was NOT signed OK");
                    throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                }
            }
            samlResponseObject = artifactResponse.getMessage();
        } else if (!(sReceivedResponse == null || "".equals(sReceivedResponse))) {
            // Handle http-post, can be unsolicited POST as well
            // Could be Base64 encoded
            // RelayState should contain intended application resource URL
            sRelayState = new String(Base64Codec.decode(sRelayState));

            _systemLogger.log(Level.FINER, MODULE, sMethod, "Received Response: " + sReceivedResponse); //   RH, 20130924, n
            //            sReceivedResponse = new String(Base64Codec.decode(sReceivedResponse));   //   RH, 20130924, o
            auth_proof = sReceivedResponse; // save original

            sReceivedResponse = new String(
                    org.apache.commons.codec.binary.Base64.decodeBase64(sReceivedResponse.getBytes("UTF-8"))); //   RH, 20130924, n
            _systemLogger.log(Level.INFO, MODULE, sMethod, "Received Response after base64 decoding: "
                    + sReceivedResponse + " RelayState=" + sRelayState); //   RH, 20130924, n
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setNamespaceAware(true);
            // dbFactory.setExpandEntityReferences(false);
            // dbFactory.setIgnoringComments(true);
            DocumentBuilder builder = dbFactory.newDocumentBuilder();

            StringReader stringReader = new StringReader(sReceivedResponse);
            InputSource inputSource = new InputSource(stringReader);
            Document docReceived = builder.parse(inputSource);
            Node eltSAMLResponse = SamlTools.getNode(docReceived, "Response");
            _systemLogger.log(Level.INFO, MODULE, sMethod,
                    "Found node Response: " + eltSAMLResponse + ((eltSAMLResponse == null) ? " NULL" : " ok"));

            // Unmarshall to the SAMLmessage
            UnmarshallerFactory factory = Configuration.getUnmarshallerFactory();
            Unmarshaller unmarshaller = factory.getUnmarshaller((Element) eltSAMLResponse);
            _systemLogger.log(Level.INFO, MODULE, sMethod,
                    "Unmarshaller" + ((unmarshaller == null) ? " NULL" : " ok"));
            samlResponseObject = (Response) unmarshaller.unmarshall((Element) eltSAMLResponse);
            _systemLogger.log(Level.INFO, MODULE, sMethod,
                    "Unmarshalling done, VerifySignature=" + is_bVerifySignature());

            // 20120308: Bauke added signature checking
            //   saml-profiles-2.0-os: The <Assertion> element(s) in the <Response> MUST be signed,
            //   if the HTTP POST binding is used, and MAY be signed if the HTTPArtifact binding is used.
            if (is_bVerifySignature())
                checkAssertionSigning = true;

        } else {
            _systemLogger.log(Level.WARNING, MODULE, sMethod,
                    "No Artifact and no Response found in the message.");
            throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
        }

        ///////
        // The object can either a Response (SSO case) or a StatusResponseType (SLO case)
        ///////////////////////////////////////////////////////////////////////////
        if (samlResponseObject instanceof Response) {
            // SSO
            Response samlResponse = (Response) samlResponseObject;
            _systemLogger.log(Level.INFO, MODULE, sMethod, "Processing 'Response'"); // +XMLHelper.prettyPrintXML(samlResponse.getDOM()));

            // RH, 20121205, sn
            MetaDataManagerSp metadataManager = MetaDataManagerSp.getHandle();
            _systemLogger.log(Level.INFO, MODULE, sMethod,
                    "Do Response signature verification=" + isVerifyResponseSignature());
            if (isVerifyResponseSignature()) {
                Issuer issuer = samlResponse.getIssuer();
                String sIssuer = (issuer == null) ? null : issuer.getValue();
                // If issuer is not present in the response, use sASelectServerUrl value retrieved from metadata
                // else use value from the response
                //               String responseIssuer = (sIssuer == null || "".equals(sIssuer))? sASelectServerUrl: sIssuer;
                String responseIssuer = (sIssuer == null || "".equals(sIssuer)) ? null : sIssuer; // There must be an issuer for now
                // Check signature of artifactResolve here
                // We get the public key from the metadata
                // Therefore we need a valid Issuer to lookup the entityID in the metadata
                // We get the metadataURL from aselect.xml so we consider this safe and authentic
                if (responseIssuer == null || "".equals(responseIssuer)) {
                    _systemLogger.log(Level.SEVERE, MODULE, sMethod,
                            "For signature verification the received response must have an Issuer");
                    throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                }

                PublicKey pkey = metadataManager.getSigningKeyFromMetadata(responseIssuer);
                if (pkey == null || "".equals(pkey)) {
                    _systemLogger.log(Level.SEVERE, MODULE, sMethod, "No valid public key in metadata");
                    throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                }

                if (SamlTools.checkSignature(samlResponse, pkey)) {
                    _systemLogger.log(Level.INFO, MODULE, sMethod, "Response was signed OK");
                } else {
                    _systemLogger.log(Level.SEVERE, MODULE, sMethod, "Response was NOT signed OK");
                    throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                }
            }
            // RH, 20121205, en

            // Detect if this is a successful or an error Response      
            String sStatusCode = samlResponse.getStatus().getStatusCode().getValue();
            String sRemoteRid = samlResponse.getID();

            // 20100531, Bauke: Remove added timestamp to get our local RID
            String sLocalRid = samlResponse.getInResponseTo();
            int len = sLocalRid.length();
            if (len > 9)
                sLocalRid = sLocalRid.substring(0, len - 9);
            _systemLogger.log(Level.INFO, MODULE, sMethod,
                    "RemoteRid=" + sRemoteRid + " LocalRid=" + sLocalRid + " StatusCode=" + sStatusCode);
            _htSessionContext = _oSessionManager.getSessionContext(sLocalRid);
            if (_htSessionContext == null) {
                _systemLogger.log(Level.WARNING, MODULE, sMethod,
                        "Unknown session in response from cross aselect server");
                throw new ASelectCommunicationException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
            }

            if (sStatusCode.equals(StatusCode.SUCCESS_URI)) {
                _systemLogger.log(Level.INFO, MODULE, sMethod,
                        "Response was successful " + samlResponse.toString());
                _systemLogger.log(Level.INFO, MODULE, sMethod,
                        "Number of Assertions found:  " + samlResponse.getAssertions().size());
                Assertion samlAssertion = samlResponse.getAssertions().get(0);
                _systemLogger.log(Level.INFO, MODULE, sMethod, "Assertion ID:" + samlAssertion.getID());
                String sAssertIssuer = samlAssertion.getIssuer().getValue();
                _systemLogger.log(Level.INFO, MODULE, sMethod,
                        "Issuer:" + sAssertIssuer + " checkAssertionSigning=" + checkAssertionSigning);

                // 20120308: Bauke added signature checking
                //               if (checkAssertionSigning) {   // RH, 20121205, o
                if (checkAssertionSigning || isVerifyAssertionSignature()) { // RH, 20121205, n
                    // Check signature of artifactResolve here. We get the public key from the metadata
                    // Therefore we need a valid Issuer to lookup the entityID in the metadata
                    // We get the metadataURL from aselect.xml so we consider this safe and authentic
                    _systemLogger.log(Level.INFO, MODULE, sMethod,
                            "Verify assertion signature, issuer=" + sAssertIssuer);
                    if (!Utils.hasValue(sAssertIssuer)) {
                        _systemLogger.log(Level.SEVERE, MODULE, sMethod, "No Issuer present in Assertion");
                        throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                    }

                    //                  MetaDataManagerSp metadataManager = MetaDataManagerSp.getHandle();   // RH, 20121205, n
                    PublicKey pkey = metadataManager.getSigningKeyFromMetadata(sAssertIssuer);
                    if (pkey == null || "".equals(pkey)) {
                        _systemLogger.log(Level.SEVERE, MODULE, sMethod, "No valid public key in metadata");
                        throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                    }
                    if (!SamlTools.checkSignature(samlAssertion, pkey)) {
                        _systemLogger.log(Level.SEVERE, MODULE, sMethod, "Assertion was NOT signed OK");
                        throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                    }
                    _systemLogger.log(Level.INFO, MODULE, sMethod, "Assertion was signed OK");
                }
                // 20120308

                String sNameID = samlAssertion.getSubject().getNameID().getValue();
                _systemLogger.log(Level.INFO, MODULE, sMethod, "NameID:" + sNameID);
                String sNameIDQualifier = samlAssertion.getSubject().getNameID().getNameQualifier();
                _systemLogger.log(Level.INFO, MODULE, sMethod, "NameIDQualifier:" + sNameIDQualifier);

                // Now check for time interval validation
                // We only check first object from the list
                // First the assertion itself
                if (is_bVerifyInterval() && !SamlTools.checkValidityInterval(samlAssertion)) {
                    _systemLogger.log(Level.SEVERE, MODULE, sMethod, "Assertion time interval was NOT valid");
                    throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                }
                // then the AuthnStatement
                if (is_bVerifyInterval()
                        && !SamlTools.checkValidityInterval(samlAssertion.getAuthnStatements().get(0))) {
                    _systemLogger.log(Level.SEVERE, MODULE, sMethod,
                            "AuthnStatement time interval was NOT valid");
                    throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                }
                // check subjectlocalityaddress
                if (isLocalityAddressRequired()
                        && !SamlTools.checkLocalityAddress(samlAssertion.getAuthnStatements().get(0),
                                servletRequest.getRemoteAddr())) {
                    _systemLogger.log(Level.SEVERE, MODULE, sMethod,
                            "AuthnStatement subjectlocalityaddress was NOT valid");
                    throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                }

                // Get the (option) sessionindex from remote
                String sSessionindex = samlAssertion.getAuthnStatements().get(0).getSessionIndex();
                _systemLogger.log(Level.INFO, MODULE, sMethod, "Sessionindex:" + sSessionindex);

                AuthnContext oAuthnContext = samlAssertion.getAuthnStatements().get(0).getAuthnContext();
                List<AuthenticatingAuthority> authAuthorities = oAuthnContext.getAuthenticatingAuthorities();
                String sAuthnAuthority = null;
                if (authAuthorities != null && authAuthorities.size() > 0)
                    sAuthnAuthority = (String) authAuthorities.get(0).getURI();
                String sAuthnContextClassRefURI = oAuthnContext.getAuthnContextClassRef()
                        .getAuthnContextClassRef();
                _systemLogger.log(Level.INFO, MODULE, sMethod,
                        "AuthnContextClassRefURI:" + sAuthnContextClassRefURI);
                ;
                /////////////////////////   digid4   ///////////////////////////////////////////
                /// Digid4 still has to decide how to provide a "face2face" declaration 
                //   String sAuthnContextDeclRefIssueMethod = samlAssertion.getAuthnStatements().get(0).getAuthnContext().
                /////////////////////////   digid4   ///////////////////////////////////////////
                String sSelectedLevel = SecurityLevel
                        .convertAuthnContextClassRefURIToLevel(sAuthnContextClassRefURI, _systemLogger);

                // Check returned security level
                Integer intAppLevel = (Integer) _htSessionContext.get("level");
                if (Integer.parseInt(sSelectedLevel) < intAppLevel) {
                    _systemLogger.log(Level.SEVERE, MODULE, sMethod, "Security level returned ("
                            + sSelectedLevel + ") must be at least: " + intAppLevel);
                    throw new ASelectException(Errors.ERROR_ASELECT_SERVER_INVALID_REQUEST);
                }

                // Retrieve the embedded attributes
                HashMap hmSamlAttributes = new HashMap();
                String sEncodedAttributes = null;
                List<AttributeStatement> lAttrStatList = samlAssertion.getAttributeStatements();
                Iterator<AttributeStatement> iASList = lAttrStatList.iterator();
                while (iASList.hasNext()) {
                    AttributeStatement sAttr = iASList.next();
                    List<Attribute> lAttr = sAttr.getAttributes();
                    Iterator<Attribute> iAttr = lAttr.iterator();
                    while (iAttr.hasNext()) {
                        Attribute attr = iAttr.next();
                        String sAttrName = attr.getName();

                        String sAttrValue = null;// RH, 20120124, sn
                        List<XMLObject> aValues = attr.getAttributeValues();
                        if (aValues != null && aValues.size() == 1) { // For now we only allow single valued simple type xs:string attributes
                            XMLObject xmlObj = aValues.get(0);
                            //                        XSStringImpl xsString = (XSStringImpl) attr.getOrderedChildren().get(0);// RH, 20120124, so
                            //                        String sAttrValue = xsString.getValue();// RH, 20120124, o
                            //                        sAttrValue = xsString.getValue();// RH, 20120124, eo
                            sAttrValue = xmlObj.getDOM().getFirstChild().getTextContent();
                            _systemLogger.log(Level.INFO, MODULE, sMethod,
                                    "Name=" + sAttrName + " Value=" + sAttrValue);
                        } else {
                            _systemLogger.log(Level.INFO, MODULE, sMethod,
                                    "Only single valued attributes allowed, skipped attribute Name="
                                            + sAttrName);
                        } // RH, 20120124, en
                        if ("attributes".equals(sAttrName))
                            sEncodedAttributes = sAttrValue;
                        else
                            hmSamlAttributes.put(sAttrName, sAttrValue);
                    }
                }

                // Since the "attributes" Attribute is used for gathering, add the Saml Attributes to it
                HashMap<String, String> hmAttributes;
                if (sEncodedAttributes != null) {
                    hmAttributes = org.aselect.server.utils.Utils.deserializeAttributes(sEncodedAttributes);
                } else {
                    hmAttributes = new HashMap<String, String>();
                }
                // Add the serialized attributes and a few specials
                hmSamlAttributes.putAll(hmAttributes);
                hmSamlAttributes.put("name_id", sNameID); // "sel_level" was already set by the IdP
                if (sAuthnAuthority != null)
                    hmSamlAttributes.put("authority", sAuthnAuthority);

                // eHerkenning addition: OrgID = KvKnummer+Vestigingsnummer
                // If EntityConcernedID = 00000003123456780000 and EntityConcernedSubID = ...0001,
                // then orgid = 1234567800000001
                //               String sEntityId = (String)hmSamlAttributes.get("urn:nl:eherkenning:0.8def:EntityConcernedID");
                // RH, 20110523, add support for other versions of eHerk
                String sEntityId = null;

                Pattern p = Pattern.compile("urn:nl:eherkenning:(.*):EntityConcernedID");

                Set<String> keys = hmSamlAttributes.keySet();
                Iterator keyIter = keys.iterator();
                String eHerkversion = null;
                while (keyIter.hasNext()) {
                    Matcher m = p.matcher((String) keyIter.next());
                    if (m.find()) {
                        sEntityId = (String) hmSamlAttributes.get(m.group());
                        eHerkversion = m.group(1);
                        _systemLogger.log(Level.INFO, MODULE, sMethod,
                                "Found sEntityId=" + sEntityId + " eHerkversion=" + eHerkversion);
                        break; // just take the first we find
                    }
                }

                if (sEntityId != null) {
                    int idx = sEntityId.length() - 12; // last 12 characters
                    if (idx > 0)
                        sEntityId = sEntityId.substring(idx);

                    //                  String sEntitySubId = (String)hmSamlAttributes.get("urn:nl:eherkenning:0.8def:EntityConcernedSubID");
                    String sEntitySubId = (String) hmSamlAttributes
                            .get("urn:nl:eherkenning:" + eHerkversion + ":EntityConcernedSubID");
                    if (sEntitySubId != null) {
                        _systemLogger.log(Level.INFO, MODULE, sMethod, "Found sEntitySubId=" + sEntitySubId);
                        idx = sEntitySubId.length() - 12; // last 12 characters to be on the safe side
                        if (idx > 0)
                            sEntitySubId = sEntitySubId.substring(idx);
                        sEntityId = sEntitySubId;
                    } else { // ditch the last 4 zeroes
                        idx = sEntityId.length() - 4;
                        if (idx > 0)
                            sEntityId = sEntityId.substring(0, idx);
                    }
                    hmSamlAttributes.put("orgid", sEntityId);
                }

                // eHerkenning: AuthID = Unique Persistent Identifier
                if (isUseNameIDAsAuthID()) { // RH, 20130923, sn
                    hmSamlAttributes.put("authid", sNameID);
                } else { // RH, 20130923, en
                    // Use the fifth word from sAuthnAuthority (split using :) and add sNameID
                    if (sNameIDQualifier != null) {
                        String sAuthID = "", sAuthSubID = "";
                        String[] tokens = sNameIDQualifier.split(":");
                        if (tokens.length > 4)
                            sAuthID = tokens[4];

                        //                  if (tokens.length > 5)
                        //                     sAuthSubID = tokens[5];
                        // Test  new layout of eherkenning
                        // Maybe do something with pattern search here
                        if (tokens.length > 6)
                            sAuthSubID = tokens[6];

                        sAuthID += "_" + sAuthSubID + "_" + sNameID; // add separator
                        hmSamlAttributes.put("authid", sAuthID);
                    }
                } // RH, 20130923, n

                if (isCarryAuthProof()) { // Put the original authentication proof in hmSamlAttributes before serialization in attributes
                                          // so they will be available for gatherer
                    hmSamlAttributes.put("auth_proof", auth_proof); // original response, still base64 encoded
                    //                  _systemLogger.log(Level.FINEST, MODULE, sMethod, "auth_proof=" + auth_proof);
                }
                // And serialize them back to where they came from
                sEncodedAttributes = org.aselect.server.utils.Utils.serializeAttributes(hmSamlAttributes);
                hmSamlAttributes.put("attributes", sEncodedAttributes);

                if (!isCarryAuthProof() && isLogAuthProof()) { // Put the original authentication proof in hmSamlAttributes only temporarily to be removed later
                    // if isCarryAuthProof() true they were already there
                    hmSamlAttributes.put("auth_proof", auth_proof); // original response, still base64 encoded
                    //                  _systemLogger.log(Level.FINEST, MODULE, sMethod, "auth_proof=" + auth_proof);
                }
                // This is the quickest way to get "name_id" into the Context
                hmSamlAttributes.put("name_id", sNameID); // also as plain attribute

                ///////////// Digid4   //////////////////////////////
                // must be made configurable and parameterized, still looking for some reference to identify the service (maybe issuer) 
                String[] splittedNameId = sNameID.split(":");
                if (splittedNameId.length == 2 && splittedNameId[0].toUpperCase().startsWith("S")
                        && splittedNameId[0].length() == 9) { // for now this identifies as digid4
                    hmSamlAttributes.put("uid", splittedNameId[1]);
                    // add special attributes for digid4
                    if ("S00000000".equalsIgnoreCase(splittedNameId[0])) {
                        hmSamlAttributes.put("bsn", splittedNameId[1]);

                    } else if ("S00000001".equalsIgnoreCase(splittedNameId[0])) {
                        hmSamlAttributes.put("sofi", splittedNameId[1]);

                    } else if ("S00000002".equalsIgnoreCase(splittedNameId[0])) {
                        hmSamlAttributes.put("anummer", splittedNameId[1]);

                    } else if ("S00000100".equalsIgnoreCase(splittedNameId[0])) {
                        hmSamlAttributes.put("oeb", splittedNameId[1]);
                    }
                }
                /////////////////////////////////////////////////////

                // 20100422, Bauke: no uid, then use NameID
                String sUid = (String) hmSamlAttributes.get("uid");
                if (sUid == null || sUid.equals(""))
                    hmSamlAttributes.put("uid", sNameID);
                _systemLogger.log(Level.INFO, MODULE, sMethod,
                        "NameID=" + sNameID + " remote_rid=" + sRemoteRid + " local_rid=" + sLocalRid
                                + " sel_level=" + sSelectedLevel + " organization/authsp=" + sAssertIssuer);

                // htRemoteAttributes.put("attributes", HandlerTools.serializeAttributes(htAttributes));
                hmSamlAttributes.put("remote_rid", sRemoteRid);
                hmSamlAttributes.put("local_rid", sLocalRid);

                hmSamlAttributes.put("sel_level", sSelectedLevel);
                hmSamlAttributes.put("authsp_level", sSelectedLevel); // default value, issueTGT will correct this
                hmSamlAttributes.put("organization", sAssertIssuer);
                hmSamlAttributes.put("authsp", sAssertIssuer);

                // RH, 20120201, sn
                // also save the provided session if present, saml2 specs say there might be more than one session to track
                if (isIncludeSessionindexes() && sSessionindex != null && sSessionindex.length() > 0) {
                    Vector sessionindexes = new Vector<String>();
                    sessionindexes.add(sSessionindex);
                    hmSamlAttributes.put("remote_sessionlist", sessionindexes);
                }
                // RH, 20120201, en

                // Bauke, 20081204: If we want to send the IdP token as an attribute
                // to the application, we will need the following code:
                /*
                 * String sAssertion = XMLHelper.nodeToString(samlAssertion.getDOM());
                 * _systemLogger.log(Level.INFO, MODULE, sMethod, "sAssertion="+sAssertion);
                 * BASE64Encoder b64Enc = new BASE64Encoder();
                 * sAssertion = b64Enc.encode(sAssertion.getBytes("UTF-8"));
                 * htRemoteAttributes.put("saml_remote_token", sAssertion);
                 */
                // End of IdP token

                _systemLogger.log(Level.INFO, MODULE, sMethod, "htRemoteAttributes=" + hmSamlAttributes);
                handleSSOResponse(_htSessionContext, hmSamlAttributes, servletRequest, servletResponse);
            } else {
                _systemLogger.log(Level.WARNING, MODULE, sMethod,
                        "Response was not successful: " + sStatusCode);
                // Handle various error conditions here
                String sErrorCode = Errors.ERROR_ASELECT_AUTHSP_COULD_NOT_AUTHENTICATE_USER; // default
                String sErrorSubCode = null;
                if (samlResponse.getStatus().getStatusCode().getStatusCode() != null) { // Get the subcode
                    sErrorSubCode = SamlTools
                            .mapStatus(samlResponse.getStatus().getStatusCode().getStatusCode().getValue());
                    _systemLogger.log(Level.FINER, MODULE, sMethod, "ErrorSubcode: " + sErrorSubCode);
                }
                StatusMessage statMsg = samlResponse.getStatus().getStatusMessage();
                if (statMsg != null) {
                    sErrorCode = statMsg.getMessage();
                    _systemLogger.log(Level.FINER, MODULE, sMethod, "StatusMessage found: " + sErrorCode);
                } else {
                    if (sErrorSubCode != null && !"".equals(sErrorSubCode)) {
                        sErrorCode = sErrorSubCode;
                    }
                }
                _systemLogger.log(Level.INFO, MODULE, sMethod, "ErrorCode=" + sErrorCode);
                //else if (samlResponse.getStatus().getStatusCode().getStatusCode().getValue().equals(StatusCode.AUTHN_FAILED_URI))
                //   sErrorCode = Errors.ERROR_ASELECT_AUTHSP_COULD_NOT_AUTHENTICATE_USER;
                // Expect these codes: Errors.ERROR_ASELECT_SERVER_CANCEL,
                // Errors.ERROR_ASELECT_AUTHSP_COULD_NOT_AUTHENTICATE_USER;

                //HashMap htRemoteAttributes = new HashMap();
                //htRemoteAttributes.put("remote_rid", sRemoteRid);
                //htRemoteAttributes.put("local_rid", sLocalRid);
                //htRemoteAttributes.put("result_code", sErrorCode);

                // Choose your response (3rd is implemented below)
                // 1. handleSSOResponse(htRemoteAttributes, request, response); // Lets application display error
                // 2. throw new ASelectException(Errors.ERROR_ASELECT_AUTHSP_ACCESS_DENIED); // Standard server error
                // 3. Show error page:
                showErrorPage(sErrorCode, _htSessionContext, pwOut, servletRequest);
            }
        } else { // SLO
            _systemLogger.log(Level.WARNING, "Unexpected SAMLObject type: " + samlResponseObject.getClass());
            throw new ASelectException(Errors.ERROR_ASELECT_INTERNAL_ERROR);
        }
    } catch (ASelectException e) {
        throw e;
    } catch (Exception e) {
        _systemLogger.log(Level.WARNING, MODULE, sMethod, "Internal error", e);
        throw new ASelectException(Errors.ERROR_ASELECT_INTERNAL_ERROR, e);
    } finally {
        if (pwOut != null)
            pwOut.close();

        // 20130821, Bauke: save friendly name after session is gone
        if (_htSessionContext != null) {
            String sStatus = (String) _htSessionContext.get("status");
            String sAppId = (String) _htSessionContext.get("app_id");
            if ("del".equals(sStatus) && Utils.hasValue(sAppId)) {
                String sUF = ApplicationManager.getHandle().getFriendlyName(sAppId);
                HandlerTools.setEncryptedCookie(servletResponse, "requestor_friendly_name", sUF,
                        _configManager.getCookieDomain(), -1/*age*/, _systemLogger);
            }
        }
        _oSessionManager.finalSessionProcessing(_htSessionContext, true/*really do it*/);
    }
    return null;
}

From source file:oscar.oscarDemographic.pageUtil.DemographicExportAction4.java

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    String strEditable = oscarProperties.getProperty("ENABLE_EDIT_APPT_STATUS");

    DemographicExportForm defrm = (DemographicExportForm) form;
    String demographicNo = defrm.getDemographicNo();
    String setName = defrm.getPatientSet();
    String pgpReady = defrm.getPgpReady();
    boolean exPersonalHistory = WebUtils.isChecked(request, "exPersonalHistory");
    boolean exFamilyHistory = WebUtils.isChecked(request, "exFamilyHistory");
    boolean exPastHealth = WebUtils.isChecked(request, "exPastHealth");
    boolean exProblemList = WebUtils.isChecked(request, "exProblemList");
    boolean exRiskFactors = WebUtils.isChecked(request, "exRiskFactors");
    boolean exAllergiesAndAdverseReactions = WebUtils.isChecked(request, "exAllergiesAndAdverseReactions");
    boolean exMedicationsAndTreatments = WebUtils.isChecked(request, "exMedicationsAndTreatments");
    boolean exImmunizations = WebUtils.isChecked(request, "exImmunizations");
    boolean exLaboratoryResults = WebUtils.isChecked(request, "exLaboratoryResults");
    boolean exAppointments = WebUtils.isChecked(request, "exAppointments");
    boolean exClinicalNotes = WebUtils.isChecked(request, "exClinicalNotes");
    boolean exReportsReceived = WebUtils.isChecked(request, "exReportsReceived");
    boolean exAlertsAndSpecialNeeds = WebUtils.isChecked(request, "exAlertsAndSpecialNeeds");
    boolean exCareElements = WebUtils.isChecked(request, "exCareElements");

    List<String> list = new ArrayList<String>();
    if (demographicNo == null) {
        list = new DemographicSets().getDemographicSet(setName);
        if (list.isEmpty()) {
            Date asofDate = UtilDateUtilities.Today();
            RptDemographicReportForm frm = new RptDemographicReportForm();
            frm.setSavedQuery(setName);/*from  w ww . j ava2 s  .  c o  m*/
            RptDemographicQueryLoader demoL = new RptDemographicQueryLoader();
            frm = demoL.queryLoader(frm);
            frm.addDemoIfNotPresent();
            frm.setAsofDate(UtilDateUtilities.DateToString(asofDate));
            RptDemographicQueryBuilder demoQ = new RptDemographicQueryBuilder();
            ArrayList<ArrayList<String>> list2 = demoQ.buildQuery(frm,
                    UtilDateUtilities.DateToString(asofDate));
            for (ArrayList<String> listDemo : list2) {
                list.add(listDemo.get(0));
            }
        }
    } else {
        list.add(demographicNo);
    }

    String ffwd = "fail";
    String tmpDir = oscarProperties.getProperty("TMP_DIR");
    if (!Util.checkDir(tmpDir)) {
        logger.debug("Error! Cannot write to TMP_DIR - Check oscar.properties or dir permissions.");
    } else {
        XmlOptions options = new XmlOptions();
        options.put(XmlOptions.SAVE_PRETTY_PRINT);
        options.put(XmlOptions.SAVE_PRETTY_PRINT_INDENT, 3);
        options.put(XmlOptions.SAVE_AGGRESSIVE_NAMESPACES);

        HashMap<String, String> suggestedPrefix = new HashMap<String, String>();
        suggestedPrefix.put("cds_dt", "cdsd");
        options.setSaveSuggestedPrefixes(suggestedPrefix);
        options.setSaveOuter();

        ArrayList<File> files = new ArrayList<File>();
        exportError = new ArrayList<String>();
        for (String demoNo : list) {
            if (StringUtils.empty(demoNo)) {
                exportError.add("Error! No Demographic Number");
                continue;
            }

            // DEMOGRAPHICS
            DemographicData d = new DemographicData();

            org.oscarehr.common.model.Demographic demographic = d.getDemographic(demoNo);

            if (demographic.getPatientStatus() != null && demographic.getPatientStatus().equals("Contact-only"))
                continue;

            HashMap<String, String> demoExt = new HashMap<String, String>();
            demoExt.putAll(demographicExtDao.getAllValuesForDemo(demoNo));

            OmdCdsDocument omdCdsDoc = OmdCdsDocument.Factory.newInstance();
            OmdCdsDocument.OmdCds omdCds = omdCdsDoc.addNewOmdCds();
            PatientRecord patientRec = omdCds.addNewPatientRecord();
            Demographics demo = patientRec.addNewDemographics();

            demo.setUniqueVendorIdSequence(demoNo);
            entries.put(PATIENTID + exportNo, Integer.valueOf(demoNo));

            cdsDt.PersonNameStandard personName = demo.addNewNames();
            cdsDt.PersonNameStandard.LegalName legalName = personName.addNewLegalName();
            cdsDt.PersonNameStandard.LegalName.FirstName firstName = legalName.addNewFirstName();
            cdsDt.PersonNameStandard.LegalName.LastName lastName = legalName.addNewLastName();
            legalName.setNamePurpose(cdsDt.PersonNamePurposeCode.L);

            String name = StringUtils.noNull(demographic.getFirstName());
            if (StringUtils.filled(name)) {
                firstName.setPart(name);
                firstName.setPartType(cdsDt.PersonNamePartTypeCode.GIV);
                //            firstName.setPartQualifier(cdsDt.PersonNamePartQualifierCode.BR);
            } else {
                exportError.add("Error! No First Name for Patient " + demoNo);
            }
            name = StringUtils.noNull(demographic.getLastName());
            if (StringUtils.filled(name)) {
                lastName.setPart(name);
                lastName.setPartType(cdsDt.PersonNamePartTypeCode.FAMC);
                //            lastName.setPartQualifier(cdsDt.PersonNamePartQualifierCode.BR);
            } else {
                exportError.add("Error! No Last Name for Patient " + demoNo);
            }

            String title = demographic.getTitle();
            if (StringUtils.filled(title)) {
                if (title.equalsIgnoreCase("MISS"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.MISS);
                if (title.equalsIgnoreCase("MR"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.MR);
                if (title.equalsIgnoreCase("MRS"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.MRS);
                if (title.equalsIgnoreCase("MS"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.MS);
                if (title.equalsIgnoreCase("MSSR"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.MSSR);
                if (title.equalsIgnoreCase("PROF"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.PROF);
                if (title.equalsIgnoreCase("REEVE"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.REEVE);
                if (title.equalsIgnoreCase("REV"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.REV);
                if (title.equalsIgnoreCase("RT_HON"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.RT_HON);
                if (title.equalsIgnoreCase("SEN"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.SEN);
                if (title.equalsIgnoreCase("SGT"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.SGT);
                if (title.equalsIgnoreCase("SR"))
                    personName.setNamePrefix(cdsDt.PersonNamePrefixCode.SR);
            }

            String lang = demographic.getOfficialLanguage();
            if (StringUtils.filled(lang)) {
                if (lang.equalsIgnoreCase("English"))
                    demo.setPreferredOfficialLanguage(cdsDt.OfficialSpokenLanguageCode.ENG);
                else if (lang.equalsIgnoreCase("French"))
                    demo.setPreferredOfficialLanguage(cdsDt.OfficialSpokenLanguageCode.FRE);
            } else {
                exportError.add("Error! No Preferred Official Language for Patient " + demoNo);
            }

            lang = demographic.getSpokenLanguage();
            if (StringUtils.filled(lang) && Util.convertLanguageToCode(lang) != null) {
                demo.setPreferredSpokenLanguage(Util.convertLanguageToCode(lang));
            }

            String sex = demographic.getSex();
            if (cdsDt.Gender.Enum.forString(sex) != null) {
                demo.setGender(cdsDt.Gender.Enum.forString(sex));
            } else {
                exportError.add("Error! No Gender for Patient " + demoNo);
            }

            String sin = demographic.getSin();
            if (StringUtils.filled(sin) && sin.length() == 9) {
                demo.setSIN(sin);
            }

            //Enrolment Status (Roster Status)
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Demographics.Enrolment enrolment;
            String rosterStatus = demographic.getRosterStatus();
            String rosterDate = "";
            if (demographic.getRosterDate() != null)
                rosterDate = formatter.format(demographic.getRosterDate());
            String rosterTermDate = "";
            if (demographic.getRosterTerminationDate() != null)
                rosterTermDate = formatter.format(demographic.getRosterTerminationDate());

            if (StringUtils.filled(rosterStatus)) {
                rosterStatus = rosterStatus.equalsIgnoreCase("RO") ? "1" : "0";
                enrolment = demo.addNewEnrolment();
                enrolment.setEnrollmentStatus(cdsDt.EnrollmentStatus.Enum.forString(rosterStatus));
                if (rosterStatus.equals("1")) {
                    if (UtilDateUtilities.StringToDate(rosterDate) != null) {
                        enrolment.setEnrollmentDate(Util.calDate(rosterDate));
                    }
                } else {
                    if (UtilDateUtilities.StringToDate(rosterTermDate) != null)
                        enrolment.setEnrollmentTerminationDate(Util.calDate(rosterTermDate));
                    String termReason = demographic.getRosterTerminationReason();
                    if (StringUtils.filled(termReason))
                        enrolment.setTerminationReason(cdsDt.TerminationReasonCode.Enum.forString(termReason));
                }
            }

            //Enrolment Status history
            List<DemographicArchive> DAs = demoArchiveDao
                    .findRosterStatusHistoryByDemographicNo(Integer.valueOf(demoNo));
            String historyRS, historyRS1;
            Date historyRD, historyRD1, historyTD, historyTD1;
            for (int i = 0; i < DAs.size(); i++) {
                historyRS = StringUtils.noNull(DAs.get(i).getRosterStatus());
                historyRS1 = i < DAs.size() - 1 ? StringUtils.noNull(DAs.get(i + 1).getRosterStatus()) : "-1";
                historyRS = historyRS.equalsIgnoreCase("RO") ? "1" : "0";
                historyRS1 = historyRS1.equalsIgnoreCase("RO") ? "1" : "0";

                historyRD = DAs.get(i).getRosterDate();
                historyRD1 = i < DAs.size() - 1 ? DAs.get(i + 1).getRosterDate() : null;
                historyTD = DAs.get(i).getRosterTerminationDate();
                historyTD1 = i < DAs.size() - 1 ? DAs.get(i + 1).getRosterTerminationDate() : null;

                if (i == 0) { //check history info with current
                    String rd = UtilDateUtilities.DateToString(historyRD);
                    String td = UtilDateUtilities.DateToString(historyTD);
                    if (historyRS.equals(rosterStatus) && rd.equals(rosterDate) && td.equals(rosterTermDate))
                        continue;
                } else { //check history info with next
                    if (historyRS.equals(historyRS1)
                            && UtilDateUtilities.nullSafeCompare(historyRD, historyRD1) == 0
                            && UtilDateUtilities.nullSafeCompare(historyTD, historyTD1) == 0)
                        continue;
                }

                enrolment = demo.addNewEnrolment();
                enrolment.setEnrollmentStatus(cdsDt.EnrollmentStatus.Enum.forString(historyRS));
                if (historyRS.equals("1")) {
                    if (historyRD != null)
                        enrolment.setEnrollmentDate(Util.calDate(historyRD));
                } else {
                    if (historyTD != null)
                        enrolment.setEnrollmentTerminationDate(Util.calDate(historyTD));
                    String termReason = DAs.get(i).getRosterTerminationReason();
                    if (StringUtils.filled(termReason))
                        enrolment.setTerminationReason(cdsDt.TerminationReasonCode.Enum.forString(termReason));
                }
            }

            //Person Status (Patient Status)
            String patientStatus = StringUtils.noNull(demographic.getPatientStatus());
            Demographics.PersonStatusCode personStatusCode = demo.addNewPersonStatusCode();
            if (StringUtils.empty(patientStatus)) {
                patientStatus = "";
                exportError.add("Error! No Person Status Code for Patient " + demoNo);
            }
            if (patientStatus.equalsIgnoreCase("AC"))
                personStatusCode.setPersonStatusAsEnum(cdsDt.PersonStatus.A);
            else if (patientStatus.equalsIgnoreCase("IN"))
                personStatusCode.setPersonStatusAsEnum(cdsDt.PersonStatus.I);
            else if (patientStatus.equalsIgnoreCase("DE"))
                personStatusCode.setPersonStatusAsEnum(cdsDt.PersonStatus.D);
            else {
                if ("MO".equalsIgnoreCase(patientStatus))
                    patientStatus = "Moved";
                else if ("FI".equalsIgnoreCase(patientStatus))
                    patientStatus = "Fired";
                personStatusCode.setPersonStatusAsPlainText(patientStatus);
            }
            String patientStatusDate = "";
            if (demographic.getPatientStatusDate() != null)
                patientStatusDate = formatter.format(demographic.getPatientStatusDate());
            if (StringUtils.filled(patientStatusDate))
                demo.setPersonStatusDate(Util.calDate(patientStatusDate));

            //patient notes
            String demoNotes = d.getDemographicNotes(demoNo);
            if (StringUtils.filled(demoNotes))
                demo.setNoteAboutPatient(demoNotes);

            String dob = StringUtils.noNull(DemographicData.getDob(demographic, "-"));
            demo.setDateOfBirth(Util.calDate(dob));
            if (UtilDateUtilities.StringToDate(dob) == null) {
                exportError.add("Error! No Date Of Birth for Patient " + demoNo);
            } else if (UtilDateUtilities.StringToDate(dob) == null) {
                exportError.add("Not exporting invalid Date of Birth for Patient " + demoNo);
            }

            String chartNo = demographic.getChartNo();
            if (StringUtils.filled(chartNo))
                demo.setChartNumber(chartNo);

            String email = demographic.getEmail();
            if (StringUtils.filled(email))
                demo.setEmail(email);

            String providerNo = demographic.getProviderNo();
            if (StringUtils.filled(providerNo)) {
                Demographics.PrimaryPhysician pph = demo.addNewPrimaryPhysician();
                ProviderData prvd = new ProviderData(providerNo);
                if (StringUtils.noNull(prvd.getOhip_no()).length() <= 6)
                    pph.setOHIPPhysicianId(prvd.getOhip_no());
                Util.writeNameSimple(pph.addNewName(), prvd.getFirst_name(), prvd.getLast_name());
                String cpso = prvd.getPractitionerNo();
                if (cpso != null && cpso.length() == 5)
                    pph.setPrimaryPhysicianCPSO(cpso);
            }

            if (StringUtils.filled(demographic.getSin())) {
                demo.setSIN(demographic.getSin());
            }

            if (StringUtils.filled(demographic.getHin())) {
                cdsDt.HealthCard healthCard = demo.addNewHealthCard();

                healthCard.setNumber(demographic.getHin());
                if (Util.setProvinceCode(demographic.getHcType()) != null)
                    healthCard.setProvinceCode(Util.setProvinceCode(demographic.getHcType()));
                else
                    healthCard.setProvinceCode(cdsDt.HealthCardProvinceCode.X_70); //Asked, unknown
                if (healthCard.getProvinceCode() == null) {
                    exportError.add("Error! No Health Card Province Code for Patient " + demoNo);
                }
                if (StringUtils.filled(demographic.getVer()))
                    healthCard.setVersion(demographic.getVer());
                String HCRenewDate = "";
                if (demographic.getHcRenewDate() != null)
                    HCRenewDate = formatter.format(demographic.getHcRenewDate());
                if (UtilDateUtilities.StringToDate(HCRenewDate) != null) {
                    healthCard.setExpirydate(Util.calDate(HCRenewDate));
                }
            }
            if (StringUtils.filled(demographic.getAddress())) {
                cdsDt.Address addr = demo.addNewAddress();
                cdsDt.AddressStructured address = addr.addNewStructured();

                addr.setAddressType(cdsDt.AddressType.R);
                address.setLine1(demographic.getAddress());
                if (StringUtils.filled(demographic.getCity()) || StringUtils.filled(demographic.getProvince())
                        || StringUtils.filled(demographic.getPostal())) {
                    address.setCity(StringUtils.noNull(demographic.getCity()));
                    address.setCountrySubdivisionCode(Util.setCountrySubDivCode(demographic.getProvince()));
                    address.addNewPostalZipCode()
                            .setPostalCode(StringUtils.noNull(demographic.getPostal()).replace(" ", ""));
                }
            }

            boolean phoneExtTooLong = false;
            if (phoneNoValid(demographic.getPhone())) {
                phoneExtTooLong = addPhone(demographic.getPhone(), demoExt.get("hPhoneExt"),
                        cdsDt.PhoneNumberType.R, demo.addNewPhoneNumber());
                if (phoneExtTooLong) {
                    exportError.add("Home phone extension too long - trimmed for Patient " + demoNo);
                }
            }

            if (phoneNoValid(demographic.getPhone2())) {
                phoneExtTooLong = addPhone(demographic.getPhone2(), demoExt.get("wPhoneExt"),
                        cdsDt.PhoneNumberType.W, demo.addNewPhoneNumber());
                if (phoneExtTooLong) {
                    exportError.add("Work phone extension too long, export trimmed for Patient " + demoNo);
                }
            }

            if (phoneNoValid(demoExt.get("demo_cell"))) {
                addPhone(demoExt.get("demo_cell"), null, cdsDt.PhoneNumberType.C, demo.addNewPhoneNumber());
            }
            demoExt = null;

            if (oscarProperties.isPropertyActive("NEW_CONTACTS_UI")) {
                addDemographicContacts(demoNo, demo);
            } else {
                addDemographicRelationships(demoNo, demo);
            }

            List<CaseManagementNote> lcmn = cmm.getNotes(demoNo);

            //find all "header"; cms4 only
            List<CaseManagementNote> headers = new ArrayList<CaseManagementNote>();
            for (CaseManagementNote cmn : lcmn) {
                if (cmn.getNote() != null && cmn.getNote().startsWith("imported.cms4.2011.06")
                        && cmm.getLinkByNote(cmn.getId()).isEmpty())
                    headers.add(cmn);
            }

            for (CaseManagementNote cmn : lcmn) {
                String famHist = "", socHist = "", medHist = "", concerns = "", reminders = "",
                        riskFactors = "", encounter = "", annotation = "", summary = "";
                Set<CaseManagementIssue> sisu = cmn.getIssues();
                boolean systemIssue = false;
                for (CaseManagementIssue isu : sisu) {
                    String _issue = isu.getIssue() != null ? isu.getIssue().getCode() : "";
                    if (_issue.equals("SocHistory")) {
                        systemIssue = true;
                        socHist = cmn.getNote();
                        break;
                    } else if (_issue.equals("FamHistory")) {
                        systemIssue = true;
                        famHist = cmn.getNote();
                        break;
                    } else if (_issue.equals("MedHistory")) {
                        systemIssue = true;
                        medHist = cmn.getNote();
                        break;
                    } else if (_issue.equals("Concerns")) {
                        systemIssue = true;
                        concerns = cmn.getNote();
                        break;
                    } else if (_issue.equals("Reminders")) {
                        systemIssue = true;
                        reminders = cmn.getNote();
                        break;
                    } else if (_issue.equals("RiskFactors")) {
                        systemIssue = true;
                        riskFactors = cmn.getNote();
                        break;
                    } else
                        continue;
                }
                if (!systemIssue && cmm.getLinkByNote(cmn.getId()).isEmpty()) { //this is not an annotation
                    encounter = cmn.getNote();
                    if (encounter.startsWith("imported.cms4.2011.06"))
                        continue; //this is a "header", cms4 only
                }

                annotation = getNonDumpNote(CaseManagementNoteLink.CASEMGMTNOTE, cmn.getId(), null);
                List<CaseManagementNoteExt> cmeList = cmm.getExtByNote(cmn.getId());

                if (exPersonalHistory) {
                    // PERSONAL HISTORY (SocHistory)
                    if (StringUtils.filled(socHist)) {
                        summary = Util.addSummary("Personal History", socHist);
                        for (CaseManagementIssue isu : sisu) {
                            String codeSystem = isu.getIssue().getType();
                            if (!codeSystem.equals("system")) {
                                summary = Util.addSummary(summary, "Diagnosis",
                                        isu.getIssue().getDescription());
                            }
                        }
                        addOneEntry(PERSONALHISTORY);
                        boolean bSTARTDATE = false, bRESOLUTIONDATE = false;
                        for (CaseManagementNoteExt cme : cmeList) {
                            if (cme.getKeyVal().equals(CaseManagementNoteExt.STARTDATE)) {
                                if (bSTARTDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.STARTDATE,
                                            Util.readPartialDate(cme));
                                }
                                bSTARTDATE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.RESOLUTIONDATE)) {
                                if (bRESOLUTIONDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.RESOLUTIONDATE,
                                            Util.readPartialDate(cme));
                                }
                                bRESOLUTIONDATE = true;
                            }
                        }
                        summary = Util.addSummary(summary, "Notes", annotation);
                        patientRec.addNewPersonalHistory().setCategorySummaryLine(summary);
                    }
                }
                if (exFamilyHistory) {
                    // FAMILY HISTORY (FamHistory)
                    if (StringUtils.filled(famHist)) {
                        FamilyHistory fHist = patientRec.addNewFamilyHistory();
                        fHist.setProblemDiagnosisProcedureDescription(famHist);
                        summary = Util.addSummary("Problem Description", famHist);

                        boolean diagnosisAssigned = false;
                        for (CaseManagementIssue isu : sisu) {
                            String codeSystem = isu.getIssue().getType();
                            if (!codeSystem.equals("system")) {
                                if (diagnosisAssigned) {
                                    summary = Util.addSummary(summary, "Diagnosis",
                                            isu.getIssue().getDescription());
                                } else {
                                    cdsDt.StandardCoding diagnosis = fHist.addNewDiagnosisProcedureCode();
                                    diagnosis.setStandardCodingSystem(codeSystem);
                                    String code = codeSystem.equalsIgnoreCase("icd9")
                                            ? Util.formatIcd9(isu.getIssue().getCode())
                                            : isu.getIssue().getCode();
                                    diagnosis.setStandardCode(code);
                                    diagnosis.setStandardCodeDescription(isu.getIssue().getDescription());
                                    summary = Util.addSummary(summary, "Diagnosis",
                                            diagnosis.getStandardCodeDescription());
                                    diagnosisAssigned = true;
                                }
                            }
                        }
                        addOneEntry(FAMILYHISTORY);
                        boolean bSTARTDATE = false, bTREATMENT = false, bAGEATONSET = false,
                                bRELATIONSHIP = false, bLIFESTAGE = false;
                        for (CaseManagementNoteExt cme : cmeList) {
                            if (cme.getKeyVal().equals(CaseManagementNoteExt.STARTDATE)) {
                                if (bSTARTDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    Util.putPartialDate(fHist.addNewStartDate(), cme);
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.STARTDATE,
                                            Util.readPartialDate(cme));
                                }
                                bSTARTDATE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.TREATMENT)) {
                                if (bTREATMENT)
                                    continue;
                                if (StringUtils.filled(cme.getValue())) {
                                    fHist.setTreatment(cme.getValue());
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.TREATMENT,
                                            cme.getValue());
                                }
                                bTREATMENT = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.AGEATONSET)) {
                                if (bAGEATONSET)
                                    continue;
                                if (StringUtils.filled(cme.getValue())) {
                                    fHist.setAgeAtOnset(BigInteger.valueOf(Long.valueOf(cme.getValue())));
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.AGEATONSET,
                                            cme.getValue());
                                }
                                bAGEATONSET = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.RELATIONSHIP)) {
                                if (bRELATIONSHIP)
                                    continue;
                                if (StringUtils.filled(cme.getValue())) {
                                    fHist.setRelationship(cme.getValue());
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.RELATIONSHIP,
                                            cme.getValue());
                                }
                                bRELATIONSHIP = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.LIFESTAGE)) {
                                if (bLIFESTAGE)
                                    continue;
                                if ("NICTA".contains(cme.getValue()) && cme.getValue().length() == 1) {
                                    fHist.setLifeStage(cdsDt.LifeStage.Enum.forString(cme.getValue()));
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.LIFESTAGE,
                                            cme.getValue());
                                }
                                bLIFESTAGE = true;
                            }
                        }
                        if (StringUtils.filled(annotation)) {
                            fHist.setNotes(annotation);
                            summary = Util.addSummary(summary, "Notes", annotation);
                        }
                        fHist.setCategorySummaryLine(summary);
                    }
                }
                if (exPastHealth) {
                    // PAST HEALTH (MedHistory)
                    if (StringUtils.filled(medHist)) {
                        PastHealth pHealth = patientRec.addNewPastHealth();
                        summary = Util.addSummary("Problem Description", medHist);

                        boolean diagnosisAssigned = false;
                        for (CaseManagementIssue isu : sisu) {
                            String codeSystem = isu.getIssue().getType();
                            if (!codeSystem.equals("system")) {
                                if (diagnosisAssigned) {
                                    summary = Util.addSummary(summary, "Diagnosis",
                                            isu.getIssue().getDescription());
                                } else {
                                    cdsDt.StandardCoding diagnosis = pHealth.addNewDiagnosisProcedureCode();

                                    diagnosis.setStandardCodingSystem(codeSystem);
                                    String code = codeSystem.equalsIgnoreCase("icd9")
                                            ? Util.formatIcd9(isu.getIssue().getCode())
                                            : isu.getIssue().getCode();
                                    diagnosis.setStandardCode(code);
                                    diagnosis.setStandardCodeDescription(isu.getIssue().getDescription());
                                    summary = Util.addSummary(summary, "Diagnosis",
                                            diagnosis.getStandardCodeDescription());
                                    diagnosisAssigned = true;
                                }
                            }
                        }
                        addOneEntry(PASTHEALTH);
                        boolean bSTARTDATE = false, bRESOLUTIONDATE = false, bPROCEDUREDATE = false,
                                bLIFESTAGE = false;
                        for (CaseManagementNoteExt cme : cmeList) {
                            if (cme.getKeyVal().equals(CaseManagementNoteExt.STARTDATE)) {
                                if (bSTARTDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    Util.putPartialDate(pHealth.addNewOnsetOrEventDate(), cme);
                                    summary = Util.addSummary(summary, "Onset/Event Date",
                                            Util.readPartialDate(cme));
                                }
                                bSTARTDATE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.RESOLUTIONDATE)) {
                                if (bRESOLUTIONDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    Util.putPartialDate(pHealth.addNewResolvedDate(), cme);
                                    summary = Util.addSummary(summary, "Resolved Date",
                                            Util.readPartialDate(cme));
                                }
                                bRESOLUTIONDATE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.PROCEDUREDATE)) {
                                if (bPROCEDUREDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    Util.putPartialDate(pHealth.addNewProcedureDate(), cme);
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.PROCEDUREDATE,
                                            Util.readPartialDate(cme));
                                }
                                bPROCEDUREDATE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.LIFESTAGE)) {
                                if (bLIFESTAGE)
                                    continue;
                                if ("NICTA".contains(cme.getValue()) && cme.getValue().length() == 1) {
                                    pHealth.setLifeStage(cdsDt.LifeStage.Enum.forString(cme.getValue()));
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.LIFESTAGE,
                                            cme.getValue());
                                }
                                bLIFESTAGE = true;
                            }
                        }
                        pHealth.setPastHealthProblemDescriptionOrProcedures(medHist);
                        if (StringUtils.filled(annotation)) {
                            pHealth.setNotes(annotation);
                            summary = Util.addSummary(summary, "Notes", annotation);
                        }
                        pHealth.setCategorySummaryLine(summary);
                    }
                }
                if (exProblemList) {
                    // PROBLEM LIST (Concerns)
                    if (StringUtils.filled(concerns)) {
                        ProblemList pList = patientRec.addNewProblemList();
                        pList.setProblemDiagnosisDescription(concerns);
                        summary = Util.addSummary("Problem Diagnosis", concerns);

                        boolean diagnosisAssigned = false;
                        for (CaseManagementIssue isu : sisu) {
                            String codeSystem = isu.getIssue().getType();
                            if (!codeSystem.equals("system")) {
                                if (diagnosisAssigned) {
                                    summary = Util.addSummary(summary, "Diagnosis",
                                            isu.getIssue().getDescription());
                                } else {
                                    cdsDt.StandardCoding diagnosis = pList.addNewDiagnosisCode();
                                    diagnosis.setStandardCodingSystem(codeSystem);
                                    String code = codeSystem.equalsIgnoreCase("icd9")
                                            ? Util.formatIcd9(isu.getIssue().getCode())
                                            : isu.getIssue().getCode();
                                    diagnosis.setStandardCode(code);
                                    diagnosis.setStandardCodeDescription(isu.getIssue().getDescription());
                                    summary = Util.addSummary(summary, "Diagnosis",
                                            diagnosis.getStandardCodeDescription());
                                    diagnosisAssigned = true;
                                }
                            }
                        }
                        addOneEntry(PROBLEMLIST);
                        boolean bPROBLEMDESC = false, bSTARTDATE = false, bRESOLUTIONDATE = false,
                                bPROBLEMSTATUS = false, bLIFESTAGE = false;
                        for (CaseManagementNoteExt cme : cmeList) {
                            if (cme.getKeyVal().equals(CaseManagementNoteExt.PROBLEMDESC)) {
                                if (bPROBLEMDESC)
                                    continue;
                                if (StringUtils.filled(cme.getValue())) {
                                    pList.setProblemDescription(cme.getValue());
                                    summary = Util.addSummary(summary, "Problem Description", cme.getValue());
                                }
                                bPROBLEMDESC = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.STARTDATE)) {
                                if (bSTARTDATE)
                                    continue;
                                Util.putPartialDate(pList.addNewOnsetDate(), cme);
                                summary = Util.addSummary(summary, "Onset Date", Util.readPartialDate(cme));
                                if (cme.getDateValue() == null) {
                                    exportError
                                            .add("Error! No Onset Date for Problem List for Patient " + demoNo);
                                }
                                bSTARTDATE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.RESOLUTIONDATE)) {
                                if (bRESOLUTIONDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    Util.putPartialDate(pList.addNewResolutionDate(), cme);
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.RESOLUTIONDATE,
                                            Util.readPartialDate(cme));
                                }
                                bRESOLUTIONDATE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.LIFESTAGE)) {
                                if (bLIFESTAGE)
                                    continue;
                                if ("NICTA".contains(cme.getValue()) && cme.getValue().length() == 1) {
                                    pList.setLifeStage(cdsDt.LifeStage.Enum.forString(cme.getValue()));
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.LIFESTAGE,
                                            cme.getValue());
                                }
                                bLIFESTAGE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.PROBLEMSTATUS)) {
                                if (bPROBLEMSTATUS)
                                    continue;
                                if (StringUtils.filled(cme.getValue())) {
                                    pList.setProblemStatus(cme.getValue());
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.PROBLEMSTATUS,
                                            cme.getValue());
                                }
                                bPROBLEMSTATUS = true;
                            }
                        }

                        if (StringUtils.filled(annotation)) {
                            pList.setNotes(annotation);
                            summary = Util.addSummary(summary, "Notes", annotation);
                        }
                        pList.setCategorySummaryLine(summary);
                    }
                }
                if (exRiskFactors) {
                    // RISK FACTORS
                    if (StringUtils.filled(riskFactors)) {
                        RiskFactors rFact = patientRec.addNewRiskFactors();

                        if (riskFactors.length() > 120)
                            riskFactors = riskFactors.substring(0, 120);
                        rFact.setRiskFactor(riskFactors);

                        summary = Util.addSummary("Risk Factor", riskFactors);
                        addOneEntry(RISKFACTOR);

                        boolean bSTARTDATE = false, bRESOLUTIONDATE = false, bAGEATONSET = false,
                                bEXPOSUREDETAIL = false, bLIFESTAGE = false;
                        for (CaseManagementNoteExt cme : cmeList) {
                            if (cme.getKeyVal().equals(CaseManagementNoteExt.STARTDATE)) {
                                if (bSTARTDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    Util.putPartialDate(rFact.addNewStartDate(), cme);
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.STARTDATE,
                                            Util.readPartialDate(cme));
                                }
                                bSTARTDATE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.RESOLUTIONDATE)) {
                                if (bRESOLUTIONDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    Util.putPartialDate(rFact.addNewEndDate(), cme);
                                    summary = Util.addSummary(summary, "End Date", Util.readPartialDate(cme));
                                }
                                bRESOLUTIONDATE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.AGEATONSET)) {
                                if (bAGEATONSET)
                                    continue;
                                if (StringUtils.filled(cme.getValue())) {
                                    rFact.setAgeOfOnset(BigInteger.valueOf(Long.valueOf(cme.getValue())));
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.AGEATONSET,
                                            cme.getValue());
                                }
                                bAGEATONSET = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.LIFESTAGE)) {
                                if (bLIFESTAGE)
                                    continue;
                                if ("NICTA".contains(cme.getValue()) && cme.getValue().length() == 1) {
                                    rFact.setLifeStage(cdsDt.LifeStage.Enum.forString(cme.getValue()));
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.LIFESTAGE,
                                            cme.getValue());
                                }
                                bLIFESTAGE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.EXPOSUREDETAIL)) {
                                if (bEXPOSUREDETAIL)
                                    continue;
                                if (StringUtils.filled(cme.getValue())) {
                                    rFact.setExposureDetails(cme.getValue());
                                    summary = Util.addSummary(summary, CaseManagementNoteExt.EXPOSUREDETAIL,
                                            cme.getValue());
                                }
                                bEXPOSUREDETAIL = true;
                            }
                        }
                        if (StringUtils.filled(annotation)) {
                            rFact.setNotes(annotation);
                            summary = Util.addSummary(summary, "Notes", annotation);
                        }
                        for (CaseManagementIssue isu : sisu) {
                            String codeSystem = isu.getIssue().getType();
                            if (!codeSystem.equals("system")) {
                                summary = Util.addSummary(summary, "Diagnosis",
                                        isu.getIssue().getDescription());
                            }
                        }
                        rFact.setCategorySummaryLine(summary);
                    }
                }

                if (exClinicalNotes) {
                    // CLINCAL NOTES
                    if (StringUtils.filled(encounter)) {
                        ClinicalNotes cNote = patientRec.addNewClinicalNotes();
                        for (CaseManagementIssue isu : sisu) {
                            String codeSystem = isu.getIssue().getType();
                            if (!codeSystem.equals("system")) {
                                encounter = Util.addLine(encounter, "Diagnosis: ",
                                        isu.getIssue().getDescription());
                            }
                        }
                        cNote.setMyClinicalNotesContent(encounter);
                        addOneEntry(CLINICALNOTE);

                        Date createDate = cmn.getCreate_date();
                        String uuid;
                        for (CaseManagementNote header : headers) {
                            uuid = header.getNote().substring("imported.cms4.2011.06".length());
                            if (uuid.equals(cmn.getUuid())) {
                                createDate = header.getCreate_date();
                            }
                        }

                        //entered datetime
                        if (createDate != null) {
                            cNote.addNewEnteredDateTime().setFullDateTime(Util.calDate(createDate));
                        }

                        //event datetime
                        if (cmn.getObservation_date() != null) {
                            cNote.addNewEventDateTime()
                                    .setFullDateTime(Util.calDate(cmn.getObservation_date()));
                        }

                        List<CaseManagementNote> cmn_same = cmm.getNotesByUUID(cmn.getUuid());
                        for (CaseManagementNote cm_note : cmn_same) {

                            //participating providers
                            if (StringUtils.filled(cm_note.getProviderNo()) && !Util.isVerified(cm_note)) {
                                //participant info
                                ClinicalNotes.ParticipatingProviders pProvider = cNote
                                        .addNewParticipatingProviders();
                                ProviderData prvd = new ProviderData(cm_note.getProviderNo());
                                Util.writeNameSimple(pProvider.addNewName(),
                                        StringUtils.noNull(prvd.getFirst_name()),
                                        StringUtils.noNull(prvd.getLast_name()));
                                if (StringUtils.noNull(prvd.getOhip_no()).length() <= 6)
                                    pProvider.setOHIPPhysicianId(prvd.getOhip_no());

                                //note created datetime
                                cdsDt.DateTimeFullOrPartial noteCreatedDateTime = pProvider
                                        .addNewDateTimeNoteCreated();
                                if (cmn.getUpdate_date() != null)
                                    noteCreatedDateTime.setFullDateTime(Util.calDate(cm_note.getUpdate_date()));
                                else
                                    noteCreatedDateTime.setFullDateTime(Util.calDate(new Date()));
                            }

                            //reviewing providers
                            if (StringUtils.filled(cm_note.getSigning_provider_no())
                                    && Util.isVerified(cm_note)) {
                                //reviewer info
                                ClinicalNotes.NoteReviewer noteReviewer = cNote.addNewNoteReviewer();
                                ProviderData prvd = new ProviderData(cm_note.getSigning_provider_no());
                                Util.writeNameSimple(noteReviewer.addNewName(), prvd.getFirst_name(),
                                        prvd.getLast_name());
                                if (StringUtils.noNull(prvd.getOhip_no()).length() <= 6)
                                    noteReviewer.setOHIPPhysicianId(prvd.getOhip_no());

                                //note reviewed datetime
                                cdsDt.DateTimeFullOrPartial noteReviewedDateTime = noteReviewer
                                        .addNewDateTimeNoteReviewed();
                                if (cm_note.getUpdate_date() != null)
                                    noteReviewedDateTime
                                            .setFullDateTime(Util.calDate(cm_note.getUpdate_date()));
                                else
                                    noteReviewer.addNewDateTimeNoteReviewed()
                                            .setFullDateTime(Util.calDate(new Date()));
                            }
                        }
                    }
                }

                if (exAlertsAndSpecialNeeds) {
                    // ALERTS AND SPECIAL NEEDS (Reminders)
                    if (StringUtils.filled(reminders)) {
                        AlertsAndSpecialNeeds alerts = patientRec.addNewAlertsAndSpecialNeeds();
                        alerts.setAlertDescription(reminders);
                        addOneEntry(ALERT);

                        summary = Util.addSummary("Alert Description", reminders);
                        boolean bSTARTDATE = false, bRESOLUTIONDATE = false;
                        for (CaseManagementNoteExt cme : cmeList) {
                            if (cme.getKeyVal().equals(CaseManagementNoteExt.STARTDATE)) {
                                if (bSTARTDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    Util.putPartialDate(alerts.addNewDateActive(), cme);
                                    summary = Util.addSummary(summary, "Date Active",
                                            Util.readPartialDate(cme));
                                }
                                bSTARTDATE = true;
                            } else if (cme.getKeyVal().equals(CaseManagementNoteExt.RESOLUTIONDATE)) {
                                if (bRESOLUTIONDATE)
                                    continue;
                                if (cme.getDateValue() != null) {
                                    Util.putPartialDate(alerts.addNewEndDate(), cme);
                                    summary = Util.addSummary(summary, "End Date", Util.readPartialDate(cme));
                                }
                                bRESOLUTIONDATE = true;
                            }
                        }
                        if (StringUtils.filled(annotation)) {
                            alerts.setNotes(annotation);
                            summary = Util.addSummary(summary, "Notes", annotation);
                        }
                        alerts.setCategorySummaryLine(summary);
                    }
                }
            }

            if (exAllergiesAndAdverseReactions) {
                // ALLERGIES & ADVERSE REACTIONS
                Allergy[] allergies = RxPatientData.getPatient(demoNo).getActiveAllergies();
                String dateFormat = null, annotation = null;
                for (int j = 0; j < allergies.length; j++) {
                    AllergiesAndAdverseReactions alr = patientRec.addNewAllergiesAndAdverseReactions();
                    Allergy allergy = allergies[j];
                    String aSummary = "";
                    addOneEntry(ALLERGY);

                    String allergyDescription = allergy.getDescription();
                    if (StringUtils.filled(allergyDescription)) {
                        alr.setOffendingAgentDescription(allergyDescription);
                        aSummary = Util.addSummary("Offending Agent Description", allergyDescription);
                    }
                    String regionalId = allergy.getRegionalIdentifier();
                    if (StringUtils.filled(regionalId) && !regionalId.trim().equalsIgnoreCase("null")) {
                        cdsDt.DrugCode drugCode = alr.addNewCode();
                        drugCode.setCodeType("DIN");
                        drugCode.setCodeValue(regionalId);
                        aSummary = Util.addSummary(aSummary, "DIN", regionalId);
                    }
                    String typeCode = String.valueOf(allergy.getTypeCode());
                    if (StringUtils.filled(typeCode)) {
                        if (typeCode.equals("0")) {
                            //alr.setReactionType(cdsDt.AdverseReactionType.AL);
                            alr.setPropertyOfOffendingAgent(cdsDt.PropertyOfOffendingAgent.ND);
                        } else {
                            //alr.setReactionType(cdsDt.AdverseReactionType.AR);
                            if (typeCode.equals("13")) {
                                alr.setPropertyOfOffendingAgent(cdsDt.PropertyOfOffendingAgent.DR);
                            } else {
                                alr.setPropertyOfOffendingAgent(cdsDt.PropertyOfOffendingAgent.UK);
                            }
                        }
                        aSummary = Util.addSummary(aSummary, "Property of Offending Agent",
                                alr.getPropertyOfOffendingAgent().toString());
                    }
                    String allergyReaction = allergy.getReaction();
                    if (StringUtils.filled(allergyReaction)) {
                        alr.setReaction(allergyReaction);
                        aSummary = Util.addSummary(aSummary, "Reaction", allergyReaction);
                    }
                    String severity = allergy.getSeverityOfReaction();
                    if (StringUtils.filled(severity)) {
                        if (severity.equals("1")) {
                            alr.setSeverity(cdsDt.AdverseReactionSeverity.MI);
                        } else if (severity.equals("2")) {
                            alr.setSeverity(cdsDt.AdverseReactionSeverity.MO);
                        } else if (severity.equals("3")) {
                            alr.setSeverity(cdsDt.AdverseReactionSeverity.LT);
                        }
                        if (alr.getSeverity() != null)
                            aSummary = Util.addSummary(aSummary, "Adverse Reaction Severity",
                                    alr.getSeverity().toString());
                    }
                    if (allergy.getStartDate() != null) {
                        dateFormat = partialDateDao.getFormat(PartialDate.ALLERGIES,
                                allergies[j].getAllergyId(), PartialDate.ALLERGIES_STARTDATE);
                        Util.putPartialDate(alr.addNewStartDate(), allergy.getStartDate(), dateFormat);
                        aSummary = Util.addSummary(aSummary, "Start Date",
                                partialDateDao.getDatePartial(allergy.getStartDate(), dateFormat));
                    }
                    if (allergy.getLifeStage() != null && "NICTA".contains(allergy.getLifeStage())
                            && allergy.getLifeStage().length() == 1) {
                        alr.setLifeStage(cdsDt.LifeStage.Enum.forString(allergy.getLifeStage()));
                        aSummary = Util.addSummary(aSummary, "Life Stage at Onset", allergy.getLifeStageDesc());
                    }

                    if (allergies[j].getEntryDate() != null) {
                        dateFormat = partialDateDao.getFormat(PartialDate.ALLERGIES,
                                allergies[j].getAllergyId(), PartialDate.ALLERGIES_ENTRYDATE);
                        Util.putPartialDate(alr.addNewRecordedDate(), allergies[j].getEntryDate(), dateFormat);
                        aSummary = Util.addSummary(aSummary, "Recorded Date",
                                partialDateDao.getDatePartial(allergies[j].getEntryDate(), dateFormat));
                    }

                    annotation = getNonDumpNote(CaseManagementNoteLink.ALLERGIES,
                            (long) allergies[j].getAllergyId(), null);
                    if (StringUtils.filled(annotation)) {
                        alr.setNotes(annotation);
                        aSummary = Util.addSummary(aSummary, "Notes", annotation);
                    }

                    if (StringUtils.empty(aSummary)) {
                        exportError.add(
                                "Error! No Category Summary Line (Allergies & Adverse Reactions) for Patient "
                                        + demoNo + " (" + (j + 1) + ")");
                    }
                    alr.setCategorySummaryLine(aSummary);
                }
            }

            if (exImmunizations) {
                // IMMUNIZATIONS
                ArrayList<Map<String, Object>> prevList = PreventionData.getPreventionData(demoNo);
                String imSummary;
                for (int k = 0; k < prevList.size(); k++) {
                    imSummary = null;
                    HashMap<String, Object> a = new HashMap<String, Object>();
                    a.putAll(prevList.get(k));
                    Immunizations immu = patientRec.addNewImmunizations();
                    HashMap<String, Object> extraData = new HashMap<String, Object>();
                    extraData.putAll(PreventionData.getPreventionById((String) a.get("id")));
                    if (StringUtils.filled((String) extraData.get("manufacture")))
                        immu.setManufacturer((String) extraData.get("manufacture"));
                    if (StringUtils.filled((String) extraData.get("lot")))
                        immu.setLotNumber((String) extraData.get("lot"));
                    if (StringUtils.filled((String) extraData.get("route")))
                        immu.setRoute((String) extraData.get("route"));
                    if (StringUtils.filled((String) extraData.get("location")))
                        immu.setSite((String) extraData.get("location"));
                    if (StringUtils.filled((String) extraData.get("dose")))
                        immu.setDose((String) extraData.get("dose"));
                    if (StringUtils.filled((String) extraData.get("comments")))
                        immu.setNotes((String) extraData.get("comments"));

                    String prevType = Util.getImmunizationType((String) a.get("type"));
                    if (cdsDt.ImmunizationType.Enum.forString(prevType) != null) {
                        immu.setImmunizationType(cdsDt.ImmunizationType.Enum.forString(prevType));
                    } else {
                        exportError.add("Error! No matching type for Immunization " + a.get("type")
                                + " for Patient " + demoNo + " (" + (k + 1) + ")");
                    }

                    if (StringUtils.filled((String) extraData.get("name")))
                        immu.setImmunizationName((String) extraData.get("name"));
                    else {
                        exportError.add("Error! No Name for Immunization " + prevType + " for Patient " + demoNo
                                + " (" + (k + 1) + ")");
                        if (StringUtils.filled(prevType)) {
                            immu.setImmunizationName(prevType);
                            imSummary = Util.addSummary("Immunization Name", prevType);
                        } else
                            immu.setImmunizationName("");
                    }
                    addOneEntry(IMMUNIZATION);

                    String refused = (String) a.get("refused");
                    if (StringUtils.empty(refused)) {
                        immu.addNewRefusedFlag();
                        exportError.add("Error! No Refused Flag for Patient " + demoNo + " (" + (k + 1) + ")");
                    } else {
                        immu.addNewRefusedFlag().setBoolean(Util.convert10toboolean(refused));
                        imSummary = Util.addSummary(imSummary, "Refused Flag",
                                Util.convert10toboolean(refused) ? "Y" : "N");
                    }

                    String preventionDate = (String) a.get("prevention_date");
                    if (UtilDateUtilities.StringToDate(preventionDate) != null) {
                        immu.addNewDate().setFullDate(Util.calDate(preventionDate));
                        imSummary = Util.addSummary(imSummary, "Date", preventionDate);
                    }

                    imSummary = Util.addSummary(imSummary, "Manufacturer", immu.getManufacturer());
                    imSummary = Util.addSummary(imSummary, "Lot No", immu.getLotNumber());
                    imSummary = Util.addSummary(imSummary, "Route", immu.getRoute());
                    imSummary = Util.addSummary(imSummary, "Site", immu.getSite());
                    imSummary = Util.addSummary(imSummary, "Dose", immu.getDose());
                    imSummary = Util.addSummary(imSummary, "Notes", immu.getNotes());

                    if (StringUtils.empty(imSummary)) {
                        exportError.add("Error! No Category Summary Line (Immunization) for Patient " + demoNo
                                + " (" + (k + 1) + ")");
                    }
                    immu.setCategorySummaryLine(StringUtils.noNull(imSummary));
                }
            }

            if (exMedicationsAndTreatments) {
                // MEDICATIONS & TREATMENTS
                RxPrescriptionData prescriptData = new RxPrescriptionData();
                RxPrescriptionData.Prescription[] arr = null;
                String annotation = null;
                arr = prescriptData.getPrescriptionsByPatient(Integer.parseInt(demoNo));
                for (int p = 0; p < arr.length; p++) {
                    MedicationsAndTreatments medi = patientRec.addNewMedicationsAndTreatments();
                    String mSummary = "";
                    if (arr[p].getWrittenDate() != null) {
                        String dateFormat = partialDateDao.getFormat(PartialDate.DRUGS, arr[p].getDrugId(),
                                PartialDate.DRUGS_WRITTENDATE);
                        Util.putPartialDate(medi.addNewPrescriptionWrittenDate(), arr[p].getWrittenDate(),
                                dateFormat);
                        mSummary = Util.addSummary("Prescription Written Date",
                                partialDateDao.getDatePartial(arr[p].getWrittenDate(), dateFormat));
                    }
                    if (arr[p].getRxDate() != null) {
                        medi.addNewStartDate().setFullDate(Util.calDate(arr[p].getRxDate()));
                        mSummary = Util.addSummary(mSummary, "Start Date",
                                UtilDateUtilities.DateToString(arr[p].getRxDate(), "yyyy-MM-dd"));
                    }
                    String regionalId = arr[p].getRegionalIdentifier();
                    if (StringUtils.filled(regionalId)) {
                        medi.setDrugIdentificationNumber(regionalId);
                        mSummary = Util.addSummary(mSummary, "DIN", regionalId);
                    }
                    String drugName = arr[p].getBrandName();
                    if (StringUtils.filled(drugName)) {
                        medi.setDrugName(drugName);
                        mSummary = Util.addSummary(mSummary, "Drug Name", drugName);
                    } else {
                        drugName = arr[p].getCustomName();
                        if (StringUtils.filled(drugName)) {
                            medi.setDrugDescription(drugName);
                            mSummary = Util.addSummary(mSummary, "Drug Description", drugName);
                        } else {
                            exportError.add(
                                    "Error! No medication name for Patient " + demoNo + " (" + (p + 1) + ")");
                        }
                    }
                    addOneEntry(MEDICATION);

                    /* no need:
                    DrugReasonDao drugReasonDao = (DrugReasonDao) SpringUtils.getBean("drugReasonDao");
                    List<DrugReason> drugReasons = drugReasonDao.getReasonsForDrugID(arr[p].getDrugId(), true);
                    if (drugReasons.size()>0 && StringUtils.filled(drugReasons.get(0).getCode()))
                       medi.setProblemCode(drugReasons.get(0).getCode());
                     *
                     */

                    if (StringUtils.filled(arr[p].getDosage())) {
                        String[] strength = arr[p].getDosage().split(" ");

                        cdsDt.DrugMeasure drugM = medi.addNewStrength();
                        if (Util.leadingNum(strength[0]).equals(strength[0])) {//amount & unit separated by space
                            drugM.setAmount(strength[0]);
                            if (strength.length > 1)
                                drugM.setUnitOfMeasure(strength[1]);
                            else
                                drugM.setUnitOfMeasure("unit"); //UnitOfMeasure cannot be null

                        } else {//amount & unit not separated, probably e.g. 50mg / 2tablet
                            if (strength.length > 1 && strength[1].equals("/")) {
                                if (strength.length > 2) {
                                    String unit1 = Util.leadingNum(strength[2]).equals("") ? "1"
                                            : Util.leadingNum(strength[2]);
                                    String unit2 = Util.trailingTxt(strength[2]).equals("") ? "unit"
                                            : Util.trailingTxt(strength[2]);

                                    drugM.setAmount(Util.leadingNum(strength[0]) + "/" + unit1);
                                    drugM.setUnitOfMeasure(Util.trailingTxt(strength[0]) + "/" + unit2);
                                }
                            } else {
                                drugM.setAmount(Util.leadingNum(strength[0]));
                                drugM.setUnitOfMeasure(Util.trailingTxt(strength[0]));
                            }
                        }
                        mSummary = Util.addSummary(mSummary, "Strength",
                                drugM.getAmount() + " " + drugM.getUnitOfMeasure());
                    }

                    String drugForm = arr[p].getDrugForm();
                    if (StringUtils.filled(drugForm)) {
                        medi.setForm(drugForm);
                        mSummary = Util.addSummary(mSummary, "Form", drugForm);
                    }

                    //Process dosage export
                    Float dosageValue = arr[p].getTakeMin();
                    if (dosageValue == 0) { //takemin=0, try takemax
                        dosageValue = arr[p].getTakeMax();
                    }
                    String drugUnit = StringUtils.noNull(arr[p].getUnit());

                    if (drugUnit.equalsIgnoreCase(getDosageUnit(arr[p].getDosage()))) {
                        //drug unit should not be same as dosage unit
                        //check drug form to see if it matches the following list

                        if (StringUtils.containsIgnoreCase(drugForm, "capsule"))
                            drugUnit = "capsule";
                        else if (StringUtils.containsIgnoreCase(drugForm, "drop"))
                            drugUnit = "drop";
                        else if (StringUtils.containsIgnoreCase(drugForm, "dosing"))
                            drugUnit = "dosing";
                        else if (StringUtils.containsIgnoreCase(drugForm, "grobule"))
                            drugUnit = "grobule";
                        else if (StringUtils.containsIgnoreCase(drugForm, "granule"))
                            drugUnit = "granule";
                        else if (StringUtils.containsIgnoreCase(drugForm, "patch"))
                            drugUnit = "patch";
                        else if (StringUtils.containsIgnoreCase(drugForm, "pellet"))
                            drugUnit = "pellet";
                        else if (StringUtils.containsIgnoreCase(drugForm, "pill"))
                            drugUnit = "pill";
                        else if (StringUtils.containsIgnoreCase(drugForm, "tablet"))
                            drugUnit = "tablet";

                        if (drugUnit.equals(arr[p].getUnit())) {
                            //drugUnit not changed by the above
                            //export dosage as "take * dosageValue"
                            dosageValue *= getDosageValue(arr[p].getDosage());
                        }
                    }

                    //export dosage
                    medi.setDosage(dosageValue.toString());
                    if (StringUtils.filled(drugUnit))
                        medi.setDosageUnitOfMeasure(drugUnit);
                    mSummary = Util.addSummary(mSummary, "Dosage", dosageValue + " " + drugUnit);

                    if (StringUtils.filled(arr[p].getSpecialInstruction())) {
                        medi.setPrescriptionInstructions(arr[p].getSpecialInstruction());
                        mSummary = Util.addSummary(mSummary, "Prescription Instructions",
                                arr[p].getSpecialInstruction());
                    }

                    if (StringUtils.filled(arr[p].getRoute())) {
                        medi.setRoute(arr[p].getRoute());
                        mSummary = Util.addSummary(mSummary, "Route", arr[p].getRoute());
                    }
                    if (StringUtils.filled(arr[p].getFreqDisplay())) {
                        medi.setFrequency(arr[p].getFreqDisplay());
                        mSummary = Util.addSummary(mSummary, "Frequency", arr[p].getFreqDisplay());
                    }
                    String duration = arr[p].getDuration();
                    if (StringUtils.filled(duration)) {
                        String durunit = StringUtils.noNull(arr[p].getDurationUnit());
                        Integer fctr = 1;
                        if (durunit.equals("W"))
                            fctr = 7;
                        else if (durunit.equals("M"))
                            fctr = 30;

                        if (NumberUtils.isDigits(duration)) {
                            duration = String.valueOf(Integer.parseInt(duration) * fctr);
                            medi.setDuration(duration);
                            mSummary = Util.addSummary(mSummary, "Duration", duration + " Day(s)");
                        }
                    }
                    if (StringUtils.filled(arr[p].getQuantity())) {
                        medi.setQuantity(arr[p].getQuantity());
                        mSummary = Util.addSummary(mSummary, "Quantity", arr[p].getQuantity());
                    }
                    /* no need:
                    if (arr[p].getNosubs()) medi.setSubstitutionNotAllowed("Y");
                    else medi.setSubstitutionNotAllowed("N");
                    mSummary = Util.addSummary(mSummary, "Substitution not Allowed", arr[p].getNosubs()?"Yes":"No");
                     *
                     */

                    if (StringUtils.filled(medi.getDrugName())
                            || StringUtils.filled(medi.getDrugIdentificationNumber())) {
                        medi.setNumberOfRefills(String.valueOf(arr[p].getRepeat()));
                        mSummary = Util.addSummary(mSummary, "Number of Refills",
                                String.valueOf(arr[p].getRepeat()));
                    }
                    if (StringUtils.filled(arr[p].getETreatmentType())) {
                        medi.setTreatmentType(arr[p].getETreatmentType());
                        mSummary = Util.addSummary(mSummary, "Treatment Type", arr[p].getETreatmentType());
                    }
                    /* no need:
                    if (StringUtils.filled(arr[p].getRxStatus())) {
                       medi.setPrescriptionStatus(arr[p].getRxStatus());
                       mSummary = Util.addSummary(mSummary, "Prescription Status", arr[p].getRxStatus());
                    }
                    /* no need:
                    if (arr[p].getDispenseInterval()!=null) {
                       medi.setDispenseInterval(String.valueOf(arr[p].getDispenseInterval()));
                       mSummary = Util.addLine(mSummary, "Dispense Interval", arr[p].getDispenseInterval().toString());
                    }
                     *
                     */
                    if (arr[p].getRefillDuration() != null) {
                        medi.setRefillDuration(String.valueOf(arr[p].getRefillDuration()));
                        mSummary = Util.addSummary(mSummary, "Refill Duration",
                                arr[p].getRefillDuration().toString());
                    }
                    if (arr[p].getRefillQuantity() != null) {
                        medi.setRefillQuantity(String.valueOf(arr[p].getRefillQuantity()));
                        mSummary = Util.addSummary(mSummary, "Refill Quantity",
                                arr[p].getRefillQuantity().toString());
                    }

                    medi.addNewLongTermMedication().setBoolean(arr[p].getLongTerm());
                    mSummary = Util.addSummary(mSummary, "Long Term Medication",
                            arr[p].getLongTerm() ? "Yes" : "No");

                    medi.addNewPastMedications().setBoolean(arr[p].getPastMed());
                    mSummary = Util.addSummary(mSummary, "Past Medcation", arr[p].getPastMed() ? "Yes" : "No");

                    cdsDt.YnIndicatorAndBlank pc = medi.addNewPatientCompliance();
                    if (arr[p].getPatientCompliance() == null) {
                        pc.setBlank(cdsDt.Blank.X);
                    } else {
                        String patientCompliance = arr[p].getPatientCompliance() ? "Yes" : "No";
                        pc.setBoolean(arr[p].getPatientCompliance());
                        mSummary = Util.addSummary(mSummary, "Patient Compliance", patientCompliance);
                    }

                    String outsideProviderName = arr[p].getOutsideProviderName();
                    if (StringUtils.filled(outsideProviderName)) {
                        MedicationsAndTreatments.PrescribedBy pcb = medi.addNewPrescribedBy();
                        String ohip = arr[p].getOutsideProviderOhip();
                        if (ohip != null && ohip.trim().length() <= 6)
                            pcb.setOHIPPhysicianId(ohip.trim());
                        Util.writeNameSimple(pcb.addNewName(), outsideProviderName);
                        mSummary = Util.addSummary(mSummary, "Prescribed by",
                                StringUtils.noNull(outsideProviderName));
                    } else {
                        String prescribeProvider = arr[p].getProviderNo();
                        if (StringUtils.filled(prescribeProvider)) {
                            MedicationsAndTreatments.PrescribedBy pcb = medi.addNewPrescribedBy();
                            ProviderData prvd = new ProviderData(prescribeProvider);
                            String ohip = prvd.getOhip_no();
                            if (ohip != null && ohip.trim().length() <= 6)
                                pcb.setOHIPPhysicianId(ohip.trim());
                            Util.writeNameSimple(pcb.addNewName(), prvd.getFirst_name(), prvd.getLast_name());
                            mSummary = Util.addSummary(mSummary, "Prescribed by",
                                    StringUtils.noNull(prvd.getFirst_name()) + " "
                                            + StringUtils.noNull(prvd.getLast_name()));
                        }
                    }

                    /* no need:
                    data = arr[p].isNonAuthoritative() ? "Y" : "N";
                    medi.setNonAuthoritativeIndicator(data);
                    mSummary = Util.addSummary(mSummary, "Non-Authoritative", data);
                            
                    /* no need:
                    medi.setPrescriptionIdentifier(String.valueOf(arr[p].getDrugId()));
                    mSummary = Util.addSummary(mSummary, "Prescription Identifier", medi.getPrescriptionIdentifier());
                     *
                     */

                    annotation = getNonDumpNote(CaseManagementNoteLink.DRUGS, (long) arr[p].getDrugId(), null);
                    if (StringUtils.filled(annotation)) {
                        medi.setNotes(annotation);
                        mSummary = Util.addSummary(mSummary, "Notes", annotation);
                    }

                    if (StringUtils.empty(mSummary))
                        exportError
                                .add("Error! No Category Summary Line (Medications & Treatments) for Patient "
                                        + demoNo + " (" + (p + 1) + ")");
                    medi.setCategorySummaryLine(mSummary);
                }
                arr = null;
            }

            if (exLaboratoryResults) {
                // LABORATORY RESULTS
                List<LabMeasurements> labMeaList = ImportExportMeasurements.getLabMeasurements(demoNo);
                String annotation = null;
                for (LabMeasurements labMea : labMeaList) {
                    LaboratoryResults labResults = patientRec.addNewLaboratoryResults();

                    //lab test code, test name, test name reported by lab
                    if (StringUtils.filled(labMea.getExtVal("identifier")))
                        labResults.setLabTestCode(labMea.getExtVal("identifier"));
                    if (StringUtils.filled(labMea.getExtVal("name_internal")))
                        labResults.setTestName(labMea.getExtVal("name_internal"));
                    if (StringUtils.filled(labMea.getExtVal("name")))
                        labResults.setTestNameReportedByLab(labMea.getExtVal("name"));

                    //laboratory name
                    labResults.setLaboratoryName(StringUtils.noNull(labMea.getExtVal("labname")));
                    addOneEntry(LABS);
                    if (StringUtils.empty(labResults.getLaboratoryName())) {
                        exportError.add("Error! No Laboratory Name for Lab Test " + labResults.getLabTestCode()
                                + " for Patient " + demoNo);
                    }

                    // lab collection datetime
                    cdsDt.DateTimeFullOrPartial collDate = labResults.addNewCollectionDateTime();
                    String sDateTime = labMea.getExtVal("datetime");
                    if (StringUtils.filled(sDateTime)) {
                        collDate.setFullDateTime(Util.calDate(sDateTime));
                    } else {
                        exportError.add("Error! No Collection Datetime for Lab Test "
                                + labResults.getLabTestCode() + " for Patient " + demoNo);
                        collDate.setFullDate(Util.calDate("0001-01-01"));
                    }

                    //lab normal/abnormal flag
                    labResults.setResultNormalAbnormalFlag(cdsDt.ResultNormalAbnormalFlag.U);
                    String abnormalFlag = StringUtils.noNull(labMea.getExtVal("abnormal"));
                    if (abnormalFlag.equals("A") || abnormalFlag.equals("L"))
                        labResults.setResultNormalAbnormalFlag(cdsDt.ResultNormalAbnormalFlag.Y);
                    if (abnormalFlag.equals("N"))
                        labResults.setResultNormalAbnormalFlag(cdsDt.ResultNormalAbnormalFlag.N);

                    //lab unit of measure
                    String measureData = StringUtils.noNull(labMea.getMeasure().getDataField());
                    if (StringUtils.filled(measureData)) {
                        LaboratoryResults.Result result = labResults.addNewResult();
                        if (measureData.length() > 120) {
                            measureData = measureData.substring(0, 120);
                            exportError.add("Error! Result text length > 120 - truncated; Lab Test "
                                    + labResults.getLabTestCode() + " for Patient " + demoNo);
                        }
                        result.setValue(measureData);
                        measureData = labMea.getExtVal("unit");
                        if (StringUtils.filled(measureData))
                            result.setUnitOfMeasure(measureData);
                    }

                    //lab accession number
                    String accessionNo = StringUtils.noNull(labMea.getExtVal("accession"));
                    if (StringUtils.filled(accessionNo)) {
                        labResults.setAccessionNumber(accessionNo);
                    }

                    //notes from lab
                    String labComments = StringUtils.noNull(labMea.getExtVal("comments"));
                    if (StringUtils.filled(labComments)) {
                        labResults.setNotesFromLab(Util.replaceTags(labComments));
                    }

                    //lab reference range
                    String range = StringUtils.noNull(labMea.getExtVal("range"));
                    String min = StringUtils.noNull(labMea.getExtVal("minimum"));
                    String max = StringUtils.noNull(labMea.getExtVal("maximum"));
                    LaboratoryResults.ReferenceRange refRange = labResults.addNewReferenceRange();
                    if (StringUtils.filled(range))
                        refRange.setReferenceRangeText(range);
                    else {
                        if (StringUtils.filled(min))
                            refRange.setLowLimit(min);
                        if (StringUtils.filled(max))
                            refRange.setHighLimit(max);
                    }

                    //lab requisition datetime
                    /*
                    HashMap<String,Object> link = LabRequestReportLink.getLinkByReport("hl7TextMessage", Long.valueOf(lab_no));
                    Date reqDate = (Date) link.get("request_date");
                    */
                    String reqDate = labMea.getExtVal("request_datetime");
                    if (StringUtils.filled(reqDate))
                        labResults.addNewLabRequisitionDateTime().setFullDateTime(Util.calDate(reqDate));

                    //OLIS test result status
                    String olis_status = labMea.getExtVal("olis_status");
                    if (StringUtils.filled(olis_status))
                        labResults.setOLISTestResultStatus(olis_status);

                    Integer labTable = CaseManagementNoteLink.LABTEST;
                    String lab_no = labMea.getExtVal("lab_no");
                    if (StringUtils.empty(lab_no)) {
                        lab_no = labMea.getExtVal("lab_ppid");
                        labTable = CaseManagementNoteLink.LABTEST2;
                    }
                    if (StringUtils.filled(lab_no)) {

                        //lab annotation
                        String other_id = StringUtils.noNull(labMea.getExtVal("other_id"));
                        annotation = getNonDumpNote(labTable, Long.valueOf(lab_no), other_id);
                        if (StringUtils.filled(annotation))
                            labResults.setPhysiciansNotes(annotation);

                        //                 String info = labRoutingInfo.get("comment"); <--for whole report, may refer to >1 lab results

                        //lab reviewer
                        HashMap<String, Object> labRoutingInfo = new HashMap<String, Object>();
                        if (labTable.equals(CaseManagementNoteLink.LABTEST))
                            labRoutingInfo.putAll(ProviderLabRouting.getInfo(lab_no, "HL7"));
                        else
                            labRoutingInfo.putAll(ProviderLabRouting.getInfo(lab_no, "CML"));

                        String timestamp = (String) labRoutingInfo.get("timestamp");
                        if (UtilDateUtilities.StringToDate(timestamp, "yyyy-MM-dd HH:mm:ss") != null) {
                            LaboratoryResults.ResultReviewer reviewer = labResults.addNewResultReviewer();
                            reviewer.addNewDateTimeResultReviewed().setFullDateTime(Util.calDate(timestamp));

                            //reviewer name
                            cdsDt.PersonNameSimple reviewerName = reviewer.addNewName();
                            String lab_provider_no = (String) labRoutingInfo.get("provider_no");
                            if (!"0".equals(lab_provider_no)) {
                                ProviderData pvd = new ProviderData(lab_provider_no);
                                Util.writeNameSimple(reviewerName, pvd.getFirst_name(), pvd.getLast_name());
                                if (StringUtils.noNull(pvd.getOhip_no()).length() <= 6)
                                    reviewer.setOHIPPhysicianId(pvd.getOhip_no());
                            }
                        }
                    }
                }
            }

            if (exAppointments) {
                // APPOINTMENTS
                OscarSuperManager oscarSuperManager = (OscarSuperManager) SpringUtils
                        .getBean("oscarSuperManager");
                List<Object> appts = oscarSuperManager.populate("appointmentDao", "export_appt",
                        new String[] { demoNo });
                ApptData ap = null;
                for (int j = 0; j < appts.size(); j++) {
                    ap = (ApptData) appts.get(j);
                    Appointments aptm = patientRec.addNewAppointments();
                    cdsDt.DateFullOrPartial apDate = aptm.addNewAppointmentDate();
                    apDate.setFullDate(Util.calDate(ap.getAppointment_date()));
                    if (ap.getAppointment_date() == null) {
                        exportError.add("Error! No Appointment Date (" + j + ") for Patient " + demoNo);
                    }

                    String startTime = ap.getStart_time();
                    aptm.setAppointmentTime(Util.calDate(ap.getStart_time()));
                    addOneEntry(APPOINTMENT);
                    if (UtilDateUtilities.StringToDate(startTime, "HH:mm:ss") == null) {
                        exportError.add("Error! No Appointment Time (" + (j + 1) + ") for Patient " + demoNo);
                    }

                    long dLong = (ap.getDateEndTime().getTime() - ap.getDateStartTime().getTime()) / 60000 + 1;
                    BigInteger duration = BigInteger.valueOf(dLong); //duration in minutes
                    aptm.setDuration(duration);

                    if (StringUtils.filled(ap.getStatus())) {
                        ApptStatusData asd = new ApptStatusData();
                        asd.setApptStatus(ap.getStatus());
                        String msg = null;
                        if (strEditable != null && strEditable.equalsIgnoreCase("yes"))
                            msg = asd.getTitle();
                        else
                            msg = getResources(request).getMessage(asd.getTitle());

                        if (StringUtils.filled(msg)) {
                            aptm.setAppointmentStatus(msg);
                        } else {
                            exportError.add("Error! No matching message for appointment status code: "
                                    + ap.getStatus());
                        }
                    }
                    if (StringUtils.filled(ap.getReason())) {
                        aptm.setAppointmentPurpose(ap.getReason());
                    }
                    if (StringUtils.filled(ap.getProviderFirstName())
                            || StringUtils.filled(ap.getProviderLastName())) {
                        Appointments.Provider prov = aptm.addNewProvider();

                        if (StringUtils.noNull(ap.getOhipNo()).length() <= 6)
                            prov.setOHIPPhysicianId(ap.getOhipNo());
                        Util.writeNameSimple(prov.addNewName(), ap.getProviderFirstName(),
                                ap.getProviderLastName());
                    }
                    if (StringUtils.filled(ap.getNotes())) {
                        aptm.setAppointmentNotes(ap.getNotes());
                    }
                }
            }

            if (exReportsReceived) {
                // REPORTS RECEIVED
                ArrayList<EDoc> edoc_list = EDocUtil.listDemoDocs(demoNo);
                String annotation = null;
                for (int j = 0; j < edoc_list.size(); j++) {
                    EDoc edoc = edoc_list.get(j);

                    File f = new File(edoc.getFilePath());
                    if (!f.exists()) {
                        exportError.add("Error! Document \"" + f.getName() + "\" does not exist!");
                    } else if (f.length() > Runtime.getRuntime().freeMemory()) {
                        exportError.add("Error! Document \"" + f.getName()
                                + "\" too big to be exported. Not enough memory!");
                    } else {
                        ReportsReceived rpr = patientRec.addNewReportsReceived();
                        rpr.setFormat(cdsDt.ReportFormat.TEXT);

                        cdsDt.ReportContent rpc = rpr.addNewContent();
                        InputStream in = new FileInputStream(f);
                        byte[] b = new byte[(int) f.length()];

                        int offset = 0, numRead = 0;
                        while ((numRead = in.read(b, offset, b.length - offset)) >= 0 && offset < b.length)
                            offset += numRead;

                        if (offset < b.length)
                            throw new IOException("Could not completely read file " + f.getName());
                        in.close();
                        if (edoc.getContentType() != null && edoc.getContentType().startsWith("text")) {
                            String str = new String(b);
                            rpc.setTextContent(str);
                            rpr.setFormat(cdsDt.ReportFormat.TEXT);
                            addOneEntry(REPORTTEXT);
                        } else {
                            rpc.setMedia(b);
                            rpr.setFormat(cdsDt.ReportFormat.BINARY);
                            addOneEntry(REPORTBINARY);
                        }

                        String contentType = Util.mimeToExt(edoc.getContentType());
                        if (StringUtils.empty(contentType))
                            contentType = cutExt(edoc.getFileName());
                        if (StringUtils.empty(contentType))
                            exportError.add("Error! No File Extension&Version info for Document \""
                                    + edoc.getFileName() + "\"");
                        rpr.setFileExtensionAndVersion(contentType);

                        String docClass = edoc.getDocClass();
                        if (cdsDt.ReportClass.Enum.forString(docClass) != null) {
                            rpr.setClass1(cdsDt.ReportClass.Enum.forString(docClass));
                        } else {
                            exportError.add("Error! No Class Type for Document \"" + edoc.getFileName() + "\"");
                            rpr.setClass1(cdsDt.ReportClass.OTHER_LETTER);
                        }
                        String docSubClass = edoc.getDocSubClass();
                        if (StringUtils.filled(docSubClass)) {
                            rpr.setSubClass(docSubClass);
                        }
                        String observationDate = edoc.getObservationDate();
                        if (UtilDateUtilities.StringToDate(observationDate) != null) {
                            rpr.addNewEventDateTime().setFullDate(Util.calDate(observationDate));
                        } else {
                            exportError.add("Not exporting invalid Event Date (Reports) for Patient " + demoNo
                                    + " (" + (j + 1) + ")");
                        }
                        String dateTimeStamp = edoc.getDateTimeStamp();
                        if (UtilDateUtilities.StringToDate(dateTimeStamp, "yyyy-MM-dd HH:mm:ss") != null) {
                            rpr.addNewReceivedDateTime().setFullDateTime(Util.calDate(dateTimeStamp));
                        } else {
                            exportError.add("Not exporting invalid Received DateTime (Reports) for Patient "
                                    + demoNo + " (" + (j + 1) + ")");
                        }
                        String reviewDateTime = edoc.getReviewDateTime();
                        if (UtilDateUtilities.StringToDate(reviewDateTime, "yyyy-MM-dd HH:mm:ss") != null) {
                            ReportsReceived.ReportReviewed reportReviewed = rpr.addNewReportReviewed();
                            reportReviewed.addNewDateTimeReportReviewed()
                                    .setFullDate(Util.calDate(reviewDateTime));
                            Util.writeNameSimple(reportReviewed.addNewName(), edoc.getReviewerName());
                            reviewDateTime = StringUtils.noNull(edoc.getReviewerOhip());
                            if (reviewDateTime.length() <= 6)
                                reportReviewed.setReviewingOHIPPhysicianId(reviewDateTime);
                        }
                        Util.writeNameSimple(rpr.addNewSourceAuthorPhysician().addNewAuthorName(),
                                edoc.getSource());

                        if (StringUtils.filled(edoc.getSourceFacility()))
                            rpr.setSourceFacility(edoc.getSourceFacility());

                        if (edoc.getDocId() == null)
                            continue;

                        annotation = getNonDumpNote(CaseManagementNoteLink.DOCUMENT,
                                Long.valueOf(edoc.getDocId()), null);
                        if (StringUtils.filled(annotation))
                            rpr.setNotes(annotation);
                    }
                }

                //HRM reports
                List<HRMDocumentToDemographic> hrmDocToDemographics = hrmDocToDemographicDao
                        .findByDemographicNo(demoNo);
                for (HRMDocumentToDemographic hrmDocToDemographic : hrmDocToDemographics) {
                    String hrmDocumentId = hrmDocToDemographic.getHrmDocumentId();
                    List<HRMDocument> hrmDocs = hrmDocDao.findById(Integer.valueOf(hrmDocumentId));
                    for (HRMDocument hrmDoc : hrmDocs) {
                        String reportFile = hrmDoc.getReportFile();
                        if (StringUtils.empty(reportFile))
                            continue;

                        File hrmFile = new File(reportFile);
                        if (!hrmFile.exists()) {
                            exportError.add("Error! HRM report file '" + reportFile
                                    + "' not exists! HRM report not exported.");
                            continue;
                        }

                        ReadHRMFile hrm = new ReadHRMFile(reportFile);
                        for (int i = 0; i < hrm.getReportsReceivedTotal(); i++) {
                            ReportsReceived rpr = patientRec.addNewReportsReceived();

                            //Message Unique ID
                            if (hrm.getTransactionMessageUniqueID(i) != null)
                                rpr.setMessageUniqueID(hrm.getTransactionMessageUniqueID(i));

                            HashMap<String, String> reportAuthor = hrm.getReportAuthorPhysician(i);
                            HashMap<String, Object> reportContent = hrm.getReportContent(i);
                            HashMap<String, String> reportStrings = hrm.getReportStrings(i);
                            HashMap<String, Calendar> reportDates = hrm.getReportDates(i);

                            if (reportAuthor != null) {
                                cdsDt.PersonNameSimple author = rpr.addNewSourceAuthorPhysician()
                                        .addNewAuthorName();
                                if (StringUtils.filled(reportAuthor.get("firstname")))
                                    author.setFirstName(reportAuthor.get("firstname"));
                                if (StringUtils.filled(reportAuthor.get("lastname")))
                                    author.setLastName(reportAuthor.get("lastname"));
                            }

                            if (reportContent != null) {
                                if (reportContent != null) {
                                    if (reportContent.get("textcontent") != null) {
                                        cdsDt.ReportContent content = rpr.addNewContent();
                                        content.setTextContent((String) reportContent.get("textcontent"));
                                    } else if (reportContent.get("media") != null) {
                                        cdsDt.ReportContent content = rpr.addNewContent();
                                        content.setMedia((byte[]) reportContent.get("media"));
                                    }
                                }
                            }

                            String reviewerId = null;
                            Calendar reviewDate = null;

                            if (reportStrings != null) {
                                //Format
                                if (reportStrings.get("format") != null) {
                                    if (reportStrings.get("format").equalsIgnoreCase("Text")) {
                                        rpr.setFormat(cdsDt.ReportFormat.TEXT);
                                    } else {
                                        rpr.setFormat(cdsDt.ReportFormat.BINARY);
                                    }
                                } else {
                                    if (rpr.getContent().getMedia() != null)
                                        rpr.setFormat(cdsDt.ReportFormat.BINARY);
                                    else
                                        rpr.setFormat(cdsDt.ReportFormat.TEXT);
                                    exportError.add("Error! No Format for HRM report! Patient " + demoNo + " ("
                                            + (i + 1) + ")");
                                }

                                //Class
                                if (reportStrings.get("class") != null) {
                                    rpr.setClass1(cdsDt.ReportClass.Enum
                                            .forString(formatHrmEnum(reportStrings.get("class"))));

                                } else {
                                    rpr.setClass1(cdsDt.ReportClass.OTHER_LETTER);
                                    exportError.add(
                                            "Error! No Class for HRM report! Export as 'Other Letter'. Patient "
                                                    + demoNo + " (" + (i + 1) + ")");
                                }

                                //Media
                                if (reportStrings.get("media") != null) {
                                    rpr.setMedia(cdsDt.ReportMedia.Enum
                                            .forString(formatHrmEnum(reportStrings.get("media"))));
                                }

                                //Subclass
                                if (reportStrings.get("subclass") != null) {
                                    rpr.setSubClass(reportStrings.get("subclass"));
                                }

                                //File extension & version
                                if (reportStrings.get("fileextension&version") != null) {
                                    rpr.setFileExtensionAndVersion(reportStrings.get("fileextension&version"));
                                }

                                //HRM Result Status
                                if (reportStrings.get("resultstatus") != null) {
                                    rpr.setHRMResultStatus(reportStrings.get("resultstatus"));
                                }

                                //Sending Facility ID
                                if (reportStrings.get("sendingfacility") != null) {
                                    rpr.setSendingFacilityId(reportStrings.get("sendingfacility"));
                                }

                                //Sending Facility Report Number
                                if (reportStrings.get("sendingfacilityreportnumber") != null) {
                                    rpr.setSendingFacilityReport(
                                            reportStrings.get("sendingfacilityreportnumber"));
                                }

                                //Reviewing OHIP Physician ID
                                reviewerId = reportStrings.get("reviewingohipphysicianid");
                            }

                            //report dates
                            if (reportDates != null) {
                                if (reportDates.get("eventdatetime") != null) {
                                    rpr.addNewEventDateTime().setFullDateTime(reportDates.get("eventdatetime"));
                                }
                                if (reportDates.get("receiveddatetime") != null) {
                                    rpr.addNewReceivedDateTime()
                                            .setFullDateTime(reportDates.get("receiveddatetime"));
                                }
                                reviewDate = reportDates.get("revieweddatetime");
                            }

                            //Source Facility
                            if (hrmDoc.getSourceFacility() != null) {
                                rpr.setSourceFacility(hrmDoc.getSourceFacility());
                            }

                            //reviewing info
                            if (reviewerId != null && reviewDate != null) {
                                ReportsReceived.ReportReviewed reviewed = rpr.addNewReportReviewed();
                                reviewed.addNewName();
                                reviewed.setReviewingOHIPPhysicianId(reviewerId);
                                reviewed.addNewDateTimeReportReviewed().setFullDate(reviewDate);
                            }

                            //Notes
                            List<HRMDocumentComment> comments = hrmDocCommentDao
                                    .getCommentsForDocument(hrmDocumentId);
                            String notes = null;
                            for (HRMDocumentComment comment : comments) {
                                notes = Util.addLine(notes, comment.getComment());
                            }
                            if (StringUtils.filled(notes))
                                rpr.setNotes(notes);

                            //OBR Content
                            for (int j = 0; j < hrm.getReportOBRContentTotal(i); j++) {
                                HashMap<String, String> obrStrings = hrm.getReportOBRStrings(i, j);
                                Calendar obrObservationDateTime = hrm.getReportOBRObservationDateTime(i, j);

                                ReportsReceived.OBRContent obrContent = rpr.addNewOBRContent();

                                if (obrStrings != null) {

                                    //Accompanying Description
                                    if (obrStrings.get("accompanyingdescription") != null) {
                                        obrContent.setAccompanyingDescription(
                                                obrStrings.get("accompanyingdescription"));
                                    }

                                    //Accompanying Mnemonic
                                    if (obrStrings.get("accompanyingmnemonic") != null) {
                                        obrContent.setAccompanyingMnemonic(
                                                obrStrings.get("accompanyingmnemonic"));
                                    }

                                    //Accompanying Subclass
                                    if (obrStrings.get("accompanyingsubclass") != null) {
                                        obrContent.setAccompanyingSubClass(
                                                obrStrings.get("accompanyingsubclass"));
                                    }
                                }

                                //OBR Observation Datetime
                                if (obrObservationDateTime != null) {
                                    obrContent.addNewObservationDateTime()
                                            .setFullDateTime(obrObservationDateTime);
                                }
                            }
                        }
                    }
                }
            }

            if (exCareElements) {
                //CARE ELEMENTS
                List<Measurements> measList = ImportExportMeasurements.getMeasurements(demoNo);
                CareElements careElm = null;
                if (measList.size() > 0)
                    careElm = patientRec.addNewCareElements();
                for (Measurements meas : measList) {
                    if (meas.getType().equals("HT")) { //Height in cm
                        cdsDt.Height height = careElm.addNewHeight();
                        height.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add("Error! No Date for Height (id=" + meas.getId() + ") for Patient "
                                    + demoNo);
                        }
                        height.setHeight(meas.getDataField());
                        height.setHeightUnit(cdsDt.Height.HeightUnit.CM);
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("WT")
                            && meas.getMeasuringInstruction().equalsIgnoreCase("in kg")) { //Weight in kg
                        cdsDt.Weight weight = careElm.addNewWeight();
                        weight.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add("Error! No Date for Weight (id=" + meas.getId() + ") for Patient "
                                    + demoNo);
                        }
                        weight.setWeight(meas.getDataField());
                        weight.setWeightUnit(cdsDt.Weight.WeightUnit.KG);
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("WAIS") || meas.getType().equals("WC")) { //Waist Circumference in cm
                        cdsDt.WaistCircumference waist = careElm.addNewWaistCircumference();
                        waist.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add("Error! No Date for Waist Circumference (id=" + meas.getId()
                                    + ") for Patient " + demoNo);
                        }
                        waist.setWaistCircumference(meas.getDataField());
                        waist.setWaistCircumferenceUnit(cdsDt.WaistCircumference.WaistCircumferenceUnit.CM);
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("BP")) { //Blood Pressure
                        cdsDt.BloodPressure bloodp = careElm.addNewBloodPressure();
                        bloodp.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add("Error! No Date for Blood Pressure (id=" + meas.getId()
                                    + ") for Patient " + demoNo);
                        }
                        String[] sdbp = meas.getDataField().split("/");
                        bloodp.setSystolicBP(sdbp[0]);
                        bloodp.setDiastolicBP(sdbp[1]);
                        bloodp.setBPUnit(cdsDt.BloodPressure.BPUnit.MM_HG);
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("POSK")) { //Packs of Cigarettes per day
                        cdsDt.SmokingPacks smokp = careElm.addNewSmokingPacks();
                        smokp.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add("Error! No Date for Smoking Packs (id=" + meas.getId()
                                    + ") for Patient " + demoNo);
                        }
                        smokp.setPerDay(new BigDecimal(meas.getDataField()));
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("SKST")) { //Smoking Status
                        cdsDt.SmokingStatus smoks = careElm.addNewSmokingStatus();
                        smoks.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add("Error! No Date for Smoking Status (id=" + meas.getId()
                                    + ") for Patient " + demoNo);
                        }
                        smoks.setStatus(Util.yn(meas.getDataField()));
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("SMBG")) { //Self Monitoring Blood Glucose
                        cdsDt.SelfMonitoringBloodGlucose bloodg = careElm.addNewSelfMonitoringBloodGlucose();
                        bloodg.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add("Error! No Date for Self-monitoring Blood Glucose (id="
                                    + meas.getId() + ") for Patient " + demoNo);
                        }
                        bloodg.setSelfMonitoring(Util.yn(meas.getDataField()));
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("DMME")) { //Diabetes Education
                        cdsDt.DiabetesEducationalSelfManagement des = careElm
                                .addNewDiabetesEducationalSelfManagement();
                        des.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add("Error! No Date for Diabetes Educational Self-management (id="
                                    + meas.getId() + ") for Patient " + demoNo);
                        }
                        des.setEducationalTrainingPerformed(Util.yn(meas.getDataField()));
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("SMCD")) { //Self Management Challenges
                        cdsDt.DiabetesSelfManagementChallenges dsc = careElm
                                .addNewDiabetesSelfManagementChallenges();
                        dsc.setCodeValue(cdsDt.DiabetesSelfManagementChallenges.CodeValue.X_44941_3);
                        dsc.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add("Error! No Date for Diabetes Self-management Challenges (id="
                                    + meas.getId() + ") for Patient " + demoNo);
                        }
                        dsc.setChallengesIdentified(Util.yn(meas.getDataField()));
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("MCCN")) { //Motivation Counseling Completed Nutrition
                        cdsDt.DiabetesMotivationalCounselling dmc = careElm
                                .addNewDiabetesMotivationalCounselling();
                        dmc.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add(
                                    "Error! No Date for Diabetes Motivational Counselling on Nutrition (id="
                                            + meas.getId() + ") for Patient " + demoNo);
                        }
                        dmc.setCounsellingPerformed(
                                cdsDt.DiabetesMotivationalCounselling.CounsellingPerformed.NUTRITION);
                        if (Util.yn(meas.getDataField()) == cdsDt.YnIndicatorsimple.N) {
                            exportError.add("Patient " + demoNo
                                    + " didn't do Diabetes Counselling (Nutrition) on "
                                    + UtilDateUtilities.DateToString(meas.getDateObserved(), "yyyy-MM-dd"));
                        }
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("MCCE")) { //Motivation Counseling Completed Exercise
                        cdsDt.DiabetesMotivationalCounselling dmc = careElm
                                .addNewDiabetesMotivationalCounselling();
                        dmc.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError
                                    .add("Error! No Date for Diabetes Motivational Counselling on Exercise (id="
                                            + meas.getId() + ") for Patient " + demoNo);
                        }
                        dmc.setCounsellingPerformed(
                                cdsDt.DiabetesMotivationalCounselling.CounsellingPerformed.EXERCISE);
                        if (Util.yn(meas.getDataField()) == cdsDt.YnIndicatorsimple.N) {
                            exportError.add("Patient " + demoNo
                                    + " didn't do Diabetes Counselling (Exercise) on "
                                    + UtilDateUtilities.DateToString(meas.getDateObserved(), "yyyy-MM-dd"));
                        }
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("MCCS")) { //Motivation Counseling Completed Smoking Cessation
                        cdsDt.DiabetesMotivationalCounselling dmc = careElm
                                .addNewDiabetesMotivationalCounselling();
                        dmc.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add(
                                    "Error! No Date for Diabetes Motivational Counselling on Smoking Cessation (id="
                                            + meas.getId() + ") for Patient " + demoNo);
                        }
                        dmc.setCounsellingPerformed(
                                cdsDt.DiabetesMotivationalCounselling.CounsellingPerformed.SMOKING_CESSATION);
                        if (Util.yn(meas.getDataField()) == cdsDt.YnIndicatorsimple.N) {
                            exportError.add("Patient " + demoNo
                                    + " didn't do Diabetes Counselling (Smoking Cessation) on "
                                    + UtilDateUtilities.DateToString(meas.getDateObserved(), "yyyy-MM-dd"));
                        }
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("MCCO")) { //Motivation Counseling Completed Other
                        cdsDt.DiabetesMotivationalCounselling dmc = careElm
                                .addNewDiabetesMotivationalCounselling();
                        dmc.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add(
                                    "Error! No Date for Diabetes Motivational Counselling on Other Matters (id="
                                            + meas.getId() + ") for Patient " + demoNo);
                        }
                        dmc.setCounsellingPerformed(
                                cdsDt.DiabetesMotivationalCounselling.CounsellingPerformed.OTHER);
                        if (Util.yn(meas.getDataField()) == cdsDt.YnIndicatorsimple.N) {
                            exportError.add("Patient " + demoNo + " didn't do Diabetes Counselling (Other) on "
                                    + UtilDateUtilities.DateToString(meas.getDateObserved(), "yyyy-MM-dd"));
                        }
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("EYEE")) { //Dilated Eye Exam
                        cdsDt.DiabetesComplicationScreening dcs = careElm
                                .addNewDiabetesComplicationsScreening();
                        dcs.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError
                                    .add("Error! No Date for Diabetes Complication Screening on Eye Exam (id="
                                            + meas.getId() + ") for Patient " + demoNo);
                        }
                        dcs.setExamCode(cdsDt.DiabetesComplicationScreening.ExamCode.X_32468_1);
                        if (Util.yn(meas.getDataField()) == cdsDt.YnIndicatorsimple.N) {
                            exportError.add("Patient " + demoNo
                                    + " didn't do Diabetes Complications Screening (Retinal Exam) on "
                                    + UtilDateUtilities.DateToString(meas.getDateObserved(), "yyyy-MM-dd"));
                        }
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("FTE")) { //Foot Exam
                        cdsDt.DiabetesComplicationScreening dcs = careElm
                                .addNewDiabetesComplicationsScreening();
                        dcs.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError
                                    .add("Error! No Date for Diabetes Complication Screening on Foot Exam (id="
                                            + meas.getId() + ") for Patient " + demoNo);
                        }
                        dcs.setExamCode(cdsDt.DiabetesComplicationScreening.ExamCode.X_11397_7);
                        if (Util.yn(meas.getDataField()) == cdsDt.YnIndicatorsimple.N) {
                            exportError.add("Patient " + demoNo
                                    + " didn't do Diabetes Complications Screening (Foot Exam) on "
                                    + UtilDateUtilities.DateToString(meas.getDateObserved(), "yyyy-MM-dd"));
                        }
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("FTLS")) { // Foot Exam Test Loss of Sensation (Neurological Exam)
                        cdsDt.DiabetesComplicationScreening dcs = careElm
                                .addNewDiabetesComplicationsScreening();
                        dcs.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add(
                                    "Error! No Date for Diabetes Complication Screening on Neurological Exam (id="
                                            + meas.getId() + ") for Patient " + demoNo);
                        }
                        dcs.setExamCode(cdsDt.DiabetesComplicationScreening.ExamCode.NEUROLOGICAL_EXAM);
                        if (Util.yn(meas.getDataField()) == cdsDt.YnIndicatorsimple.N) {
                            exportError.add("Patient " + demoNo
                                    + " didn't do Diabetes Complications Screening (Neurological Exam) on "
                                    + UtilDateUtilities.DateToString(meas.getDateObserved(), "yyyy-MM-dd"));
                        }
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("CGSD")) { //Collaborative Goal Setting
                        cdsDt.DiabetesSelfManagementCollaborative dsco = careElm
                                .addNewDiabetesSelfManagementCollaborative();
                        dsco.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add(
                                    "Error! No Date for Diabetes Self-management Collaborative Goal Setting (id="
                                            + meas.getId() + ") for Patient " + demoNo);
                        }
                        dsco.setCodeValue(cdsDt.DiabetesSelfManagementCollaborative.CodeValue.X_44943_9);
                        dsco.setDocumentedGoals(meas.getDataField());
                        addOneEntry(CAREELEMENTS);
                    } else if (meas.getType().equals("HYPE")) { //Hypoglycemic Episodes
                        cdsDt.HypoglycemicEpisodes he = careElm.addNewHypoglycemicEpisodes();
                        he.setDate(Util.calDate(meas.getDateObserved()));
                        if (meas.getDateObserved() == null) {
                            exportError.add("Error! No Date for Hypoglycemic Episodes (id=" + meas.getId()
                                    + ") for Patient " + demoNo);
                        }
                        he.setNumOfReportedEpisodes(new BigInteger(meas.getDataField()));
                        addOneEntry(CAREELEMENTS);
                    }
                }
            }
            exportNo++;

            //export file to temp directory
            try {
                File directory = new File(tmpDir);
                if (!directory.exists()) {
                    throw new Exception("Temporary Export Directory does not exist!");
                }

                //Standard format for xml exported file : PatientFN_PatientLN_PatientUniqueID_DOB (DOB: ddmmyyyy)
                String expFile = demographic.getFirstName() + "_" + demographic.getLastName();
                expFile += "_" + demoNo;
                expFile += "_" + demographic.getDateOfBirth() + demographic.getMonthOfBirth()
                        + demographic.getYearOfBirth();
                files.add(new File(directory, expFile + ".xml"));
            } catch (Exception e) {
                logger.error("Error", e);
            }
            try {
                omdCdsDoc.save(files.get(files.size() - 1), options);
            } catch (IOException ex) {
                logger.error("Error", ex);
                throw new Exception(
                        "Cannot write .xml file(s) to export directory.\n Please check directory permissions.");
            }
        }

        //create ReadMe.txt & ExportEvent.log
        files.add(makeReadMe(files));
        files.add(makeExportLog(files.get(0).getParentFile()));

        //zip all export files
        String zipName = files.get(0).getName().replace(".xml", ".zip");
        if (setName != null)
            zipName = "export_" + setName.replace(" ", "") + "_" + UtilDateUtilities.getToday("yyyyMMddHHmmss")
                    + ".zip";
        //   if (setName!=null) zipName = "export_"+setName.replace(" ","")+"_"+UtilDateUtilities.getToday("yyyyMMddHHmmss")+".pgp";
        if (!Util.zipFiles(files, zipName, tmpDir)) {
            logger.debug("Error! Failed to zip export files");
        }

        if (pgpReady.equals("Yes")) {
            //PGP encrypt zip file
            PGPEncrypt pgp = new PGPEncrypt();
            if (pgp.encrypt(zipName, tmpDir)) {
                Util.downloadFile(zipName + ".pgp", tmpDir, response);
                Util.cleanFile(zipName + ".pgp", tmpDir);
                ffwd = "success";
            } else {
                request.getSession().setAttribute("pgp_ready", "No");
            }
        } else {
            logger.info("Warning: PGP Encryption NOT available - unencrypted file exported!");
            Util.downloadFile(zipName, tmpDir, response);
            ffwd = "success";
        }

        //Remove zip & export files from temp dir
        Util.cleanFile(zipName, tmpDir);
        Util.cleanFiles(files);
    }

    return mapping.findForward(ffwd);
}

From source file:org.apache.rampart.builder.AsymmetricBindingBuilder.java

private void doEncryptBeforeSig(RampartMessageData rmd) throws RampartException {

    long t0 = 0, t1 = 0, t2 = 0;
    if (tlog.isDebugEnabled()) {
        t0 = System.currentTimeMillis();
    }//from w w w.ja  va 2s .com
    RampartPolicyData rpd = rmd.getPolicyData();
    Document doc = rmd.getDocument();
    RampartConfig config = rpd.getRampartConfig();

    /*
     * We need to hold on to these two element to use them as refence in the
     * case of encypting the signature
     */
    Element encrDKTokenElem = null;
    WSSecEncrypt encr = null;
    Element refList = null;
    WSSecDKEncrypt dkEncr = null;

    /*
     * We MUST use keys derived from the same token
     */
    Token encryptionToken = null;
    if (rmd.isInitiator()) {
        encryptionToken = rpd.getRecipientToken();
    } else {
        encryptionToken = rpd.getInitiatorToken();
    }
    Vector encrParts = RampartUtil.getEncryptedParts(rmd);

    //Signed parts are determined before encryption because encrypted signed  headers
    //will not be included otherwise
    this.sigParts = RampartUtil.getSignedParts(rmd);

    if (encryptionToken == null && encrParts.size() > 0) {
        throw new RampartException("encryptionTokenMissing");
    }

    if (encryptionToken != null && encrParts.size() > 0) {

        //Check for RampartConfig assertion
        if (rpd.getRampartConfig() == null) {
            //We'er missing the extra info rampart needs
            throw new RampartException("rampartConigMissing");
        }

        if (encryptionToken.isDerivedKeys()) {
            try {
                this.setupEncryptedKey(rmd, encryptionToken);
                // Create the DK encryption builder
                dkEncr = new WSSecDKEncrypt();
                dkEncr.setParts(encrParts);
                dkEncr.setExternalKey(this.encryptedKeyValue, this.encryptedKeyId);
                dkEncr.setDerivedKeyLength(rpd.getAlgorithmSuite().getEncryptionDerivedKeyLength() / 8);
                dkEncr.prepare(doc);

                // Get and add the DKT element
                this.encrDKTElement = dkEncr.getdktElement();
                encrDKTokenElem = RampartUtil.appendChildToSecHeader(rmd, this.encrDKTElement);

                refList = dkEncr.encryptForExternalRef(null, encrParts);

            } catch (WSSecurityException e) {
                throw new RampartException("errorCreatingEncryptedKey", e);
            } catch (ConversationException e) {
                throw new RampartException("errorInDKEncr", e);
            }
        } else {
            try {
                encr = new WSSecEncrypt();
                encr.setParts(encrParts);
                encr.setWsConfig(rmd.getConfig());
                encr.setDocument(doc);
                RampartUtil.setEncryptionUser(rmd, encr);
                encr.setSymmetricEncAlgorithm(rpd.getAlgorithmSuite().getEncryption());
                RampartUtil.setKeyIdentifierType(rmd, encr, encryptionToken);
                encr.setKeyEncAlgo(rpd.getAlgorithmSuite().getAsymmetricKeyWrap());
                encr.prepare(doc, RampartUtil.getEncryptionCrypto(config, rmd.getCustomClassLoader()));

                Element bstElem = encr.getBinarySecurityTokenElement();
                if (bstElem != null) {
                    RampartUtil.appendChildToSecHeader(rmd, bstElem);
                }

                this.encrTokenElement = encr.getEncryptedKeyElement();
                this.encrTokenElement = RampartUtil.appendChildToSecHeader(rmd, encrTokenElement);

                refList = encr.encryptForExternalRef(null, encrParts);

            } catch (WSSecurityException e) {
                throw new RampartException("errorInEncryption", e);
            }
        }

        RampartUtil.appendChildToSecHeader(rmd, refList);

        if (tlog.isDebugEnabled()) {
            t1 = System.currentTimeMillis();
        }

        this.setInsertionLocation(encrTokenElement);

        RampartUtil.handleEncryptedSignedHeaders(encrParts, this.sigParts, doc);

        HashMap sigSuppTokMap = null;
        HashMap endSuppTokMap = null;
        HashMap sgndEndSuppTokMap = null;
        HashMap sgndEncSuppTokMap = null;
        HashMap endEncSuppTokMap = null;
        HashMap sgndEndEncSuppTokMap = null;

        if (this.timestampElement != null) {
            sigParts.add(
                    new WSEncryptionPart(RampartUtil.addWsuIdToElement((OMElement) this.timestampElement)));
        }

        if (rmd.isInitiator()) {

            // Now add the supporting tokens
            SupportingToken sgndSuppTokens = rpd.getSignedSupportingTokens();
            sigSuppTokMap = this.handleSupportingTokens(rmd, sgndSuppTokens);

            SupportingToken endSuppTokens = rpd.getEndorsingSupportingTokens();
            endSuppTokMap = this.handleSupportingTokens(rmd, endSuppTokens);

            SupportingToken sgndEndSuppTokens = rpd.getSignedEndorsingSupportingTokens();
            sgndEndSuppTokMap = this.handleSupportingTokens(rmd, sgndEndSuppTokens);

            SupportingToken sgndEncryptedSuppTokens = rpd.getSignedEncryptedSupportingTokens();
            sgndEncSuppTokMap = this.handleSupportingTokens(rmd, sgndEncryptedSuppTokens);

            SupportingToken endorsingEncryptedSuppTokens = rpd.getEndorsingEncryptedSupportingTokens();
            endEncSuppTokMap = this.handleSupportingTokens(rmd, endorsingEncryptedSuppTokens);

            SupportingToken sgndEndEncSuppTokens = rpd.getSignedEndorsingEncryptedSupportingTokens();
            sgndEndEncSuppTokMap = this.handleSupportingTokens(rmd, sgndEndEncSuppTokens);

            Vector supportingToks = rpd.getSupportingTokensList();
            for (int i = 0; i < supportingToks.size(); i++) {
                this.handleSupportingTokens(rmd, (SupportingToken) supportingToks.get(i));
            }

            SupportingToken encryptedSupportingToks = rpd.getEncryptedSupportingTokens();
            this.handleSupportingTokens(rmd, encryptedSupportingToks);

            //Setup signature parts
            sigParts = addSignatureParts(sigSuppTokMap, sigParts);
            sigParts = addSignatureParts(sgndEncSuppTokMap, sigParts);
            sigParts = addSignatureParts(sgndEndSuppTokMap, sigParts);
            sigParts = addSignatureParts(sgndEndEncSuppTokMap, sigParts);

        } else {
            addSignatureConfirmation(rmd, sigParts);
        }

        if ((sigParts.size() > 0 && rmd.isInitiator() && rpd.getInitiatorToken() != null)
                || (!rmd.isInitiator() && rpd.getRecipientToken() != null)) {
            if (rpd.getInitiatorToken() instanceof IssuedToken) {
                String sigTokId = rmd.getIssuedSignatureTokenId();
                org.apache.rahas.Token sigTok = null;
                Element sigTokElem = null;
                sigToken = rpd.getInitiatorToken();
                if (!(sigToken instanceof KerberosToken)) {
                    sigTok = getToken(rmd, sigTokId);
                    if (5 == sigToken.getInclusion() || 2 == sigToken.getInclusion()
                            || rmd.isInitiator() && 3 == sigToken.getInclusion())
                        sigTokElem = RampartUtil.appendChildToSecHeader(rmd, sigTok.getToken());
                    else if (rmd.isInitiator() && (sigToken instanceof X509Token)
                            || (sigToken instanceof SecureConversationToken))
                        sigTokElem = RampartUtil.appendChildToSecHeader(rmd, sigTok.getToken());
                }
                if (sigTokElem != null)
                    setInsertionLocation(sigTokElem);
                doSymmSignature(rmd, rpd.getInitiatorToken(), sigTok, sigParts);
            } else {
                doSignature(rmd);
            }

        }

        if (rmd.isInitiator()) {

            endSuppTokMap.putAll(endEncSuppTokMap);
            // Do endorsed signatures
            Vector endSigVals = this.doEndorsedSignatures(rmd, endSuppTokMap);
            for (Iterator iter = endSigVals.iterator(); iter.hasNext();) {
                signatureValues.add(iter.next());
            }

            sgndEndSuppTokMap.putAll(sgndEndEncSuppTokMap);
            // Do signed endorsing signatures
            Vector sigEndSigVals = this.doEndorsedSignatures(rmd, sgndEndSuppTokMap);
            for (Iterator iter = sigEndSigVals.iterator(); iter.hasNext();) {
                signatureValues.add(iter.next());
            }
        }

        if (tlog.isDebugEnabled()) {
            t2 = System.currentTimeMillis();
            tlog.debug("Encryption took :" + (t1 - t0) + ", Signature tool :" + (t2 - t1));
        }

        // Check for signature protection
        if (rpd.isSignatureProtection() && this.mainSigId != null) {
            long t3 = 0, t4 = 0;
            if (tlog.isDebugEnabled()) {
                t3 = System.currentTimeMillis();
            }
            Vector secondEncrParts = new Vector();

            // Now encrypt the signature using the above token
            secondEncrParts.add(new WSEncryptionPart(this.mainSigId, "Element"));

            if (rmd.isInitiator()) {
                for (int i = 0; i < encryptedTokensIdList.size(); i++) {
                    secondEncrParts.add(new WSEncryptionPart((String) encryptedTokensIdList.get(i), "Element"));
                }
            }

            Element secondRefList = null;

            if (encryptionToken.isDerivedKeys()) {
                try {

                    secondRefList = dkEncr.encryptForExternalRef(null, secondEncrParts);
                    RampartUtil.insertSiblingAfter(rmd, encrDKTokenElem, secondRefList);

                } catch (WSSecurityException e) {
                    throw new RampartException("errorCreatingEncryptedKey", e);
                }
            } else {
                try {
                    // Encrypt, get hold of the ref list and add it
                    secondRefList = encr.encryptForExternalRef(null, secondEncrParts);

                    // Insert the ref list after the encrypted key elem
                    this.setInsertionLocation(
                            RampartUtil.insertSiblingAfter(rmd, encrTokenElement, secondRefList));
                } catch (WSSecurityException e) {
                    throw new RampartException("errorInEncryption", e);
                }
            }
            if (tlog.isDebugEnabled()) {
                t4 = System.currentTimeMillis();
                tlog.debug("Signature protection took :" + (t4 - t3));
            }
        }
    }

}

From source file:com.ibm.bi.dml.parser.ParForStatementBlock.java

@Override
public VariableSet validate(DMLProgram dmlProg, VariableSet ids, HashMap<String, ConstIdentifier> constVars,
        boolean conditional) throws LanguageException, ParseException, IOException {
    LOG.trace("PARFOR(" + _ID + "): validating ParForStatementBlock.");

    //create parent variable set via cloning
    _vsParent = new VariableSet(ids);

    if (LOG.isTraceEnabled()) //note: A is matrix, and A[i,1] is scalar  
        for (DataIdentifier di : _vsParent.getVariables().values())
            LOG.trace("PARFOR: non-local " + di._name + ": " + di.getDataType().toString() + " with rowDim = "
                    + di.getDim1());//from ww  w  .java  2  s  .com

    //normal validate via ForStatement (sequential)
    //NOTES:
    // * validate/dependency checking of nested parfor-loops happens at this point
    // * validate includes also constant propagation for from, to, incr expressions
    // * this includes also function inlining
    VariableSet vs = super.validate(dmlProg, ids, constVars, conditional);

    //check of correctness of specified parfor parameter names and 
    //set default parameter values for all not specified parameters 
    ParForStatement pfs = (ParForStatement) _statements.get(0);
    IterablePredicate predicate = pfs.getIterablePredicate();
    HashMap<String, String> params = predicate.getParForParams();
    if (params != null) //if parameter specified
    {
        //check for valid parameter types
        for (String key : params.keySet())
            if (!_paramNames.contains(key)) { //always unconditional
                raiseValidateError(
                        "PARFOR: The specified parameter '" + key + "' is no valid parfor parameter.", false);
            }
        //set defaults for all non-specified values
        //(except if CONSTRAINT optimizer, in order to distinguish specified parameters)
        boolean constrained = (params.containsKey(OPT_MODE)
                && params.get(OPT_MODE).equals(POptMode.CONSTRAINED.toString()));
        for (String key : _paramNames)
            if (!params.containsKey(key)) {
                if (constrained) {
                    params.put(key, _paramDefaults2.get(key));
                }
                //special treatment for degree of parallelism
                else if (key.equals(PAR) && params.containsKey(EXEC_MODE)
                        && params.get(EXEC_MODE).equals(PExecMode.REMOTE_MR.toString())) {
                    int maxPMap = InfrastructureAnalyzer.getRemoteParallelMapTasks();
                    //correction max number of reducers on yarn clusters
                    if (InfrastructureAnalyzer.isYarnEnabled())
                        maxPMap = (int) Math.max(maxPMap, YarnClusterAnalyzer.getNumCores());
                    params.put(key, String.valueOf(maxPMap));
                } else if (key.equals(PAR) && params.containsKey(EXEC_MODE)
                        && params.get(EXEC_MODE).equals(PExecMode.REMOTE_MR_DP.toString())) {
                    int maxPRed = InfrastructureAnalyzer.getRemoteParallelReduceTasks();
                    //correction max number of reducers on yarn clusters
                    if (InfrastructureAnalyzer.isYarnEnabled())
                        maxPRed = (int) Math.max(maxPRed, YarnClusterAnalyzer.getNumCores() / 2);
                    params.put(key, String.valueOf(maxPRed));
                } else //default case
                    params.put(key, _paramDefaults.get(key));
            }

        //check for disabled parameters values
        if (params.containsKey(OPT_MODE)) {
            String optStr = params.get(OPT_MODE);
            if (optStr.equals(POptMode.HEURISTIC.toString()) || optStr.equals(POptMode.GREEDY.toString())
                    || optStr.equals(POptMode.FULL_DP.toString())) { //always unconditional
                raiseValidateError(
                        "Sorry, parfor optimization mode '" + optStr + "' is disabled for external usage.",
                        false);
            }
        }

    } else {
        //set all defaults
        params = new HashMap<String, String>();
        params.putAll(_paramDefaults);
        predicate.setParForParams(params);
    }

    //start time measurement for normalization and dependency analysis
    Timing time = new Timing(true);

    // LOOP DEPENDENCY ANALYSIS (test for dependency existence)
    // no false negative guaranteed, but possibly false positives

    /* Basic intuition: WRITES to NON-local variables are only permitted iff
     *   - no data dep (no read other than own iteration w i < r j)
     *   - no anti dep (no read other than own iteration w i > r j)
     *   - no output dep (no write other than own iteration)
     *   
     * ALGORITHM:
     * 1) Determine candidates C (writes to non-local variables)
     * 2) Prune all c from C where no dependencies --> C'
     * 3) Raise an exception/warning if C' not the empty set 
     * 
     * RESTRICTIONS:
     * - array subscripts of non-local variables must be linear functions of the form 
     *   a0+ a1*i + ... + a2*j, where i and j are for or parfor indexes.
     * - for and parfor increments must be integer values 
     * - only static (integer lower, upper bounds) range indexing
     * - only input variables considered as potential candidates for checking 
     * 
     *   (TODO: in order to remove the last restriction, dependencies must be checked again after 
     *   live variable analysis against LIVEOUT)
     * 
     * NOTE: validity is only checked during compilation, i.e., for dynamic from, to, incr MIN MAX values assumed.        
     */

    LOG.trace("PARFOR: running loop dependency analysis ...");

    //### Step 1 ###: determine candidate set C
    HashSet<Candidate> C = new HashSet<Candidate>();
    HashSet<Candidate> C2 = new HashSet<Candidate>();
    Integer sCount = 0; //object for call by ref 
    rDetermineCandidates(pfs.getBody(), C, sCount);

    boolean check = (Integer.parseInt(params.get(CHECK)) == 1);
    if (check) {
        //### Step 2 ###: prune c without dependencies
        _bounds = new Bounds();
        for (FunctionStatementBlock fsb : dmlProg.getFunctionStatementBlocks())
            rDetermineBounds(fsb, false); //writes to _bounds   
        rDetermineBounds(dmlProg.getStatementBlocks(), false); //writes to _bounds

        for (Candidate c : C) {
            DataType cdt = _vsParent.getVariables().get(c._var).getDataType(); //might be different in DataIdentifier

            //assume no dependency
            sCount = 0;
            boolean[] dep = new boolean[] { false, false, false }; //output, data, anti
            rCheckCandidates(c, cdt, pfs.getBody(), sCount, dep);

            if (LOG.isTraceEnabled()) {
                if (dep[0])
                    LOG.trace("PARFOR: output dependency detected for var '" + c._var + "'.");
                if (dep[1])
                    LOG.trace("PARFOR: data dependency detected for var '" + c._var + "'.");
                if (dep[2])
                    LOG.trace("PARFOR: anti dependency detected for var '" + c._var + "'.");
            }

            if (dep[0] || dep[1] || dep[2]) {
                C2.add(c);
                if (ABORT_ON_FIRST_DEPENDENCY)
                    break;
            }
        }

        //### Step 3 ###: raise an exception / warning
        if (C2.size() > 0) {
            LOG.trace("PARFOR: loop dependencies detected.");

            StringBuilder depVars = new StringBuilder();
            for (Candidate c : C2) {
                if (depVars.length() > 0)
                    depVars.append(", ");
                depVars.append(c._var);
            }

            //always unconditional (to ensure we always raise dependency issues)
            raiseValidateError(
                    "PARFOR loop dependency analysis: "
                            + "inter-iteration (loop-carried) dependencies detected for variable(s): "
                            + depVars.toString() + ". \n " + "Please, ensure independence of iterations.",
                    false);
        } else {
            LOG.trace("PARFOR: no loop dependencies detected.");
        }

    } else {
        LOG.debug("INFO: PARFOR(" + _ID + "): loop dependency analysis skipped.");
    }

    //if successful, prepare result variables (all distinct vars in all candidates)
    //a) add own candidates
    for (Candidate var : C)
        if (check || var._dat.getDataType() != DataType.SCALAR)
            addToResultVariablesNoDup(var._var);
    //b) get and add child result vars (if required)
    ArrayList<String> tmp = new ArrayList<String>();
    rConsolidateResultVars(pfs.getBody(), tmp);
    for (String var : tmp)
        if (_vsParent.containsVariable(var))
            addToResultVariablesNoDup(var);
    if (LDEBUG)
        for (String rvar : _resultVars)
            LOG.debug("INFO: PARFOR final result variable: " + rvar);

    //cleanup function cache in order to prevent side effects between parfor statements
    if (USE_FN_CACHE)
        _fncache.clear();

    LOG.debug("INFO: PARFOR(" + _ID + "): validate successful (no dependencies) in " + time.stop() + "ms.");

    return vs;
}

From source file:axiom.servlet.AbstractServletClient.java

protected void parseParameters(HttpServletRequest request, RequestTrans reqtrans, String encoding)
        throws Exception {

    HashMap parameters = new HashMap();

    try {//w ww  . j ava  2s  . c  o  m
        Context cx = Context.enter();
        cx.setClassShutter(new ClassShutter() {
            public boolean visibleToScripts(String fullClassName) {
                return false;
            }
        });

        ImporterTopLevel scope = new ImporterTopLevel(cx, true);

        // Parse any posted parameters in the input stream
        String contentType;
        boolean isPost = false, isForm = false, isJson = false, isXml = false;
        if ("POST".equals(request.getMethod())) {
            isPost = true;
        }
        if (isPost && (contentType = request.getContentType()) != null) {
            contentType = contentType.split(";")[0];
            if ("application/x-www-form-urlencoded".equals(contentType)) {
                isForm = true;
            } else if ("text/json".equals(contentType)) {
                isJson = true;
            } else if ("text/xml".equals(contentType)) {
                isXml = true;
            }
        }

        // Parse any query string parameters from the request
        String queryString = request.getQueryString();
        if (queryString != null) {
            try {
                parseParameters(parameters, queryString.getBytes(), encoding, isPost);

                Scriptable sqparam = cx.newObject(scope);
                for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {
                    Map.Entry entry = (Map.Entry) i.next();
                    String key = (String) entry.getKey();
                    String[] values = (String[]) entry.getValue();

                    if ((values != null) && (values.length > 0)) {
                        if (values.length == 1) {
                            sqparam.put(key, sqparam, values[0]);
                        } else {
                            NativeArray na = new NativeArray(values);
                            ScriptRuntime.setObjectProtoAndParent(na, scope);
                            sqparam.put(key, sqparam, na);
                        }
                    }
                }
                reqtrans.setQueryParams(sqparam);
            } catch (Exception e) {
                System.err.println("Error parsing query string: " + e);
            }
        }

        if (isForm || isJson || isXml) {
            try {
                int max = request.getContentLength();
                int len = 0;
                byte[] buf = new byte[max];
                ServletInputStream is = request.getInputStream();

                while (len < max) {
                    int next = is.read(buf, len, max - len);

                    if (next < 0) {
                        break;
                    }

                    len += next;
                }

                if (isForm) {
                    HashMap formMap = new HashMap();
                    parseParameters(formMap, buf, encoding, isPost);
                    Scriptable spparam = cx.newObject(scope);
                    for (Iterator i = formMap.entrySet().iterator(); i.hasNext();) {
                        Map.Entry entry = (Map.Entry) i.next();
                        String key = (String) entry.getKey();
                        String[] values = (String[]) entry.getValue();
                        if (values.length > 0) {
                            if (values.length == 1) {
                                spparam.put(key, spparam, values[0]);
                            } else {
                                NativeArray na = new NativeArray(values);
                                ScriptRuntime.setObjectProtoAndParent(na, scope);
                                spparam.put(key, spparam, na);
                            }
                        }
                    }

                    reqtrans.setPostBody(new String(buf, encoding));
                    reqtrans.setPostParams(spparam);
                    parameters.putAll(formMap);
                } else if (isJson) {
                    String json = new String(buf, encoding);
                    Scriptable post = (Scriptable) cx.evaluateString(scope, "eval(" + json + ")", "", 0, null);
                    reqtrans.setPostBody(post);
                    Object[] ids = post.getIds();
                    int idslen = ids.length;
                    for (int i = 0; i < idslen; i++) {
                        parameters.put(ids[i], post.get((String) ids[i], post));
                    }
                } else if (isXml) {
                    String xml = new String(buf, encoding);
                    int startProlog = xml.indexOf("<?xml version="), endProlog;
                    if (startProlog > -1 && (endProlog = xml.indexOf(">", startProlog + 1)) > -1) {
                        xml = new StringBuffer(xml).replace(startProlog, endProlog + 1, "").toString();
                    }
                    xml = xml.replaceAll("\\\"", "\\\\\"").replaceAll("\n", "").replaceAll("\r", "");
                    Scriptable post = (Scriptable) cx.evaluateString(scope, "new XML(\"" + xml + "\");", "", 0,
                            null);
                    reqtrans.setPostBody(post);
                }
            } catch (Exception e) {
                throw e;
            }
        }

        Scriptable sreqdata = cx.newObject(scope);
        for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {
            Map.Entry entry = (Map.Entry) i.next();
            String key = (String) entry.getKey();
            Object val = entry.getValue();

            if (val != null) {
                if (val instanceof String[]) {
                    String[] values = (String[]) val;
                    if (values.length > 0) {
                        if (values.length == 1) {
                            sreqdata.put(key, sreqdata, values[0]);
                        } else {
                            NativeArray na = new NativeArray(values);
                            ScriptRuntime.setObjectProtoAndParent(na, scope);
                            sreqdata.put(key, sreqdata, na);
                        }
                    }
                } else {
                    sreqdata.put(key, sreqdata, val);
                }
            }

        }
        reqtrans.setData(sreqdata);

    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    } finally {
        Context.exit();
    }
}

From source file:org.opendatakit.services.database.utlities.ODKDatabaseImplUtils.java

/**
 * Write checkpoint into the database//from w ww .  j  ava2s .  c  om
 *
 * @param db
 * @param tableId
 * @param orderedColumns
 * @param cvValues
 * @param activeUser
 * @param rolesList
 * @param locale
 * @param isNewRow
 */
private void insertCheckpointIntoExistingTable(OdkConnectionInterface db, String tableId,
        OrderedColumns orderedColumns, HashMap<String, Object> cvValues, String activeUser, String rolesList,
        String locale, boolean isNewRow, String priorFilterType, String priorFilterValue)
        throws ActionNotAuthorizedException {
    String rowId = null;

    if (cvValues.size() <= 0) {
        throw new IllegalArgumentException(t + ": No values to add into table " + tableId);
    }

    HashMap<String, Object> cvDataTableVal = new HashMap<String, Object>();
    cvDataTableVal.putAll(cvValues);

    if (cvDataTableVal.containsKey(DataTableColumns.ID)) {

        rowId = (String) cvDataTableVal.get(DataTableColumns.ID);
        if (rowId == null) {
            throw new IllegalArgumentException(DataTableColumns.ID + ", if specified, cannot be null");
        }
    } else {
        throw new IllegalArgumentException(
                t + ": rowId should not be null in insertCheckpointIntoExistingTable in the ContentValues");
    }

    if (!cvDataTableVal.containsKey(DataTableColumns.ROW_ETAG)
            || cvDataTableVal.get(DataTableColumns.ROW_ETAG) == null) {
        cvDataTableVal.put(DataTableColumns.ROW_ETAG, DataTableColumns.DEFAULT_ROW_ETAG);
    }

    if (!cvDataTableVal.containsKey(DataTableColumns.CONFLICT_TYPE)) {
        cvDataTableVal.put(DataTableColumns.CONFLICT_TYPE, null);
    }

    if (!cvDataTableVal.containsKey(DataTableColumns.FORM_ID)) {
        cvDataTableVal.put(DataTableColumns.FORM_ID, null);
    }

    if (!cvDataTableVal.containsKey(DataTableColumns.LOCALE)
            || (cvDataTableVal.get(DataTableColumns.LOCALE) == null)) {
        cvDataTableVal.put(DataTableColumns.LOCALE, locale);
    }

    if (!cvDataTableVal.containsKey(DataTableColumns.SAVEPOINT_TYPE)
            || (cvDataTableVal.get(DataTableColumns.SAVEPOINT_TYPE) == null)) {
        cvDataTableVal.put(DataTableColumns.SAVEPOINT_TYPE, null);
    }

    if (!cvDataTableVal.containsKey(DataTableColumns.SAVEPOINT_TIMESTAMP)
            || cvDataTableVal.get(DataTableColumns.SAVEPOINT_TIMESTAMP) == null) {
        String timeStamp = TableConstants.nanoSecondsFromMillis(System.currentTimeMillis());
        cvDataTableVal.put(DataTableColumns.SAVEPOINT_TIMESTAMP, timeStamp);
    }

    if (!cvDataTableVal.containsKey(DataTableColumns.SAVEPOINT_CREATOR)
            || (cvDataTableVal.get(DataTableColumns.SAVEPOINT_CREATOR) == null)) {
        cvDataTableVal.put(DataTableColumns.SAVEPOINT_CREATOR, activeUser);
    }

    cleanUpValuesMap(orderedColumns, cvDataTableVal);

    boolean dbWithinTransaction = db.inTransaction();
    try {
        if (!dbWithinTransaction) {
            db.beginTransactionNonExclusive();
        }

        List<String> rolesArray = getRolesArray(rolesList);

        // get the security settings
        TableSecuritySettings tss = getTableSecuritySettings(db, tableId);

        if (isNewRow) {

            // ensure that filter type and value are defined. Use defaults if not.

            if (!cvDataTableVal.containsKey(DataTableColumns.FILTER_TYPE)
                    || (cvDataTableVal.get(DataTableColumns.FILTER_TYPE) == null)) {
                cvDataTableVal.put(DataTableColumns.FILTER_TYPE, tss.filterTypeOnCreation);
            }

            if (!cvDataTableVal.containsKey(DataTableColumns.FILTER_VALUE)) {
                cvDataTableVal.put(DataTableColumns.FILTER_VALUE, activeUser);
            }

            cvDataTableVal.put(DataTableColumns.SYNC_STATE, SyncState.new_row.name());

            tss.allowRowChange(activeUser, rolesArray, SyncState.new_row.name(), priorFilterType,
                    priorFilterValue, RowChange.NEW_ROW);

        } else {

            // don't allow changes to filter type or value or syncState when inserting checkpoints
            cvDataTableVal.put(DataTableColumns.FILTER_TYPE, priorFilterType);

            cvDataTableVal.put(DataTableColumns.FILTER_VALUE, priorFilterValue);

            // for this call path, syncState is already updated by caller

            tss.allowRowChange(activeUser, rolesArray, (String) cvDataTableVal.get(DataTableColumns.SYNC_STATE),
                    priorFilterType, priorFilterValue, RowChange.CHANGE_ROW);
        }

        db.insertOrThrow(tableId, null, cvDataTableVal);

        if (!dbWithinTransaction) {
            db.setTransactionSuccessful();
        }
    } finally {
        if (!dbWithinTransaction) {
            db.endTransaction();
        }
    }

}

From source file:com.globalsight.everest.workflowmanager.WorkflowManagerLocal.java

/**
 * Update the default path tasks by setting their estimated acceptance, and
 * completion times. Also set the estimated completion time for the
 * workflow./*  w  ww. j  av a  2 s .c om*/
 */
@SuppressWarnings("unchecked")
private Map modifyWorkflowInstance(Date p_baseDate, String p_sessionId, WorkflowInstance p_wfInstance,
        TaskEmailInfo p_emailInfo, String p_companyId) throws Exception {
    Vector wfTaskInstances = p_wfInstance.getWorkflowInstanceTasks();
    int size = wfTaskInstances.size();
    DefaultPathTasks dpt = null;
    boolean isReassigned = false;
    HashMap<String, Object> map = new HashMap<String, Object>(1);
    for (int i = 0; (!isReassigned && i < size); i++) {
        WorkflowTaskInstance wft = (WorkflowTaskInstance) wfTaskInstances.get(i);
        boolean isActive = wft.getTaskState() == WorkflowConstants.TASK_ACTIVE;

        if (isActive) {
            // add the id of the active node to the map. This is used for
            // updating the default path (so it starts from the active
            // node).
            map.put(ACTIVE_NODE_ID, new Long(wft.getTaskId()));
        }

        isReassigned = isActive && wft.isReassigned();

        if (isReassigned) {
            dpt = new DefaultPathTasks();
            FluxCalendar calendar = ServerProxy.getCalendarManager().findDefaultCalendar(p_companyId);

            Date acceptBy = ServerProxy.getEventScheduler().determineDate(p_baseDate, calendar,
                    wft.getAcceptTime());

            Date completeBy = ServerProxy.getEventScheduler().determineDate(p_baseDate, calendar,
                    (wft.getAcceptTime() + wft.getCompletedTime()));

            dpt.addTaskInfo(new TaskInfo(wft.getTaskId(), wft.getActivityName(), wft.getTaskState(), acceptBy,
                    completeBy, null, null, wft.getActivity().getType()));

            // This is used to determine the base date in updating
            // the default path
            map.put(REASSIGNED_NODE_ID, Boolean.TRUE);
        }
    }
    map.putAll(getWFServer().modifyWorkflowInstance(p_sessionId, p_wfInstance, dpt, p_emailInfo));

    return map;
}

From source file:org.opendatakit.services.database.utlities.ODKDatabaseImplUtils.java

private void upsertDataIntoExistingTable(OdkConnectionInterface db, String tableId,
        OrderedColumns orderedColumns, Map<String, Object> cvValues, boolean shouldUpdate,
        boolean asServerRequestedChange, String activeUser, String rolesList, String locale,
        boolean asCsvRequestedChange) throws ActionNotAuthorizedException {

    String rowId = null;/*from  www . j a  v  a 2s .  c  o  m*/
    String whereClause = null;
    boolean specifiesConflictType = cvValues.containsKey(DataTableColumns.CONFLICT_TYPE);
    boolean nullConflictType = specifiesConflictType && (cvValues.get(DataTableColumns.CONFLICT_TYPE) == null);
    Object[] whereArgs = new Object[specifiesConflictType ? (1 + (nullConflictType ? 0 : 1)) : 1];
    boolean update = false;
    String updatedSyncState = SyncState.new_row.name();
    String priorFilterType = DataTableColumns.DEFAULT_FILTER_TYPE;
    String priorFilterValue = null;

    if (cvValues.size() <= 0) {
        throw new IllegalArgumentException(t + ": No values to add into table " + tableId);
    }

    HashMap<String, Object> cvDataTableVal = new HashMap<String, Object>();
    cvDataTableVal.putAll(cvValues);

    // if this is a server-requested change, all the user fields and admin columns should be specified.
    if (asServerRequestedChange && !asCsvRequestedChange) {
        for (String columnName : orderedColumns.getRetentionColumnNames()) {
            if (!cvDataTableVal.containsKey(columnName)) {
                throw new IllegalArgumentException(t + ": Not all user field values are set during server "
                        + (shouldUpdate ? "update" : "insert") + " in table " + tableId + " missing: "
                        + columnName);
            }
        }
        for (String columnName : ADMIN_COLUMNS) {
            if (!cvDataTableVal.containsKey(columnName)) {
                throw new IllegalArgumentException(t + ": Not all metadata field values are set during server "
                        + (shouldUpdate ? "update" : "insert") + " in table " + tableId + " missing: "
                        + columnName);
            }
        }
    }

    boolean dbWithinTransaction = db.inTransaction();
    try {
        if (!dbWithinTransaction) {
            db.beginTransactionNonExclusive();
        }

        if (cvDataTableVal.containsKey(DataTableColumns.ID)) {
            // The user specified a row id; we need to determine whether to
            // insert or update the record, or to reject the action because
            // there are either checkpoint records for this row id, or, if
            // a server conflict is associated with this row, that the
            // _conflict_type to update was not specified.
            //
            // i.e., the tuple (_id, _conflict_type) should be unique. If
            // we find that there are more than 0 or 1 records matching this
            // tuple, then we should reject the update request.
            //
            // TODO: perhaps we want to allow updates to the local conflict
            // row if there are no checkpoints on it? I.e., change the
            // tri-state conflict type to a pair of states (local / remote).
            // and all local changes are flagged local. Remote only exists
            // if the server is in conflict.

            rowId = (String) cvDataTableVal.get(DataTableColumns.ID);
            if (rowId == null) {
                throw new IllegalArgumentException(DataTableColumns.ID + ", if specified, cannot be null");
            }

            if (specifiesConflictType) {
                if (nullConflictType) {
                    whereClause = K_DATATABLE_ID_EQUALS_PARAM + S_AND + DataTableColumns.CONFLICT_TYPE
                            + S_IS_NULL;
                    whereArgs[0] = rowId;
                } else {
                    whereClause = K_DATATABLE_ID_EQUALS_PARAM + S_AND + DataTableColumns.CONFLICT_TYPE
                            + S_EQUALS_PARAM;
                    whereArgs[0] = rowId;
                    whereArgs[1] = cvValues.get(DataTableColumns.CONFLICT_TYPE);
                }
            } else {
                whereClause = K_DATATABLE_ID_EQUALS_PARAM;
                whereArgs[0] = rowId;
            }

            AccessContext accessContext = getAccessContext(db, tableId, activeUser,
                    RoleConsts.ADMIN_ROLES_LIST);

            StringBuilder b = new StringBuilder();
            b.append(K_SELECT_FROM).append(tableId).append(K_WHERE).append(whereClause);
            BaseTable data = privilegedQuery(db, tableId, b.toString(), whereArgs, null, accessContext);

            // There must be only one row in the db for the update to work
            if (shouldUpdate) {
                if (data.getNumberOfRows() == 1) {
                    int filterTypeCursorIndex = data.getColumnIndexOfElementKey(DataTableColumns.FILTER_TYPE);
                    priorFilterType = data.getRowAtIndex(0).getDataByIndex(filterTypeCursorIndex);
                    if (priorFilterType == null) {
                        priorFilterType = DataTableColumns.DEFAULT_FILTER_TYPE;
                    }
                    int filterValueCursorIndex = data.getColumnIndexOfElementKey(DataTableColumns.FILTER_VALUE);
                    priorFilterValue = data.getRowAtIndex(0).getDataByIndex(filterValueCursorIndex);
                    int syncStateCursorIndex = data.getColumnIndexOfElementKey(DataTableColumns.SYNC_STATE);
                    updatedSyncState = data.getRowAtIndex(0).getDataByIndex(syncStateCursorIndex);

                    if (updatedSyncState.equals(SyncState.deleted.name())
                            || updatedSyncState.equals(SyncState.in_conflict.name())) {
                        throw new IllegalStateException(t + ": Cannot update a deleted or in-conflict row");
                    } else if (updatedSyncState.equals(SyncState.synced.name())
                            || updatedSyncState.equals(SyncState.synced_pending_files.name())) {
                        updatedSyncState = SyncState.changed.name();
                    }
                    update = true;
                } else if (data.getNumberOfRows() > 1) {
                    throw new IllegalArgumentException(
                            t + ": row id " + rowId + " has more than 1 row in table " + tableId);
                }
            } else {
                if (data.getNumberOfRows() > 0) {
                    throw new IllegalArgumentException(
                            t + ": row id " + rowId + " is already present in table " + tableId);
                }
            }

        } else {
            rowId = "uuid:" + UUID.randomUUID().toString();
        }

        // TODO: This is broken w.r.t. updates of partial fields
        // TODO: This is broken w.r.t. updates of partial fields
        // TODO: This is broken w.r.t. updates of partial fields
        // TODO: This is broken w.r.t. updates of partial fields

        if (!cvDataTableVal.containsKey(DataTableColumns.ID)) {
            cvDataTableVal.put(DataTableColumns.ID, rowId);
        }

        List<String> rolesArray = getRolesArray(rolesList);

        // get the security settings
        TableSecuritySettings tss = getTableSecuritySettings(db, tableId);

        if (!asServerRequestedChange) {
            // do not allow filterType or filterValue to be modified in normal workflow
            if (cvDataTableVal.containsKey(DataTableColumns.FILTER_TYPE)
                    || cvDataTableVal.containsKey(DataTableColumns.FILTER_VALUE)) {

                tss.canModifyFilterTypeAndValue(rolesArray);
            }
        }

        if (update) {

            // MODIFYING

            if (!cvDataTableVal.containsKey(DataTableColumns.SYNC_STATE)
                    || (cvDataTableVal.get(DataTableColumns.SYNC_STATE) == null)) {
                cvDataTableVal.put(DataTableColumns.SYNC_STATE, updatedSyncState);
            }

            if (!asServerRequestedChange) {

                // apply row access restrictions
                // this will throw an IllegalArgumentException
                tss.allowRowChange(activeUser, rolesArray, updatedSyncState, priorFilterType, priorFilterValue,
                        RowChange.CHANGE_ROW);

            }

            if (cvDataTableVal.containsKey(DataTableColumns.LOCALE)
                    && (cvDataTableVal.get(DataTableColumns.LOCALE) == null)) {
                cvDataTableVal.put(DataTableColumns.LOCALE, locale);
            }

            if (cvDataTableVal.containsKey(DataTableColumns.SAVEPOINT_TYPE)
                    && (cvDataTableVal.get(DataTableColumns.SAVEPOINT_TYPE) == null)) {
                cvDataTableVal.put(DataTableColumns.SAVEPOINT_TYPE, SavepointTypeManipulator.complete());
            }

            if (!cvDataTableVal.containsKey(DataTableColumns.SAVEPOINT_TIMESTAMP)
                    || cvDataTableVal.get(DataTableColumns.SAVEPOINT_TIMESTAMP) == null) {
                String timeStamp = TableConstants.nanoSecondsFromMillis(System.currentTimeMillis());
                cvDataTableVal.put(DataTableColumns.SAVEPOINT_TIMESTAMP, timeStamp);
            }

            if (!cvDataTableVal.containsKey(DataTableColumns.SAVEPOINT_CREATOR)
                    || (cvDataTableVal.get(DataTableColumns.SAVEPOINT_CREATOR) == null)) {
                cvDataTableVal.put(DataTableColumns.SAVEPOINT_CREATOR, activeUser);
            }
        } else {

            // INSERTING

            if (!cvDataTableVal.containsKey(DataTableColumns.ROW_ETAG)
                    || cvDataTableVal.get(DataTableColumns.ROW_ETAG) == null) {
                cvDataTableVal.put(DataTableColumns.ROW_ETAG, DataTableColumns.DEFAULT_ROW_ETAG);
            }

            if (!cvDataTableVal.containsKey(DataTableColumns.SYNC_STATE)
                    || (cvDataTableVal.get(DataTableColumns.SYNC_STATE) == null)) {
                cvDataTableVal.put(DataTableColumns.SYNC_STATE, SyncState.new_row.name());
            }

            if (!cvDataTableVal.containsKey(DataTableColumns.CONFLICT_TYPE)) {
                cvDataTableVal.put(DataTableColumns.CONFLICT_TYPE, null);
            }

            if (!asServerRequestedChange) {

                cvDataTableVal.put(DataTableColumns.FILTER_TYPE, tss.filterTypeOnCreation);

                // activeUser
                cvDataTableVal.put(DataTableColumns.FILTER_VALUE, activeUser);

                tss.allowRowChange(activeUser, rolesArray, updatedSyncState, priorFilterType, priorFilterValue,
                        RowChange.NEW_ROW);
            }

            if (!cvDataTableVal.containsKey(DataTableColumns.FORM_ID)) {
                cvDataTableVal.put(DataTableColumns.FORM_ID, null);
            }

            if (!cvDataTableVal.containsKey(DataTableColumns.LOCALE)
                    || (cvDataTableVal.get(DataTableColumns.LOCALE) == null)) {
                cvDataTableVal.put(DataTableColumns.LOCALE, locale);
            }

            if (!cvDataTableVal.containsKey(DataTableColumns.SAVEPOINT_TYPE)
                    || (cvDataTableVal.get(DataTableColumns.SAVEPOINT_TYPE) == null)) {
                cvDataTableVal.put(DataTableColumns.SAVEPOINT_TYPE, SavepointTypeManipulator.complete());
            }

            if (!cvDataTableVal.containsKey(DataTableColumns.SAVEPOINT_TIMESTAMP)
                    || cvDataTableVal.get(DataTableColumns.SAVEPOINT_TIMESTAMP) == null) {
                String timeStamp = TableConstants.nanoSecondsFromMillis(System.currentTimeMillis());
                cvDataTableVal.put(DataTableColumns.SAVEPOINT_TIMESTAMP, timeStamp);
            }

            if (!cvDataTableVal.containsKey(DataTableColumns.SAVEPOINT_CREATOR)
                    || (cvDataTableVal.get(DataTableColumns.SAVEPOINT_CREATOR) == null)) {
                cvDataTableVal.put(DataTableColumns.SAVEPOINT_CREATOR, activeUser);
            }
        }

        cleanUpValuesMap(orderedColumns, cvDataTableVal);

        if (update) {
            db.update(tableId, cvDataTableVal, whereClause, whereArgs);
        } else {
            db.insertOrThrow(tableId, null, cvDataTableVal);
        }

        if (!dbWithinTransaction) {
            db.setTransactionSuccessful();
        }
    } finally {
        if (!dbWithinTransaction) {
            db.endTransaction();
        }
    }
}

From source file:org.apache.rampart.builder.AsymmetricBindingBuilder.java

private void doSignBeforeEncrypt(RampartMessageData rmd) throws RampartException {

    long t0 = 0, t1 = 0, t2 = 0;

    RampartPolicyData rpd = rmd.getPolicyData();
    Document doc = rmd.getDocument();

    HashMap sigSuppTokMap = null;
    HashMap endSuppTokMap = null;
    HashMap sgndEndSuppTokMap = null;
    HashMap sgndEncSuppTokMap = null;
    HashMap endEncSuppTokMap = null;
    HashMap sgndEndEncSuppTokMap = null;

    sigParts = RampartUtil.getSignedParts(rmd);

    //Add timestamp
    if (this.timestampElement != null) {
        sigParts.add(new WSEncryptionPart(RampartUtil.addWsuIdToElement((OMElement) this.timestampElement)));
    } else {/*from   w w w . j a v a2s  .  com*/
        this.setInsertionLocation(null);
    }

    if (tlog.isDebugEnabled()) {
        t0 = System.currentTimeMillis();
    }

    if (rmd.isInitiator()) {

        //      Now add the supporting tokens
        SupportingToken sgndSuppTokens = rpd.getSignedSupportingTokens();
        sigSuppTokMap = this.handleSupportingTokens(rmd, sgndSuppTokens);

        SupportingToken endSuppTokens = rpd.getEndorsingSupportingTokens();
        endSuppTokMap = this.handleSupportingTokens(rmd, endSuppTokens);

        SupportingToken sgndEndSuppTokens = rpd.getSignedEndorsingSupportingTokens();
        sgndEndSuppTokMap = this.handleSupportingTokens(rmd, sgndEndSuppTokens);

        SupportingToken sgndEncryptedSuppTokens = rpd.getSignedEncryptedSupportingTokens();
        sgndEncSuppTokMap = this.handleSupportingTokens(rmd, sgndEncryptedSuppTokens);

        SupportingToken endorsingEncryptedSuppTokens = rpd.getEndorsingEncryptedSupportingTokens();
        endEncSuppTokMap = this.handleSupportingTokens(rmd, endorsingEncryptedSuppTokens);

        SupportingToken sgndEndEncSuppTokens = rpd.getSignedEndorsingEncryptedSupportingTokens();
        sgndEndEncSuppTokMap = this.handleSupportingTokens(rmd, sgndEndEncSuppTokens);

        Vector supportingToks = rpd.getSupportingTokensList();
        for (int i = 0; i < supportingToks.size(); i++) {
            this.handleSupportingTokens(rmd, (SupportingToken) supportingToks.get(i));
        }

        SupportingToken encryptedSupportingToks = rpd.getEncryptedSupportingTokens();
        this.handleSupportingTokens(rmd, encryptedSupportingToks);

        //Setup signature parts
        sigParts = addSignatureParts(sigSuppTokMap, sigParts);
        sigParts = addSignatureParts(sgndEncSuppTokMap, sigParts);
        sigParts = addSignatureParts(sgndEndSuppTokMap, sigParts);
        sigParts = addSignatureParts(sgndEndEncSuppTokMap, sigParts);

    } else {
        addSignatureConfirmation(rmd, sigParts);
    }

    if (sigParts.size() > 0 && ((rmd.isInitiator() && rpd.getInitiatorToken() != null)
            || (!rmd.isInitiator() && rpd.getRecipientToken() != null))) {
        if (rpd.getInitiatorToken() instanceof IssuedToken && rmd.isInitiator()) {
            String sigTokId = rmd.getIssuedSignatureTokenId();
            org.apache.rahas.Token sigTok = null;
            Element sigTokElem = null;
            sigToken = rpd.getInitiatorToken();
            if (!(sigToken instanceof KerberosToken)) {
                sigTok = getToken(rmd, sigTokId);
                if (5 == sigToken.getInclusion() || 2 == sigToken.getInclusion()
                        || rmd.isInitiator() && 3 == sigToken.getInclusion())
                    sigTokElem = RampartUtil.appendChildToSecHeader(rmd, sigTok.getToken());
                else if (rmd.isInitiator() && (sigToken instanceof X509Token)
                        || (sigToken instanceof SecureConversationToken))
                    sigTokElem = RampartUtil.appendChildToSecHeader(rmd, sigTok.getToken());
            }
            if (sigTokElem != null)
                setInsertionLocation(sigTokElem);
            doSymmSignature(rmd, rpd.getInitiatorToken(), sigTok, sigParts);
        } else {
            doSignature(rmd);
        }
    }

    Vector supportingToks = rpd.getSupportingPolicyData();
    for (int i = 0; i < supportingToks.size(); i++) {
        SupportingPolicyData policyData = null;
        if (supportingToks.get(i) != null) {
            policyData = (SupportingPolicyData) supportingToks.get(i);
            Vector supportingSigParts = RampartUtil.getSupportingSignedParts(rmd, policyData);

            if (supportingSigParts.size() > 0 && ((rmd.isInitiator() && rpd.getInitiatorToken() != null)
                    || (!rmd.isInitiator() && rpd.getRecipientToken() != null))) {
                // Do signature for policies defined under SupportingToken.
                this.doSupportingSignature(rmd, supportingSigParts, policyData);
            }
        }
    }

    //Do endorsed signature

    if (rmd.isInitiator()) {

        // Adding the endorsing encrypted supporting tokens to endorsing supporting tokens
        endSuppTokMap.putAll(endEncSuppTokMap);
        // Do endorsed signatures
        Vector endSigVals = this.doEndorsedSignatures(rmd, endSuppTokMap);
        for (Iterator iter = endSigVals.iterator(); iter.hasNext();) {
            signatureValues.add(iter.next());
        }

        //Adding the signed endorsed encrypted tokens to signed endorsed supporting tokens
        sgndEndSuppTokMap.putAll(sgndEndEncSuppTokMap);
        // Do signed endorsing signatures
        Vector sigEndSigVals = this.doEndorsedSignatures(rmd, sgndEndSuppTokMap);
        for (Iterator iter = sigEndSigVals.iterator(); iter.hasNext();) {
            signatureValues.add(iter.next());
        }
    }

    if (tlog.isDebugEnabled()) {
        t1 = System.currentTimeMillis();
    }

    Vector encrParts = RampartUtil.getEncryptedParts(rmd);

    //Check for signature protection
    if (rpd.isSignatureProtection() && this.mainSigId != null) {
        encrParts.add(new WSEncryptionPart(RampartUtil.addWsuIdToElement((OMElement) this.signatureElement),
                "Element"));
    }

    if (rmd.isInitiator()) {
        for (int i = 0; i < encryptedTokensIdList.size(); i++) {
            encrParts.add(new WSEncryptionPart((String) encryptedTokensIdList.get(i), "Element"));
        }
    }

    //Do encryption
    Token encrToken;
    if (rmd.isInitiator()) {
        encrToken = rpd.getRecipientToken();
    } else {
        encrToken = rpd.getInitiatorToken();
    }

    if (encrToken != null && encrParts.size() > 0) {
        Element refList = null;
        AlgorithmSuite algorithmSuite = rpd.getAlgorithmSuite();
        if (encrToken.isDerivedKeys()) {

            try {
                WSSecDKEncrypt dkEncr = new WSSecDKEncrypt();

                if (this.encrKey == null) {
                    this.setupEncryptedKey(rmd, encrToken);
                }

                dkEncr.setExternalKey(this.encryptedKeyValue, this.encryptedKeyId);
                dkEncr.setCustomValueType(WSConstants.SOAPMESSAGE_NS11 + "#" + WSConstants.ENC_KEY_VALUE_TYPE);
                dkEncr.setSymmetricEncAlgorithm(algorithmSuite.getEncryption());
                dkEncr.setDerivedKeyLength(algorithmSuite.getEncryptionDerivedKeyLength() / 8);
                dkEncr.prepare(doc);

                if (this.encrTokenElement != null) {
                    this.encrDKTElement = RampartUtil.insertSiblingAfter(rmd, this.encrTokenElement,
                            dkEncr.getdktElement());
                } else {
                    this.encrDKTElement = RampartUtil.insertSiblingBefore(rmd, this.sigDKTElement,
                            dkEncr.getdktElement());
                }

                refList = dkEncr.encryptForExternalRef(null, encrParts);

                RampartUtil.insertSiblingAfter(rmd, this.encrDKTElement, refList);

            } catch (WSSecurityException e) {
                throw new RampartException("errorInDKEncr", e);
            } catch (ConversationException e) {
                throw new RampartException("errorInDKEncr", e);
            }
        } else {
            try {

                WSSecEncrypt encr = new WSSecEncrypt();

                RampartUtil.setKeyIdentifierType(rmd, encr, encrToken);

                encr.setWsConfig(rmd.getConfig());

                encr.setDocument(doc);
                RampartUtil.setEncryptionUser(rmd, encr);
                encr.setSymmetricEncAlgorithm(algorithmSuite.getEncryption());
                encr.setKeyEncAlgo(algorithmSuite.getAsymmetricKeyWrap());
                encr.prepare(doc,
                        RampartUtil.getEncryptionCrypto(rpd.getRampartConfig(), rmd.getCustomClassLoader()));

                if (this.timestampElement != null) {
                    this.setInsertionLocation(this.timestampElement);
                } else {
                    this.setInsertionLocation(null);
                }

                if (encr.getBSTTokenId() != null) {
                    this.setInsertionLocation(RampartUtil.insertSiblingAfterOrPrepend(rmd,
                            this.getInsertionLocation(), encr.getBinarySecurityTokenElement()));
                }

                Element encryptedKeyElement = encr.getEncryptedKeyElement();

                //Encrypt, get hold of the ref list and add it
                refList = encr.encryptForInternalRef(null, encrParts);

                //Add internal refs
                encryptedKeyElement.appendChild(refList);

                this.setInsertionLocation(RampartUtil.insertSiblingAfterOrPrepend(rmd,
                        this.getInsertionLocation(), encryptedKeyElement));

                //                    RampartUtil.insertSiblingAfter(rmd,
                //                                                    this.getInsertionLocation(),
                //                                                    refList);
            } catch (WSSecurityException e) {
                throw new RampartException("errorInEncryption", e);
            }
        }
    }

    Vector supportingTokens = rpd.getSupportingPolicyData();
    for (int i = 0; i < supportingTokens.size(); i++) {
        SupportingPolicyData policyData = null;
        if (supportingTokens.get(i) != null) {
            policyData = (SupportingPolicyData) supportingTokens.get(i);
            Token supportingEncrToken = policyData.getEncryptionToken();
            Vector supoortingEncrParts = RampartUtil.getSupportingEncryptedParts(rmd, policyData);

            if (supportingEncrToken != null && supoortingEncrParts.size() > 0) {
                doEncryptionWithSupportingToken(rpd, rmd, supportingEncrToken, doc, supoortingEncrParts);
            }
        }
    }

    if (tlog.isDebugEnabled()) {
        t2 = System.currentTimeMillis();
        tlog.debug("Signature took :" + (t1 - t0) + ", Encryption took :" + (t2 - t1));
    }

}

From source file:structuredPredictionNLG.SFX.java

/**
 *
 * @param dataSize/*from w w  w .  j  ava  2s.c  o  m*/
 * @param trainedAttrClassifiers_0
 * @param trainedWordClassifiers_0
 * @return
 */
@Override
public boolean loadInitClassifiers(int dataSize, HashMap<String, JAROW> trainedAttrClassifiers_0,
        HashMap<String, HashMap<String, JAROW>> trainedWordClassifiers_0) {
    String file1 = "cache/attrInitClassifiers" + getDataset() + "_" + dataSize;
    String file2 = "cache/wordInitClassifiers" + getDataset() + "_" + dataSize;
    FileInputStream fin1 = null;
    ObjectInputStream ois1 = null;
    FileInputStream fin2 = null;
    ObjectInputStream ois2 = null;
    if ((new File(file1)).exists() && (new File(file2)).exists()) {
        try {
            System.out.print("Load initial classifiers...");
            fin1 = new FileInputStream(file1);
            ois1 = new ObjectInputStream(fin1);
            Object o1 = ois1.readObject();
            if (o1 instanceof HashMap) {
                trainedAttrClassifiers_0.putAll((Map<? extends String, ? extends JAROW>) o1);
            }

            fin2 = new FileInputStream(file2);
            ois2 = new ObjectInputStream(fin2);
            Object o2 = ois2.readObject();
            if (o2 instanceof HashMap) {
                trainedWordClassifiers_0.putAll((Map<? extends String, ? extends HashMap<String, JAROW>>) o2);
            }

        } catch (ClassNotFoundException | IOException ex) {
        } finally {
            try {
                fin1.close();
                fin2.close();
            } catch (IOException ex) {
            }
            try {
                ois1.close();
                ois2.close();
            } catch (IOException ex) {
            }
        }
    } else {
        return false;
    }
    return true;
}