|
| 1 | +package com.danubetech.verifiablecredentials; |
| 2 | + |
| 3 | +import com.apicatalog.jsonld.loader.DocumentLoader; |
| 4 | +import com.danubetech.verifiablecredentials.credentialstatus.CredentialStatus; |
| 5 | +import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialContexts; |
| 6 | +import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialKeywords; |
| 7 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 8 | +import foundation.identity.jsonld.JsonLDObject; |
| 9 | +import foundation.identity.jsonld.JsonLDUtils; |
| 10 | +import info.weboftrust.ldsignatures.LdProof; |
| 11 | + |
| 12 | +import java.io.Reader; |
| 13 | +import java.net.URI; |
| 14 | +import java.util.Date; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +public class VerifiableCredentialV2 extends JsonLDObject { |
| 19 | + |
| 20 | + public static final URI[] DEFAULT_JSONLD_CONTEXTS = { VerifiableCredentialContexts.JSONLD_CONTEXT_W3C_CREDENTIALS_V2 }; |
| 21 | + public static final String[] DEFAULT_JSONLD_TYPES = { VerifiableCredentialKeywords.JSONLD_TERM_VERIFIABLE_CREDENTIAL }; |
| 22 | + public static final String DEFAULT_JSONLD_PREDICATE = VerifiableCredentialKeywords.JSONLD_TERM_VERIFIABLECREDENTIAL; |
| 23 | + public static final DocumentLoader DEFAULT_DOCUMENT_LOADER = VerifiableCredentialContexts.DOCUMENT_LOADER; |
| 24 | + |
| 25 | + @JsonCreator |
| 26 | + public VerifiableCredentialV2() { |
| 27 | + super(); |
| 28 | + } |
| 29 | + |
| 30 | + protected VerifiableCredentialV2(Map<String, Object> jsonObject) { |
| 31 | + super(jsonObject); |
| 32 | + } |
| 33 | + |
| 34 | + /* |
| 35 | + * Factory methods |
| 36 | + */ |
| 37 | + |
| 38 | + public static class Builder<B extends Builder<B>> extends JsonLDObject.Builder<B> { |
| 39 | + |
| 40 | + private Object issuer; |
| 41 | + private Date validFrom; |
| 42 | + private Date validUntil; |
| 43 | + private CredentialSubject credentialSubject; |
| 44 | + private Object credentialStatus; |
| 45 | + private String name; |
| 46 | + private String description; |
| 47 | + private LdProof ldProof; |
| 48 | + |
| 49 | + public Builder(VerifiableCredentialV2 jsonLdObject) { |
| 50 | + super(jsonLdObject); |
| 51 | + this.forceContextsArray(true); |
| 52 | + this.forceTypesArray(true); |
| 53 | + this.defaultContexts(true); |
| 54 | + this.defaultTypes(true); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public VerifiableCredentialV2 build() { |
| 59 | + |
| 60 | + super.build(); |
| 61 | + |
| 62 | + // add JSON-LD properties |
| 63 | + if (this.issuer != null) JsonLDUtils.jsonLdAdd(this.jsonLdObject, VerifiableCredentialKeywords.JSONLD_TERM_ISSUER, this.issuer); |
| 64 | + if (this.validFrom != null) JsonLDUtils.jsonLdAdd(this.jsonLdObject, VerifiableCredentialKeywords.JSONLD_TERM_VALIDFROM, JsonLDUtils.dateToString(this.validFrom)); |
| 65 | + if (this.validUntil != null) JsonLDUtils.jsonLdAdd(this.jsonLdObject, VerifiableCredentialKeywords.JSONLD_TERM_VALIDUNTIL, JsonLDUtils.dateToString(this.validUntil)); |
| 66 | + if (this.credentialSubject != null) this.credentialSubject.addToJsonLDObject(this.jsonLdObject); |
| 67 | + if (this.credentialStatus != null) JsonLDUtils.jsonLdAdd(this.jsonLdObject, VerifiableCredentialKeywords.JSONLD_TERM_CREDENTIALSTATUS, this.credentialStatus); |
| 68 | + if(this.name != null) JsonLDUtils.jsonLdAdd(this.jsonLdObject, VerifiableCredentialKeywords.JSONLD_TERM_NAME, this.name); |
| 69 | + if(this.description != null) JsonLDUtils.jsonLdAdd(this.jsonLdObject, VerifiableCredentialKeywords.JSONLD_TERM_NAME, this.description); |
| 70 | + if (this.ldProof != null) this.ldProof.addToJsonLDObject(this.jsonLdObject); |
| 71 | + |
| 72 | + return (VerifiableCredentialV2) this.jsonLdObject; |
| 73 | + } |
| 74 | + |
| 75 | + public B issuer(URI issuer) { |
| 76 | + this.issuer = issuer; |
| 77 | + return (B) this; |
| 78 | + } |
| 79 | + |
| 80 | + public B validFrom(Date validFrom) { |
| 81 | + this.validFrom = validFrom; |
| 82 | + return (B) this; |
| 83 | + } |
| 84 | + |
| 85 | + public B validUntil(Date validUntil) { |
| 86 | + this.validUntil = validUntil; |
| 87 | + return (B) this; |
| 88 | + } |
| 89 | + |
| 90 | + public B credentialSubject(CredentialSubject credentialSubject) { |
| 91 | + this.credentialSubject = credentialSubject; |
| 92 | + return (B) this; |
| 93 | + } |
| 94 | + |
| 95 | + public B credentialStatus(CredentialStatus credentialStatus) { |
| 96 | + this.credentialStatus = credentialStatus; |
| 97 | + return (B) this; |
| 98 | + } |
| 99 | + |
| 100 | + public B credentialStatus(List<CredentialStatus> credentialStatus) { |
| 101 | + this.credentialStatus = credentialStatus; |
| 102 | + return (B) this; |
| 103 | + } |
| 104 | + |
| 105 | + public B ldProof(LdProof ldProof) { |
| 106 | + this.ldProof = ldProof; |
| 107 | + return (B) this; |
| 108 | + } |
| 109 | + |
| 110 | + public B name(String name) { |
| 111 | + this.name = name; |
| 112 | + return (B) this; |
| 113 | + } |
| 114 | + |
| 115 | + public B description(String description) { |
| 116 | + this.description = description; |
| 117 | + return (B) this; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static Builder<? extends Builder<?>> builder() { |
| 122 | + return new Builder<>(new VerifiableCredentialV2()); |
| 123 | + } |
| 124 | + |
| 125 | + public static VerifiableCredentialV2 fromJsonObject(Map<String, Object> jsonObject) { |
| 126 | + return new VerifiableCredentialV2(jsonObject); |
| 127 | + } |
| 128 | + |
| 129 | + public static VerifiableCredentialV2 fromJsonLDObject(JsonLDObject jsonLDObject) { return fromJsonObject(jsonLDObject.getJsonObject()); } |
| 130 | + |
| 131 | + public static VerifiableCredentialV2 fromJson(Reader reader) { |
| 132 | + return new VerifiableCredentialV2(readJson(reader)); |
| 133 | + } |
| 134 | + |
| 135 | + public static VerifiableCredentialV2 fromJson(String json) { |
| 136 | + return new VerifiableCredentialV2(readJson(json)); |
| 137 | + } |
| 138 | + |
| 139 | + public static VerifiableCredentialV2 fromMap(Map<String, Object> map) { |
| 140 | + return new VerifiableCredentialV2(map); |
| 141 | + } |
| 142 | + |
| 143 | + /* |
| 144 | + * Adding, getting, and removing the JSON-LD object |
| 145 | + */ |
| 146 | + |
| 147 | + public static VerifiableCredentialV2 getFromJsonLDObject(JsonLDObject jsonLdObject) { |
| 148 | + return JsonLDObject.getFromJsonLDObject(VerifiableCredentialV2.class, jsonLdObject); |
| 149 | + } |
| 150 | + |
| 151 | + public static void removeFromJsonLdObject(JsonLDObject jsonLdObject) { |
| 152 | + JsonLDObject.removeFromJsonLdObject(VerifiableCredentialV2.class, jsonLdObject); |
| 153 | + } |
| 154 | + |
| 155 | + /* |
| 156 | + * Getters |
| 157 | + */ |
| 158 | + |
| 159 | + public Object getIssuer() { |
| 160 | + return JsonLDUtils.stringToUri(JsonLDUtils.jsonLdGetStringOrObjectId(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_ISSUER)); |
| 161 | + } |
| 162 | + |
| 163 | + public Date getValidFrom() { |
| 164 | + return JsonLDUtils.stringToDate(JsonLDUtils.jsonLdGetString(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_VALIDFROM)); |
| 165 | + } |
| 166 | + |
| 167 | + public Date getValidUntil() { |
| 168 | + return JsonLDUtils.stringToDate(JsonLDUtils.jsonLdGetString(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_VALIDUNTIL)); |
| 169 | + } |
| 170 | + |
| 171 | + public CredentialSubject getCredentialSubject() { |
| 172 | + return CredentialSubject.getFromJsonLDObject(this); |
| 173 | + } |
| 174 | + |
| 175 | + public LdProof getLdProof() { |
| 176 | + return LdProof.getFromJsonLDObject(this); |
| 177 | + } |
| 178 | + |
| 179 | + //The object can be either CredentialStatus object or list of CredentialStatus objects |
| 180 | + public Object getCredentialStatus() { |
| 181 | + return CredentialStatus.getFromJsonLDObject(this); |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + public String getName(){ |
| 186 | + return JsonLDUtils.jsonLdGetString(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_NAME); |
| 187 | + } |
| 188 | + |
| 189 | + public String getDescription(){ |
| 190 | + return JsonLDUtils.jsonLdGetString(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_DESCRIPTION); |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | +} |
0 commit comments