public Pack GetPackData(Pack pack)
 {
     Pack pack1;
     using (var ent = new CommonEntitiesManager())
     {
         ent.ContextOptions.LazyLoadingEnabled = false;
         pack1 = ent.Packs.FirstOrDefault(p => p.Code == pack.Code);
     }
     return pack1;
 }
        public List<Pack> GetPack(DateTime startDate, DateTime endDate)
        {
            var ret = new List<Pack>();

            using (var ent = new CommonEntitiesManager())
            {
                ent.ContextOptions.LazyLoadingEnabled = false;
                ret.AddRange(ent.Packs.Where(p => p.Stamp >= startDate && p.Stamp <= endDate));
            }
            ret.ForEach(p => p.Data = null);
            return ret;
        }
        public RemoteFileInfo DownloadFile(ServerFileInfo request)
        {
            Stream stream = null;
            long length = 0;
            // get some info about the input file
            if (!request.IsFromDB)
            {
                string filePath = Path.Combine(AppDirectories.DownloadFolder, request.FileName);
                var fileInfo = new FileInfo(filePath);
                length = fileInfo.Length;
                // report start
                Trace.WriteLineIf(ZeroCommonClasses.Environment.Config.LogLevel.TraceVerbose, "Sending stream " + request.FileName + " to client");
                Trace.WriteLineIf(ZeroCommonClasses.Environment.Config.LogLevel.TraceVerbose,"Size " + fileInfo.Length);

                // check if exists
                if (!fileInfo.Exists)
                {
                    Trace.WriteLineIf(ZeroCommonClasses.Environment.Config.LogLevel.TraceError, "File not found");
                    throw new FileNotFoundException("File not found", request.FileName);
                }

                // open stream
                stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            }
            else
            {
                using (var ent = new CommonEntitiesManager())
                {

                    Pack P = ent.Packs.FirstOrDefault(p => p.Code == request.Code);
                    if (P != null)
                    {
                        Trace.WriteLineIf(ZeroCommonClasses.Environment.Config.LogLevel.TraceVerbose, "Sending pack " + request.Code + " to client","Verbose");
                        stream = new MemoryStream(P.Data);
                    }
                    else
                    {
                        Trace.WriteLineIf(ZeroCommonClasses.Environment.Config.LogLevel.TraceWarning, "Pack " + request.Code + " Not found", "Verbose");
                    }
                }
            }
            // return result
            var result = new RemoteFileInfo();
            result.FileName = request.FileName;
            result.Length = length;
            result.FileByteStream = stream;
            return result;

            // after returning to the client download starts. Stream remains open and on server and the client reads it, although the execution of this method is completed.
        }
        public Dictionary<int, int> GetPacksToSend(int terminalCode)
        {
            var ret = new Dictionary<int, int>();
            using (var ent = new CommonEntitiesManager())
            {
                var query = ent.GetPacksToSend(terminalCode);
                foreach (var item in query)
                {
                    int module;
                    if (int.TryParse(item.PackName.Substring(0, item.PackName.IndexOf('_')), out module))
                    {
                        ret.Add(item.PackCode, module);
                    }
                }
            }

            return ret;
        }
        private void a_Imported(object sender, PackProcessEventArgs e)
        {
            Trace.WriteIf(ZeroCommonClasses.Environment.Config.LogLevel.TraceInfo,
                string.Format("Import Finished: Status = {3}, ConnID = {0}, DB Pack = {1}, Pack Module = {2}",
                              e.ConnectionID, e.Pack.Code, e.PackInfo != null ? e.PackInfo.ModuleCode : -1,
                              (PackManager.PackStatus)e.Pack.PackStatusCode), "Information");

            if (e.Pack.PackStatusCode == (int)PackManager.PackStatus.Imported)
            {
                if ((e.Pack.IsMasterData.GetValueOrDefault()) || (e.Pack.IsUpgrade.GetValueOrDefault()))
                {
                    using (var packEnt = new CommonEntitiesManager())
                    {
                        using (var ent = new ConfigurationModelManager())
                        {
                            foreach (var item in ent.Terminals.Where(t => t.Code != e.PackInfo.TerminalCode))
                            {
                                Trace.WriteIf(ZeroCommonClasses.Environment.Config.LogLevel.TraceVerbose, string.Format("Saved to Pendings of Terminal {0}", item.Code), "Verbose");
                                PackPending pp = PackPending.CreatePackPending(e.Pack.Code, item.Code);
                                pp.Stamp = DateTime.Now;
                                packEnt.PackPendings.AddObject(pp);
                            }
                        }
                        packEnt.SaveChanges();
                    }
                }
                else
                {

                    if (e.PackInfo != null && e.PackInfo.TerminalToCodes.Count > 0)
                    {
                        using (var packEnt = new CommonEntitiesManager())
                        {
                            using (var ent = new ConfigurationModelManager())
                            {
                                foreach (int terminal in e.PackInfo.TerminalToCodes.Where(c => c != e.PackInfo.TerminalCode))
                                {
                                    if (ent.Terminals.FirstOrDefault(t => t.Code == terminal) != null)
                                    {
                                        Trace.WriteIf(ZeroCommonClasses.Environment.Config.LogLevel.TraceVerbose, string.Format("Saved to Pendings of Terminal {0}", terminal), "Verbose");
                                        PackPending pp = PackPending.CreatePackPending(e.Pack.Code, terminal);
                                        pp.Stamp = DateTime.Now;
                                        packEnt.PackPendings.AddObject(pp);
                                    }

                                }
                            }
                            packEnt.SaveChanges();
                        }
                    }
                }
            }
        }
