Exemplo n.º 1
0
        public EntryFieldRow(ClientEntry sourceEntry, EntryField sourceField)
        {
            this.SourceEntry = sourceEntry;
            this.SourceField = sourceField;

            Reset();
        }
Exemplo n.º 2
0
        public AddEditFieldDialog(EntryField entryField)
        {
            if (entryField == null)
                throw new ArgumentNullException("entryField");

            InitializeComponent();

            this.EntryField = entryField;
        }
Exemplo n.º 3
0
        public EntryFieldView(EntryField field)
        {
            if (field == null)
                throw new ArgumentNullException("field");

            this.Field = field;

            this.Name = this.Field.Name;
            this.Type = this.Field.EntryType;
            this.DefaultValue = this.Field.DefaultValue;
        }
Exemplo n.º 4
0
        public void AddField(EntryField templateField)
        {
            if (templateField == null)
                throw new ArgumentNullException("templateField");
            if (_fields.Contains(templateField))
                throw new ArgumentException("This template already contains this field.");
            if (templateField.Container != null)
                throw new ArgumentException("This template is already contained in a field container.");

            _fields.Add(templateField);

            templateField.Container = this;

            OnFieldAdded(new EntryFieldEventArgs(templateField));
        }
Exemplo n.º 5
0
 internal EntryFieldEventArgs(EntryField entryField)
 {
     this.EntryField = entryField;
 }
        public DeleteCustomFieldDialog(ClientEntry container, EntryField field)
        {
            InitializeComponent();

            mainInstructionLabel.Text = String.Format(mainInstructionLabel.Text, field.Name, container.Name);
        }
Exemplo n.º 7
0
        public void RefreshTemplates()
        {
            if (!this.Connected)
                throw new InvalidOperationException("Cannot refresh the template list because the database is not connected.");
            if (!this.Authenticated)
                throw new InvalidOperationException("Cannot refresh the template list because the current user is not authenticated with the Database.");

            var getListCommand = new SqlCommand("sp_GetAllTemplates", _connection) { CommandType = System.Data.CommandType.StoredProcedure };
            getListCommand.Parameters.Add(new SqlParameter("token", "null_token"));    //TODO: token will go here

            using (var reader = getListCommand.ExecuteReader())
            {
                HashSet<int> templateIdsInMemory = new HashSet<int>(_templates.Keys);

                while (reader.Read())
                {
                    int templateId = (int)reader[0];
                    if (templateIdsInMemory.Contains(templateId))
                        templateIdsInMemory.Remove(templateId);

                    EntryTemplate template;
                    if (!_templates.TryGetItem(templateId, out template))
                    {
                        template = new EntryTemplate();
                        _templates.Add(templateId, template);
                    }

                    //update all fields
                    template.Name = reader[1] as string;
                    template.IconIndex = (int)reader[2];
                    template.AllowCustomFields = (bool)reader[3];
                }

                //delete templates that were not returned
                foreach (var id in templateIdsInMemory)
                {
                    _templates.Remove(id);
                }

                reader.NextResult();

                HashSet<int> templateFieldIdsInMemory = new HashSet<int>(_templateFields.Keys);

                while (reader.Read())
                {
                    int templateFieldId = (int)reader[0];
                    if (templateFieldIdsInMemory.Contains(templateFieldId))
                        templateFieldIdsInMemory.Remove(templateFieldId);

                    EntryField templateField;
                    if (!_templateFields.TryGetItem(templateFieldId, out templateField))
                    {
                        //add the template field to the appropriate template
                        int templateId = (int)reader[1];
                        EntryTemplate template;
                        if (!_templates.TryGetItem(templateId, out template))
                            throw new InvalidOperationException("A template field was returned for a template that is not defined in the database.");

                        templateField = new EntryField();
                        template.AddField(templateField);

                        _templateFields.Add(templateFieldId, templateField);
                    }

                    templateField.Name = reader[2] as string;
                    templateField.EntryType = (EntryFieldType)reader[3];
                    templateField.DefaultValue = reader[4] as string;
                }

                //remove fields that were not returned
                foreach (int id in templateFieldIdsInMemory)
                {
                    _templateFields.Remove(id);
                }
            }
        }
