View Javadoc
1   /*
2    * Copyright 2009 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.responses;
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   * Status information according to the XDS specification.
26   * @author Jens Riemschneider
27   */
28  @XmlType(name = "Status")
29  @XmlEnum(String.class)
30  public enum Status {
31      /** The request execution failed. */
32      @XmlEnumValue("Failure") FAILURE("Failure", "urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Failure"),
33      /** The request execution succeeded. */
34      @XmlEnumValue("Success") SUCCESS("Success", "urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Success"),
35      /** The request execution partially succeeded. */
36      @XmlEnumValue("PartialSuccess") PARTIAL_SUCCESS("PartialSuccess", "urn:ihe:iti:2007:ResponseStatusType:PartialSuccess");
37      
38      private final String opcode21;
39      private final String opcode30;
40      
41      Status(String opcode21, String opcode30) {
42          this.opcode21 = opcode21;
43          this.opcode30 = opcode30;
44      }
45  
46      /**
47       * @return a string representation in ebXML 2.1.
48       */
49      public String getOpcode21() {
50          return opcode21;
51      }
52  
53      /**
54       * @return a string representation in ebXML 3.1.
55       */
56      public String getOpcode30() {
57          return opcode30;
58      }
59      
60      /**
61       * <code>null</code>-safe version of {@link #getOpcode21()}.
62       * @param status
63       *          the type for which to get the opcode. Can be <code>null</code>.
64       * @return the opcode or <code>null</code> if type was <code>null</code>.
65       */
66      public static String getOpcode21(Status status) {
67          return status != null ? status.getOpcode21() : null;
68      }
69  
70      /**
71       * <code>null</code>-safe version of {@link #getOpcode30()}.
72       * @param status
73       *          the type for which to get the opcode. Can be <code>null</code>.
74       * @return the opcode or <code>null</code> if type was <code>null</code>.
75       */
76      public static String getOpcode30(Status status) {
77          return status != null ? status.getOpcode30() : null;
78      }
79      
80      /**
81       * Returns the status that is represented by the given opcode.
82       * <p>
83       * This method looks up the opcode via the ebXML 2.1 and 3.0 representations.
84       * @param opcode
85       *          the string representation. Can be <code>null</code>.
86       * @return the status or <code>null</code> if the opcode was <code>null</code>.
87       */
88      public static Status valueOfOpcode(String opcode) {
89          if (opcode == null) {
90              return null;
91          }
92          
93          for (Status status : values()) {
94              if (opcode.equals(status.getOpcode21()) || opcode.equals(status.getOpcode30())) {
95                  return status;
96              }
97          }
98          
99          throw new XDSMetaDataException(ValidationMessage.INVALID_STATUS_IN_RESPONSE);
100     }
101 }
102