/// <summary>
 /// Get all reports.
 /// </summary>
 /// <returns>Reports list.</returns>
 public List<SavedReport> GetAllReports()
 {
     using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
     {
         return (from o in context.SavedReports select o).ToList();
     }
 }
 /// <summary>
 /// Get all archives.
 /// </summary>
 /// <returns>Archives list.</returns>
 public List<Archive> GetAllArchives()
 {
     using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
     {
         return (from o in context.Archives select o).ToList();
     }
 }
 /// <summary>
 /// Gets all exif.
 /// </summary>
 /// <returns>All exif collection.</returns>
 public List<ExifAttribute> GetAllExif()
 {
     using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
     {
         return (from o in context.ExifAttributes select o).ToList();
     }
 }
        /// <summary>
        /// Deleting report by id.
        /// </summary>
        /// <param name="id">Report ID for deletion.</param>
        /// <returns>Operation status (for future dependencies).</returns>
        public bool DeleteReport(int id)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                context.SavedReports.DeleteObject((from o in context.SavedReports where o.ID == id select o).First());
                context.SaveChanges();

                return true;
            }
        }
        /// <summary>
        /// Adding new tag into database.
        /// </summary>
        /// <param name="newTag">New tag entity.</param>
        /// <returns>ID for created tag.</returns>
        public int AddTag(Tag newTag)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                newTag.ID = context.Tags.NextId(p => p.ID);

                context.Tags.AddObject(newTag);
                context.SaveChanges();

                return newTag.ID;
            }
        }
        /// <summary>
        /// Adding new attributes into database.
        /// </summary>
        /// <param name="newAttributes">New attribute entity.</param>
        /// <returns>ID for created attribute.</returns>
        public int AddAttribute(AdditionalAttribute newAttributes)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                newAttributes.ID = context.AdditionalAttributes.NextId(p => p.ID);

                context.AdditionalAttributes.AddObject(newAttributes);
                context.SaveChanges();

                return newAttributes.ID;
            }
        }
        /// <summary>
        /// Adding new exif into database.
        /// </summary>
        /// <param name="newExif">New exif entity.</param>
        /// <returns>ID for created exif.</returns>
        public int AddExif(ExifAttribute newExif)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                newExif.ID = context.ExifAttributes.NextId(p => p.ID);

                context.ExifAttributes.AddObject(newExif);
                context.SaveChanges();

                return newExif.ID;
            }
        }
        /// <summary>
        /// Adding new report into database.
        /// </summary>
        /// <param name="newReport">New report entity.</param>
        /// <returns>ID for created report.</returns>
        public int AddReport(SavedReport newReport)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                newReport.ID = context.Tags.NextId(p => p.ID);

                context.SavedReports.AddObject(newReport);
                context.SaveChanges();

                return newReport.ID;
            }
        }
        /// <summary>
        /// Adding new historical usage of report into database.
        /// </summary>
        /// <param name="newHistory">New report's history entity.</param>
        /// <returns>ID for created history.</returns>
        public int AddReportHistoricalEntry(ReportsHistory newHistory)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                newHistory.ID = context.ReportsHistories.NextId(p => p.ID);

                context.ReportsHistories.AddObject(newHistory);
                context.SaveChanges();

                return newHistory.ID;
            }
        }
        /// <summary>
        /// Adding new archive into database.
        /// </summary>
        /// <param name="newArchive">New archive entity.</param>
        /// <returns>ID for created archive.</returns>
        public int AddArchive(Archive newArchive)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                newArchive.ID = context.Tags.NextId(p => p.ID);

                context.Archives.AddObject(newArchive);
                context.SaveChanges();

                return newArchive.ID;
            }
        }
        /// <summary>
        /// Deleting report history by saved report id.
        /// </summary>
        /// <param name="savedReportId">Saved report's ID for deletion.</param>
        /// <returns>Operation status (for future dependencies).</returns>
        public bool DeleteReportsHistoryBySavedReportId(int savedReportId)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                foreach(var entry in (from o in context.ReportsHistories
                                      join report in context.SavedReports on savedReportId equals report.ID
                                      select o))
                {
                    context.ReportsHistories.DeleteObject(entry);
                }

                context.SaveChanges();

                return true;
            }
        }
        /// <summary>
        /// Adds the template tag.
        /// </summary>
        /// <returns>Sample tag ID.</returns>
        public int AddTemplateTag()
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                Tag tag = new Tag();

                tag.ID = context.Tags.NextId(t => t.ID);
                tag.IconPath = "none";
                tag.CreationDate = System.Convert.ToDateTime(System.DateTime.Now);
                tag.Name = "TemporaryTag";
                tag.ParentID = null;

                context.Tags.AddObject(tag);
                context.SaveChanges();

                return tag.ID;
            }
        }
        /// <summary>
        /// Adds the template archive.
        /// </summary>
        /// <returns>Sample archive ID.</returns>
        public int AddTemplateArchive()
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                Archive archive = new Archive();

                archive.ID = context.Archives.NextId(a => a.ID);
                archive.Capacity = 1048567;
                archive.IsExternal = false;
                archive.DeviceID = "007";
                archive.HddLetter = "C";

                context.Archives.AddObject(archive);
                context.SaveChanges();

                return archive.ID;
            }
        }
        /// <summary>
        /// Adding a tag into tag callection of a photo database.
        /// </summary>
        /// <param name="photoID">Id of a photo that is being tagged.</param>
        /// <param name="tagName">Name od tag.</param>
        /// <returns>Operation's status.</returns>
        public static bool TagPhoto(int photoID, String tagName)
        {
            // Updates collection.
            string connectionString = ConnectionStringHelper.GetActualConnectionString();
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(connectionString))
            {
                Photo resultPhoto = context.Photos.Where(p => p.ID == photoID).First();
                Tag resultTag = context.Tags.Where(t => t.Name == tagName).First();

                if (resultPhoto != null && resultTag != null)
                {
                    context.Tags2PhotosSet.AddObject(new Tags2Photos() { PhotoID = resultPhoto.ID, TagID = resultTag.ID});
                    context.SaveChanges();
                }
            }

            return true;
        }
        /// <summary>
        /// Get one archive.
        /// </summary>
        /// <param name="id">Archive ID.</param>
        /// <returns>One Archive.</returns>
        public Archive GetArchive(int id)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                IEnumerable<Archive> archive = from o in context.Archives where o.ID == id select o;

                if (archive.Count() != 0)
                {
                    return archive.First();
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// Untaging photo.
        /// </summary>
        /// <param name="photoID">Photo ID.</param>
        /// <param name="tagName">Tag name.</param>
        /// <returns></returns>
        public static bool UntagPhoto(int photoID, String tagName)
        {
            string connectionString = ConnectionStringHelper.GetActualConnectionString();

            // Updates collections.
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(connectionString))
            {
                Photo resultPhoto = context.Photos.Where(p => p.ID == photoID).First();
                Tag resultTag = context.Tags.Where(t => t.Name == tagName).First();

                if (resultPhoto != null && resultTag != null)
                {
                    context.Tags2PhotosSet.DeleteObject((from o in context.Tags2PhotosSet
                                                         where o.PhotoID == resultPhoto.ID && o.TagID == resultTag.ID
                                                         select o).First());
                    context.SaveChanges();
                }
            }

            return true;
        }
        /// <summary>
        /// Get one report history by saved report ID.
        /// </summary>
        /// <param name="savedReportId">Saved report ID.</param>
        /// <returns>One report's history.</returns>
        public ReportsHistory GetReportHistoryBySavedReportId(int savedReportId)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                IEnumerable<ReportsHistory> reportHistory = from o in context.ReportsHistories
                                                            join report in context.SavedReports on savedReportId equals report.ID
                                                            select o;

                if (reportHistory.Count() != 0)
                {
                    return reportHistory.First();
                }
                else
                {
                    return null;
                }
            }
        }
 /// <summary>
 /// Gets all locations for photo id.
 /// </summary>
 /// <param name="photoId">The photo id.</param>
 /// <returns>All assigned locations to the photo.</returns>
 public List<Archive> GetLocationsForPhoto(int photoId)
 {
     using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
     {
         return (from o in context.Archives
                 join p in context.Photos on photoId equals p.ID
                 select o).ToList();
     }
 }
 /// <summary>
 /// Get one exif.
 /// </summary>
 /// <param name="id">Exif ID.</param>
 /// <returns>One exif.</returns>
 public ExifAttribute GetExif(int id)
 {
     using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
     {
         return (from o in context.ExifAttributes where o.ID == id select o).First();
     }
 }
        /// <summary>
        /// Get one AdditionalAttribute.
        /// </summary>
        /// <param name="id">Additional Attribute ID.</param>
        /// <returns>One AdditionalAttributes entity.</returns>
        public AdditionalAttribute GetAttribute(int id)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                IEnumerable<AdditionalAttribute> attribute = from o in context.AdditionalAttributes where o.ID == id select o;

                if (attribute.Count() != 0)
                {
                    return attribute.First();
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// Get one ExifAttribute by photo ID.
        /// </summary>
        /// <param name="photoId">Photo ID.</param>
        /// <returns>One ExifAttributes entity.</returns>
        public ExifAttribute GetExifAttributeByPhoto(int photoId)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                IEnumerable<ExifAttribute> attribute = from o in context.ExifAttributes
                                                       join photo in context.Photos on photoId equals photo.ID
                                                       select o;

                if (attribute.Count() != 0)
                {
                    return attribute.First();
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// Gets the tag.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>Tag entity.</returns>
        public Tag GetTag(int id)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                IEnumerable<Tag> tag = from o in context.Tags where o.ID == id select o;

                if (tag.Count() != 0)
                {
                    return tag.First();
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// Get one saved report.
        /// </summary>
        /// <param name="id">Report ID.</param>
        /// <returns>One report.</returns>
        public SavedReport GetReport(int id)
        {
            using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
            {
                IEnumerable<SavedReport> report = from o in context.SavedReports where o.ID == id select o;

                if (report.Count() != 0)
                {
                    return report.First();
                }
                else
                {
                    return null;
                }
            }
        }
 /// <summary>
 /// Get all history of reports.
 /// </summary>
 /// <returns>Reports history list.</returns>
 public List<ReportsHistory> GetAllReportsHistory()
 {
     using (PhotoCollectionDatabaseEntities context = new PhotoCollectionDatabaseEntities(_connectionString))
     {
         return (from o in context.ReportsHistories select o).ToList();
     }
 }