Inheritance: INotifyPropertyChanging, INotifyPropertyChanged
 public static void InsertCollectionContributionMapping(Collection_Contribution_Mapping c)
 {
     naturenet_dataclassDataContext db = new naturenet_dataclassDataContext();
     db.Collection_Contribution_Mappings.InsertOnSubmit(c);
     Action a = new Action();
     a.date = DateTime.Now; a.type_id = 1; a.object_type = c.GetType().ToString();
     try { db.SubmitChanges(); a.object_id = c.id; a.user_id = c.Collection.user_id; db.Actions.InsertOnSubmit(a); db.SubmitChanges(); }
     catch (Exception e) { log.WriteErrorLog(e); }
 }
Esempio n. 2
0
        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;
                }
            }
        }
Esempio n. 3
0
        public static void retrieve_and_process_idea_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);

            FilesResource.ListRequest list_request = gd_service.Files.List();
            list_request.Q = "title = '" + configurations.googledrive_ideafiletitle + "'";
            FileList file_list = list_request.Fetch();
            if (file_list.Items.Count == 0) return;
            Google.Apis.Drive.v2.Data.File ideas_list_file = file_list.Items[0];
            if (String.IsNullOrEmpty(ideas_list_file.DownloadUrl)) return;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(ideas_list_file.DownloadUrl));
            authenticator.ApplyAuthenticationToRequest(request);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode != HttpStatusCode.OK) return;
            System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
            string ideas_list = reader.ReadToEnd();
            reader.Close();
            List<string> ideas = ideas_list.Split(new Char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();

            naturenet_dataclassDataContext db = new naturenet_dataclassDataContext();
            var r = from c in db.Contributions
                    select c.note;
            List<string> notes = r.ToList<string>();
            foreach (string idea in ideas)
            {
                if (!notes.Contains(configurations.GetItem_GDText(idea, 2)))
                {
                    string username = configurations.GetItem_GDText(idea, 0);
                    string avatarname = configurations.GetItem_GDText(idea, 1);
                    if (avatarname.Substring(avatarname.Length - 4, 4) != ".png")
                        avatarname = avatarname + ".png";
                    int col_id = configurations.get_or_create_collection(db, username, avatarname, 1, DateTime.Now);

                    Contribution design_idea = new Contribution();
                    design_idea.date = DateTime.Now;
                    design_idea.location_id = 0;
                    design_idea.media_url = "";
                    design_idea.note = configurations.GetItem_GDText(idea, 2);
                    design_idea.tags = "design idea";
                    db.Contributions.InsertOnSubmit(design_idea);
                    db.SubmitChanges();

                    Collection_Contribution_Mapping map = new Collection_Contribution_Mapping();
                    map.collection_id = col_id;
                    map.contribution_id = design_idea.id;
                    map.date = DateTime.Now;
                    db.Collection_Contribution_Mappings.InsertOnSubmit(map);
                    db.SubmitChanges();
                }
            }
        }
		private void detach_Collection_Contribution_Mappings(Collection_Contribution_Mapping entity)
		{
			this.SendPropertyChanging();
			entity.Contribution = null;
		}
 partial void DeleteCollection_Contribution_Mapping(Collection_Contribution_Mapping instance);
 partial void InsertCollection_Contribution_Mapping(Collection_Contribution_Mapping instance);