Exemplo n.º 1
0
        public object CreateInstance(CSEntryChange csentry)
        {
            GoogleCourse c = new GoogleCourse();

            c.Course.Id = csentry.DN;
            return(c);
        }
        public IList<AttributeChange> GetChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
        {
            List<AttributeChange> attributeChanges = new List<AttributeChange>();

            CourseStudents students = source as CourseStudents;

            if (students == null)
            {
                GoogleCourse course = source as GoogleCourse;

                if (course == null)
                {
                    throw new InvalidOperationException();
                }
                else
                {
                    students = course.Students;
                }
            }

            foreach (IAttributeAdapter typeDef in ManagementAgent.Schema[SchemaConstants.Course].AttributeAdapters.Where(t => t.Api == this.Api))
            {
                foreach (string attributeName in typeDef.MmsAttributeNames)
                {
                    if (type.HasAttribute(attributeName))
                    {
                        attributeChanges.AddRange(typeDef.CreateAttributeChanges(dn, modType, students));
                    }
                }
            }

            return attributeChanges;
        }
Exemplo n.º 3
0
        private bool SetDNValue(CSEntryChange csentry, GoogleCourse course)
        {
            if (csentry.ObjectModificationType != ObjectModificationType.Replace && csentry.ObjectModificationType != ObjectModificationType.Update)
            {
                return(false);
            }

            string newDN = csentry.GetNewDNOrDefault <string>();

            if (newDN == null)
            {
                return(false);
            }

            throw new NotSupportedException("Renaming the DN of this object is not supported");
        }
Exemplo n.º 4
0
        public object GetInstance(CSEntryChange csentry)
        {
            GoogleCourse googleCourse = new GoogleCourse();

            googleCourse.Course = this.config.ClassroomService.GetCourse(csentry.GetAnchorValueOrDefault <string>("id") ?? csentry.DN);
            string[] fieldNames = new string[] { "primaryEmail", "id" };
            string   fields     = string.Join(",", fieldNames);

            try
            {
                googleCourse.Course.OwnerId = this.config.UsersService.Get(googleCourse.Course.OwnerId, fields).PrimaryEmail;
            }
            catch (GoogleApiException ex)
            {
                Logger.WriteLine($"Error fetching OwnerId {googleCourse.Course.OwnerId} for Course {googleCourse.Course.Id}");
                Logger.WriteException(ex);
            }
            return(googleCourse);
        }
Exemplo n.º 5
0
        private CSEntryChange GetCSEntryForCourse(GoogleCourse course, Microsoft.MetadirectoryServices.Schema schema)
        {
            CSEntryChange csentry;

            if (course.Errors.Count > 0)
            {
                csentry                        = CSEntryChange.Create();
                csentry.ObjectType             = "course";
                csentry.ObjectModificationType = ObjectModificationType.Add;
                csentry.DN                     = course.Course.Id;
                csentry.ErrorCodeImport        = MAImportError.ImportErrorCustomContinueRun;
                csentry.ErrorDetail            = course.Errors.FirstOrDefault()?.StackTrace;
                csentry.ErrorName              = course.Errors.FirstOrDefault()?.Message;
            }
            else
            {
                csentry = ImportProcessor.GetCSEntryChange(course, schema.Types[SchemaConstants.Course], this.config);
            }

            return(csentry);
        }
Exemplo n.º 6
0
        private List <AttributeChange> GetLocalChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
        {
            List <AttributeChange> attributeChanges = new List <AttributeChange>();

            GoogleCourse googleCourse = source as GoogleCourse;
            Course       course       = null;

            if (googleCourse != null)
            {
                course = googleCourse.Course;
            }

            if (course == null)
            {
                course = source as Course;
            }

            if (course == null)
            {
                throw new InvalidOperationException();
            }

            foreach (IAttributeAdapter typeDef in this.SchemaType.AttributeAdapters.Where(t => t.Api == this.Api))
            {
                if (typeDef.IsAnchor)
                {
                    continue;
                }

                foreach (AttributeChange change in typeDef.CreateAttributeChanges(dn, modType, course))
                {
                    if (type.HasAttribute(change.Name))
                    {
                        attributeChanges.Add(change);
                    }
                }
            }

            return(attributeChanges);
        }
Exemplo n.º 7
0
        public IList <AttributeChange> ApplyChanges(CSEntryChange csentry, SchemaType type, ref object target, bool patch = false)
        {
            bool hasChanged = false;
            List <AttributeChange> changes = new List <AttributeChange>();

            GoogleCourse googleCourse = target as GoogleCourse;
            Course       course       = null;

            if (googleCourse != null)
            {
                course = googleCourse.Course;
            }

            if (course == null)
            {
                course = target as Course;
            }

            if (course == null)
            {
                throw new InvalidOperationException();
            }

            hasChanged |= this.SetDNValue(csentry, googleCourse);

            List <string> updateMask = new List <string>();

            foreach (IAttributeAdapter typeDef in this.SchemaType.AttributeAdapters.Where(t => t.Api == this.Api))
            {
                if (typeDef.UpdateField(csentry, course))
                {
                    hasChanged = true;
                    updateMask.Add(typeDef.FieldName);
                }
            }

            if (hasChanged)
            {
                Course result;

                // Keep OwnerId for Delta import
                string ownerId = course.OwnerId;

                if (csentry.ObjectModificationType == ObjectModificationType.Add)
                {
                    // Remove Id if invalid
                    if (course.Id != null && !course.Id.StartsWith(ProjectPrefix) && !course.Id.StartsWith(DomainPrefix))
                    {
                        course.Id = null;
                    }

                    Logger.WriteLine($"Adding course {csentry.DN}");
                    result = this.config.ClassroomService.Add(course);
                    Logger.WriteLine($"Added course {csentry.DN} (Id: {result.Id})");

                    // Get Id and result
                    csentry.DN          = result.Id;
                    course              = result;
                    googleCourse.Course = course;
                }
                else if (csentry.ObjectModificationType == ObjectModificationType.Replace || csentry.ObjectModificationType == ObjectModificationType.Update)
                {
                    string id = csentry.GetAnchorValueOrDefault <string>("id");

                    if (patch)
                    {
                        result = this.config.ClassroomService.Patch(id, course, string.Join(",", updateMask));
                    }
                    else
                    {
                        result = this.config.ClassroomService.Update(course);
                    }
                }
                else
                {
                    throw new InvalidOperationException();
                }

                // Reset OwnerId for Delta import
                if (updateMask.Contains(SchemaConstants.OwnerId))
                {
                    result.OwnerId = ownerId;
                }

                changes.AddRange(this.GetLocalChanges(csentry.DN, csentry.ObjectModificationType, type, result));
            }

            foreach (IApiInterface i in this.internalInterfaces)
            {
                foreach (AttributeChange c in i.ApplyChanges(csentry, type, ref target, patch))
                {
                    //changes.RemoveAll(t => t.Name == c.Name);
                    changes.Add(c);
                }
            }

            return(changes);
        }