001/* ***** BEGIN LICENSE BLOCK ***** 002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 003 * 004 * The contents of this file are subject to the Mozilla Public License Version 005 * 1.1 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * http://www.mozilla.org/MPL/ 008 * 009 * Software distributed under the License is distributed on an "AS IS" basis, 010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 011 * for the specific language governing rights and limitations under the 012 * License. 013 * 014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in 015 * Java(TM), hosted at https://github.com/gunterze/dcm4che. 016 * 017 * The Initial Developer of the Original Code is 018 * Agfa Healthcare. 019 * Portions created by the Initial Developer are Copyright (C) 2011 020 * the Initial Developer. All Rights Reserved. 021 * 022 * Contributor(s): 023 * See @authors listed below 024 * 025 * Alternatively, the contents of this file may be used under the terms of 026 * either the GNU General Public License Version 2 or later (the "GPL"), or 027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 028 * in which case the provisions of the GPL or the LGPL are applicable instead 029 * of those above. If you wish to allow use of your version of this file only 030 * under the terms of either the GPL or the LGPL, and not to allow others to 031 * use your version of this file under the terms of the MPL, indicate your 032 * decision by deleting the provisions above and replace them with the notice 033 * and other provisions required by the GPL or the LGPL. If you do not delete 034 * the provisions above, a recipient may use your version of this file under 035 * the terms of any one of the MPL, the GPL or the LGPL. 036 * 037 * ***** END LICENSE BLOCK ***** */ 038 039package org.dcm4che3.data; 040 041import java.util.Collections; 042import java.util.HashSet; 043import java.util.Set; 044 045import org.dcm4che3.data.Tag; 046import org.dcm4che3.util.StringUtils; 047 048/** 049 * @author Gunter Zeilinger <gunterze@gmail.com> 050 */ 051public class IDWithIssuer { 052 053 public static final IDWithIssuer[] EMPTY = {}; 054 055 private final String id; 056 private String identifierTypeCode; 057 private Issuer issuer; 058 059 public IDWithIssuer(String id, Issuer issuer) { 060 if (id.isEmpty()) 061 throw new IllegalArgumentException("empty id"); 062 this.id = id; 063 this.setIssuer(issuer); 064 } 065 066 public IDWithIssuer(String id, String issuer) { 067 this.id = id; 068 this.setIssuer(issuer != null ? new Issuer(issuer, '&') : null); 069 } 070 071 public IDWithIssuer(String cx) { 072 String[] ss = StringUtils.split(cx, '^'); 073 this.id = ss[0]; 074 this.setIdentifierTypeCode(ss.length > 4 ? ss[4] : null); 075 this.setIssuer(ss.length > 3 ? new Issuer(ss[3], '&') : null); 076 077 } 078 079 public final String getID() { 080 return id; 081 } 082 083 public final String getIdentifierTypeCode() { 084 return identifierTypeCode; 085 } 086 087 public final void setIdentifierTypeCode(String identifierTypeCode) { 088 this.identifierTypeCode = identifierTypeCode; 089 } 090 091 public final Issuer getIssuer() { 092 return issuer; 093 } 094 095 public final void setIssuer(Issuer issuer) { 096 this.issuer = issuer; 097 } 098 099 @Override 100 public String toString() { 101 if (issuer == null && identifierTypeCode == null) 102 return id; 103 104 StringBuilder sb = new StringBuilder(id); 105 sb.append("^^^"); 106 if (issuer != null) 107 sb.append(issuer.toString('&')); 108 if (identifierTypeCode != null) 109 sb.append('^').append(identifierTypeCode); 110 return sb.toString(); 111 } 112 113 @Override 114 public int hashCode() { 115 int result = id.hashCode(); 116 if (identifierTypeCode != null) 117 result += identifierTypeCode.hashCode() * 31; 118 if (issuer != null) 119 result += issuer.hashCode() * 31; 120 return result; 121 } 122 123 @Override 124 public boolean equals(Object obj) { 125 if (this == obj) 126 return true; 127 if (!(obj instanceof IDWithIssuer)) 128 return false; 129 IDWithIssuer other = (IDWithIssuer) obj; 130 return id.equals(other.id) && 131 (identifierTypeCode == null 132 ? other.identifierTypeCode == null 133 : identifierTypeCode.equals(identifierTypeCode)) && 134 (issuer == null 135 ? other.issuer == null 136 : issuer.equals(other.issuer)); 137 } 138 139 public boolean matches(IDWithIssuer other) { 140 if (other==null) 141 return false; 142 143 return id.equals(other.id) && 144 (issuer == null 145 ? other.issuer == null 146 : issuer.matches(other.issuer)); 147 } 148 149 public Attributes exportPatientIDWithIssuer(Attributes attrs) { 150 if (attrs == null) 151 attrs = new Attributes(3); 152 153 attrs.setString(Tag.PatientID, VR.LO, id); 154 if (issuer == null && identifierTypeCode == null) { 155 return attrs; 156 } 157 158 if (issuer != null) 159 issuer.toIssuerOfPatientID(attrs); 160 161 if (identifierTypeCode != null) { 162 Attributes item = attrs.getNestedDataset( 163 Tag.IssuerOfPatientIDQualifiersSequence); 164 if (item == null) { 165 item = new Attributes(1); 166 attrs.newSequence(Tag.IssuerOfPatientIDQualifiersSequence, 1) 167 .add(item); 168 } 169 item.setString(Tag.IdentifierTypeCode, VR.CS, identifierTypeCode); 170 } 171 return attrs; 172 } 173 174 public static IDWithIssuer valueOf(Attributes attrs, int idTag, 175 int issuerSeqTag) { 176 String id = attrs.getString(idTag); 177 if (id == null) 178 return null; 179 180 return new IDWithIssuer(id, 181 Issuer.valueOf(attrs.getNestedDataset(issuerSeqTag))); 182 } 183 184 public static IDWithIssuer pidOf(Attributes attrs) { 185 String id = attrs.getString(Tag.PatientID); 186 if (id == null) 187 return null; 188 189 IDWithIssuer result = 190 new IDWithIssuer(id, Issuer.fromIssuerOfPatientID(attrs)); 191 result.setIdentifierTypeCode(identifierTypeCodeOf(attrs)); 192 return result; 193 } 194 195 private static String identifierTypeCodeOf(Attributes attrs) { 196 Attributes qualifiers = attrs.getNestedDataset(Tag.IssuerOfPatientIDQualifiersSequence); 197 return qualifiers != null 198 ? qualifiers.getString(Tag.IdentifierTypeCode) 199 : null; 200 } 201 202 public static Set<IDWithIssuer> pidsOf(Attributes attrs) { 203 IDWithIssuer pid = IDWithIssuer.pidOf(attrs); 204 Sequence opidseq = attrs.getSequence(Tag.OtherPatientIDsSequence); 205 if (opidseq == null) 206 if (pid == null) 207 return Collections.emptySet(); 208 else 209 return Collections.singleton(pid); 210 211 Set<IDWithIssuer> pids = 212 new HashSet<IDWithIssuer>((1 + opidseq.size()) << 1); 213 if (pid != null) 214 pids.add(pid); 215 for (Attributes item : opidseq) { 216 pid = IDWithIssuer.pidOf(item); 217 if (pid != null) 218 pids.add(pid); 219 } 220 return pids; 221 } 222}