Ejemplo n.º 1
0
 public void Close()
 {
     if (Current != null)
     {
         Close(Current);
         Trace.WriteLine(string.Format("DataBase closed {0}", Iori.ToFileName()));
         Current = null;
     }
 }
Ejemplo n.º 2
0
        protected override ThingGraphContent OpenInternal(Iori source)
        {
            Iori sourceInfo = null;

            using (var file = File.OpenRead(source.ToFileName())) {
                sourceInfo = Read(file);
                if (sourceInfo != null)
                {
                    if (string.IsNullOrEmpty(sourceInfo.Path))
                    {
                        sourceInfo.Path = source.Path;
                    }
                }
            }
            var sinkIo        = ThingGraphIoManager.GetSinkIO(sourceInfo, IoMode.Read) as ThingGraphIo;
            var sourceContent = sinkIo.Open(sourceInfo);

            return(sourceContent);
        }
Ejemplo n.º 3
0
        public virtual void Save(ThingGraphContent source, Iori sinkInfo)
        {
            if (source == null)
            {
                return;
            }

            using (var sink = new FileStream(sinkInfo.ToFileName(), FileMode.Create)) {
                var serializer = new ThingXmlSerializer {
                    Graph = source.Data, Things = source.Data.Elements().ToList()
                };

                serializer.Write(sink);

                sink.Flush();
                sink.Close();
            }
            source.Source = sinkInfo;
        }
Ejemplo n.º 4
0
 protected override ThingGraphContent OpenInternal(Iori source)
 {
     try {
         IThingGraph thingGraph = null;
         var         fileName   = source.ToFileName();
         if (File.Exists(fileName))
         {
             using (var file = new FileStream(fileName, FileMode.Open))
                 thingGraph = Open(file);
         }
         else
         {
             thingGraph = new ThingGraph();
         }
         return(new ThingGraphContent {
             Data = thingGraph, Source = source, ContentType = XmlThingGraphSpot.ContentType
         });
     } catch (Exception ex) {
         Registry.Pooled <IExceptionHandler>()
         .Catch(new Exception("File load failed: " + ex.Message, ex), MessageType.OK);
     }
     return(null);
 }
Ejemplo n.º 5
0
 public void Open()
 {
     if (Current != null)
     {
         Trace.WriteLine(string.Format("Provider already opened {0}", Current.Description));
         var conn = Current.Data as IGatewayConnection;
         if (conn != null)
         {
             Trace.WriteLine(string.Format("Connection already opened {0}/{1}", conn.Gateway.IsOpen, conn.Gateway.Iori.ToFileName()));
         }
     }
     else
     {
         var ioManager = new ThingGraphIoManager {
         };
         var sinkIo    = ioManager.GetSinkIO(Iori, IoMode.Read) as ThingGraphIo;
         try {
             var sink = sinkIo.Open(Iori);
             if (sink != null)
             {
                 Trace.WriteLine(string.Format("DataBase opened {0}", Iori.ToFileName()));
                 Current = sink;
                 var graph = new SchemaThingGraph(Current.Data);
                 PrepareGraph(graph);
                 _thingGraph = graph;
             }
             else
             {
                 throw new Exception("Database not found: " + Iori.ToString());
             }
         } catch (Exception e) {
             Trace.WriteLine(e.Message);
             _thingGraph = new ThingGraph();
             Trace.WriteLine(string.Format("Empty Graph created {0}", Iori.ToFileName()));
         }
     }
 }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
 public virtual void Write(Iori source, Iori sink)
 {
     using (var file = File.Open(sink.ToFileName(), FileMode.Create)) {
         Write(source, file);
     }
 }