edc_web_components/models/
asset_item.rs1use crate::models::dataset_extra_fields::DatasetExtraFields;
2use crate::models::{Creator, DataspaceDataset, Thumbnail};
3use edc_connector_client::types::asset::Asset;
4use edc_connector_client::types::catalog::Dataset;
5use edc_connector_client::types::data_address::DataAddress;
6use edc_connector_client::types::properties::{Properties, PropertyValue};
7
8#[derive(Clone, Debug, PartialEq)]
9pub struct AssetItem {
10 pub id: String,
11 pub name: String,
12 pub version: Option<semver::Version>,
13 pub description: Option<String>,
14 pub creator: Option<Creator>,
15 pub thumbnail: Option<Thumbnail>,
16 pub keywords: Vec<String>,
17 pub landing_page: Option<String>,
18 pub dcterm_types: Vec<String>,
19 pub base_url: String,
20 pub proxy_path: bool,
21 pub proxy_query_params: bool,
22 pub proxy_method: bool,
23 pub proxy_body: bool,
24 pub oauth2_client_id: String,
25 pub oauth2_client_secret_key: String,
26 pub oauth2_token_url: String,
27 pub oauth2_scope: String,
28}
29
30impl AssetItem {
31 pub fn is_filtered(&self, search: &Option<String>, dcterm_types: &[String]) -> bool {
32 let text_content = format!(
33 "{} {} {} {}",
34 self.name.to_lowercase(),
35 self.description.clone().unwrap_or_default().to_lowercase(),
36 self.keywords.join(" ").to_lowercase(),
37 self
38 .creator
39 .clone()
40 .map(|creator| creator.name.unwrap_or_default().to_lowercase())
41 .unwrap_or_default()
42 );
43
44 if !dcterm_types.is_empty() {
45 if self
46 .dcterm_types
47 .iter()
48 .any(|dcterm_type| dcterm_types.contains(dcterm_type))
49 {
50 if let Some(search) = &search {
51 text_content.contains(&search.to_lowercase())
52 } else {
53 true
54 }
55 } else {
56 false
57 }
58 } else {
59 if let Some(search) = &search {
60 text_content.contains(&search.to_lowercase())
61 } else {
62 true
63 }
64 }
65 }
66}
67
68impl From<Asset> for AssetItem {
69 fn from(asset: Asset) -> Self {
70 let id = asset.id().to_string();
71 let name = get_property(asset.properties(), "name");
72 let description = asset
73 .properties()
74 .get::<String>("http://www.w3.org/2000/01/rdf-schema#comment")
75 .unwrap_or_default();
76
77 let version = asset
78 .properties()
79 .get::<String>("http://www.w3.org/ns/dcat#version")
80 .unwrap_or_default()
81 .and_then(|version| semver::Version::parse(version.as_str()).ok());
82
83 let creator = asset
84 .properties()
85 .get_raw("http://purl.org/dc/terms/creator")
86 .and_then(|property_value| serde_json::from_value::<Creator>(property_value.0.clone()).ok());
87
88 let thumbnail = asset
89 .properties()
90 .get_raw("http://xmlns.com/foaf/0.1/thumbnail")
91 .and_then(|property_value| {
92 serde_json::from_value::<Thumbnail>(property_value.0.clone()).ok()
93 });
94
95 let keywords = asset
96 .properties()
97 .get_raw("http://www.w3.org/ns/dcat#keyword")
98 .and_then(|property_value| {
99 serde_json::from_value::<Vec<String>>(property_value.0.clone()).ok()
100 })
101 .unwrap_or_default();
102
103 let landing_page = asset
104 .properties()
105 .get::<String>("http://www.w3.org/ns/dcat#landingPage")
106 .unwrap_or_default();
107
108 let dcterm_types = asset
109 .properties()
110 .get_raw("http://purl.org/dc/terms/type")
111 .and_then(|property_value| {
112 if let Ok(value) = PropertyValue::try_from(property_value) {
113 Some(vec![value])
114 } else {
115 serde_json::from_value::<Vec<String>>(property_value.0.clone()).ok()
116 }
117 })
118 .unwrap_or_default();
119
120 let base_url = asset
121 .data_address()
122 .property("baseUrl")
123 .unwrap_or_default()
124 .unwrap_or_default();
125
126 let proxy_path = get_boolean_property(asset.data_address(), "proxyPath");
127 let proxy_query_params = get_boolean_property(asset.data_address(), "proxyQueryParams");
128 let proxy_method = get_boolean_property(asset.data_address(), "proxyMethod");
129 let proxy_body = get_boolean_property(asset.data_address(), "proxyBody");
130
131 let oauth2_client_id = asset
132 .data_address()
133 .property("oauth2:clientId")
134 .unwrap_or_default()
135 .unwrap_or_default();
136
137 let oauth2_client_secret_key = asset
138 .data_address()
139 .property("oauth2:clientSecretKey")
140 .unwrap_or_default()
141 .unwrap_or_default();
142
143 let oauth2_scope = asset
144 .data_address()
145 .property("oauth2:scope")
146 .unwrap_or_default()
147 .unwrap_or_default();
148
149 let oauth2_token_url = asset
150 .data_address()
151 .property("oauth2:tokenUrl")
152 .unwrap_or_default()
153 .unwrap_or_default();
154
155 AssetItem {
156 id,
157 name,
158 version,
159 description,
160 creator,
161 thumbnail,
162 keywords,
163 landing_page,
164 dcterm_types,
165 base_url,
166 proxy_path,
167 proxy_query_params,
168 proxy_method,
169 proxy_body,
170 oauth2_client_id,
171 oauth2_client_secret_key,
172 oauth2_token_url,
173 oauth2_scope,
174 }
175 }
176}
177
178impl From<edc_federated_catalog_client::models::Dataset> for AssetItem {
179 fn from(dataset: edc_federated_catalog_client::models::Dataset) -> Self {
180 Self {
181 id: dataset.id.clone(),
182 name: dataset.name,
183 version: dataset
184 .version
185 .and_then(|version| semver::Version::parse(&version).ok()),
186 description: dataset.description,
187 creator: dataset.creator.map(|creator| Creator {
188 name: Some(creator.name),
189 thumbnail: Some(Thumbnail {
190 resource: Some(creator.thumbnail.resource),
191 }),
192 }),
193 thumbnail: dataset.thumbnail.map(|thumbnail| Thumbnail {
194 resource: Some(thumbnail.resource),
195 }),
196 keywords: dataset.keywords,
197 landing_page: dataset.landing_page,
198 dcterm_types: dataset.dcterm_types,
199 base_url: "".to_string(),
200 proxy_path: false,
201 proxy_query_params: false,
202 proxy_method: false,
203 proxy_body: false,
204 oauth2_client_id: "".to_string(),
205 oauth2_client_secret_key: "".to_string(),
206 oauth2_token_url: "".to_string(),
207 oauth2_scope: "".to_string(),
208 }
209 }
210}
211
212impl From<&Dataset<DatasetExtraFields>> for AssetItem {
213 fn from(dataset: &Dataset<DatasetExtraFields>) -> Self {
214 let id = dataset.id().to_string();
215 let extra = &dataset.extra;
216
217 Self {
218 id,
219 name: extra.name.clone(),
220 version: extra
221 .version
222 .as_ref()
223 .and_then(|version| semver::Version::parse(version).ok()),
224 description: extra.description.to_owned(),
225 creator: extra.creator.as_ref().map(|creator| Creator {
226 name: Some(creator.name.to_owned()),
227 thumbnail: Some(Thumbnail {
228 resource: Some(creator.thumbnail.resource.to_owned()),
229 }),
230 }),
231 thumbnail: extra.thumbnail.as_ref().map(|thumbnail| Thumbnail {
232 resource: Some(thumbnail.resource.to_owned()),
233 }),
234 keywords: extra.keywords.clone(),
235 landing_page: extra.landing_page.clone(),
236 dcterm_types: extra.dcterm_types.clone(),
237 base_url: "".to_string(),
238 proxy_path: false,
239 proxy_query_params: false,
240 proxy_method: false,
241 proxy_body: false,
242 oauth2_client_id: "".to_string(),
243 oauth2_client_secret_key: "".to_string(),
244 oauth2_token_url: "".to_string(),
245 oauth2_scope: "".to_string(),
246 }
247 }
248}
249
250impl From<AssetItem> for DataspaceDataset {
251 fn from(asset_item: AssetItem) -> Self {
252 Self {
253 id: asset_item.id,
254 title: asset_item.name,
255 version: asset_item.version,
256 comment: asset_item.description,
257 thumbnail: asset_item.thumbnail,
258 creator: asset_item.creator,
259 keywords: asset_item.keywords,
260 dcterm_types: asset_item.dcterm_types,
261 policies: vec![],
262 }
263 }
264}
265
266fn get_property(properties: &Properties, name: &str) -> String {
267 properties
268 .get::<String>(name)
269 .unwrap_or_default()
270 .unwrap_or_default()
271}
272
273fn get_boolean_property(data_address: &DataAddress, name: &str) -> bool {
274 data_address
275 .property::<String>(name)
276 .unwrap_or_default()
277 .unwrap_or_default()
278 == "true"
279}