PageRenderTime 1ms CodeModel.GetById 39ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/no/digipost/api/client/representations/Document.java

https://github.com/digipost/digipost-api-client-java
Java | 238 lines | 183 code | 37 blank | 18 comment | 21 complexity | 4a8514252585a4ec4eae73d1f3174c07 MD5 | raw file
  1/**
  2 * Copyright (C) Posten Norge AS
  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
 17package no.digipost.api.client.representations;
 18
 19import no.digipost.api.datatypes.DataType;
 20
 21import javax.xml.bind.annotation.XmlAccessType;
 22import javax.xml.bind.annotation.XmlAccessorType;
 23import javax.xml.bind.annotation.XmlAttribute;
 24import javax.xml.bind.annotation.XmlElement;
 25import javax.xml.bind.annotation.XmlType;
 26import java.util.ArrayList;
 27import java.util.List;
 28import java.util.Objects;
 29import java.util.Optional;
 30import java.util.UUID;
 31import java.util.stream.Stream;
 32
 33import static java.util.Optional.ofNullable;
 34import static java.util.stream.Collectors.collectingAndThen;
 35import static java.util.stream.Collectors.joining;
 36import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
 37import static org.apache.commons.lang3.StringUtils.join;
 38
 39@XmlAccessorType(XmlAccessType.FIELD)
 40@XmlType(name = "document", propOrder = {
 41        "uuid",
 42        "subject",
 43        "digipostFileType",
 44        "opened",
 45        "openingReceipt",
 46        "smsNotification",
 47        "emailNotification",
 48        "authenticationLevel",
 49        "sensitivityLevel",
 50        "encrypted",
 51        "contentHash",
 52        "links",
 53        "dataType"
 54})
 55public class Document extends Representation {
 56
 57    @XmlElement(name = "uuid", required = true)
 58    public final UUID uuid;
 59    @XmlElement(name = "subject", required = true)
 60    public final String subject;
 61    @XmlElement(name = "file-type", required = true)
 62    protected String digipostFileType;
 63    @XmlElement(nillable = false)
 64    protected Boolean opened;
 65    @XmlElement(name = "opening-receipt")
 66    public final String openingReceipt;
 67    @XmlElement(name = "sms-notification")
 68    public final SmsNotification smsNotification;
 69    @XmlElement(name = "email-notification")
 70    public final EmailNotification emailNotification;
 71    @XmlElement(name = "authentication-level")
 72    public final AuthenticationLevel authenticationLevel;
 73    @XmlElement(name = "sensitivity-level")
 74    public final SensitivityLevel sensitivityLevel;
 75    @XmlElement(name = "encrypted")
 76    protected EncryptionInfo encrypted;
 77    @XmlElement(name = "content-hash", nillable = false)
 78    protected ContentHash contentHash;
 79
 80    @XmlElement(name="data-type")
 81    protected DataTypeHolder dataType;
 82
 83    @XmlElement(name = "link")
 84    protected List<Link> getLinks() {
 85        return links;
 86    }
 87
 88    @XmlAttribute(name = "technical-type")
 89    private String technicalType;
 90
 91    public Document() { this(null, null, null); }
 92
 93    /**
 94     * Constructor for just the required fields of a document.
 95     */
 96    public Document(UUID uuid, String subject, FileType fileType) {
 97        this(uuid, subject, fileType, null, null, null, null, null, null, null, (String[]) null);
 98    }
 99
