001    package ca.uhn.hl7v2.validation.app;
002    
003    import ca.uhn.hl7v2.HL7Exception;
004    import ca.uhn.hl7v2.model.Message;
005    import ca.uhn.hl7v2.validation.ValidationException;
006    import ca.uhn.hl7v2.validation.impl.ConformanceProfileRule;
007    
008    /**
009     * <p>Tests messages against conformance profiles.  A constant profile
010     * can be used, or each message can be tested against the profiles
011     * that it declares.</p>
012     * <p>Note that profiles are referenced by ID, and that the ca.uhn.hl7v2.conf.store
013     * package is used to get the actual profiles. </p>
014     * @author Bryan Tripp
015     * @deprecated
016     */
017    public class ProfileTestApplication extends TestApplication {
018        
019        private String profileID;
020        private ConformanceProfileRule rule;
021        
022        /**
023         * Creates a new instance of ProfileTestApplication that tests using profiles 
024         * declared in each message (if any)
025         */
026        public ProfileTestApplication() {
027            rule = new ConformanceProfileRule();
028        }
029        
030        /**
031         * Creates a new instance of ProfileTestApplication.
032         * @param profileID the ID of the profile against which to test messages;
033         *      null means use profiles declared in the message (if any)
034         */
035        public ProfileTestApplication(String profileID) {
036            this.profileID = profileID;
037            rule = new ConformanceProfileRule(profileID);
038        }
039        
040        /**
041         * Returns true if this Application wishes to accept the message.  By returning
042         * true, this Application declares itself the recipient of the message, accepts
043         * responsibility for it, and must be able to respond appropriately to the sending system.
044         */
045        public boolean canProcess(Message in) {
046            return true;
047        }
048        
049        /**
050         * Tests the message against a profile or profiles.  A constant profile
051         * is used if one was provided to the constructor, otherwise any profiles
052         * declared in the message are used.
053         */
054        public HL7Exception[] test(Message in) throws HL7Exception {
055            ValidationException[] errors = rule.test(in);
056            
057            HL7Exception[] result = new HL7Exception[errors.length];
058            for (int i = 0; i < errors.length; i++) {
059                Throwable t = errors[i].getCause();
060                if ((t != null) && (t instanceof HL7Exception)) {
061                    result[i] = (HL7Exception) t;
062                } else {
063                    result[i] = new HL7Exception(errors[i]);
064                }            
065            }
066            
067            return result;
068        }
069        
070        public String getProfileID() {
071            return this.profileID;
072        }
073    }