コード例 #1
0
        /// <summary>
        /// Updates or inserts an annotation into the current database context.
        /// </summary>
        /// <param name="context">Http context</param>
        /// <param name="externalId">Unique document identifier generated from document's absolute path</param>
        /// <param name="userName">User's name as a query param</param>
        private void upsertAnnotation(HttpContext context, string externalId, string userName)
        {
            var rawData = String.Empty;
            // Make sure we're at the beginning
            context.Request.InputStream.Position = 0;
            using (StreamReader inputStream = new StreamReader(context.Request.InputStream))
            {
                rawData = inputStream.ReadToEnd();
            }

            var jsonData = serializer.Deserialize<List<Dictionary<String, Object>>>(rawData);

            var creationDateTime = jsonData[0]["creationDateTime"];

            var type = creationDateTime.GetType();

            using (var dbContext = new AnnotationModel.AnnotationContext())
            {
                // Entity Framework does not currently support unique constraints
                // Check if this document already exists
                // LINQ will return 0 or the primary key for the current document
                Pcc.DocumentModel.Document document = null;

                try
                {
                    document = dbContext.Documents
                                .Where(d => d.ExternalId == externalId)
                                .FirstOrDefault();
                }
                catch (Exception e)
                {

                    sendResponse(context, (int)HttpStatusCode.BadGateway, "Failed to connect to database. Please check your conneciton string.");
                }

                if (document != null)
                {
                    // Document already exists
                    // In this use case, we are only supporting one set of annotations per user.
                    // Does the current user already own a set of annotations for this document?

                    var annotation = dbContext.Annotations
                        .Where(a => a.UserName == userName &&
                        a.DocumentID == document.DocumentID)
                        .FirstOrDefault();

                    if (annotation != null)
                    {
                        // User already owns a set of annotations, update those annotations
                        annotation.AnnotationText = rawData;

                        // Mark as modified and save
                        dbContext.Entry(annotation).State = EntityState.Modified;
                        dbContext.SaveChanges();

                        sendResponse(context, (int)HttpStatusCode.OK, "Annotation Updated");

                    }
                    else
                    {
                        // This is a new set of annotations for this user
                        AnnotationModel.Annotation newAnno = new AnnotationModel.Annotation()
                        {
                            UserName = userName,
                            AnnotationText = rawData,
                            DocumentID = document.DocumentID
                        };

                        // Insert into the local context and save to the store
                        dbContext.Annotations.Add(newAnno);
                        dbContext.SaveChanges();

                        sendResponse(context, (int)HttpStatusCode.OK, "Annotation Created");

                    }

                }
                else
                {
                    // If this document's external ID does not exist in the current context or in the store
                    // Insert the document for this request
                    DocumentModel.Document newDoc = new DocumentModel.Document() { ExternalId = externalId };

                    // Insert into the local context and save to the store
                    dbContext.Documents.Add(newDoc);
                    dbContext.SaveChanges();

                    // Now get the primary key for that document so we can use it
                    document = dbContext.Documents
                    .Where(d => d.ExternalId == externalId)
                    .FirstOrDefault();

                    // In this use case, we are only supporting one set of annotations per user.
                    // Does the current user already own a set of annotations for this document?
                    var annotation = dbContext.Annotations
                        .Where(a => a.UserName == userName &&
                        a.DocumentID == document.DocumentID)
                        .FirstOrDefault();

                    if (annotation != null)
                    {
                        // User already owns a set of annotations, update those annotations locallly
                        // Note that this condition should never happen (ie - no document means no annotation)
                        annotation.AnnotationText = rawData;
                        annotation.AnnotationText = jsonData[0]["modificationDateTime"].ToString();

                        // Mark as modified and save
                        dbContext.Entry(annotation).State = EntityState.Modified;
                        dbContext.SaveChanges();

                        sendResponse(context, (int)HttpStatusCode.OK, "Annotation Updated");
                    }
                    else
                    {
                        // This is a new set of annotations for this user
                        AnnotationModel.Annotation newAnno = new AnnotationModel.Annotation()
                        {
                            UserName = userName,
                            AnnotationText = rawData,
                            DocumentID = document.DocumentID,
                            Document = document
                        };

                        // Insert into the local context and save to the store
                        dbContext.Annotations.Add(newAnno);
                        dbContext.SaveChanges();

                        sendResponse(context, (int)HttpStatusCode.OK, "Annotation Created");
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Updates or inserts an annotation into the current database context.
        /// </summary>
        /// <param name="context">Http context</param>
        /// <param name="externalId">Unique document identifier generated from document's absolute path</param>
        /// <param name="userName">User's name as a query param</param>
        private void upsertAnnotation(HttpContext context, string externalId, string userName)
        {
            var rawData = String.Empty;

            // Make sure we're at the beginning
            context.Request.InputStream.Position = 0;
            using (StreamReader inputStream = new StreamReader(context.Request.InputStream))
            {
                rawData = inputStream.ReadToEnd();
            }

            var jsonData = serializer.Deserialize <List <Dictionary <String, Object> > >(rawData);

            var creationDateTime = jsonData[0]["creationDateTime"];

            var type = creationDateTime.GetType();

            using (var dbContext = new AnnotationModel.AnnotationContext())
            {
                // Entity Framework does not currently support unique constraints
                // Check if this document already exists
                // LINQ will return 0 or the primary key for the current document
                Pcc.DocumentModel.Document document = null;

                try
                {
                    document = dbContext.Documents
                               .Where(d => d.ExternalId == externalId)
                               .FirstOrDefault();
                }
                catch (Exception e)
                {
                    sendResponse(context, (int)HttpStatusCode.BadGateway, "Failed to connect to database. Please check your conneciton string.");
                }

                if (document != null)
                {
                    // Document already exists
                    // In this use case, we are only supporting one set of annotations per user.
                    // Does the current user already own a set of annotations for this document?

                    var annotation = dbContext.Annotations
                                     .Where(a => a.UserName == userName &&
                                            a.DocumentID == document.DocumentID)
                                     .FirstOrDefault();

                    if (annotation != null)
                    {
                        // User already owns a set of annotations, update those annotations
                        annotation.AnnotationText = rawData;

                        // Mark as modified and save
                        dbContext.Entry(annotation).State = EntityState.Modified;
                        dbContext.SaveChanges();

                        sendResponse(context, (int)HttpStatusCode.OK, "Annotation Updated");
                    }
                    else
                    {
                        // This is a new set of annotations for this user
                        AnnotationModel.Annotation newAnno = new AnnotationModel.Annotation()
                        {
                            UserName       = userName,
                            AnnotationText = rawData,
                            DocumentID     = document.DocumentID
                        };

                        // Insert into the local context and save to the store
                        dbContext.Annotations.Add(newAnno);
                        dbContext.SaveChanges();

                        sendResponse(context, (int)HttpStatusCode.OK, "Annotation Created");
                    }
                }
                else
                {
                    // If this document's external ID does not exist in the current context or in the store
                    // Insert the document for this request
                    DocumentModel.Document newDoc = new DocumentModel.Document()
                    {
                        ExternalId = externalId
                    };

                    // Insert into the local context and save to the store
                    dbContext.Documents.Add(newDoc);
                    dbContext.SaveChanges();

                    // Now get the primary key for that document so we can use it
                    document = dbContext.Documents
                               .Where(d => d.ExternalId == externalId)
                               .FirstOrDefault();

                    // In this use case, we are only supporting one set of annotations per user.
                    // Does the current user already own a set of annotations for this document?
                    var annotation = dbContext.Annotations
                                     .Where(a => a.UserName == userName &&
                                            a.DocumentID == document.DocumentID)
                                     .FirstOrDefault();

                    if (annotation != null)
                    {
                        // User already owns a set of annotations, update those annotations locallly
                        // Note that this condition should never happen (ie - no document means no annotation)
                        annotation.AnnotationText = rawData;
                        annotation.AnnotationText = jsonData[0]["modificationDateTime"].ToString();

                        // Mark as modified and save
                        dbContext.Entry(annotation).State = EntityState.Modified;
                        dbContext.SaveChanges();

                        sendResponse(context, (int)HttpStatusCode.OK, "Annotation Updated");
                    }
                    else
                    {
                        // This is a new set of annotations for this user
                        AnnotationModel.Annotation newAnno = new AnnotationModel.Annotation()
                        {
                            UserName       = userName,
                            AnnotationText = rawData,
                            DocumentID     = document.DocumentID,
                            Document       = document
                        };

                        // Insert into the local context and save to the store
                        dbContext.Annotations.Add(newAnno);
                        dbContext.SaveChanges();

                        sendResponse(context, (int)HttpStatusCode.OK, "Annotation Created");
                    }
                }
            }
        }