View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.consumer;
15  
16  import java.io.IOException;
17  import java.util.HashMap;
18  import java.util.Map;
19  import java.util.Properties;
20  
21  import com.xmlsolutions.annotation.Requirement;
22  
23  import uk.nhs.interoperability.capabilities.ITKProfileManager;
24  import uk.nhs.interoperability.util.ITKApplicationProperties;
25  import uk.nhs.interoperability.util.Logger;
26  
27  /**
28   * The Class ITKProfileManagerImpl.
29   *
30   * @author Michael Odling-Smee
31   * @author Nicholas Jones
32   * @since 0.1
33   */
34  public class ITKProfileManagerImpl implements ITKProfileManager {
35  	
36  	/** The Constant SUPPORTED_PROFILE_PREFIX. */
37  	private static final String SUPPORTED_PROFILE_PREFIX = "profileId.";
38  	
39  	/** The supported profiles. */
40  	private Map<String, Integer> supportedProfiles;
41  
42  	/**
43  	 * Instantiates a new iTK profile manager impl.
44  	 */
45  	public ITKProfileManagerImpl() {
46  		Properties props = new Properties();
47  		try {
48  			props.load(this.getClass().getResourceAsStream("/profile.properties"));
49  			this.loadProps(props);
50  		} catch (IOException e) {
51  			Logger.error("Could not load supported profile configuration - no profiles will be supported", e);
52  		}
53  		
54  	}
55  	
56  	/**
57  	 * Instantiates a new iTK profile manager impl.
58  	 *
59  	 * @param props the props
60  	 */
61  	public ITKProfileManagerImpl(Properties props) {
62  		this.loadProps(props);
63  	}
64  	
65  	/**
66  	 * Load props.
67  	 *
68  	 * @param props the props
69  	 */
70  	private void loadProps(Properties props) {
71  		this.supportedProfiles = new HashMap<String, Integer>();
72  		try {
73  			//Read in supported profile from properties
74  			Properties profileProps = ITKApplicationProperties.getPropertiesMatching(props, SUPPORTED_PROFILE_PREFIX);
75  			for (Map.Entry<Object, Object> entry : profileProps.entrySet()) {
76  				String profileId = entry.getKey().toString().substring(SUPPORTED_PROFILE_PREFIX.length());
77  				String supportLevelStr = entry.getValue().toString();
78  				if (supportLevelStr.equalsIgnoreCase("ACCEPTED")) {
79  					this.supportedProfiles.put(profileId, new Integer(ITKProfileManager.ACCEPTED));
80  				} else if (supportLevelStr.equalsIgnoreCase("DEPRECATED")) {
81  					this.supportedProfiles.put(profileId, new Integer(ITKProfileManager.DEPRECATED));
82  				} else {
83  					//By default set to not supported
84  					this.supportedProfiles.put(profileId, new Integer(ITKProfileManager.NOT_SUPPORTED));
85  				}
86  			}
87  		} catch (Throwable t) {
88  			Logger.fatal("Error loading supported profiles information, no profiles will be supported!", t);
89  		}
90  	}
91  
92  	/* (non-Javadoc)
93  	 * @see uk.nhs.interoperability.capabilities.ITKProfileManager#getProfileSupportLevel(java.lang.String)
94  	 */
95  	@Override
96  	@Requirement(traceTo={"COR-DEH-09"}, status="implemented")
97  	public int getProfileSupportLevel(String profileId) {
98  		return this.supportedProfiles.containsKey(profileId) ? this.supportedProfiles.get(profileId).intValue() : ITKProfileManager.NOT_SUPPORTED;
99  	}
100 
101 }