Exemplo n.º 1
0
 public static bool HandlerRoutine(int CtrlType)
 {
     switch (CtrlType)
     {
     case 0:    //Ctrl+C关闭
     case 2:    //按控制台关闭按钮关闭
         Gateway.Close();
         break;
     }
     return(false);
 }
Exemplo n.º 2
0
        public IThingGraph RawImport(Iori source, IThingGraph sink, bool repair)
        {
            this.Log = new StringWriter();

            var links = new List <ILink>();

            if (repair)
            {
                var db4oGraph = sink as ThingGraph;
                if (db4oGraph != null)
                {
                    ReportClazzes(db4oGraph.Gateway as Gateway);
                }
            }

            var gateway = new Gateway();

            if (!repair)
            {
                gateway.Configuration.AllowVersionUpdates = true;
                gateway.Configuration.DetectSchemaChanges = true;
                //gateway.Configuration.RecoveryMode(true);
            }

            ConfigureAliases(gateway.Configuration);

            gateway.Open(source);
            var session = gateway.Session;

            ReportClazzes(gateway);
            SchemaFacade.InitSchemata();

            var cache = new Dictionary <IReflectClass, Tuple <IReflectClass, List <IReflectField>, Type> >();

            foreach (var item in session.Query <object>())
            {
                var thing = item as IThing;
                if (thing != null)
                {
                    ReportDetail(thing.Id.ToString());
                }
                var go = item as GenericObject;
                if (go != null)
                {
                    IReflectClass clazz = go.GetGenericClass();
                    Tuple <IReflectClass, List <IReflectField>, Type> defs = null;
                    if (!cache.TryGetValue(clazz, out defs))
                    {
                        var name = clazz.GetName();
                        name = name.Substring(0, name.LastIndexOf(','));

                        var newType = typeof(IThing).Assembly.GetType(name);

                        var fields = new List <IReflectField>();
                        var iClazz = clazz;
                        while (iClazz != null)
                        {
                            fields.AddRange(iClazz.GetDeclaredFields());
                            iClazz = iClazz.GetSuperclass();
                        }
                        defs = Tuple.Create(clazz, fields, newType);
                        cache.Add(clazz, defs);
                    }

                    var newThing = Activator.CreateInstance(defs.Item3, new object[] { null });

                    foreach (var field in defs.Item2)
                    {
                        var       val  = field.Get(go);
                        FieldInfo info = null;
                        var       type = defs.Item3;
                        while (info == null && type != null)
                        {
                            info = type.GetField(field.GetName(), BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
                            if (info == null)
                            {
                                info = type.GetField(field.GetName(), BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                            }
                            if (info == null && field.GetName() == "_writeDate")
                            {
                                info = type.GetField("_changeDate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
                            }
                            type = type.BaseType;
                        }
                        if (info != null && val != null && val.GetType() == info.FieldType)
                        {
                            info.SetValue(newThing, val);
                        }
                    }
                    ReportDetail(newThing.ToString());
                    if (repair)
                    {
                        var link = newThing as Link;
                        if (link != null)
                        {
                            link.GetByID = (id) => sink.GetById(id);
                            links.Add(newThing as ILink);
                        }
                        else if (newThing is IThing)
                        {
                            sink.Add(newThing as IThing);
                        }
                        else if (newThing is IIdContent <long> )
                        {
                            sink.ContentContainer.Add(newThing as IIdContent <long>);
                        }
                    }
                }
            }
            if (repair)
            {
                ReportDetail("write links...");

                foreach (var link in links)
                {
                    var idLink = link as ILink <Id>;
                    if (link.Marker == null && idLink.Marker != 0)
                    {
                        IThing marker = null;
                        if (Schema.IdentityMap.TryGetValue(idLink.Marker, out marker))
                        {
                            link.Marker = marker;
                        }
                    }
                    if (link.Marker == null)
                    {
                        link.Marker = CommonSchema.EmptyMarker;
                    }
                    if (link.Root != null && link.Leaf != null)
                    {
                        sink.Add(link);
                    }
                }
            }
            gateway.Close();
            ReportDetail("done:\t");
            if (this.Log != null)
            {
                var logfilename = source.ToFileName() + ".log";
                if (File.Exists(logfilename))
                {
                    File.Delete(logfilename);
                }
                var logfile = new StreamWriter(logfilename);
                logfile.Write(this.Log.ToString());
                logfile.Close();
            }
            return(sink);
        }