public Tuple<IEnumerable<PhotoDesc>, Exception> GetPhotos(int skip, int taken)
        {
            List<PhotoDesc> oRet = new List<PhotoDesc>();
            Exception err = null;
            try
            {
                using (var ctx = getContext())
                {
                    int nMax = ctx.DbPhotoes.Count();
                    if (skip < 0)
                    {
                        skip = 0;
                    }
                    if (taken < 0)
                    {
                        taken = 0;
                    }
                    if ((skip + taken) > nMax)
                    {
                        taken = nMax - skip;
                        if (taken < 0)
                        {
                            taken = 0;
                        }
                    }
                    if (taken == 0)
                    {
                        if (skip > 0)
                        {
                            var q = (from x in ctx.DbPhotoes orderby x.Name ascending select x).Skip(skip);
                            foreach (var p in q)
                            {
                                PhotoDesc pp = new PhotoDesc();
                                convertPhoto(p, pp);
                                oRet.Add(pp);
                            }// p
                        }
                        else
                        {
                            var q = from x in ctx.DbPhotoes orderby x.Name ascending select x;
                            foreach (var p in q)
                            {
                                PhotoDesc pp = new PhotoDesc();
                                convertPhoto(p, pp);
                                oRet.Add(pp);
                            }// p
                        }

                    }
                    else
                    {
                        if (skip > 0)
                        {
                            var q = (from x in ctx.DbPhotoes orderby x.Name ascending select x).Skip(skip).Take(taken);
                            foreach (var p in q)
                            {
                                PhotoDesc pp = new PhotoDesc();
                                convertPhoto(p, pp);
                                oRet.Add(pp);
                            }// p
                        }
                        else
                        {
                            var q = (from x in ctx.DbPhotoes orderby x.Name ascending select x).Take(taken);
                            foreach (var p in q)
                            {
                                PhotoDesc pp = new PhotoDesc();
                                convertPhoto(p, pp);
                                oRet.Add(pp);
                            }// p
                        }
                    }
                }// ctx
            }
            catch (Exception ex)
            {
                err = ex;
            }
            return new Tuple<IEnumerable<PhotoDesc>, Exception>(oRet, err);
        }
 public Tuple<IEnumerable<PhotoDesc>, Exception> GetPhotos(IEnumerable<PhotoDesc> oPhotos)
 {
     if (oPhotos == null)
     {
         return new Tuple<IEnumerable<PhotoDesc>, Exception>(null, new ArgumentNullException());
     }
     List<PhotoDesc> oRet = new List<PhotoDesc>();
     Exception err = null;
     try
     {
         using (var ctx = getContext())
         {
             foreach (var oPhoto in oPhotos)
             {
                 DbPhoto p = findPhoto(ctx, oPhoto);
                 if (p != null)
                 {
                     PhotoDesc pp = new PhotoDesc();
                     convertPhoto(p, pp);
                     oRet.Add(pp);
                 }
             }// oPhoto
         }// ctx
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<IEnumerable<PhotoDesc>, Exception>(oRet, err);
 }
 protected DbPhoto findPhoto(statdataEntities ctx, PhotoDesc oPhoto)
 {
     DbPhoto pRet = null;
     if (oPhoto == null)
     {
         return pRet;
     }
     if (oPhoto.Id != 0)
     {
         var q = from x in ctx.DbPhotoes where x.Id == oPhoto.Id select x;
         if (q.Count() > 0)
         {
             return q.First();
         }
     }
     String s = oPhoto.Name;
     if (!String.IsNullOrEmpty(s))
     {
         String ss = s.ToLower();
         var qq = from x in ctx.DbPhotoes where x.Name.Trim().ToLower() == ss select x;
         if (qq.Count() > 0)
         {
             return qq.First();
         }
     }
     return pRet;
 }
 public Tuple<PhotoDesc, Exception> FindPhoto(PhotoDesc oPhoto)
 {
     PhotoDesc pRet = null;
     Exception err = null;
     if (oPhoto == null)
     {
         return new Tuple<PhotoDesc, Exception>(null, new ArgumentNullException());
     }
     try
     {
         using (var ctx = getContext())
         {
             DbPhoto p = findPhoto(ctx, oPhoto);
             if (p != null)
             {
                 pRet = new PhotoDesc();
                 convertPhoto(p, pRet);
             }
         }// ctx
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<PhotoDesc, Exception>(pRet, err);
 }
        public Tuple<IEnumerable<PhotoDesc>, Exception> SearchPhotos(string searchString, int skip, int taken)
        {
            String ss = String.IsNullOrEmpty(searchString) ? null : searchString.Trim().ToLower();
            if (String.IsNullOrEmpty(ss))
            {
                return GetPhotos(skip, taken);
            }
            List<PhotoDesc> oRet = null;
            Exception err = null;
            try
            {
                if (skip < 0)
                {
                    skip = 0;
                }
                if (taken < 0)
                {
                    taken = 0;
                }

                using (var ctx = getContext())
                {
                    oRet = new List<PhotoDesc>();
                    IEnumerable<DbPhoto> q = null;
                    if (taken < 1)
                    {
                        if (skip > 0)
                        {
                            q = (from x in ctx.DbPhotoes
                                 where x.Name.Trim().ToLower().Contains(ss)
                                 orderby x.Name ascending
                                 select x).Skip(skip);
                        }
                        else
                        {
                            q = from x in ctx.DbPhotoes
                                where x.Name.Trim().ToLower().Contains(ss)
                                orderby x.Name ascending
                                select x;
                        }
                    }
                    else
                    {
                        if (skip > 0)
                        {
                            q = (from x in ctx.DbPhotoes
                                 where x.Name.Trim().ToLower().Contains(ss)
                                 orderby x.Name ascending
                                 select x).Skip(skip).Take(taken);
                        }
                        else
                        {
                            q = (from x in ctx.DbPhotoes
                                 where x.Name.Trim().ToLower().Contains(ss)
                                 orderby x.Name ascending
                                 select x).Take(taken);
                        }
                    }
                    foreach (var p in q)
                    {
                        PhotoDesc pp = new PhotoDesc();
                        convertPhoto(p, pp);
                        oRet.Add(pp);
                    }// p
                }// ctx
            }// try
            catch (Exception ex)
            {
                err = ex;
            }
            return new Tuple<IEnumerable<PhotoDesc>, Exception>(oRet, err);
        }
 protected void convertPhoto(DbPhoto p, PhotoDesc pp)
 {
     pp.Id = p.Id;
     pp.Name = p.Name;
     pp.DataBytes = p.DataBytes;
     pp.IsModified = false;
 }
 public Tuple<bool, Exception> RemovePhoto(PhotoDesc oPhoto)
 {
     if (oPhoto == null)
     {
         return new Tuple<bool, Exception>(false, new ArgumentNullException());
     }
     bool bRet = false;
     Exception err = null;
     try
     {
         using (var ctx = getContext())
         {
             DbPhoto p = findPhoto(ctx, oPhoto);
             if (p != null)
             {
                 ctx.DbPhotoes.Remove(p);
                 ctx.SaveChanges();
                 bRet = true;
             }
         }// cts
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<bool, Exception>(bRet, err);
 }
 public Tuple<PhotoDesc, Exception> MaintainsPhoto(PhotoDesc oPhoto)
 {
     if (oPhoto == null)
     {
         return new Tuple<PhotoDesc, Exception>(null, new ArgumentNullException());
     }
     if (!oPhoto.IsWriteable)
     {
         return new Tuple<PhotoDesc, Exception>(null, new ArgumentException());
     }
     PhotoDesc pRet = null;
     Exception err = null;
     try
     {
         using (var ctx = getContext())
         {
             DbPhoto p = findPhoto(ctx, oPhoto);
             if (p != null)
             {
                 p.Name = oPhoto.Name;
                 p.DataBytes = oPhoto.DataBytes;
                 ctx.SaveChanges();
             }
             else
             {
                 p = new DbPhoto();
                 p.Id = nextId(ctx, TAB_PHOTO);
                 p.Name = oPhoto.Name;
                 p.DataBytes = oPhoto.DataBytes;
                 ctx.DbPhotoes.Add(p);
                 ctx.SaveChanges();
             }
             pRet = new PhotoDesc();
             convertPhoto(p, pRet);
         }// ctd
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<PhotoDesc, Exception>(pRet, err);
 }
 }// RefreshShowPlotsAsync
 private DisplayItemsArray createOrdDisplayData()
 {
     List<DisplayItems> oRet = null;
     try
     {
         var inds = this.Individus.ToArray();
         int nr = inds.Length;
         if (nr < 1)
         {
             return null;
         }
         var pMan = this.DataService;
         if (pMan == null)
         {
             return null;
         }
         var allVars = m_main.Variables;
         VariableDesc idsvars = null;
         VariableDesc namesvars = null;
         VariableDesc photosvars = null;
         bool bString = true;
         foreach (var v in allVars)
         {
             if (v.IsIdVar)
             {
                 idsvars = v;
             }
             if (v.IsNameVar)
             {
                 namesvars = v;
             }
             if (v.IsImageVar)
             {
                 photosvars = v;
             }
         }// v
         if (photosvars != null)
         {
             String stype = photosvars.DataType.Trim().ToLower();
             if (!stype.Contains("string"))
             {
                 bString = false;
             }
         }// photovars
         var vars = this.CurrentVariables.ToArray();
         int nv = vars.Length;
         oRet = new List<DisplayItems>();
         DisplayItems header = new DisplayItems();
         header.Add(new DisplayItem("Num", true));
         header.Add(new DisplayItem("Index", true));
         header.Add(new DisplayItem("ID", true));
         header.Add(new DisplayItem("Nom", true));
         header.Add(new DisplayItem("Photo", true));
         //
         header.Add(new DisplayItem("CU", true));
         header.Add(new DisplayItem("KM", true));
         header.Add(new DisplayItem("HR", true));
         header.Add(new DisplayItem("AR", true));
         for (int i = 0; i < nv; ++i)
         {
             header.Add(new DisplayItem(vars[i].Name, true));
         }// i
         oRet.Add(header);
         for (int irow = 0; irow < nr; ++irow)
         {
             var ind = inds[irow];
             DisplayItems line = new DisplayItems();
             line.Tag = ind;
             line.Add(new DisplayItem(irow + 1));
             int index = ind.IndivIndex;
             line.Add(new DisplayItem(index));
             String sid = String.Empty;
             if (idsvars != null)
             {
                 var q = from x in ind.Values where x.VariableId == idsvars.Id select x;
                 if (q.Count() > 0)
                 {
                     sid = StatHelpers.ConvertValue(q.First().DataStringValue);
                 }
             }// idsvars
             line.Add(new DisplayItem(sid));
             String sname = String.Empty;
             if (namesvars != null)
             {
                 var q = from x in ind.Values where x.VariableId == namesvars.Id select x;
                 if (q.Count() > 0)
                 {
                     sname = StatHelpers.ConvertValue(q.First().DataStringValue);
                 }
             }// idsvars
             line.Add(new DisplayItem(sname));
             DisplayItem photo = new DisplayItem();
             if (photosvars != null)
             {
                 var q = from x in ind.Values where x.VariableId == photosvars.Id select x;
                 if (q.Count() > 0)
                 {
                     String sval = StatHelpers.ConvertValue(q.First().DataStringValue);
                     if (!String.IsNullOrEmpty(sval))
                     {
                         int nid = 0;
                         String name = null;
                         if (!bString)
                         {
                             double d = 0.0;
                             double.TryParse(sval, out d);
                             nid = (int)d;
                         }
                         else
                         {
                             name = sval;
                         }
                         PhotoDesc p = new PhotoDesc();
                         p.Id = nid;
                         p.Name = name;
                         var xx = pMan.FindPhoto(p);
                         if ((xx != null) && (xx.Item1 != null) && (xx.Item2 == null))
                         {
                             photo = new DisplayItem(xx.Item1.DataBytes);
                         }
                     }// sval
                 }
             }// photos
             line.Add(photo);
             //
             line.Add(new DisplayItem(ind.UtilityClusterIndex));
             line.Add(new DisplayItem(ind.KMeansClusterIndex));
             line.Add(new DisplayItem(ind.HierarClusterIndex));
             line.Add(new DisplayItem(ind.Position));
             //
             for (int i = 0; i < nv; ++i)
             {
                 String sval = String.Empty;
                 var vx = vars[i];
                 var q = from x in ind.Values where x.VariableId == vx.Id select x;
                 if (q.Count() > 0)
                 {
                     sval = StatHelpers.ConvertValue(q.First().DataStringValue);
                 }
                 line.Add(new DisplayItem(sval));
             }// i
             //
             oRet.Add(line);
         }// ind
     }
     catch (Exception /*ex */)
     {
         oRet = null;
     }
     return (oRet != null) ? new DisplayItemsArray(oRet) : null;
 }// createOrdDisplayData
 public DisplayItemsArray CreateDataDisplay(IEnumerable<IndivDesc> oIndivs,
     IEnumerable<VariableDesc> allVars, IEnumerable<VariableDesc> oVars)
 {
     if ((oIndivs == null) || (allVars == null) || (oVars == null))
     {
         return null;
     }
     if ((oIndivs.Count() < 1) || (allVars.Count() < 1) || (oVars.Count() < 1))
     {
         return null;
     }
     List<DisplayItems> oRet = null;
     try
     {
         var pMan = this.DataService;
         if (pMan == null)
         {
             return null;
         }
         VariableDesc idsvars = null;
         VariableDesc namesvars = null;
         VariableDesc photosvars = null;
         bool bString = true;
         foreach (var v in allVars)
         {
             if (v.IsIdVar)
             {
                 idsvars = v;
             }
             if (v.IsNameVar)
             {
                 namesvars = v;
             }
             if (v.IsImageVar)
             {
                 photosvars = v;
             }
         }// v
         if (photosvars != null)
         {
             String stype = photosvars.DataType.Trim().ToLower();
             if (!stype.Contains("string"))
             {
                 bString = false;
             }
         }// photovars
         var vars = oVars.ToArray();
         int nv = vars.Length;
         oRet = new List<DisplayItems>();
         DisplayItems header = new DisplayItems();
         header.Add(new DisplayItem("Num", true));
         header.Add(new DisplayItem("Index", true));
         header.Add(new DisplayItem("ID", true));
         header.Add(new DisplayItem("Nom", true));
         header.Add(new DisplayItem("Photo", true));
         for (int i = 0; i < nv; ++i)
         {
             header.Add(new DisplayItem(vars[i].Name, true));
         }// i
         oRet.Add(header);
         int irow = 1;
         foreach (var ind in oIndivs)
         {
             DisplayItems line = new DisplayItems();
             line.Add(new DisplayItem(irow++));
             int index = ind.IndivIndex;
             line.Add(new DisplayItem(index));
             String sid = String.Empty;
             if (idsvars != null)
             {
                 var q = from x in ind.Values where x.VariableId == idsvars.Id select x;
                 if (q.Count() > 0)
                 {
                     sid = StatHelpers.ConvertValue(q.First().DataStringValue);
                 }
             }// idsvars
             line.Add(new DisplayItem(sid));
             String sname = String.Empty;
             if (namesvars != null)
             {
                 var q = from x in ind.Values where x.VariableId == namesvars.Id select x;
                 if (q.Count() > 0)
                 {
                     sname = StatHelpers.ConvertValue(q.First().DataStringValue);
                 }
             }// idsvars
             line.Add(new DisplayItem(sname));
             DisplayItem photo = new DisplayItem();
             if (photosvars != null)
             {
                 var q = from x in ind.Values where x.VariableId == photosvars.Id select x;
                 if (q.Count() > 0)
                 {
                     String sval = StatHelpers.ConvertValue(q.First().DataStringValue);
                     if (!String.IsNullOrEmpty(sval))
                     {
                         int nid = 0;
                         String name = null;
                         if (!bString)
                         {
                             double d = 0.0;
                             double.TryParse(sval, out d);
                             nid = (int)d;
                         }
                         else
                         {
                             name = sval;
                         }
                         PhotoDesc p = new PhotoDesc();
                         p.Id = nid;
                         p.Name = name;
                         var xx = pMan.FindPhoto(p);
                         if ((xx != null) && (xx.Item1 != null) && (xx.Item2 == null))
                         {
                             photo = new DisplayItem(xx.Item1.DataBytes);
                         }
                     }// sval
                 }
             }// photos
             line.Add(photo);
             //
             for (int i = 0; i < nv; ++i)
             {
                 String sval = String.Empty;
                 var vx = vars[i];
                 var q = from x in ind.Values where x.VariableId == vx.Id select x;
                 if (q.Count() > 0)
                 {
                     sval = StatHelpers.ConvertValue(q.First().DataStringValue);
                 }
                 line.Add(new DisplayItem(sval));
             }// i
             //
             oRet.Add(line);
         }// ind
     }
     catch (Exception /*ex */)
     {
         oRet = null;
     }
     return (oRet != null) ? new DisplayItemsArray(oRet) : null;
 }
 private Task importPhotosAsync(IStoreDataManager pMan, String[] filenames,
     CancellationToken cancellationToken,
     IProgress<int> progress)
 {
     return Task.Run(() =>
     {
         try
         {
             for (int i = 0; i < filenames.Length; ++i)
             {
                 if (cancellationToken.IsCancellationRequested)
                 {
                     break;
                 }
                 String filename = filenames[i];
                 if (!String.IsNullOrEmpty(filename))
                 {
                     var xx = PhotosModelView.GetJPEGBytes(filename);
                     byte[] data = xx.Item1;
                     String name = xx.Item2;
                     Exception err = xx.Item3;
                     if ((data != null) && (data.Length > 1) && (err == null) && (!String.IsNullOrEmpty(name)))
                     {
                         PhotoDesc oPhoto = new PhotoDesc();
                         oPhoto.DataBytes = data;
                         oPhoto.Name = name;
                         var yy = pMan.MaintainsPhoto(oPhoto);
                         if ((yy.Item1 == null) || (yy.Item2 != null))
                         {
                             break;
                         }
                     }// data
                 }
                 if (progress != null)
                 {
                     progress.Report(i);
                 }
             }// i
         }
         catch (Exception /*ex */)
         {
         }
     }, cancellationToken);
 }//importPhotosAsync