Esempio n. 6
0
        private void InternalImport()
        {
            Entities.Pack aPack = null;
            CommonEntitiesManager dbent = null;
            var args = new PackProcessEventArgs();
            try
            {
                dbent = new CommonEntitiesManager();

                aPack = InsertPackInDb(workingPackPath, dbent);
                string workingDirectory = Path.Combine(Path.GetDirectoryName(workingPackPath), Path.GetFileNameWithoutExtension(workingPackPath)); ;

                UpdatePackStatus(aPack, dbent, PackStatus.Starting, null);
                args.Pack = aPack;

                ExtractZip(workingPackPath, workingDirectory);
                DeserializePackInfo(workingDirectory);
                PackInfo.TerminalToCodes = new List<int>(GetTerminalDestinationList(args.Pack));
                args.PackInfo = PackInfo;

                aPack.IsMasterData = aPack.IsMasterData.GetValueOrDefault(false);
                aPack.IsUpgrade = aPack.IsUpgrade.GetValueOrDefault(false);
                UpdatePackStatus(aPack, dbent, PackStatus.InProgress, null);
                ImportProcess(args);
                UpdatePackStatus(aPack, dbent, PackStatus.Imported, null);
                OnImported(args);
            }
            catch (Exception ex)
            {
                if (dbent != null && aPack != null)
                    UpdatePackStatus(aPack, dbent, PackStatus.Error, ex.ToString());

                throw;
            }
            finally
            {
                if (dbent != null)
                {
                    dbent.Dispose();
                }
            }
        }
Esempio n. 7
0
        private Entities.Pack InsertPackInDb(string packFilePath, CommonEntitiesManager dbent)
        {
            if (dbent == null) throw new ArgumentNullException("dbent");

            string name = Path.GetFileName(packFilePath);
            Entities.Pack P = dbent.Packs.FirstOrDefault(p => p.Name == name);
            if (default(Entities.Pack) == P)
            {
                P = Entities.Pack.CreatePack(0, true);
                P.Name = name;
                P.Data = File.ReadAllBytes(packFilePath);
                if (!string.IsNullOrWhiteSpace(ConnectionID))
                    P.ConnectionCode = ConnectionID;
                dbent.AddToPacks(P);
                dbent.SaveChanges();
            }

            return P;
        }
Esempio n. 8
0
 private static void UpdatePackStatus(Entities.Pack aPack, CommonEntitiesManager dbent, PackStatus newStatus, string message)
 {
     aPack.Stamp = DateTime.Now;
     aPack.PackStatusCode = (int)newStatus;
     aPack.Result = message != null ? aPack.Result +"\n"+ message : aPack.Result;
     dbent.SaveChanges();
 }
        public void MarkPackReceived(int terminalCode,int packCode)
        {
            PackPending packPending;
            using (var ent = new CommonEntitiesManager())
            {
                packPending = ent.PackPendings.FirstOrDefault(pp => pp.PackCode == packCode && pp.TerminalCode == terminalCode);
                if (packPending != null)
                {
                    ent.PackPendings.DeleteObject(packPending);
                    ent.SaveChanges();
                }

                bool mark = Enumerable.All(ent.Packs.Where(p => p.Code == packCode), item => !item.IsMasterData.HasValue || !item.IsMasterData.Value);
                if (!mark)
                {

                }
            }
        }