Ejemplo n.º 1
0
        // create data object from data record
        protected IDataObject CreateDataObject(string objectType, int objectIndex)
        {
            Dictionary <string, string> dataRecord = _dataRecords[objectIndex];
            DataObject         objDef        = _dictionary.dataObjects.First(c => c.objectName.ToUpper() == objectType.ToUpper());
            List <KeyProperty> keyProperties = objDef.keyProperties;
            string             keyDelimeter  = objDef.keyDelimeter;
            string             identifier    = String.Empty;
            bool firstSegment = true;

            foreach (KeyProperty keyProperty in keyProperties)
            {
                if (dataRecord.ContainsKey(keyProperty.keyPropertyName))
                {
                    if (firstSegment)
                    {
                        firstSegment = false;
                    }
                    else // add the configured delimeter prior to concatenating subsequent segments of a composite key
                    {
                        identifier += keyDelimeter;
                    }

                    identifier += dataRecord[keyProperty.keyPropertyName];
                }
            }

            SerializableDataObject dataObject = new SerializableDataObject()
            {
                Type = objDef.objectName
            };

            if (!string.IsNullOrEmpty(identifier))
            {
                dataObject.Id = identifier;
            }

            SetAppCode(dataObject);

            foreach (var pair in dataRecord)
            {
                SetPropertyValue(objDef, pair, dataObject);
            }

            return(dataObject);
        }
Ejemplo n.º 2
0
        protected void AddRelatedObject(string relatedObjectType, string relatedObjectIdentifier, Dictionary <string, string> relatedRecord)
        {
            //TODO: set state
            IDataObject relatedObject = new SerializableDataObject()
            {
                Type = relatedObjectType,
                Id   = relatedObjectIdentifier
            };

            if (relatedObject.GetType() == typeof(GenericDataObject))
            {
                ((GenericDataObject)relatedObject).ObjectType = relatedObjectType;
            }

            foreach (var relatedRecordPair in relatedRecord)
            {
                relatedObject.SetPropertyValue(relatedRecordPair.Key, relatedRecordPair.Value);
            }

            _dataObjects.Add(relatedObject);
        }
Ejemplo n.º 3
0
        public override List <IDataObject> ToDataObjects(string objectType, ref XDocument xml)
        {
            try
            {
                List <IDataObject> dataObjects      = new List <IDataObject>();
                DataObject         objectDefinition = FindGraphDataObject(objectType);

                if (objectDefinition != null)
                {
                    XNamespace ns         = xml.Root.Attribute("xmlns").Value;
                    string     objectName = Utility.TitleCase(objectDefinition.objectName);

                    XElement rootEl = xml.Element(ns + objectName + "List");
                    IEnumerable <XElement> objEls = from el in rootEl.Elements(ns + objectName) select el;

                    foreach (XElement objEl in objEls)
                    {
                        SerializableDataObject dataObject = new SerializableDataObject();
                        dataObject.Type = objectDefinition.objectName;

                        if (objectDefinition.hasContent)
                        {
                            XElement xElement = objEl.Element(ns + "content");

                            if (xElement != null)
                            {
                                string base64Content = xElement.Value;

                                if (!String.IsNullOrEmpty(base64Content))
                                {
                                    ((IContentObject)dataObject).Content = base64Content.ToMemoryStream();
                                }
                            }
                        }

                        foreach (DataProperty property in objectDefinition.dataProperties)
                        {
                            string   propertyName = property.propertyName;
                            XElement valueEl      = objEl.Element(ns + Utility.TitleCase(propertyName));

                            if (valueEl != null)
                            {
                                string value = valueEl.Value;

                                if (value != null)
                                {
                                    dataObject.SetPropertyValue(propertyName, value);
                                }
                            }
                        }

                        dataObjects.Add(dataObject);
                    }
                }

                return(dataObjects);
            }
            catch (Exception e)
            {
                string message = "Error marshalling data items to data objects." + e;
                _logger.Error(message);
                throw new Exception(message);
            }
        }
