/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (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.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is part of dcm4che, an implementation of DICOM(TM) in * Java(TM), hosted at https://github.com/gunterze/dcm4che. * * The Initial Developer of the Original Code is * Agfa Healthcare. * Portions created by the Initial Developer are Copyright (C) 2011-2014 * the Initial Developer. All Rights Reserved. * * Contributor(s): * See @authors listed below * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ package org.dcm4chee.archive.entity; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import javax.persistence.*; import org.dcm4che3.data.PersonName.Component; import org.dcm4che3.data.PersonName.Group; import org.dcm4che3.soundex.FuzzyStr; /** * @author Gunter Zeilinger <gunterze@gmail.com> * */ @Entity @Table(name = "person_name") public class PersonName implements Serializable { private static final long serialVersionUID = -6581572195671154849L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "pk") private long pk; @Column(name = "family_name") private String familyName; @Column(name = "given_name") private String givenName; @Column(name = "middle_name") private String middleName; @Column(name = "name_prefix") private String namePrefix; @Column(name = "name_suffix") private String nameSuffix; @Column(name = "i_family_name") private String ideographicFamilyName; @Column(name = "i_given_name") private String ideographicGivenName; @Column(name = "i_middle_name") private String ideographicMiddleName; @Column(name = "i_name_prefix") private String ideographicNamePrefix; @Column(name = "i_name_suffix") private String ideographicNameSuffix; @Column(name = "p_family_name") private String phoneticFamilyName; @Column(name = "p_given_name") private String phoneticGivenName; @Column(name = "p_middle_name") private String phoneticMiddleName; @Column(name = "p_name_prefix") private String phoneticNamePrefix; @Column(name = "p_name_suffix") private String phoneticNameSuffix; @OneToMany(mappedBy = "personName", cascade = CascadeType.ALL, orphanRemoval = true) private Collection<SoundexCode> soundexCodes; public PersonName() { } public PersonName(org.dcm4che3.data.PersonName pn, FuzzyStr fuzzyStr, String nullValueStr) { fromDicom(pn, fuzzyStr, nullValueStr); } public void fromDicom(org.dcm4che3.data.PersonName pn, FuzzyStr fuzzyStr, String nullValueStr) { familyName = pn.get(Group.Alphabetic, Component.FamilyName); givenName = pn.get(Group.Alphabetic, Component.GivenName); middleName = pn.get(Group.Alphabetic, Component.MiddleName); namePrefix = pn.get(Group.Alphabetic, Component.NamePrefix); nameSuffix = pn.get(Group.Alphabetic, Component.NameSuffix); ideographicFamilyName = pn.get(Group.Ideographic, Component.FamilyName); ideographicGivenName = pn.get(Group.Ideographic, Component.GivenName); ideographicMiddleName = pn.get(Group.Ideographic, Component.MiddleName); ideographicNamePrefix = pn.get(Group.Ideographic, Component.NamePrefix); ideographicNameSuffix = pn.get(Group.Ideographic, Component.NameSuffix); phoneticFamilyName = pn.get(Group.Phonetic, Component.FamilyName); phoneticGivenName = pn.get(Group.Phonetic, Component.GivenName); phoneticMiddleName = pn.get(Group.Phonetic, Component.MiddleName); phoneticNamePrefix = pn.get(Group.Phonetic, Component.NamePrefix); phoneticNameSuffix = pn.get(Group.Phonetic, Component.NameSuffix); createOrUpdateSoundexCodes(fuzzyStr, nullValueStr, familyName, givenName, middleName); } private void createOrUpdateSoundexCodes(FuzzyStr fuzzy, String nullValueStr, String familyName, String givenName, String middleName) { createOrUpdateSoundexCode(Component.FamilyName, familyName, fuzzy, nullValueStr); createOrUpdateSoundexCode(Component.GivenName, givenName, fuzzy, nullValueStr); createOrUpdateSoundexCode(Component.MiddleName, middleName, fuzzy, nullValueStr); } private void createOrUpdateSoundexCode(Component component, String name, FuzzyStr fuzzy, String nullValue) { if (name == null || component == null || fuzzy == null) return; if (soundexCodes == null) soundexCodes = new ArrayList<SoundexCode>(); else for (Iterator<SoundexCode> iterator = soundexCodes.iterator(); iterator.hasNext();) { SoundexCode code = iterator.next(); if (code.getPersonNameComponent().equals(component)) iterator.remove(); } Iterator<String> parts = SoundexCode.tokenizePersonNameComponent(name); for (int i = 0; parts.hasNext(); i++) { String fuzzyValue = fuzzy.toFuzzy(parts.next()); if (fuzzyValue.length()>0) { SoundexCode soundexCode = new SoundexCode(component, i, fuzzyValue, nullValue); soundexCode.setPersonName(this); soundexCodes.add(soundexCode); } } } public org.dcm4che3.data.PersonName toPersonName() { org.dcm4che3.data.PersonName pn = new org.dcm4che3.data.PersonName(); pn.set(Group.Alphabetic, Component.FamilyName, familyName); pn.set(Group.Alphabetic, Component.GivenName, givenName); pn.set(Group.Alphabetic, Component.MiddleName, middleName); pn.set(Group.Alphabetic, Component.NamePrefix, namePrefix); pn.set(Group.Alphabetic, Component.NameSuffix, nameSuffix); pn.set(Group.Ideographic, Component.FamilyName, ideographicFamilyName); pn.set(Group.Ideographic, Component.GivenName, ideographicGivenName); pn.set(Group.Ideographic, Component.MiddleName, ideographicMiddleName); pn.set(Group.Ideographic, Component.NamePrefix, ideographicNamePrefix); pn.set(Group.Ideographic, Component.NameSuffix, ideographicNameSuffix); pn.set(Group.Phonetic, Component.FamilyName, phoneticFamilyName); pn.set(Group.Phonetic, Component.GivenName, phoneticGivenName); pn.set(Group.Phonetic, Component.MiddleName, phoneticMiddleName); pn.set(Group.Phonetic, Component.NamePrefix, phoneticNamePrefix); pn.set(Group.Phonetic, Component.NameSuffix, phoneticNameSuffix); return pn; } public static PersonName valueOf(String s, FuzzyStr fuzzyStr, String nullValueStr, PersonName prev) { if (s == null) return null; org.dcm4che3.data.PersonName pn = new org.dcm4che3.data.PersonName(s,true); if (pn.isEmpty()) return null; if (prev != null) { if (!pn.equals(prev.toPersonName())) prev.fromDicom(pn, fuzzyStr, nullValueStr); //update values return prev; } else return new PersonName(pn, fuzzyStr, nullValueStr); //create new } @Override public String toString() { return toPersonName().toString(); } public long getPk() { return pk; } public String getFamilyName() { return familyName; } public void setFamilyName(String familyName, FuzzyStr fuzzy, String nullValue) { this.familyName = familyName; createOrUpdateSoundexCode(Component.FamilyName, familyName, fuzzy, nullValue); } public String getGivenName() { return givenName; } public void setGivenName(String givenName, FuzzyStr fuzzy, String nullValue) { this.givenName = givenName; createOrUpdateSoundexCode(Component.GivenName, givenName, fuzzy, nullValue); } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName, FuzzyStr fuzzy, String nullValue) { this.middleName = middleName; createOrUpdateSoundexCode(Component.MiddleName, middleName, fuzzy, nullValue); } public String getNamePrefix() { return namePrefix; } public void setNamePrefix(String namePrefix) { this.namePrefix = namePrefix; } public String getNameSuffix() { return nameSuffix; } public void setNameSuffix(String nameSuffix) { this.nameSuffix = nameSuffix; } public String getIdeographicFamilyName() { return ideographicFamilyName; } public void setIdeographicFamilyName(String ideographicFamilyName) { this.ideographicFamilyName = ideographicFamilyName; } public String getIdeographicGivenName() { return ideographicGivenName; } public void setIdeographicGivenName(String ideographicGivenName) { this.ideographicGivenName = ideographicGivenName; } public String getIdeographicMiddleName() { return ideographicMiddleName; } public void setIdeographicMiddleName(String ideographicMiddleName) { this.ideographicMiddleName = ideographicMiddleName; } public String getIdeographicNamePrefix() { return ideographicNamePrefix; } public void setIdeographicNamePrefix(String ideographicNamePrefix) { this.ideographicNamePrefix = ideographicNamePrefix; } public String getIdeographicNameSuffix() { return ideographicNameSuffix; } public void setIdeographicNameSuffix(String ideographicNameSuffix) { this.ideographicNameSuffix = ideographicNameSuffix; } public String getPhoneticFamilyName() { return phoneticFamilyName; } public void setPhoneticFamilyName(String phoneticFamilyName) { this.phoneticFamilyName = phoneticFamilyName; } public String getPhoneticGivenName() { return phoneticGivenName; } public void setPhoneticGivenName(String phoneticGivenName) { this.phoneticGivenName = phoneticGivenName; } public String getPhoneticMiddleName() { return phoneticMiddleName; } public void setPhoneticMiddleName(String phoneticMiddleName) { this.phoneticMiddleName = phoneticMiddleName; } public String getPhoneticNamePrefix() { return phoneticNamePrefix; } public void setPhoneticNamePrefix(String phoneticNamePrefix) { this.phoneticNamePrefix = phoneticNamePrefix; } public String getPhoneticNameSuffix() { return phoneticNameSuffix; } public void setPhoneticNameSuffix(String phoneticNameSuffix) { this.phoneticNameSuffix = phoneticNameSuffix; } }