importdata.cs

 Back to Parent Page
//-----------------------------------------------------------------------
//  This file is developed by Alex Feigin on November,2006 in framework of   
//  Ecozvit( Soft fund ltd. 2005-2007) application development.
//  
//  Implements data import supplementary class from the predefined data 
//  source to the XmlDocument 
//  
//  Revisions history :
//    may, 2007 added support of auto recalculation of emission 
//      concentrations with reduction to normal conditions
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Data;
using System.Xml;
using System.Globalization;
using Sure.SharedObjects;
using Sure.DataPipe;
using Sure.BaseObjects;

namespace Sure
{
    namespace SureControl
    {
        /// <summary>
        /// Implementation of data source topics reader.  
        /// Implements mostly common features. 
        /// </summary>
        public class CDataTopic
        {
            protected    string topicName; 
            protected    string topicPath;
            protected    XmlQualifiedName typeName=null;
            protected    XmlQualifiedName nodeName=null;
            public        delegate void OnTopicChanged(XmlNode target);
            public        event     OnTopicChanged    TopicChanged=null;
            public        delegate bool OnNewData(ref string fldName, ref object fldVal);
            public        event     OnNewData        NewData=null;            
            protected    XmlNode  baseNode=null;
            protected    XmlNode  refNode=null;
            protected    bool     insertBefore=false;
            
            /// <summary>
            // Constructor input parameters (for topics to create and fill):
            //    aTopicName    - the name of the topic in the input data source
            //    aTopicPath    - the target XML file path to write topic data
            //    aTypeName    - the type of the Xml element - recipient of the topic data
            //    aNodeName    - the name of the Xml element - recipient of the topic data
            /// <summary>
            public CDataTopic(string aTopicName,string aTopicPath,
            XmlQualifiedName aTypeName,XmlQualifiedName aNodeName)
            {
                topicName=aTopicName;
                topicPath=aTopicPath;
                typeName=aTypeName;
                nodeName=aNodeName;
            }
            /// <summary>
            // Simplified constructor (for topics to create only):
            //    aTopicName    - the name of the topic in the input data source
            //    aTopicPath    - the target XML file path to write topic data
            /// <summary>
            public CDataTopic(string aTopicName,string aTopicPath)
            {
                topicName=aTopicName;
                topicPath=aTopicPath;
            }
            /// <summary>
            // Method to flush imported data to the target Xml file
            //    sDoc - target XmlDocument
            //  pipe - source data slices list 
            /// <summary>
            public virtual void Flush(SureDoc sDoc,IDataPipe pipe)
            {
                string  fldName;
                object  fldValue;
                XmlNamespaceManager ns=SureTools.NameSpaceManager(sDoc);
                bool    found=pipe.GetFirstDataSliceByTopic(topicName,out fldName,out fldValue);
                XmlNode target=null;
                if(baseNode==null) 
                {
                    if(topicPath!=String.Empty)
                    {
                        baseNode=sDoc.SelectSingleNode(topicPath);
                        if(baseNode==null)
                        {
                            throw new Sure.DataError(String.Format("Не знайдено {0}",topicPath));
                        }
                    }                        
                }
                if(typeName!=null && nodeName!=null)
                    target=sDoc.PrepareNode(typeName,nodeName);                        
                Hashtable row=new Hashtable();
                while(found)
                {
                    if(row[fldName]!=null)
                    {    
                        if(target!=null)
                        {
                            // Topic change event
                            if(TopicChanged!=null)
                                TopicChanged(target);
                            if(baseNode!=null)    
                            {                            
                                if(refNode!=null) 
                                {
                                    if(insertBefore)
                                        refNode=baseNode.InsertBefore(target,refNode);
                                    else
                                        refNode=baseNode.InsertAfter(target,refNode);
                                }
                                else
                                    baseNode.AppendChild(target);                            
                            }
                        }
                        if(typeName!=null && nodeName!=null)
                            target=sDoc.PrepareNode(typeName,nodeName);                                            
                        row.Clear();
                    }                
                    string fldOrigin=Sure.DataPipe.DataSlice.GetFldOrigin(fldName);
                    if(target!=null && NewData!=null )
                    {    
                        // New data element initialization event
                        bool ret=NewData(ref fldOrigin,ref fldValue);
                        if(ret==false)
                            target=null;
                    }
                    if(target!=null)
                    {
                        string fldPath=Sure.DataPipe.DataSlice.GetFldPath(fldOrigin);
                        row.Add(fldName,fldValue);                            
                        if(fldPath!=String.Empty)
                        {
                            SureTools.SetNode(target,fldPath,fldValue.ToString(),ns);
                        }
                    }
                    found=pipe.GetNextDataSlice(out fldName,out fldValue);                
                }
                if(row.Count>0 && target!=null)
                {
                    // Topic change event
                    if(TopicChanged!=null)
                        TopicChanged(target);
                    if(baseNode!=null)
                    {
                        if(baseNode.Name.ToString()==target.Name.ToString())
                        {
                            XmlNode parent=baseNode.ParentNode;
                            XmlNode refnode=baseNode.PreviousSibling;
                            parent.RemoveChild(baseNode);
                            parent.InsertAfter(target,refnode);
                        }
                        else
                        {
                            if(refNode!=null)
                                refNode=baseNode.InsertAfter(target,refNode);
                            else
                            {
                                baseNode.AppendChild(target);                            
                            }
                        }
                    }
                }                
            }
            /// <summary>
            // Method to cast floating point values in fixed point format
            /// <summary>
            protected System.Decimal ValueCast(object fldVal)
            {
                string fldValue=fldVal.ToString();
                int i=fldValue.IndexOf("E");
                if(i>=0)
                {
                    string dec=fldValue.Substring(0,i);
                    string power=fldValue.Substring(i+1,fldValue.Length-i-1);
                    System.Decimal val=System.Decimal.Parse(dec);
                    for(int j=0;j<Int32.Parse(power);j++)
                        val=System.Decimal.Divide(val,10);
                    return val;
                }    
                else
                    if(fldVal==null)
                        return 0;
                    else
                        return Convert.ToDecimal(fldVal);
            }
            public XmlNode BaseNode
            {
                get
                {
                    return baseNode;
                }
            }
        }
        /// <summary>
        // Implementation of the terrain data topic specific features
        /// <summary>
        public class CTerraTopic:CDataTopic
        {
            public CTerraTopic(string aTopicName,string aTopicPath,
		XmlQualifiedName aTypeName,XmlQualifiedName aNodeName):
                base(aTopicName,aTopicPath,aTypeName,aNodeName)
            {
                
            }
            public override void Flush(SureDoc sDoc,IDataPipe pipe)
            {
                XmlNamespaceManager ns=SureTools.NameSpaceManager(sDoc);
                XmlNode baseNode=sDoc.SelectSingleNode(topicPath,ns);
                if(baseNode==null)
                {
                    XmlNode newNode=sDoc.PrepareNode(
				new XmlQualifiedName("CCalcConcentration","ovnsdisp"),
                                new XmlQualifiedName("CalcConcentration","ovnsdisp"));
                    XmlNode parentNode=sDoc.SelectSingleNode("//bases:estimateConcentration",ns);
                    if(parentNode!=null)
                        parentNode.PrependChild(newNode);
                }
                base.Flush(sDoc,pipe);
            }
        }
        /// <summary>
        // Implementation of the meteo data topic specific features
        /// <summary>
        public class CMeteoDataTopic:CDataTopic
        {
            COrgEmissionSourceTopic        OrgEmissionTopic;
            CUnorgEmissionSourceTopic    UnOrgEmissionTopic;
            CIndAreaTopic                IndAreaTopic;
            XmlNamespaceManager            ns;

