001    package ca.uhn.hl7v2.util;
002    
003    import ca.uhn.hl7v2.model.DataTypeException;
004    import ca.uhn.hl7v2.HL7Exception;
005    import ca.uhn.hl7v2.model.*;
006    
007    /**
008     * Tools for copying data recurvisely from one message element into another.  Currently only Types are 
009     * supported.  
010     * @author Bryan Tripp
011     */
012    public class DeepCopy {
013        
014        /**
015         * Copies data from the "from" Type into the "to" Type.  Either Type may be 
016         * a Primitive, Composite, or Varies.  If a Varies is provided, the operation is 
017         * performed on the result of calling its getData() method.  A Primitive may be 
018         * copied into a Composite, in which case the value is copied into the first 
019         * component of the Composite.  A Composite may be copied into a Primitive, 
020         * in which case the first component is copied.  Given Composites with different 
021         * numbers of components, the first components are copied, up to the length 
022         * of the smaller one.  
023         */
024        public static void copy(Type from, Type to) throws DataTypeException {
025            for (int i = 1; i <= Terser.numComponents(from); i++) {
026                for (int j = 1; j <= Terser.numSubComponents(from, i); j++) {
027                    String val = Terser.getPrimitive(from, i, j).getValue();
028                    Terser.getPrimitive(to, i, j).setValue(val);
029                }
030            }
031        }
032        
033        /*    if (from instanceof Varies) {
034                from = ((Varies) from).getData();
035            }        
036            if (to instanceof Varies) {
037                to = ((Varies) to).getData();
038            }
039            if (from instanceof Primitive) {
040                if (to instanceof Primitive) {
041                    copy((Primitive) from, (Primitive) to);
042                } else {
043                    copy((Primitive) from, (Composite) to);                
044                }
045            } else {
046                if (to instanceof Primitive) {
047                    copy((Composite) from, (Primitive) to);                
048                } else {
049                    copy((Composite) from, (Composite) to);                
050                }            
051            }
052        }*/
053        
054        /** Copies a primitive to a primitive */
055        /*private static void copy(Primitive from, Primitive to) throws DataTypeException {
056            to.setValue(from.getValue());
057        }*/
058        
059        /** Copies composite to composite - could be different # of components (extras either way are ignored) */
060        /*private static void copy(Composite from, Composite to) throws DataTypeException {
061            Type[] froms = from.getComponents();
062            Type[] tos = to.getComponents();
063            int limit = Math.min(froms.length, tos.length);
064            for (int i = 0; i < limit; i++) {
065                copy(froms[i], tos[i]);
066            }
067        }*/
068        
069        /** Copies primitive to first component of composite.  */
070        /*private static void copy(Primitive from, Composite to) throws DataTypeException {
071            Type firstComponent = to.getComponent(0);
072            copy(from, firstComponent);
073        }*/
074        
075        /** Copies first component of composite to a primitive */
076        /*private static void copy(Composite from, Primitive to) throws DataTypeException {
077            Type firstComponent = from.getComponent(0);
078            copy(firstComponent, to);
079        }*/
080        
081        /**
082         * Copies contents from the source segment to the destination segment.  This 
083         * method calls copy(Type, Type) on each repetition of each field (see additional 
084         * behavioural description there).  An attempt is made to copy each repetition of 
085         * each field in the source segment, regardless of whether the corresponding 
086         * destination field is repeating or even exists.  
087         * @param from the segment from which data are copied 
088         * @param to the segment into which data are copied
089         */
090        public static void copy(Segment from, Segment to) throws HL7Exception {
091            int n = from.numFields();
092            for (int i = 1; i <= n; i++) {
093                Type[] reps = from.getField(i);
094                for (int j = 0; j < reps.length; j++) {
095                    copy(reps[j], to.getField(i, j));
096                }
097            }
098        }
099    }