oxigdal-services 0.1.4

OGC Web Services (WFS, WCS, WPS, CSW) for OxiGDAL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! WFS Transaction operations
//!
//! Implements WFS-T (Transactional WFS) for feature insert, update, and delete operations.

use crate::error::{ServiceError, ServiceResult};
use crate::wfs::{FeatureSource, WfsState};
use axum::{
    http::header,
    response::{IntoResponse, Response},
};
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use serde::Deserialize;
use std::io::Cursor;

/// Transaction action types
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum TransactionAction {
    /// Insert new features
    Insert {
        /// Feature type name
        type_name: String,
        /// Features to insert
        features: Box<Vec<geojson::Feature>>,
    },
    /// Update existing features
    Update {
        /// Feature type name
        type_name: String,
        /// Filter to select features
        filter: Option<String>,
        /// Properties to update
        properties: Box<serde_json::Map<String, serde_json::Value>>,
    },
    /// Delete features
    Delete {
        /// Feature type name
        type_name: String,
        /// Filter to select features
        filter: Option<String>,
    },
    /// Replace features
    Replace {
        /// Feature type name
        type_name: String,
        /// Filter to select features
        filter: String,
        /// Replacement feature
        feature: Box<geojson::Feature>,
    },
}

/// Transaction request
#[derive(Debug, Deserialize)]
pub struct Transaction {
    /// Transaction actions
    pub actions: Vec<TransactionAction>,
    /// Release action (ALL or SOME)
    #[serde(default = "default_release_action")]
    pub release_action: String,
    /// Lock ID for locked features
    pub lock_id: Option<String>,
}

fn default_release_action() -> String {
    "ALL".to_string()
}

/// Transaction response
#[derive(Debug)]
pub struct TransactionResponse {
    /// Number of features inserted
    pub total_inserted: usize,
    /// Number of features updated
    pub total_updated: usize,
    /// Number of features deleted
    pub total_deleted: usize,
    /// Number of features replaced
    pub total_replaced: usize,
    /// Inserted feature IDs
    pub inserted_fids: Vec<String>,
}

/// Handle transaction request
pub async fn handle_transaction(
    state: &WfsState,
    _version: &str,
    params: &serde_json::Value,
) -> Result<Response, ServiceError> {
    if !state.transactions_enabled {
        return Err(ServiceError::UnsupportedOperation(
            "Transactions not enabled".to_string(),
        ));
    }

    // Parse transaction from POST body (typically XML)
    // For simplicity, we'll accept JSON as well
    let transaction: Transaction = serde_json::from_value(params.clone())
        .map_err(|e| ServiceError::InvalidParameter("Transaction".to_string(), e.to_string()))?;

    // Execute transaction
    let response = execute_transaction(state, transaction).await?;

    // Generate response
    generate_transaction_response(&response)
}

/// Execute transaction actions
async fn execute_transaction(
    state: &WfsState,
    transaction: Transaction,
) -> ServiceResult<TransactionResponse> {
    let mut total_inserted = 0;
    let mut total_updated = 0;
    let mut total_deleted = 0;
    let mut total_replaced = 0;
    let mut inserted_fids = Vec::new();

    for action in transaction.actions {
        match action {
            TransactionAction::Insert {
                type_name,
                features,
            } => {
                let result = insert_features(state, &type_name, *features).await?;
                total_inserted += result.len();
                inserted_fids.extend(result);
            }
            TransactionAction::Update {
                type_name,
                filter,
                properties,
            } => {
                let count =
                    update_features(state, &type_name, filter.as_deref(), *properties).await?;
                total_updated += count;
            }
            TransactionAction::Delete { type_name, filter } => {
                let count = delete_features(state, &type_name, filter.as_deref()).await?;
                total_deleted += count;
            }
            TransactionAction::Replace {
                type_name,
                filter,
                feature,
            } => {
                let count = replace_features(state, &type_name, &filter, *feature).await?;
                total_replaced += count;
            }
        }
    }

    Ok(TransactionResponse {
        total_inserted,
        total_updated,
        total_deleted,
        total_replaced,
        inserted_fids,
    })
}