100    public Document(UUID uuid, String subject, FileType fileType, DataType data) {
101        this(uuid, subject, fileType, null, null, null, null, null, null, data, (String[]) null);
102    }
103
104    public Document(UUID uuid, String subject, FileType fileType, String openingReceipt,
105                    SmsNotification smsNotification, EmailNotification emailNotification,
106                    AuthenticationLevel authenticationLevel,
107                    SensitivityLevel sensitivityLevel) {
108        this(uuid, subject, fileType, openingReceipt, smsNotification, emailNotification, authenticationLevel, sensitivityLevel, null, null, (String[]) null);
109    }
110
111
112    public Document(UUID uuid, String subject, FileType fileType, String openingReceipt,
113                    SmsNotification smsNotification, EmailNotification emailNotification,
114                    AuthenticationLevel authenticationLevel,
115                    SensitivityLevel sensitivityLevel, Boolean opened, DataType data, String... technicalType) {
116        this.uuid = uuid;
117        this.subject = subject;
118        this.digipostFileType = Objects.toString(fileType, null);
119        this.openingReceipt = defaultIfBlank(openingReceipt, null);
120        this.opened = Boolean.TRUE.equals(opened) ? true : null;
121        this.smsNotification = smsNotification;
122        this.emailNotification = emailNotification;
123        this.authenticationLevel = authenticationLevel;
124        this.sensitivityLevel = sensitivityLevel;
125        this.technicalType = parseTechnicalTypes(technicalType);
126        this.dataType = data != null ? new DataTypeHolder(data) : null;
127        this.validate();
128    }
129
130    static String parseTechnicalTypes(String... technicalTypes){
131        if(technicalTypes == null || technicalTypes.length == 0) {
132            return null;
133        }
134
135        return Stream.of(technicalTypes)
136            .filter(s -> Objects.nonNull(s) && !s.isEmpty())
137            .map(String::trim)
138            .collect(collectingAndThen(joining(","), joined -> joined.isEmpty() ? null : joined));
139    }
140
141    public Document copyDocumentAndSetDigipostFileTypeToPdf(){
142        Document newDoc = new Document(this.uuid, this.subject, new FileType("pdf"), this.openingReceipt, this.smsNotification, this.emailNotification,
143                this.authenticationLevel, this.sensitivityLevel, this.opened, this.dataType != null ? this.dataType.get() : null, this.getTechnicalType());
144
145        newDoc.encrypted  = this.encrypted == null ? null : this.encrypted.copy();
146        newDoc.setContentHash(this.contentHash);
147
148        return newDoc;
149    }
150
151    private void validate() {
152        List<String> errors = new ArrayList<>();
153        if (openingReceipt != null && opened != null) {
154            errors.add("Both openingReceipt and opened was set");
155        }
156        if (!errors.isEmpty()) {
157            throw new IllegalStateException(
158                    errors.size() + " errors when instantiating " + Document.class.getSimpleName() +
159                    "\n - " + join(errors, "\n - "));
160        }
161    }
162
163    public static Document technicalAttachment(FileType fileType, String... type) {
164        Document document = new Document(UUID.randomUUID(), null, fileType);
165        document.technicalType = parseTechnicalTypes(type);
166        return document;
167    }
168
169    public void setContentHash(ContentHash contentHash){
170        this.contentHash = contentHash;
171    }
172
173    public void setDigipostFileType(FileType fileType) {
174        this.digipostFileType = fileType.toString();
175    }
176
177    public String getDigipostFileType() {
178        return digipostFileType;
179    }
180
181    public boolean is(FileType fileType) {
182        return fileType.equals(new FileType(digipostFileType));
183    }
184
185    public Document encrypt() {
186        if (this.encrypted != null) {
187            throw new IllegalStateException("Document already set to encrypted, are you calling encrypt() twice?");
188        }
189        this.encrypted = new EncryptionInfo();
190        return this;
191    }
192
193    public boolean willBeEncrypted() {
194        return encrypted != null;
195    }
196
197    public EncryptionInfo getEncrypted() {
198        return encrypted;
199    }
200
201    public Link getAddContentLink() {
202        return getLinkByRelationName(Relation.ADD_CONTENT);
203    }
204
205    public AddDataLink getAddDataLink() {
206        return new AddDataLink(getLinkByRelationName(Relation.ADD_DATA).getUri().getPath());
207    }
208
209    public Link getEncryptionKeyLink() {
210        return getLinkByRelationName(Relation.GET_ENCRYPTION_KEY);
211    }
212
213    public String[] getTechnicalType() {
214        return technicalType != null ? technicalType.split(",") : null;
215    }
216
217    public boolean isOpened() {
218        return opened != null && opened;
219    }
220
221    public Optional<DataType> getDataType() {
222        return Optional.ofNullable(dataType).map(DataTypeHolder::get);
223    }
224
225    @Override
226    public String toString() {
227        return getClass().getSimpleName() + " with uuid '" + uuid + "'" +
228                ofNullable(technicalType).map(t -> ", technicalType '" + t + "'").orElse("") +
229                (willBeEncrypted() ? ofNullable(subject).map(s -> ", subject '" + s + "'").orElse(", no subject") : ", encrypted");
230    }
231
232    public void setNumberOfEncryptedPages(int pages) {
233        if (this.encrypted == null) {
234            throw new IllegalStateException("Tried setting number of encrypted pages, but document is not set to be encrypted. Have you called Document.encrypt()?");
235        }
236        this.encrypted.setNumberOfPages(pages);
237    }
238}