/*
 * Copyright 2009 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openehealth.ipf.commons.ihe.hl7v2.definitions.pdq.v25.segment;

import java.util.Collection;

import ca.uhn.hl7v2.model.Group;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.Varies;
import ca.uhn.hl7v2.model.v25.datatype.*;

import ca.uhn.log.HapiLogFactory;
import ca.uhn.hl7v2.parser.ModelClassFactory;
import ca.uhn.hl7v2.HL7Exception;
import org.openehealth.ipf.modules.hl7.model.AbstractSegment;


/**
 * <p>Represents an HL7 QPD message segment.
 * The fields contained in this segment:</p><p>
 * QPD-1: Message Query Name (CE)<br>
 * QPD-2: Query Tag (ST)<br>
 * QPD-3: Hierarchic Designator (HD)<br>
 * QPD-4: Query Input Parameter List (QIP)<br>
 * QPD-5,6,7: User Parameters (in successive fields) (varies)<br>
 * QPD-8: Extended Composite ID with Check Digit (CX)<br>
 */
@SuppressWarnings("serial")
public class QPD extends AbstractSegment {

    /**
     * Creates a QPD (Query Parameter Definition) segment object
     * which belongs to the given message
     */
    public QPD(Group parentGroup, ModelClassFactory modelFactory) {
        super(parentGroup, modelFactory);
        Message msg = getMessage();
        try {
            add("CE", true, 1, 250, new Object[]{msg});
            add("ST", false, 1, 32, new Object[]{msg});
            add("HD", true, 0, 256, new Object[]{msg});
            add("QIP", false, 0, 256, new Object[]{msg});
            add(Varies.class, false, 0, 256, new Object[]{msg}, null);
            add(Varies.class, false, 0, 256, new Object[]{msg}, null);
            add(Varies.class, false, 0, 256, new Object[]{msg}, null);
            add("CX", true, 0, 256, new Object[]{msg}, null);
        } catch (HL7Exception e) {
            HapiLogFactory.getHapiLog(getClass()).error("Can't instantiate " + getClass().getName(), e);
        }
    }

    /**
     * Returns Message Query Name (QPD-1).
     */
    public CE getMessageQueryName() {
        return getTypedField(1, 0);
    }

    /**
     * Returns Query Tag (QPD-2).
     */
    public ST getQueryTag() {
        return getTypedField(2, 0);
    }

    /**
     * Returns Demographics Fields (QPD-3).
     */
    public QIP getDemographicsFields(int rep) {
        return getTypedField(3, rep);
    }

    /**
     * Returns Demographics Fields (QPD-3).
     */
    public QIP[] getDemographicsFields() {
        Collection<QIP> result = getTypedField(3);
        return result.toArray(new QIP[result.size()]);
    }

    /**
     * Returns What Domains to be returned (QPD-8).
     */
    public CX getWhatDomainsReturned(int rep) {
        return getTypedField(8, rep);
    }

    /**
     * Returns What Domains to be returned (QPD-8).
     *
     * @return movement IDs
     */
    public CX[] getWhatDomainsReturned() {
        Collection<CX> result = getTypedField(8);
        return result.toArray(new CX[result.size()]);
    }

}
@@ -1220,49 +1220,8 @@
 D-3:
- Hierarchic Designator (HD)%3Cbr%3E%0A * QPD-4:
  Que
@@ -1261,16 +1261,18 @@
 %0A * QPD-
+4,
 5,6,7: U
@@ -1820,77 +1820,20 @@
 ST%22,
- false, 1, 32, new Object%5B%5D%7Bmsg%7D);%0A            add(%22HD%22,
  true, 
-0
+1
 , 
+3
 2
-56
 , ne
@@ -1873,20 +1873,19 @@
 (%22QIP%22, 
-fals
+tru
 e, 0, 25
@@ -2112,32 +2112,103 @@
 %5B%5D%7Bmsg%7D, null);%0A
+            add(Varies.class, false, 0, 256, new Object%5B%5D%7Bmsg%7D, null);%0A
             add(
@@ -2213,19 +2213,20 @@
 d(%22CX%22, 
-tru
+fals
 e, 0, 25

fix QPD segment for ITI-21/22 -- second attempt to resolve #84