Exemplo n.º 8
0
        private void RefreshClientEntriesInternal(ClientRecord client)
        {
            //preparation
            int clientId = _clients[client];
            if (!_clients.TryGetKey(client, out clientId))
                throw new ArgumentException("The specified client is not in this database.");

            var newEntries = new List<ClientEntry>();
            var prevEntryIds = client.Entries.Select(e => _entries[e]).ToList();

            //Query the Database
            var getListCommand = new SqlCommand("sp_GetClientEntries", _connection) { CommandType = System.Data.CommandType.StoredProcedure };
            getListCommand.Parameters.Add(new SqlParameter("token", "null_token"));    //TODO: token will go here
            getListCommand.Parameters.Add(new SqlParameter("client_id", clientId));

            using (var reader = getListCommand.ExecuteReader())
            {
                //result set 1: entry list for the specified client
                while (reader.Read())
                {
                    ClientEntry entry;
                    int clientEntryDbId = (int)reader[0];

                    //index the entry if it is new
                    if (!_entries.TryGetItem(clientEntryDbId, out entry))
                    {
                        entry = new ClientEntry();
                        _entries.Add(clientEntryDbId, entry);
                        newEntries.Add(entry);
                    }

                    //update the properties of the entry, regardless if it existed before
                    entry.Name = (string)reader[2];
                    entry.IconIndex = (int)reader[3];
                    entry.Notes = WebUtility.HtmlDecode((reader[5] as string ?? String.Empty).Replace("<br>", Environment.NewLine));

                    //set the template, it will get updated later if it doesn't exist yet
                    //we index the new template right away
                    EntryTemplate template;
                    int templateId = (reader[4] == DBNull.Value) ? -1 : (int)reader[4];
                    if (templateId == -1)
                    {
                        template = null;    //orphaned record
                    }
                    else if (!_templates.TryGetItem(templateId, out template))
                    {
                        template = new EntryTemplate();
                        _templates.Add(templateId, template);
                    }
                    entry.Template = template;
                }

                //result set 2: template definition
                reader.NextResult();
                while (reader.Read())
                {
                    EntryTemplate template;
                    int templateDbId = (int)reader[0];

                    if (!_templates.TryGetItem(templateDbId, out template))
                        throw new InvalidOperationException("A template was returned that was not part of the first result set.");

                    //update the properties
                    template.Name = reader[1] as string;
                    template.IconIndex = (int)reader[2];
                    template.AllowCustomFields = (bool)reader[3];
                }

                //result set 3: fields for the previous templates
                reader.NextResult();
                while (reader.Read())
                {
                    EntryField templateField;
                    EntryTemplate template;
                    int templateFieldId = (int)reader[0];
                    int templateId = (int)reader[1];
                    if (!_templates.TryGetItem(templateId, out template))
                        throw new InvalidOperationException("A template field was returned that belongs to a template that was not returned in the previous result set.");

                    if (!_templateFields.TryGetItem(templateFieldId, out templateField))
                    {
                        templateField = new EntryField();
                        _templateFields.Add(templateFieldId, templateField);
                    }

                    if (!template.Fields.Contains(templateField))
                        template.AddField(templateField);

                    //update the properties
                    templateField.Name = reader[2] as string;
                    templateField.EntryType = (EntryFieldType)reader[3];
                    templateField.DefaultValue = reader[4] as string;
                }
                //TODO: remove any fields that were not returned

                //result set 4: field values
                reader.NextResult();
                while (reader.Read())
                {
                    int fieldValueId = (int)reader[0];
                    int entryId = (int)reader[1];

                    ClientEntry entry;
                    if (!_entries.TryGetItem(entryId, out entry))
                        throw new InvalidOperationException("A ClientEntry was returned that was not defined in a previous result set.");

                    int templateFieldId = (reader[2] == DBNull.Value) ? -1 : (int)reader[2];
                    string entryValue = reader[4] as string;

                    //indexing of fields values:
                    //  template field values are indexed by TFVR objects in the _templateFieldsById dictionary
                    //  custom field values are indexed by just the EntryField object in the _customFieldsById dictionary
                    if (templateFieldId == -1)
                    {
                        //custom field
                        string fieldName = reader[3] as string;
                        EntryField customField;
                        if (!_customFields.TryGetItem(fieldValueId, out customField))
                        {
                            customField = new EntryField() { Name = fieldName };    //todo: type
                            entry.AddCustomField(customField);
                            _customFields.Add(fieldValueId, customField);
                        }

                        //update the name and value
                        customField.Name = fieldName;
                        entry.SetValue(customField, DataSecurity.DecryptStringAES(entryValue));
                    }
                    else
                    {
                        //find the template EntryField by ID
                        EntryField templateField;
                        if (_templateFields.TryGetItem(templateFieldId, out templateField))
                        {
                            //save the ID of the field value for saving later
                            if (!_templateValues.Values.Any(tfvr => tfvr.ClientEntry == entry && tfvr.TemplateField == templateField))
                                _templateValues.Add(fieldValueId, new TemplateFieldValueRelation() { ClientEntry = entry, TemplateField = templateField });

                            entry.SetValue(templateField, DataSecurity.DecryptStringAES(entryValue));
                        }
                        else
                        {
                            throw new InvalidOperationException("A template field was returned that cannot be matched to an existing template.");
                        }
                    }
                }
                //TODO: remove any fields that were not returned
            }

            //clean up the internal list, and raise events for the UI
            var deletedEntryIds = prevEntryIds.Except(_entries.Keys).ToList();
            if (deletedEntryIds.Any())
            {
                var deletedEntries = new List<ClientEntry>();
                foreach (var id in deletedEntryIds)
                {
                    var entry = _entries[id];
                    deletedEntries.Add(entry);
                    _entries.Remove(entry);
                }

                client.RemoveEntries(deletedEntries);
            }

            if (newEntries.Any())
                client.AddEntries(newEntries);
        }