/// Insert features
async fn insert_features(
    state: &WfsState,
    type_name: &str,
    features: Vec<geojson::Feature>,
) -> ServiceResult<Vec<String>> {
    let ft = state
        .get_feature_type(type_name)
        .ok_or_else(|| ServiceError::NotFound(format!("Feature type: {}", type_name)))?;

    // Generate feature IDs
    let mut fids = Vec::new();
    for _ in 0..features.len() {
        let fid = format!("{}.{}", type_name, uuid::Uuid::new_v4());
        fids.push(fid);
    }

    // For now, only support in-memory features
    match &ft.source {
        FeatureSource::Memory(_) => {
            // In a real implementation, we would store the features
            // For now, just return success
            Ok(fids)
        }
        FeatureSource::File(_) => Err(ServiceError::Transaction(
            "File-based transactions not yet implemented".to_string(),
        )),
        FeatureSource::Database(_) => Err(ServiceError::Transaction(
            "Database transactions not yet implemented".to_string(),
        )),
        FeatureSource::DatabaseSource(db) => {
            // Generate INSERT SQL for the database source
            let table = db.qualified_table_name();
            let geom_col = &db.geometry_column;
            let id_col = db.id_column.as_deref().unwrap_or("id");

            // Build column list from first feature's properties
            let columns: Vec<String> = if let Some(first) = features.first() {
                let mut cols = vec![id_col.to_string(), geom_col.clone()];
                if let Some(props) = &first.properties {
                    cols.extend(props.keys().cloned());
                }
                cols
            } else {
                return Ok(fids); // No features to insert
            };

            // Generate INSERT statements for each feature
            let _insert_sql = format!(
                "INSERT INTO {} ({}) VALUES ({})",
                table,
                columns
                    .iter()
                    .map(|c| format!("\"{}\"", c))
                    .collect::<Vec<_>>()
                    .join(", "),
                columns.iter().map(|_| "?").collect::<Vec<_>>().join(", ")
            );

            // Database execution requires an actual connection
            // Return error until oxigdal-postgis or oxigdal-db-connectors is integrated
            Err(ServiceError::Transaction(format!(
                "Database insert requires connection. Use oxigdal-postgis for PostGIS. Table: {}, Features: {}",
                table,
                features.len()
            )))
        }
    }
}

/// Update features
async fn update_features(
    state: &WfsState,
    type_name: &str,
    filter: Option<&str>,
    properties: serde_json::Map<String, serde_json::Value>,
) -> ServiceResult<usize> {
    let ft = state
        .get_feature_type(type_name)
        .ok_or_else(|| ServiceError::NotFound(format!("Feature type: {}", type_name)))?;

    match &ft.source {
        FeatureSource::Memory(_) => {
            // In a real implementation, we would update the features
            Ok(0)
        }
        FeatureSource::File(_) => Err(ServiceError::Transaction(
            "File-based transactions not yet implemented".to_string(),
        )),
        FeatureSource::Database(_) => Err(ServiceError::Transaction(
            "Database transactions not yet implemented".to_string(),
        )),
        FeatureSource::DatabaseSource(db) => {
            // Generate UPDATE SQL for the database source
            let table = db.qualified_table_name();

            // Build SET clause from properties
            let set_clauses: Vec<String> = properties
                .keys()
                .map(|k| format!("\"{}\" = ?", k))
                .collect();

            if set_clauses.is_empty() {
                return Ok(0); // No properties to update
            }

            // Build WHERE clause from filter
            let where_clause = filter.map(|f| format!(" WHERE {}", f)).unwrap_or_default();

            let _update_sql = format!(
                "UPDATE {} SET {}{}",
                table,
                set_clauses.join(", "),
                where_clause
            );

            // Database execution requires an actual connection
            Err(ServiceError::Transaction(format!(
                "Database update requires connection. Use oxigdal-postgis for PostGIS. Table: {}, Properties: {}",
                table,
                properties.len()
            )))
        }
    }
}

/// Delete features
async fn delete_features(
    state: &WfsState,
    type_name: &str,
    filter: Option<&str>,
) -> ServiceResult<usize> {
    let ft = state
        .get_feature_type(type_name)
        .ok_or_else(|| ServiceError::NotFound(format!("Feature type: {}", type_name)))?;

    match &ft.source {
        FeatureSource::Memory(_) => {
            // In a real implementation, we would delete the features
            Ok(0)
        }
        FeatureSource::File(_) => Err(ServiceError::Transaction(
            "File-based transactions not yet implemented".to_string(),
        )),
        FeatureSource::Database(_) => Err(ServiceError::Transaction(
            "Database transactions not yet implemented".to_string(),
        )),
        FeatureSource::DatabaseSource(db) => {
            // Generate DELETE SQL for the database source
            let table = db.qualified_table_name();

            // Build WHERE clause from filter (required for safe deletes)
            let where_clause = match filter {
                Some(f) => format!(" WHERE {}", f),
                None => {
                    // Disallow delete without filter for safety
                    return Err(ServiceError::Transaction(
                        "Delete operation requires a filter for database sources".to_string(),
                    ));
                }
            };

            let _delete_sql = format!("DELETE FROM {}{}", table, where_clause);

            // Database execution requires an actual connection
            Err(ServiceError::Transaction(format!(
                "Database delete requires connection. Use oxigdal-postgis for PostGIS. Table: {}",
                table
            )))
        }
    }
}