Ejemplo n.º 4
0
        public override List <IDataObject> ToDataObjects(string graphName, ref XDocument xml)
        {
            try
            {
                List <IDataObject> dataObjects = new List <IDataObject>();
                DataObject         objectType  = FindGraphDataObject(graphName);
                string             previousItemResourceName = graphName;

                if (objectType != null)
                {
                    DataItems dataItems = Utility.DeserializeDataContract <DataItems>(xml.ToString());

                    foreach (DataItem dataItem in dataItems.items)
                    {
                        if (dataItem.type != null && previousItemResourceName != dataItem.type) // handling differnet type of DataItems in same collection
                        {
                            objectType = FindGraphDataObject(dataItem.type);
                            previousItemResourceName = dataItem.type;
                        }

                        if (dataItem.id != null)
                        {
                            dataItem.id = Utility.ConvertSpecialCharInbound(dataItem.id, arrSpecialcharlist, arrSpecialcharValue); //Handling special Characters here.
                        }
                        else // if id doesn't exist, make it from key properties.
                        {
                            if (objectType.keyProperties.Count == 1)
                            {
                                string keyProp = objectType.keyProperties[0].keyPropertyName;
                                //object id = dataItem.properties[keyProp];

                                //if (id == null || id.ToString() == string.Empty)
                                //{
                                //  throw new Exception("Value of key property: " + keyProp + " cannot be null.");
                                //}

                                //dataItem.id = id.ToString();
                                if (dataItem.properties.ContainsKey(keyProp))
                                {
                                    object id = dataItem.properties[keyProp];

                                    if (id != null)
                                    {
                                        dataItem.id = id.ToString();
                                    }
                                }
                            }
                            else
                            {
                                StringBuilder builder = new StringBuilder();

                                foreach (KeyProperty keyProp in objectType.keyProperties)
                                {
                                    string propName  = objectType.keyProperties[0].keyPropertyName;
                                    object propValue = dataItem.properties[propName];

                                    // it is acceptable to have some key property values to be null but not all
                                    if (propValue == null)
                                    {
                                        propValue = string.Empty;
                                    }

                                    builder.Append(objectType.keyDelimeter + propValue);
                                }

                                builder.Remove(0, objectType.keyDelimeter.Length);

                                if (builder.Length == 0)
                                {
                                    throw new Exception("Invalid identifier.");
                                }

                                dataItem.id = builder.ToString();
                            }
                        }

                        SerializableDataObject dataObject = new SerializableDataObject();
                        dataObject.Type = objectType.objectName;
                        dataObject.Id   = dataItem.id;

                        if (objectType.hasContent)
                        {
                            string base64Content = dataItem.content;

                            if (!String.IsNullOrEmpty(base64Content))
                            {
                                dataObject.Content          = base64Content.ToMemoryStream();
                                dataObject.Content.Position = 0;
                                dataObject.HasContent       = true;
                                dataObject.ContentType      = dataItem.contentType;
                            }
                        }

                        //
                        // set key properties from id
                        //
                        if (objectType.keyProperties.Count == 1)
                        {
                            if (!string.IsNullOrEmpty(dataItem.id))
                            {
                                dataObject.SetPropertyValue(objectType.keyProperties[0].keyPropertyName, dataItem.id);
                            }
                        }
                        else if (objectType.keyProperties.Count > 1)
                        {
                            string[] idParts = dataItem.id.Split(new string[] { objectType.keyDelimeter }, StringSplitOptions.None);

                            for (int i = 0; i < objectType.keyProperties.Count; i++)
                            {
                                string keyProp  = objectType.keyProperties[i].keyPropertyName;
                                string keyValue = idParts[i];

                                if (!string.IsNullOrEmpty(keyValue))
                                {
                                    dataObject.SetPropertyValue(keyProp, keyValue);
                                }
                            }
                        }

                        //
                        // set data properties
                        //
                        foreach (var pair in dataItem.properties)
                        {
                            dataObject.SetPropertyValue(pair.Key, pair.Value);
                        }

                        dataObjects.Add(dataObject);
                    }
                }

                return(dataObjects);
            }
            catch (Exception e)
            {
                string message = "Error marshalling data items to data objects." + e;
                _logger.Error(message);
                throw new Exception(message);
            }
        }