001/* ***** BEGIN LICENSE BLOCK *****
002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003 *
004 * The contents of this file are subject to the Mozilla Public License Version
005 * 1.1 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 * http://www.mozilla.org/MPL/
008 *
009 * Software distributed under the License is distributed on an "AS IS" basis,
010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011 * for the specific language governing rights and limitations under the
012 * License.
013 *
014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in
015 * Java(TM), hosted at https://github.com/gunterze/dcm4che.
016 *
017 * The Initial Developer of the Original Code is
018 * Agfa Healthcare.
019 * Portions created by the Initial Developer are Copyright (C) 2011
020 * the Initial Developer. All Rights Reserved.
021 *
022 * Contributor(s):
023 * See @authors listed below
024 *
025 * Alternatively, the contents of this file may be used under the terms of
026 * either the GNU General Public License Version 2 or later (the "GPL"), or
027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
028 * in which case the provisions of the GPL or the LGPL are applicable instead
029 * of those above. If you wish to allow use of your version of this file only
030 * under the terms of either the GPL or the LGPL, and not to allow others to
031 * use your version of this file under the terms of the MPL, indicate your
032 * decision by deleting the provisions above and replace them with the notice
033 * and other provisions required by the GPL or the LGPL. If you do not delete
034 * the provisions above, a recipient may use your version of this file under
035 * the terms of any one of the MPL, the GPL or the LGPL.
036 *
037 * ***** END LICENSE BLOCK ***** */
038
039package org.dcm4che3.net.pdu;
040
041import org.dcm4che3.util.StringUtils;
042import org.dcm4che3.util.UIDUtils;
043
044/**
045 * @author Gunter Zeilinger <gunterze@gmail.com>
046 * 
047 */
048public class PresentationContext {
049
050    public static final int ACCEPTANCE = 0;
051    public static final int USER_REJECTION = 1;
052    public static final int PROVIDER_REJECTION = 2;
053    public static final int ABSTRACT_SYNTAX_NOT_SUPPORTED = 3;
054    public static final int TRANSFER_SYNTAX_NOT_SUPPORTED = 4;
055
056    private static final String[] RESULTS = {
057        "0 - acceptance",
058        "1 - user-rejection",
059        "2 - no-reason (provider rejection)",
060        "3 - abstract-syntax-not-supported (provider rejection)",
061        "4 - transfer-syntaxes-not-supported (provider rejection)"
062    };
063
064    private final int pcid;
065    private final int result;
066    private final String as;
067    private final String[] tss;
068
069    public PresentationContext(int pcid, int result, String as, String... tss) {
070        this.pcid = pcid;
071        this.result = result;
072        this.as = as;
073        this.tss = tss;
074    }
075
076    public PresentationContext(int pcid, String as, String... tss) {
077        this(pcid, 0, as, tss);
078    }
079
080    public PresentationContext(int pcid, int result, String ts) {
081        this(pcid, result, null, ts);
082    }
083
084    private static String resultAsString(int result) {
085        try {
086            return RESULTS[result];
087        } catch (IndexOutOfBoundsException e) {
088            return Integer.toString(result);
089        }
090    }
091
092    public final int getPCID() {
093        return pcid;
094    }
095
096    public final int getResult() {
097        return result;
098    }
099
100    public boolean isAccepted() {
101        return result == ACCEPTANCE;
102    }
103
104   public final String getAbstractSyntax() {
105        return as;
106    }
107
108    public final String[] getTransferSyntaxes() {
109        return tss;
110    }
111
112    public boolean containsTransferSyntax(String ts) {
113        for (String ts0 : tss)
114            if (ts.equals(ts0))
115                return true;
116        return false;
117    }
118
119    public String getTransferSyntax() {
120        return tss[0];
121    }
122
123    public int length() {
124        int len = 4;
125        if (as != null)
126            len += 4 + as.length();
127        for (String ts : tss)
128            len += 4 + ts.length();
129        return len;
130    }
131
132    @Override
133    public String toString() {
134        return promptTo(new StringBuilder()).toString();
135    }
136
137    StringBuilder promptTo(StringBuilder sb) {
138        sb.append("  PresentationContext[id: ").append(pcid)
139          .append(StringUtils.LINE_SEPARATOR);
140        if (as != null)
141            UIDUtils.promptTo(as, sb.append("    as: "));
142        else
143            sb.append("    result: ").append(resultAsString(result));
144        sb.append(StringUtils.LINE_SEPARATOR);
145        for (String ts : tss)
146            UIDUtils.promptTo(ts, sb.append("    ts: "))
147                .append(StringUtils.LINE_SEPARATOR);
148        return sb.append("  ]");
149    }
150
151}