            public CMeteoDataTopic(string aTopicName,string aTopicPath,
			XmlQualifiedName aTypeName,XmlQualifiedName aNodeName,
                	SureDoc aCurrentDocument,COrgEmissionSourceTopic orgEmissionTopic,
			CUnorgEmissionSourceTopic unOrgEmissionTopic,CIndAreaTopic indAreaTopic):
                base(aTopicName,aTopicPath,aTypeName,aNodeName)
            {
                OrgEmissionTopic=orgEmissionTopic;
                UnOrgEmissionTopic=unOrgEmissionTopic;
                IndAreaTopic=indAreaTopic;
                ns=SureTools.NameSpaceManager(aCurrentDocument);

                TopicChanged+=new OnTopicChanged(DoTopicChanged);
                NewData+=new OnNewData(DoNewData);
            }
            public bool DoNewData(ref string fldName, ref object fldVal)
            {
                if(fldName=="gor")
                {
                    IndAreaTopic.SetObjectPlace(fldVal,ns);
                }
                return true;
            }
            public void DoTopicChanged(XmlNode target)
            {
                System.Decimal relief=System.Decimal.Round(
			System.Decimal.Divide(OrgEmissionTopic.Relief+UnOrgEmissionTopic.Relief,2),2);
                if(relief>=1)
                {
                    SureTools.SetNode(target,"disp:Relief",relief.ToString(),ns);
                }
            }
        }
 Back to Parent Page