Exemplo n.º 9
0
        private int GetOrCreateFieldValueId(ClientEntry entry, EntryField field)
        {
            int match = -1;

            bool isTemplate = field.Container is EntryTemplate;
            if (isTemplate)
            {
                var matches = _templateValues
                    .Where(kvp => kvp.Value.ClientEntry == entry && kvp.Value.TemplateField == field)
                    .ToList();

                if (matches.Count() == 1)
                    match = matches.First().Key;
                else if (matches.Count() > 1)
                    throw new InvalidOperationException("Database inconsistency detected.  Restart Pogs before continuting.");
            }
            else if (field.Container is ClientEntry && _customFields.ContainsItem(field))
            {
                match = _customFields[field];
            }

            if (match != -1)
            {
                return match;
            }
            else
            {
                var addCommand = new SqlCommand("sp_AddFieldValue", _connection) { CommandType = System.Data.CommandType.StoredProcedure };
                addCommand.Parameters.Add(new SqlParameter("token", "null_token"));    //TODO: token will go here
                addCommand.Parameters.Add(new SqlParameter("entry_id", _entries[entry]));
                addCommand.Parameters.Add(new SqlParameter("template_field_id", isTemplate ? (object)_templateFields[field] : DBNull.Value));
                addCommand.Parameters.Add(new SqlParameter("name", isTemplate ? (object)DBNull.Value : (object)field.Name));
                addCommand.Parameters.Add(new SqlParameter("value", DBNull.Value));

                //assign the IDs
                int newId = (int)addCommand.ExecuteScalar();
                if (isTemplate)
                    _templateValues.Add(newId, new TemplateFieldValueRelation() { ClientEntry = entry, TemplateField = field });
                else
                    _customFields.Add(newId, field);

                return newId;
            }
        }
