blob: 1246e16798249d6727bb4971e4c5c07c46067e33 [file] [log] [blame]
Constantin Ziesche857c7ab2020-02-25 11:24:51 +01001/*******************************************************************************
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*******************************************************************************/
11using BaSyx.Models.Core.AssetAdministrationShell.References;
12using Newtonsoft.Json;
13using System.Runtime.Serialization;
14using System.Xml.Serialization;
15
16namespace BaSyx.Models.Core.AssetAdministrationShell.Identification
17{
18 [DataContract]
19 public class Identifier
20 {
21 [DataMember(EmitDefaultValue = false, IsRequired = true, Name = "id")]
22 [JsonProperty(Required = Required.Always, DefaultValueHandling = DefaultValueHandling.Include)]
23 [XmlText]
24 public string Id { get; set; }
25
26 [DataMember(EmitDefaultValue = false, IsRequired = true, Name = "idType")]
27 [JsonProperty(Required = Required.Always, DefaultValueHandling = DefaultValueHandling.Include)]
28 [XmlAttribute("idType")]
29 public KeyType IdType { get; set; }
30
31 internal Identifier() { }
32
33 public Identifier(string id, KeyType idType)
34 {
35 Id = id;
36 IdType = idType;
37 }
38
39
40 public class UniformResourceIdentifier : Identifier
41 {
42 public string Organisation { get; internal set; }
43 public string SubUnit { get; internal set; }
44 public string DomainId { get; internal set; }
45 public string Version { get; internal set; }
46 public string Revision { get; internal set; }
47 public string ElementId { get; internal set; }
48 public string InstanceNumber { get; internal set; }
49
50 public UniformResourceIdentifier(string organisation, string subUnit, string domainId, string version, string revision, string elementId, string instanceNumber)
51 : base(ToUrn(organisation, subUnit, domainId, version, revision, elementId, instanceNumber), KeyType.IRI)
52 {
53 Organisation = organisation;
54 SubUnit = subUnit;
55 DomainId = domainId;
56 Version = version;
57 Revision = revision;
58 ElementId = elementId;
59 InstanceNumber = instanceNumber;
60 }
61
62 public string ToUri() => ToUri(Organisation, SubUnit, DomainId, Version, Revision, ElementId, InstanceNumber);
63 public string ToUrn() => ToUrn(Organisation, SubUnit, DomainId, Version, Revision, ElementId, InstanceNumber);
64
65 public static string ToUri(string organisation, string subUnit, string domainId, string version, string revision, string elementId, string instanceNumber)
66 {
67 string uri = "http://";
68
69 uri += organisation + "/";
70
71 if (!string.IsNullOrEmpty(subUnit))
72 uri += subUnit + "/";
73 if (!string.IsNullOrEmpty(domainId))
74 uri += domainId + "/";
75 if (!string.IsNullOrEmpty(version))
76 uri += version + "/";
77 if (!string.IsNullOrEmpty(revision))
78 uri += revision + "/";
79 if (!string.IsNullOrEmpty(elementId))
80 uri += elementId + "/";
81
82 if (!string.IsNullOrEmpty(instanceNumber))
83 {
84 uri = uri.Substring(0, uri.Length - 2);
85 uri += "#" + instanceNumber;
86 }
87 return uri;
88 }
89 public static string ToUrn(string organisation, string subUnit, string domainId, string version, string revision, string elementId, string instanceNumber)
90 {
91 string urn = "urn:";
92
93 urn += organisation + ":";
94
95 if (!string.IsNullOrEmpty(subUnit))
96 urn += subUnit + ":";
97 if (!string.IsNullOrEmpty(domainId))
98 urn += domainId + ":";
99 if (!string.IsNullOrEmpty(version))
100 urn += version + ":";
101 if (!string.IsNullOrEmpty(revision))
102 urn += revision + ":";
103 if (!string.IsNullOrEmpty(elementId))
104 urn += elementId + ":";
105
106 if (!string.IsNullOrEmpty(instanceNumber))
107 {
108 urn = urn.Substring(0, urn.Length - 2);
109 urn += "#" + instanceNumber;
110 }
111 return urn;
112 }
113 }
114
115 }
116
117}