nature_net.file_manager.retrieve_and_process_media_changes_from_googledrive C# (CSharp) Метод

retrieve_and_process_media_changes_from_googledrive() публичный статический Метод

public static retrieve_and_process_media_changes_from_googledrive ( ) : void
Результат void
        public static void retrieve_and_process_media_changes_from_googledrive()
        {
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = configurations.googledrive_client_id;
            provider.ClientSecret = configurations.googledrive_client_secret;
            IAuthenticator authenticator = new OAuth2Authenticator<NativeApplicationClient>(provider, googledrive_getauthorization);
            DriveService gd_service = new DriveService(authenticator);

            string startChangeId = configurations.googledrive_lastchange;
            List<Change> result = new List<Change>();
            ChangesResource.ListRequest request = gd_service.Changes.List();

            if (!String.IsNullOrEmpty(startChangeId))
            {
                request.StartChangeId = startChangeId;
            }
            do
            {
                try
                {
                    ChangeList changes = request.Fetch();
                    configurations.googledrive_lastchange = changes.LargestChangeId;
                    configurations.SaveChangeID();
                    result.AddRange(changes.Items);
                    request.PageToken = changes.NextPageToken;
                }
                catch (Exception e)
                {
                    // write log of the exception
                    log.WriteErrorLog(e);
                    request.PageToken = null;
                }
            } while (!String.IsNullOrEmpty(request.PageToken));

            naturenet_dataclassDataContext db = new naturenet_dataclassDataContext();
            foreach (Change c in result)
            {
                try
                {
                    if (c.Deleted.HasValue)
                        if (c.Deleted.Value)
                        {
                            //delete from database if exists
                            //System.Windows.Forms.MessageBox.Show("Delete Requested: " + c.FileId);
                            IQueryable<Collection_Contribution_Mapping> ccmap;
                            if (c.File != null)
                            {
                                ccmap = from cc in db.Collection_Contribution_Mappings
                                        where cc.Contribution.media_url.Contains(c.File.Title)
                                        select cc;
                                //System.Windows.Forms.MessageBox.Show("The file was not null.");
                            }
                            else
                            {
                                ccmap = from cc in db.Collection_Contribution_Mappings
                                        where cc.Contribution.technical_info.Contains(c.FileId)
                                        select cc;
                                //System.Windows.Forms.MessageBox.Show("Delete Requested: " + c.FileId + "with " + ccmap.Count() + " mapping(s).");
                            }

                            if (ccmap.Count() == 0) continue;
                            string mappings = "";
                            foreach (Collection_Contribution_Mapping ccm in ccmap)
                                mappings = mappings + ";" + ccm.collection_id + "," + ccm.contribution_id + "," + ccm.date.ToString() + "," + ccm.technical_info;
                            db.Collection_Contribution_Mappings.DeleteAllOnSubmit(ccmap);
                            Action del = new Action();
                            del.type_id = 3;
                            del.user_id = ccmap.First().Collection.user_id;
                            del.date = DateTime.Now;
                            del.object_type = "nature_net.Collection_Contribution_Mapping";
                            del.technical_info = mappings;
                            db.Actions.InsertOnSubmit(del);
                            db.SubmitChanges();
                            //System.Windows.Forms.MessageBox.Show("Delete Was Successful.");
                            //delete from hard drive if exists
                            continue;
                        }

                    if (c.File == null)
                        continue;

                    if (c.File.Title == configurations.googledrive_userfiletitle)
                    {
                        file_manager.retrieve_and_process_user_changes_from_googledrive();
                        continue;
                    }

                    if (c.File.Title == configurations.googledrive_ideafiletitle)
                    {
                        file_manager.retrieve_and_process_idea_changes_from_googledrive();
                        continue;
                    }

                    if (c.File.Parents[0].IsRoot.HasValue)
                        if (!c.File.Parents[0].IsRoot.Value || c.File.Parents.Count > 1)
                            continue;

                    if (c.File.Description == null) continue;

                    string desc = c.File.Description;
                    string username = configurations.GetItemFromJSON(desc, "username");
                    if (username == "") continue;

                    var contribution_list = from contrib in db.Contributions
                                            where contrib.media_url.Contains(c.File.Title)
                                            select contrib;
                    if (contribution_list.Count() == 0)
                    {
                        Contribution contribute = new Contribution();
                        contribute.media_url = c.File.Title;
                        contribute.technical_info = c.File.Id;
                        DateTime dt = DateTime.Now;
                        bool hasdate = DateTime.TryParse(c.File.CreatedDate, out dt);
                        contribute.date = dt;
                        int location_id = 0;
                        try { location_id = Convert.ToInt32(configurations.GetItemFromJSON(desc, "landmarkId")); }
                        catch (Exception) { location_id = 0; }
                        if (location_id == -1)
                            contribute.location_id = 0;
                        else
                            contribute.location_id = location_id;
                        contribute.note = configurations.GetItemFromJSON(desc, "comment");
                        contribute.tags = configurations.GetItemFromJSON(desc, "categories");
                        db.Contributions.InsertOnSubmit(contribute);
                        db.SubmitChanges();
                        int activity_id = 0;
                        try { activity_id = Convert.ToInt32(configurations.GetItemFromJSON(desc, "activityId")); }
                        catch (Exception) { activity_id = 0; }
                        if (activity_id != 0)
                            activity_id++;
                        string avatar_name = configurations.GetItemFromJSON(desc, "avatarName");
                        if (avatar_name.Substring(avatar_name.Length - 4, 4) != ".png")
                            avatar_name = avatar_name + ".png";
                        int collection_id = configurations.get_or_create_collection(db, username, avatar_name, activity_id, dt);
                        Collection_Contribution_Mapping map = new Collection_Contribution_Mapping();
                        map.collection_id = collection_id;
                        map.contribution_id = contribute.id;
                        map.date = DateTime.Now;
                        db.Collection_Contribution_Mappings.InsertOnSubmit(map);
                        db.SubmitChanges();
                    }
                    else
                    {
                        //sync metadata
                        Contribution c1 = contribution_list.First<Contribution>();
                        int location_id = 0;
                        try { location_id = Convert.ToInt32(configurations.GetItemFromJSON(desc, "landmarkId")); }
                        catch (Exception) { location_id = 0; }
                        if (location_id == -1)
                            c1.location_id = 0;
                        else
                            c1.location_id = location_id;
                        c1.note = configurations.GetItemFromJSON(desc, "comment");
                        c1.tags = configurations.GetItemFromJSON(desc, "categories");
                        db.SubmitChanges();
                        int activity_id = 0;
                        try { activity_id = Convert.ToInt32(configurations.GetItemFromJSON(desc, "activityId")); }
                        catch (Exception) { activity_id = 0; }
                        if (activity_id != 0)
                            activity_id++;
                        var ccm = from cc in db.Collection_Contribution_Mappings
                                  where cc.contribution_id == c1.id
                                  select cc.Collection;
                        Collection c_first = ccm.First<Collection>();
                        if (c_first.activity_id != activity_id)
                        {
                            string avatar_name = configurations.GetItemFromJSON(desc, "avatarName");
                            if (avatar_name.Substring(avatar_name.Length - 4, 4) != ".png")
                                avatar_name = avatar_name + ".png";
                            int collection_id = configurations.get_or_create_collection(db, username, avatar_name, activity_id, c_first.date);
                            Collection_Contribution_Mapping map = new Collection_Contribution_Mapping();
                            map.collection_id = collection_id;
                            map.contribution_id = c1.id;
                            map.date = DateTime.Now;
                            db.Collection_Contribution_Mappings.InsertOnSubmit(map);
                            // delete the old mapping
                            var ccm2 = from cc2 in db.Collection_Contribution_Mappings
                                       where cc2.contribution_id == c1.id && cc2.collection_id == c_first.id
                                       select cc2;
                            db.Collection_Contribution_Mappings.DeleteAllOnSubmit(ccm2);
                            db.SubmitChanges();
                        }
                    }
                }
                catch (Exception ex_)
                {
                    // write log of the exception
                    log.WriteErrorLog(ex_);
                    continue;
                }
            }
        }