Exemplo n.º 10
0
        private void DemoteTemplateField(EntryTemplate template, EntryField field)
        {
            foreach (var entry in _entries.Items.Where(e => e.Template == template))
            {
                //transfer the value to the custom field if applicable
                var relation = _templateValues.FirstOrDefault(tfvr => tfvr.Value.ClientEntry == entry && tfvr.Value.TemplateField == field);
                if (relation.Value != null)
                {
                    var newField = new EntryField() { Name = field.Name, EntryType = field.EntryType };
                    entry.AddCustomField(newField);

                    var value = entry.GetOrphanedValue(field);
                    entry.SetValue(newField, DataSecurity.DecryptStringAES(value));

                    _templateValues.Remove(relation.Key);
                    _customFields.Add(relation.Key, newField);
                }
            }

            //call the DB to do the same as above
            int fieldId = _templateFields[field];
            var purgeCommand = new SqlCommand("sp_DeleteTemplateField", _connection) { CommandType = System.Data.CommandType.StoredProcedure };
            purgeCommand.Parameters.Add(new SqlParameter("token", "null_token"));    //TODO: token will go here
            purgeCommand.Parameters.Add(new SqlParameter("template_field_id", fieldId));
            purgeCommand.ExecuteNonQuery();

            _templateFields.Remove(fieldId);
        }
Exemplo n.º 11
0
        public void RemoveField(EntryField field)
        {
            if (field == null)
                throw new ArgumentNullException("field");
            if (!_fields.Contains(field))
                throw new ArgumentException("This template does not contain the specified field.");

            _fields.Remove(field);

            OnFieldRemoved(new EntryFieldEventArgs(field));
        }
Exemplo n.º 12
0
        public void SetValue(EntryField field, string value)
        {
            if (!_customFields.Contains(field) &&
                (this.Template == null || !this.Template.Fields.Contains(field)))
                throw new ArgumentException("The field specified is not part of this entry template, or is not one of this entry's custom fields.");

            _values[field] = value;
        }
Exemplo n.º 13
0
        public void RemoveCustomField(EntryField entryField)
        {
            if (!_customFields.Contains(entryField))
                throw new ArgumentException("The EntryField specified is not a custom field for this entry.");

            _customFields.Remove(entryField);
            entryField.Container = null;

            OnCustomFieldRemoved(new EntryFieldEventArgs(entryField));
        }
Exemplo n.º 14
0
        /// <summary>
        /// Gets the value for a particular field for this entry.
        /// </summary>
        /// <param name="field">The custom field or template field to retrieve the value of.</param>
        /// <returns></returns>
        public string GetValue(EntryField field)
        {
            if (!_customFields.Contains(field) &&
                (this.Template == null || !this.Template.Fields.Contains(field)))
                throw new ArgumentException("The field specified is not part of this entry template, or is not one of this entry's custom fields.");

            if (!_values.ContainsKey(field))
            {
                _values.Add(field, field.DefaultValue ?? String.Empty);
            }

            return _values[field];
        }
Exemplo n.º 15
0
 public string GetOrphanedValue(EntryField field)
 {
     string result;
     _values.TryGetValue(field, out result);
     return result;
 }
Exemplo n.º 16
0
        public void AddCustomField(EntryField field)
        {
            if (_customFields.Contains(field))
                throw new ArgumentException("The EntryField specified already exists in this ClientEntry.");
            if (field.Container != null)
                throw new ArgumentException("The EntryField specified is alredy contained within another container.");

            _customFields.Add(field);
            field.Container = this;

            OnCustomFieldAdded(new EntryFieldEventArgs(field));
        }