/// Replace features
async fn replace_features(
    state: &WfsState,
    type_name: &str,
    filter: &str,
    feature: geojson::Feature,
) -> ServiceResult<usize> {
    let ft = state
        .get_feature_type(type_name)
        .ok_or_else(|| ServiceError::NotFound(format!("Feature type: {}", type_name)))?;

    match &ft.source {
        FeatureSource::Memory(_) => {
            // In a real implementation, we would replace the features
            Ok(0)
        }
        FeatureSource::File(_) => Err(ServiceError::Transaction(
            "File-based transactions not yet implemented".to_string(),
        )),
        FeatureSource::Database(_) => Err(ServiceError::Transaction(
            "Database transactions not yet implemented".to_string(),
        )),
        FeatureSource::DatabaseSource(db) => {
            // Replace is implemented as UPDATE with all columns from the feature
            let table = db.qualified_table_name();
            let geom_col = &db.geometry_column;

            // Build SET clause from feature properties
            let mut set_clauses = Vec::new();

            // Add geometry column update
            if feature.geometry.is_some() {
                set_clauses.push(format!("\"{}\" = ?", geom_col));
            }

            // Add property updates
            if let Some(props) = &feature.properties {
                for key in props.keys() {
                    set_clauses.push(format!("\"{}\" = ?", key));
                }
            }

            if set_clauses.is_empty() {
                return Ok(0); // Nothing to replace
            }

            let _replace_sql = format!(
                "UPDATE {} SET {} WHERE {}",
                table,
                set_clauses.join(", "),
                filter
            );

            // Database execution requires an actual connection
            Err(ServiceError::Transaction(format!(
                "Database replace requires connection. Use oxigdal-postgis for PostGIS. Table: {}",
                table
            )))
        }
    }
}

/// Generate transaction response XML
fn generate_transaction_response(response: &TransactionResponse) -> Result<Response, ServiceError> {
    use quick_xml::{
        Writer,
        events::{BytesDecl, BytesEnd, BytesStart, Event},
    };
    use std::io::Cursor;

    let mut writer = Writer::new(Cursor::new(Vec::new()));

    writer
        .write_event(Event::Decl(BytesDecl::new("1.0", Some("UTF-8"), None)))
        .map_err(|e| ServiceError::Xml(e.to_string()))?;

    let mut root = BytesStart::new("wfs:TransactionResponse");
    root.push_attribute(("version", "2.0.0"));
    root.push_attribute(("xmlns:wfs", "http://www.opengis.net/wfs/2.0"));
    root.push_attribute(("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"));

    writer
        .write_event(Event::Start(root))
        .map_err(|e| ServiceError::Xml(e.to_string()))?;

    // TransactionSummary
    writer
        .write_event(Event::Start(BytesStart::new("wfs:TransactionSummary")))
        .map_err(|e| ServiceError::Xml(e.to_string()))?;

    write_text_element(
        &mut writer,
        "wfs:totalInserted",
        &response.total_inserted.to_string(),
    )?;
    write_text_element(
        &mut writer,
        "wfs:totalUpdated",
        &response.total_updated.to_string(),
    )?;
    write_text_element(
        &mut writer,
        "wfs:totalDeleted",
        &response.total_deleted.to_string(),
    )?;

    writer
        .write_event(Event::End(BytesEnd::new("wfs:TransactionSummary")))
        .map_err(|e| ServiceError::Xml(e.to_string()))?;

    // InsertResults
    if !response.inserted_fids.is_empty() {
        writer
            .write_event(Event::Start(BytesStart::new("wfs:InsertResults")))
            .map_err(|e| ServiceError::Xml(e.to_string()))?;

        for fid in &response.inserted_fids {
            writer
                .write_event(Event::Start(BytesStart::new("wfs:Feature")))
                .map_err(|e| ServiceError::Xml(e.to_string()))?;

            write_text_element(&mut writer, "wfs:FeatureId", fid)?;

            writer
                .write_event(Event::End(BytesEnd::new("wfs:Feature")))
                .map_err(|e| ServiceError::Xml(e.to_string()))?;
        }

        writer
            .write_event(Event::End(BytesEnd::new("wfs:InsertResults")))
            .map_err(|e| ServiceError::Xml(e.to_string()))?;
    }

    writer
        .write_event(Event::End(BytesEnd::new("wfs:TransactionResponse")))
        .map_err(|e| ServiceError::Xml(e.to_string()))?;

    let xml = String::from_utf8(writer.into_inner().into_inner())
        .map_err(|e| ServiceError::Xml(e.to_string()))?;

    Ok(([(header::CONTENT_TYPE, "application/xml")], xml).into_response())
}

/// Helper to write simple text element
fn write_text_element(
    writer: &mut quick_xml::Writer<Cursor<Vec<u8>>>,
    tag: &str,
    text: &str,
) -> ServiceResult<()> {
    writer
        .write_event(Event::Start(BytesStart::new(tag)))
        .map_err(|e| ServiceError::Xml(e.to_string()))?;

    writer
        .write_event(Event::Text(BytesText::new(text)))
        .map_err(|e| ServiceError::Xml(e.to_string()))?;

    writer
        .write_event(Event::End(BytesEnd::new(tag)))
        .map_err(|e| ServiceError::Xml(e.to_string()))?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_transaction_response_generation() -> Result<(), Box<dyn std::error::Error>> {
        let response = TransactionResponse {
            total_inserted: 5,
            total_updated: 3,
            total_deleted: 2,
            total_replaced: 1,
            inserted_fids: vec!["layer.123".to_string(), "layer.456".to_string()],
        };

        let result = generate_transaction_response(&response)?;

        let (parts, _) = result.into_parts();
        assert_eq!(
            parts
                .headers
                .get(header::CONTENT_TYPE)
                .and_then(|h| h.to_str().ok()),
            Some("application/xml")
        );
        Ok(())
    }
}