// GET api/careplans
        public HttpResponseMessage Get(Guid relatedId)
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var query = from c in ctx.new_careplanSet
                        where c.new_ChildId.Id == relatedId
                        select new CrmEarlyBound.new_careplan
                        {
                            Id = c.Id,
                            new_name = c.new_name,
                            new_Description = c.new_Description
                        };

            var queryResults = query.ToList();

            var listReturn = new List<TestCarePlanObject>();
            foreach (new_careplan x in queryResults)
            {
                var returnObject = new TestCarePlanObject();
                returnObject.Id = x.Id;
                returnObject.Name = x.new_name;
                returnObject.Description = x.new_Description;

                listReturn.Add(returnObject);
            }

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                listReturn);
        }
        // GET api/notes
        public HttpResponseMessage Get(string RelatedId)
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var query = from a in ctx.AnnotationSet
                        where a.ObjectId.Id == new Guid(RelatedId)
                        select new TestAnnotationObject
                        {
                            Id = a.Id,
                            CreatedOn = a.CreatedOn,
                            OwnerId = a.OwnerId.Id,
                            OwnerName = a.OwnerId.Name,
                            ObjectId = a.ObjectId.Id,
                            Subject = a.Subject,
                            NoteText = a.NoteText
                        };

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                query);
        }
        public HttpResponseMessage Get()
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var query = from e in ctx.new_employmentSet
                        select new EmploymentObject
                        {
                            Id = e.Id,
                            BeginDate = e.new_BeginDate,
                            ContactId = e.new_ContactEmploymentId.Id,
                            Employer = e.new_employer,
                            EndDate = e.new_EndDate,
                            Position = e.new_Position,
                            ReasonForLeaving = e.new_ReasonForLeaving
                        };

            var queryResults = query.ToList();

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                queryResults);
        }
        public HttpResponseMessage Post(Guid taskId)
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var taskReference = new EntityReference("task", taskId);

            try
            {
                var req = new SetStateRequest();
                req.State = new OptionSetValue(1);
                req.Status = new OptionSetValue(5);
                req.EntityMoniker = taskReference;

                var res = (SetStateResponse)ctx.Execute(req);
            }
            catch (Exception e)
            {
                return this.Request.CreateResponse(
                    HttpStatusCode.InternalServerError, e);
            }

            return this.Request.CreateResponse(
                HttpStatusCode.Created);
        }
        // GET api/allergies
        public HttpResponseMessage Get()
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var query = from a in ctx.new_allergySet
                        select new CrmEarlyBound.new_allergy
                        {
                            Id = a.Id,
                            new_name = a.new_name,
                            new_Description = a.new_Description,
                        };

            var queryResults = query.ToList();

            var listReturn = new List<TestAllergyObject>();
            foreach (new_allergy x in queryResults)
            {
                var returnObject = new TestAllergyObject();
                returnObject.Id = x.Id;
                returnObject.Name = x.new_name;
                returnObject.Description = x.new_Description;

                listReturn.Add(returnObject);
            }

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                listReturn);
        }
        public HttpResponseMessage Get()
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var query = from e in ctx.new_educationSet
                        select new EducationObject
                        {
                            Id = e.Id,
                            Name = e.new_name,
                            ContactId = e.new_ContactEducationId.Id,
                            EnrollmentDate = e.new_EnrollmentDate,
                            Grade = e.new_Grade,
                            GradeRepeat = e.new_GradeRepeat
                        };

            var queryResults = query.ToList();

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                queryResults);
        }
        // GET api/home
        public HttpResponseMessage Get()
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var query = from c in ctx.IncidentSet
                        select new CrmEarlyBound.Incident
                        {
                           Id = c.Id,
                           Title = c.Title,
                           new_AllegationType = c.new_AllegationType,
                           new_Allegation = c.new_Allegation,
                           new_Reporter = c.new_Reporter
                        };

            var cases = query.ToList();

            var listReturn = new List<TestCaseObject>();
            foreach (Incident c in cases)
            {
                var caseObject = new TestCaseObject();
                caseObject.Id = c.Id;
                caseObject.Title = c.Title;
                caseObject.Allegation = c.new_Allegation;
                caseObject.Reporter = c.new_Reporter;

                switch (c.new_AllegationType)
                {
                    case 100000000:
                        caseObject.AllegationType = "Abuse";
                        break;
                    case 100000001:
                        caseObject.AllegationType = "Truancy";
                        break;
                    case 100000002:
                        caseObject.AllegationType = "Neglect";
                        break;
                }

                listReturn.Add(caseObject);
            }

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                listReturn);
        }
        //Household Contacts for Case
        public HttpResponseMessage Get(Guid caseId)
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var queryResult = from n in ctx.new_incident_contactSet
                         join c in ctx.ContactSet on n.contactid equals c.ContactId
                         where n.incidentid == caseId
                         select new TestContactObject
                         {
                            Id = c.ContactId.Value,
                            FirstName = c.FirstName,
                            LastName = c.LastName,
                            MobilePhone = c.MobilePhone,
                            Address1_Line1 = c.Address1_Line1,
                            Address1_Line2 = c.Address1_Line2,
                            Address1_City = c.Address1_City,
                            Address1_StateOrProvince = c.Address1_StateOrProvince,
                            Address1_PostalCode = c.Address1_PostalCode,
                            ContactType = c.new_ContactType
                         };

            var returnList = new List<TestContactObject>();
            foreach (TestContactObject ctact in queryResult)
            {
                switch (ctact.ContactType)
                {
                    case 100000000:
                        ctact.ContactTypeString = "Child";
                        break;
                    case 100000001:
                        ctact.ContactTypeString = "Parent/Guardian";
                        break;
                    case 100000002:
                        ctact.ContactTypeString = "Other";
                        break;
                }

                returnList.Add(ctact);
            }

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                returnList);
        }
        // GET api/contacts
        public HttpResponseMessage Get()
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var query = from c in ctx.ContactSet
                        select new CrmEarlyBound.Contact
                        {
                            Id = c.Id,
                            FirstName = c.FirstName,
                            LastName = c.LastName,
                            MobilePhone = c.MobilePhone,
                            Address1_Line1 = c.Address1_Line1,
                            Address1_Line2 = c.Address1_Line2,
                            Address1_City = c.Address1_City,
                            Address1_StateOrProvince = c.Address1_StateOrProvince,
                            Address1_PostalCode = c.Address1_PostalCode,
                            new_ContactType = c.new_ContactType
                        };

            var contacts = query.ToList();

            var listReturn = new List<TestContactObject>();
            foreach (Contact c in contacts)
            {
                var returnObject = new TestContactObject();
                returnObject.Id = c.Id;
                returnObject.FirstName = c.FirstName;
                returnObject.LastName = c.LastName;
                returnObject.MobilePhone = c.MobilePhone;
                returnObject.Address1_Line1 = c.Address1_Line1;
                returnObject.Address1_Line2 = c.Address1_Line2;
                returnObject.Address1_City = c.Address1_City;
                returnObject.Address1_StateOrProvince = c.Address1_StateOrProvince;
                returnObject.Address1_PostalCode = c.Address1_PostalCode;
                returnObject.ContactType = c.new_ContactType;

                listReturn.Add(returnObject);
            }

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                listReturn);
        }
        // GET api/tasks
        public HttpResponseMessage Get(string RelatedId)
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var query = from a in ctx.TaskSet
                        where a.RegardingObjectId.Id == new Guid(RelatedId)
                        select new CrmEarlyBound.Task
                        {
                            Id = a.Id,
                            RegardingObjectId = a.RegardingObjectId,
                            Subject = a.Subject,
                            StatusCode = a.StatusCode
                        };

            var queryResults = query.ToList();

            var listReturn = new List<TestTaskObject>();
            foreach (Task x in queryResults)
            {
                var returnObject = new TestTaskObject();
                returnObject.Id = x.Id;
                returnObject.RegardingObjectId = x.RegardingObjectId.Id.ToString();
                returnObject.Subject = x.Subject;

                if (x.StatusCode.Value == 5)
                {
                    returnObject.IsComplete = true;
                }
                else
                {
                    returnObject.IsComplete = false;
                }

                listReturn.Add(returnObject);
            }

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                listReturn);
        }
        // POST api/notes?caseId=
        public HttpResponseMessage Post(Guid caseId, string noteTitle, string noteText)
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var note = new Annotation();
            note.ObjectId = new CrmEntityReference("incident", caseId);
            note.Subject = noteTitle;
            note.NoteText = noteText;

            try
            {
                ctx.Create(note);
            }
            catch (Exception e)
            {
                return this.Request.CreateResponse(
                    HttpStatusCode.InternalServerError, e);
            }

            return this.Request.CreateResponse(
                HttpStatusCode.Created);
        }
        // GET api/pictures
        public HttpResponseMessage Get(string RelatedId)
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var query = from p in ctx.new_pictureSet
                        where p.new_CaseId.Id == new Guid(RelatedId)
                        select new CrmEarlyBound.new_picture
                        {
                            Id = p.Id,
                            new_CaseId = p.new_CaseId,
                            new_Description = p.new_Description,
                            EntityImage = p.EntityImage
                        };

            var queryResults = query.ToList();

            var listReturn = new List<TestPictureObject>();
            foreach (new_picture x in queryResults)
            {
                var returnObject = new TestPictureObject();
                returnObject.Id = x.Id;
                returnObject.CaseId = x.new_CaseId.Id.ToString();
                returnObject.EntityImage = x.EntityImage;

                listReturn.Add(returnObject);
            }

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                listReturn);
        }
 public CrmServices(IOrganizationService service, CrmServiceContext xrmContext, ITracingService t)
 {
     this.Service = service;
     this.T = t;
     this.Context = xrmContext;
 }
        public HttpResponseMessage Post(Guid appointmentId, string operationName, string operationTime)
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var appointment = new Appointment();
            appointment.Id = appointmentId;
            switch (operationName)
            {
                case "EnteredBuilding":
                    appointment.new_EnteredBuilding = Convert.ToDateTime(operationTime);
                    break;
                case "LeftBuilding":
                    appointment.new_LeftBuilding = Convert.ToDateTime(operationTime);
                    break;
                case "DistressCallReceived":
                    appointment.new_DistressCallReceived = Convert.ToDateTime(operationTime);
                    break;
            }

            try
            {
                ctx.Update(appointment);
            }
            catch (Exception e)
            {
                return this.Request.CreateResponse(
                    HttpStatusCode.InternalServerError, e);
            }

            return this.Request.CreateResponse(
                HttpStatusCode.Accepted);
        }
        // GET api/appointment/[Guid.ToString()]
        public HttpResponseMessage Get()
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            WhoAmIRequest who = new WhoAmIRequest();
            var userId = ctx.Execute(who).Results["UserId"];

            var query = from a in ctx.AppointmentSet join i in ctx.IncidentSet on a.RegardingObjectId.Id equals i.Id join c in ctx.ContactSet on i.CustomerId.Id equals c.Id
                        where a.OwnerId.Id == new Guid(userId.ToString())
                        select new TestAppointmentObject
                        {
                            Id = a.Id,
                            ScheduledStart = a.ScheduledStart,
                            ScheduledEnd = a.ScheduledEnd,
                            RegardingObjectId = a.RegardingObjectId.Id,
                            Subject = a.Subject,
                            ContactId = i.CustomerId.Id,
                            FirstName = c.FirstName,
                            LastName = c.LastName,
                            MobilePhone = c.MobilePhone,
                            Address1_Line1 = c.Address1_Line1,
                            Address1_Line2 = c.Address1_Line2,
                            Address1_City = c.Address1_City,
                            Address1_StateOrProvince = c.Address1_StateOrProvince,
                            Address1_PostalCode = c.Address1_PostalCode,
                            ImageName = c.new_SienaImageName,
                            EnteredBuilding = a.new_EnteredBuilding,
                            LeftBuilding = a.new_LeftBuilding,
                            DistressCallReceived = a.new_DistressCallReceived
                        };

            var queryResults = query.ToList();

            foreach (TestAppointmentObject x in queryResults)
            {
                var today = DateTime.Today;
                if (((DateTime)x.ScheduledStart).Date == today.Date)
                {
                    x.RelativeDay = "Today";
                }
                else if (((DateTime)x.ScheduledStart).Date == today.Add(new System.TimeSpan(1, 0, 0, 0)).Date)
                {
                    x.RelativeDay = "Tomorrow";
                }
                else if (((DateTime)x.ScheduledStart).Date == today.Add(new System.TimeSpan(2, 0, 0, 0)).Date)
                {
                    x.RelativeDay = "2 Days";
                }
                else if (((DateTime)x.ScheduledStart).Date < today.Date)
                {
                    x.RelativeDay = "Previous";
                }
                else
                {
                    x.RelativeDay = "Later";
                }
            }

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                queryResults);
        }
        public HttpResponseMessage Get()
        {
            CrmConnection connection = CrmConnection.Parse(Strings.urlCreds);
            var ctx = new CrmServiceContext(new OrganizationService(connection));

            var query = from d in ctx.new_offenseSet
                        select new OffenseObject
                        {
                            Id = d.Id,
                            Name = d.new_name,
                            BeginDate = d.new_BeginDate,
                            ContactId = d.new_ContactOffensesId.Id,
                            Details = d.new_Details,
                            EndDate = d.new_EndDate,
                            Incarcerated = d.new_Incarcerated,
                            OffenseDate = d.new_OffenseDate,
                            OffenseType = d.new_OffenseType,
                            OnParole = d.new_OnParole,
                            OnProbation = d.new_OnProbation
                        };

            var queryResults = query.ToList();

            foreach (OffenseObject o in queryResults)
            {
                switch (o.OffenseType)
                {
                    case 100000000:
                        o.OffenseTypeString = "Violence";
                        break;
                    case 100000001:
                        o.OffenseTypeString = "Weapons";
                        break;
                    case 100000002:
                        o.OffenseTypeString = "Drug Related";
                        break;
                    case 100000003:
                        o.OffenseTypeString = "Other";
                        break;
                }
            }

            var configuration = GlobalConfiguration.Configuration;
            configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);

            //return listReturn;
            return this.Request.CreateResponse(
                HttpStatusCode.OK,
                queryResults);
        }