View Javadoc
1   /*
2    * Copyright 2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.xds.core.metadata;
17  
18  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage;
19  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
20  
21  import javax.xml.bind.annotation.XmlEnum;
22  import javax.xml.bind.annotation.XmlEnumValue;
23  import javax.xml.bind.annotation.XmlType;
24  
25  /**
26   *
27   */
28  @XmlType(name = "DocumentAvailability")
29  @XmlEnum(String.class)
30  public enum DocumentAvailability {
31  
32      /** Online indicates the Document in the Document Repository is available to be retrieved. */
33      @XmlEnumValue("Offline") OFFLINE("Offline", "urn:ihe:iti:2010:DocumentAvailability:Offline"),
34      /** Offline indicates the Document in the Document Repository is not available to be retrieved. */
35      @XmlEnumValue("Online") ONLINE("Online", "urn:ihe:iti:2010:DocumentAvailability:Online");
36  
37      private final String opcode;
38      private final String fullQualified;
39  
40      DocumentAvailability(String opcode, String fullQualified) {
41          this.opcode = opcode;
42          this.fullQualified = fullQualified;
43      }
44  
45  
46      public String getOpcode() {
47          return opcode;
48      }
49  
50      public String getFullQualified() {
51          return fullQualified;
52      }
53  
54      /**
55       * Returns the document availability represented by the given opcode.
56       * This method takes standard opcodes and fully qualified opcodes into account.
57       * @param opcode
58       *          the opcode to look up. Can be <code>null</code>.
59       * @return the status. <code>null</code> if the opcode was <code>null</code>
60       *          or could not be found.
61       *          <br>See IHE_ITI_Suppl_XDS_Metadata_Update Table 4.2.3.2-1.
62       */
63      public static DocumentAvailability valueOfOpcode(String opcode) {
64          if (opcode == null) {
65              return null;
66          }
67  
68          for (DocumentAvailability documentAvailability : DocumentAvailability.values()) {
69              if (opcode.equals(documentAvailability.getOpcode()) || opcode.equals(documentAvailability.getFullQualified())) {
70                  return documentAvailability;
71              }
72          }
73  
74          throw new XDSMetaDataException(ValidationMessage.INVALID_DOCUMENT_AVAILABILITY, opcode);
75      }
76  
77      /**
78       * Retrieves the representation of a given document availability.
79       * <p>
80       * This is a <code>null</code>-safe version of {@link #getOpcode()}.
81       * @param documentAvailability
82       *          the documentAvailability. Can be <code>null</code>.
83       * @return the representation or <code>null</code> if the status was <code>null</code>.
84       */
85      public static String toOpcode(DocumentAvailability documentAvailability) {
86          return documentAvailability != null ? documentAvailability.getOpcode() : null;
87      }
88  
89      /**
90       * Retrieves the fully qualified representation of a document availability.
91       * <p>
92       * This is a <code>null</code>-safe version of {@link #getFullQualified()}.
93       * @param status
94       *          the status. Can be <code>null</code>.
95       * @return the representation or <code>null</code> if the status was <code>null</code>.
96       */
97      public static String toFullQualifiedOpcode(DocumentAvailability status) {
98          return status != null ? status.getFullQualified() : null;
99      }
100 }