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
use std::{cell::RefCell, collections::HashSet, rc::Rc};

use crate::{DeletePlugin, Hit, Id, IndexEntryProperty, Model, ObjectValue, ObjectValues, Plugin};
use crate::{HitError, InitEntryPlugin};

use super::unique_in_parent_value_index::UniqueInParentValueIndex;

pub struct UniqueInParentPlugin {
    pub(in crate::prelude::validators::unique_in_parent) property_names: HashSet<String>,
    pub(in crate::prelude::validators::unique_in_parent) model_names: HashSet<String>,
    pub(in crate::prelude::validators::unique_in_parent) index:
        Rc<RefCell<UniqueInParentValueIndex>>,
}

impl UniqueInParentPlugin {
    pub fn new(index: Rc<RefCell<UniqueInParentValueIndex>>) -> Self {
        UniqueInParentPlugin {
            property_names: HashSet::new(),
            model_names: HashSet::new(),
            index: index,
        }
    }

    fn handle_new_object(
        &mut self,
        model: Rc<Model>,
        id: &str,
        data: ObjectValues,
        parent: IndexEntryProperty,
    ) {
        // only if model is tracked (faster)
        if self.model_names.contains(model.get_name()) {
            for (field_name, _field) in model.get_fields().iter() {
                // only for matched field names
                if self.property_names.contains(field_name) {
                    // only if there is data and it has a string value
                    match data.get(field_name) {
                        Some(data) => match data {
                            ObjectValue::String(value) => {
                                self.index.borrow_mut().set(
                                    // TODO : use the propertyname defined in the validator config
                                    field_name,
                                    &parent.id,
                                    &parent.property,
                                    id,
                                    Some(value.to_string()),
                                );
                            }
                            _ => {}
                        },
                        None => {}
                    }
                }
            }
        }
    }

    fn validate_index(
        &self,
        instance: &mut Hit,
        property_name: &str,
        parent_id: &str,
        parent_property_name: &str,
    ) -> Result<(), HitError> {
        match self
            .index
            .borrow()
            .get(property_name, parent_id, parent_property_name)
        {
            Some(index) => {
                for entry in index.iter() {
                    instance.validate_field(&entry.id, property_name)?;
                }
            }
            None => {}
        }
        Ok(())
    }
}

impl InitEntryPlugin for UniqueInParentPlugin {
    fn on_init_add_entry(
        &mut self,
        model: Rc<crate::Model>,
        id: &str,
        data: crate::ObjectValues,
        parent: Option<crate::IndexEntryProperty>,
    ) {
        // is not applied for the root object
        match parent {
            Some(parent) => self.handle_new_object(model, id, data, parent),
            _ => {}
        }
    }
}

impl DeletePlugin for UniqueInParentPlugin {
    fn on_before_delete_entry(
        &mut self,
        _entry: &crate::HitEntry,
        _instance: &mut crate::Hit,
    ) -> Result<(), HitError> {
        Ok(())
    }

    fn on_after_delete_entry(
        &mut self,
        entry: &crate::HitEntry,
        _instance: &mut crate::Hit,
    ) -> Result<(), HitError> {
        let model = entry.get_model();
        let parent = entry.get_parent().ok_or(HitError::NoParent())?;
        for (name, _field) in model.get_fields().iter() {
            if self.property_names.contains(name) {
                // Delete from index
                self.index.borrow_mut().remove_value(
                    name,
                    &parent.id,
                    &parent.property,
                    &entry.get_id(),
                );
                self.validate_index(_instance, name, &parent.id, &parent.property)?;
            }
        }
        Ok(())
    }
}

impl Plugin for UniqueInParentPlugin {
    fn on_before_add_entry(
        &mut self,
        _model: Rc<crate::Model>,
        _id: &str,
        _data: crate::ObjectValues,
        _parent: crate::IndexEntryProperty,
        _before_id: &Option<Id>,
        _instance: &crate::Hit,
    ) -> Result<(), HitError> {
        Ok(())
    }

    fn on_after_add_entry(
        &mut self,
        model: Rc<crate::Model>,
        id: &str,
        data: crate::ObjectValues,
        parent: crate::IndexEntryProperty,
        _before_id: &Option<Id>,
        instance: &mut crate::Hit,
    ) -> Result<(), HitError> {
        self.handle_new_object(model.clone(), id, data, parent.clone());
        if self.model_names.contains(model.get_name()) {
            for (field_name, _field) in model.get_fields().iter() {
                // only for matched field names
                if self.property_names.contains(field_name) {
                    self.validate_index(instance, field_name, &parent.id, &parent.property)?;
                }
            }
        }
        Ok(())
    }

    fn on_before_set_value(
        &mut self,
        _property: crate::IndexEntryProperty,
        _value: &ObjectValue,
        _old_value: &Option<ObjectValue>,
        _instance: &crate::Hit,
    ) -> Result<(), HitError> {
        Ok(())
    }

    fn on_after_set_value(
        &mut self,
        property: crate::IndexEntryProperty,
        value: &ObjectValue,
        _old_value: &Option<ObjectValue>,
        instance: &mut crate::Hit,
    ) -> Result<(), HitError> {
        if self.property_names.contains(&property.property) {
            match instance.get_parent(&property.id).clone() {
                Some(parent) => match value {
                    ObjectValue::String(value) => {
                        self.index.borrow_mut().set(
                            &property.property,
                            &parent.id,
                            &parent.property,
                            &property.id,
                            Some(value.to_string()),
                        );
                        self.validate_index(
                            instance,
                            &property.property,
                            &parent.id,
                            &parent.property,
                        )?;
                    }
                    _ => {}
                },
                None => {}
            }
        }
        Ok(())
    }

    fn on_before_move_subobject(
        &mut self,
        _id: &str,
        _target: crate::IndexEntryProperty,
        _before_id: Option<String>,
        _instance: &crate::Hit,
    ) -> Result<(), HitError> {
        Ok(())
    }

    fn on_after_move_subobject(
        &mut self,
        id: &str,
        target: crate::IndexEntryProperty,
        original_parent: crate::IndexEntryProperty,
        _before_id: Option<String>,
        instance: &mut crate::Hit,
    ) -> Result<(), HitError> {
        let model = instance
            .get_model(id)
            .ok_or(HitError::NoModelForId(id.to_string()))?;
        if self.model_names.contains(model.get_name()) {
            for (field_name, _field) in model.get_fields().iter() {
                if self.property_names.contains(field_name) {
                    // Remove from origin index
                    self.index.borrow_mut().remove_value(
                        field_name,
                        &original_parent.id,
                        &original_parent.property,
                        id,
                    );
                    self.validate_index(
                        instance,
                        field_name,
                        &original_parent.id,
                        &original_parent.property,
                    )?;

                    // Add to target index
                    let value = {
                        match instance.get_value(id, field_name) {
                            Some(value) => match value {
                                ObjectValue::String(value) => Some(value),
                                _ => None,
                            },
                            None => None,
                        }
                    };
                    self.index.borrow_mut().set(
                        field_name,
                        &target.id,
                        &target.property,
                        id,
                        value,
                    );

                    self.validate_index(instance, field_name, &target.id, &target.property)?;
                }
            }
        }
        Ok(())
    }
}