001/*
002 * **** BEGIN LICENSE BLOCK *****
003 *  Version: MPL 1.1/GPL 2.0/LGPL 2.1
004 *
005 *  The contents of this file are subject to the Mozilla Public License Version
006 *  1.1 (the "License"); you may not use this file except in compliance with
007 *  the License. You may obtain a copy of the License at
008 *  http://www.mozilla.org/MPL/
009 *
010 *  Software distributed under the License is distributed on an "AS IS" basis,
011 *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012 *  for the specific language governing rights and limitations under the
013 *  License.
014 *
015 *  The Original Code is part of dcm4che, an implementation of DICOM(TM) in
016 *  Java(TM), hosted at https://github.com/gunterze/dcm4che.
017 *
018 *  The Initial Developer of the Original Code is
019 *  Agfa Healthcare.
020 *  Portions created by the Initial Developer are Copyright (C) 2014
021 *  the Initial Developer. All Rights Reserved.
022 *
023 *  Contributor(s):
024 *  See @authors listed below
025 *
026 *  Alternatively, the contents of this file may be used under the terms of
027 *  either the GNU General Public License Version 2 or later (the "GPL"), or
028 *  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
029 *  in which case the provisions of the GPL or the LGPL are applicable instead
030 *  of those above. If you wish to allow use of your version of this file only
031 *  under the terms of either the GPL or the LGPL, and not to allow others to
032 *  use your version of this file under the terms of the MPL, indicate your
033 *  decision by deleting the provisions above and replace them with the notice
034 *  and other provisions required by the GPL or the LGPL. If you do not delete
035 *  the provisions above, a recipient may use your version of this file under
036 *  the terms of any one of the MPL, the GPL or the LGPL.
037 *
038 *  ***** END LICENSE BLOCK *****
039 */
040package org.dcm4che3.conf.dicom;
041
042import org.dcm4che3.conf.api.TCGroupsProvider;
043import org.dcm4che3.conf.api.hl7.HL7Configuration;
044import org.dcm4che3.conf.core.api.Configuration;
045import org.dcm4che3.conf.core.api.ConfigurationException;
046import org.dcm4che3.conf.core.api.internal.ConfigTypeAdapter;
047import org.dcm4che3.net.Device;
048import org.dcm4che3.net.hl7.HL7Application;
049import org.dcm4che3.net.hl7.HL7DeviceExtension;
050
051import java.util.*;
052
053public class CommonDicomConfigurationWithHL7 extends CommonDicomConfiguration implements HL7Configuration {
054
055
056    public CommonDicomConfigurationWithHL7(Configuration configurationStorage, Map<Class, List<Class>> extensionsByClass) {
057        super(configurationStorage, extensionsByClass);
058    }
059    public CommonDicomConfigurationWithHL7(Configuration configurationStorage, Map<Class, List<Class>> extensionsByClass, boolean doCacheTCGroups) {
060        super(configurationStorage, extensionsByClass, doCacheTCGroups);
061    }
062
063
064    @Override
065    public boolean registerHL7Application(String name) throws ConfigurationException {
066        return true;
067    }
068
069    @Override
070    public void unregisterHL7Application(String name) throws ConfigurationException {
071    }
072
073    @Override
074    public HL7Application findHL7Application(String name) throws ConfigurationException {
075        String pathForDeviceName = DicomPath.DeviceNameByHL7AppName.set("hl7AppName", name).path();
076
077        try {
078            Iterator search = lowLevelConfig.search(pathForDeviceName);
079            String deviceName = (String) search.next();
080
081            Device device = findDevice(deviceName);
082
083            return device.getDeviceExtension(HL7DeviceExtension.class).getHL7Application(name);
084        } catch (NoSuchElementException e) {
085            throw new ConfigurationException("HL7 app with name '" + name + "' not found", e);
086        } catch (Exception e) {
087            throw new ConfigurationException("Error while searching for HL7 app with name '" + name + "'", e);
088        }
089    }
090
091    @Override
092    public String[] listRegisteredHL7ApplicationNames() throws ConfigurationException {
093        String hl7NamesPath = DicomPath.AllHL7AppNames.path();
094        List<String> list = new ArrayList<String>();
095        try {
096            Iterator search = lowLevelConfig.search(hl7NamesPath);
097            while (search.hasNext())
098                list.add((String) search.next());
099        } catch (Exception e) {
100            throw new ConfigurationException("Error while getting a list of HL7 app names", e);
101        }
102        return list.toArray(new String[list.size()]);
103    }
104
105    @SuppressWarnings("unchecked")
106    @Override
107    public <T> T getDicomConfigurationExtension(Class<T> clazz) {
108        // workaround for Weld, if we just return 'this' - it will replace it with a proxy
109        if (clazz.equals(HL7Configuration.class)) {
110            return (T) new HL7Configuration() {
111                @Override
112                public boolean registerHL7Application(String name) throws ConfigurationException {
113                    return CommonDicomConfigurationWithHL7.this.registerHL7Application(name);
114                }
115
116                @Override
117                public void unregisterHL7Application(String name) throws ConfigurationException {
118                    CommonDicomConfigurationWithHL7.this.unregisterHL7Application(name);
119                }
120
121                @Override
122                public HL7Application findHL7Application(String name) throws ConfigurationException {
123                    return CommonDicomConfigurationWithHL7.this.findHL7Application(name);
124                }
125
126                @Override
127                public String[] listRegisteredHL7ApplicationNames() throws ConfigurationException {
128                    return CommonDicomConfigurationWithHL7.this.listRegisteredHL7ApplicationNames();
129                }
130            };
131        }
132
133        return super.getDicomConfigurationExtension(clazz);
134    }
135}