Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2020 Robert Bosch GmbH |
| 3 | * Author: Constantin Ziesche (constantin.ziesche@bosch.com) |
| 4 | * |
| 5 | * This program and the accompanying materials are made available under the |
| 6 | * terms of the Eclipse Public License 2.0 which is available at |
| 7 | * http://www.eclipse.org/legal/epl-2.0 |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | *******************************************************************************/ |
| 11 | using BaSyx.Models.Core.AssetAdministrationShell.References; |
| 12 | using Newtonsoft.Json; |
| 13 | using System.Runtime.Serialization; |
| 14 | using System.Xml.Serialization; |
| 15 | |
| 16 | namespace BaSyx.Models.Core.AssetAdministrationShell.Identification |
| 17 | { |
Constantin Ziesche | 310d2aa | 2020-03-25 11:48:26 +0100 | [diff] [blame^] | 18 | /// <summary> |
| 19 | /// Used to uniquely identify an entity by using an identifier. |
| 20 | /// </summary> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 21 | [DataContract] |
| 22 | public class Identifier |
| 23 | { |
Constantin Ziesche | 310d2aa | 2020-03-25 11:48:26 +0100 | [diff] [blame^] | 24 | /// <summary> |
| 25 | /// Identifier of the element. Its type is defined in idType. |
| 26 | /// </summary> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 27 | [DataMember(EmitDefaultValue = false, IsRequired = true, Name = "id")] |
| 28 | [JsonProperty(Required = Required.Always, DefaultValueHandling = DefaultValueHandling.Include)] |
| 29 | [XmlText] |
| 30 | public string Id { get; set; } |
| 31 | |
Constantin Ziesche | 310d2aa | 2020-03-25 11:48:26 +0100 | [diff] [blame^] | 32 | /// <summary> |
| 33 | /// Type of the Identifierr, e.g. IRI, IRDI etc. The supported Identifier types are defined in the enumeration IdentifierType. |
| 34 | /// </summary> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 35 | [DataMember(EmitDefaultValue = false, IsRequired = true, Name = "idType")] |
| 36 | [JsonProperty(Required = Required.Always, DefaultValueHandling = DefaultValueHandling.Include)] |
| 37 | [XmlAttribute("idType")] |
| 38 | public KeyType IdType { get; set; } |
| 39 | |
| 40 | internal Identifier() { } |
| 41 | |
| 42 | public Identifier(string id, KeyType idType) |
| 43 | { |
| 44 | Id = id; |
| 45 | IdType = idType; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | public class UniformResourceIdentifier : Identifier |
| 50 | { |
| 51 | public string Organisation { get; internal set; } |
| 52 | public string SubUnit { get; internal set; } |
| 53 | public string DomainId { get; internal set; } |
| 54 | public string Version { get; internal set; } |
| 55 | public string Revision { get; internal set; } |
| 56 | public string ElementId { get; internal set; } |
| 57 | public string InstanceNumber { get; internal set; } |
| 58 | |
Constantin Ziesche | 310d2aa | 2020-03-25 11:48:26 +0100 | [diff] [blame^] | 59 | public UniformResourceIdentifier(string organisation, string subUnit, string domainId, string version, string revision, string elementId, string instanceNumber) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 60 | : base(ToUrn(organisation, subUnit, domainId, version, revision, elementId, instanceNumber), KeyType.IRI) |
| 61 | { |
| 62 | Organisation = organisation; |
| 63 | SubUnit = subUnit; |
| 64 | DomainId = domainId; |
| 65 | Version = version; |
| 66 | Revision = revision; |
| 67 | ElementId = elementId; |
| 68 | InstanceNumber = instanceNumber; |
| 69 | } |
| 70 | |
| 71 | public string ToUri() => ToUri(Organisation, SubUnit, DomainId, Version, Revision, ElementId, InstanceNumber); |
| 72 | public string ToUrn() => ToUrn(Organisation, SubUnit, DomainId, Version, Revision, ElementId, InstanceNumber); |
| 73 | |
| 74 | public static string ToUri(string organisation, string subUnit, string domainId, string version, string revision, string elementId, string instanceNumber) |
| 75 | { |
| 76 | string uri = "http://"; |
| 77 | |
| 78 | uri += organisation + "/"; |
| 79 | |
| 80 | if (!string.IsNullOrEmpty(subUnit)) |
| 81 | uri += subUnit + "/"; |
| 82 | if (!string.IsNullOrEmpty(domainId)) |
| 83 | uri += domainId + "/"; |
| 84 | if (!string.IsNullOrEmpty(version)) |
| 85 | uri += version + "/"; |
| 86 | if (!string.IsNullOrEmpty(revision)) |
| 87 | uri += revision + "/"; |
| 88 | if (!string.IsNullOrEmpty(elementId)) |
| 89 | uri += elementId + "/"; |
| 90 | |
| 91 | if (!string.IsNullOrEmpty(instanceNumber)) |
| 92 | { |
| 93 | uri = uri.Substring(0, uri.Length - 2); |
| 94 | uri += "#" + instanceNumber; |
| 95 | } |
| 96 | return uri; |
| 97 | } |
| 98 | public static string ToUrn(string organisation, string subUnit, string domainId, string version, string revision, string elementId, string instanceNumber) |
| 99 | { |
| 100 | string urn = "urn:"; |
| 101 | |
| 102 | urn += organisation + ":"; |
| 103 | |
| 104 | if (!string.IsNullOrEmpty(subUnit)) |
| 105 | urn += subUnit + ":"; |
| 106 | if (!string.IsNullOrEmpty(domainId)) |
| 107 | urn += domainId + ":"; |
| 108 | if (!string.IsNullOrEmpty(version)) |
| 109 | urn += version + ":"; |
| 110 | if (!string.IsNullOrEmpty(revision)) |
| 111 | urn += revision + ":"; |
| 112 | if (!string.IsNullOrEmpty(elementId)) |
| 113 | urn += elementId + ":"; |
| 114 | |
| 115 | if (!string.IsNullOrEmpty(instanceNumber)) |
| 116 | { |
| 117 | urn = urn.Substring(0, urn.Length - 2); |
| 118 | urn += "#" + instanceNumber; |
| 119 | } |
| 120 | return urn; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |
| 126 | } |