Skip to content

Instantly share code, notes, and snippets.

@MattisOlsson
Last active March 3, 2016 13:15
Show Gist options
  • Save MattisOlsson/f4335a6459c86416f412 to your computer and use it in GitHub Desktop.
Save MattisOlsson/f4335a6459c86416f412 to your computer and use it in GitHub Desktop.
EmployeeContentProvider
public class EmployeesContentProvider : ContentProvider
{
private readonly NameValueCollection _parameters = new NameValueCollection(2);
protected readonly string OrganizationId;
protected readonly IEmployeeService EmployeeService;
protected readonly IdentityMappingService IdentityMappingService;
public EmployeesContentProvider(string organizationId, ContentReference entryRoot, IEmployeeService employeeService, IdentityMappingService identityMappingService)
{
if (organizationId == null) throw new ArgumentNullException("organizationId");
if (entryRoot == null) throw new ArgumentNullException("entryRoot");
if (employeeService == null) throw new ArgumentNullException("employeeService");
if (identityMappingService == null) throw new ArgumentNullException("identityMappingService");
OrganizationId = organizationId;
EntryRoot = entryRoot;
EmployeeService = employeeService;
IdentityMappingService = identityMappingService;
Parameters.Add(ContentProviderElement.CapabilitiesString, ContentProviderCapabilities.None.ToString());
Parameters.Add(ContentProviderElement.EntryPointString, EntryRoot.ID.ToString(CultureInfo.InvariantCulture));
}
public ContentReference EntryRoot { get; set; }
public override string ProviderKey { get { return string.Format("NhoEmployees-{0}", EntryRoot.ID); } }
public override string Name { get { return ProviderKey; } }
public override NameValueCollection Parameters { get { return _parameters; } }
protected override IList<GetChildrenReferenceResult> LoadChildrenReferencesAndTypes(ContentReference contentLink, string languageID, out bool languageSpecific)
{
languageSpecific = false;
var childrenList = new List<GetChildrenReferenceResult>();
// If it is entry root, then return folder pages to put the employees in.
if (EntryRoot.CompareToIgnoreWorkID(contentLink))
{
Uri mappedFolderUri;
MappedIdentity mappedFolderIdentity;
// A-E
mappedFolderUri = MappedIdentity.ConstructExternalIdentifier(ProviderKey, "A-E");
mappedFolderIdentity = IdentityMappingService.Get(mappedFolderUri, true);
childrenList.Add(new GetChildrenReferenceResult { ContentLink = mappedFolderIdentity.ContentLink, IsLeafNode = false, ModelType = typeof(FolderPage) });
AddContentToCache(CreateFolder(mappedFolderIdentity, "A-E"));
// F-J
mappedFolderUri = MappedIdentity.ConstructExternalIdentifier(ProviderKey, "F-J");
mappedFolderIdentity = IdentityMappingService.Get(mappedFolderUri, true);
childrenList.Add(new GetChildrenReferenceResult { ContentLink = mappedFolderIdentity.ContentLink, IsLeafNode = false, ModelType = typeof(FolderPage) });
AddContentToCache(CreateFolder(mappedFolderIdentity, "F-J"));
// K-O
mappedFolderUri = MappedIdentity.ConstructExternalIdentifier(ProviderKey, "K-O");
mappedFolderIdentity = IdentityMappingService.Get(mappedFolderUri, true);
childrenList.Add(new GetChildrenReferenceResult { ContentLink = mappedFolderIdentity.ContentLink, IsLeafNode = false, ModelType = typeof(FolderPage) });
AddContentToCache(CreateFolder(mappedFolderIdentity, "K-O"));
// P-T
mappedFolderUri = MappedIdentity.ConstructExternalIdentifier(ProviderKey, "P-T");
mappedFolderIdentity = IdentityMappingService.Get(mappedFolderUri, true);
childrenList.Add(new GetChildrenReferenceResult { ContentLink = mappedFolderIdentity.ContentLink, IsLeafNode = false, ModelType = typeof(FolderPage) });
AddContentToCache(CreateFolder(mappedFolderIdentity, "P-T"));
// U-Z
mappedFolderUri = MappedIdentity.ConstructExternalIdentifier(ProviderKey, "U-Z");
mappedFolderIdentity = IdentityMappingService.Get(mappedFolderUri, true);
childrenList.Add(new GetChildrenReferenceResult { ContentLink = mappedFolderIdentity.ContentLink, IsLeafNode = false, ModelType = typeof(FolderPage) });
AddContentToCache(CreateFolder(mappedFolderIdentity, "U-Z"));
// Æ-Å
mappedFolderUri = MappedIdentity.ConstructExternalIdentifier(ProviderKey, "AE-AA");
mappedFolderIdentity = IdentityMappingService.Get(mappedFolderUri, true);
childrenList.Add(new GetChildrenReferenceResult { ContentLink = mappedFolderIdentity.ContentLink, IsLeafNode = false, ModelType = typeof(FolderPage) });
AddContentToCache(CreateFolder(mappedFolderIdentity, "Æ-Å"));
return childrenList;
}
MappedIdentity mappedIdentity = IdentityMappingService.Get(contentLink);
var employees = EmployeeService.FindAllInOrganization(OrganizationLevel.Level1, OrganizationId);
foreach (var employee in employees)
{
string folderSegment = mappedIdentity.ExternalIdentifier.Segments[1];
string employeeFolderSegment = GetFolderSegment(employee.Fornavn);
if (!employeeFolderSegment.Equals(folderSegment))
{
continue;
}
Uri uri = MappedIdentity.ConstructExternalIdentifier(ProviderKey, string.Format("{0}/{1}", folderSegment, employee.Brukerid));
MappedIdentity mappedChild = IdentityMappingService.Get(uri, true);
childrenList.Add(new GetChildrenReferenceResult
{
ContentLink = mappedChild.ContentLink,
IsLeafNode = true,
ModelType = typeof(EmployeePage)
});
AddContentToCache(CreateEmployeePage(mappedChild, employee));
}
return childrenList;
}
protected override IContent LoadContent(ContentReference contentLink, ILanguageSelector languageSelector)
{
MappedIdentity mappedItem = IdentityMappingService.Get(contentLink);
if (mappedItem == null)
{
return null;
}
switch (GetResourceType(mappedItem.ExternalIdentifier))
{
case EmployeeResourceType.Folder:
string folderName = mappedItem.ExternalIdentifier.LocalPath.Substring(1);
if (folderName.Equals("AE-AA"))
{
folderName = "Æ-Å";
}
return CreateFolder(mappedItem, folderName);
case EmployeeResourceType.Employee:
string employeeId = GetEmployeeId(mappedItem);
if (string.IsNullOrWhiteSpace(employeeId))
{
return null;
}
EsPerson employee = EmployeeService.Get(employeeId);
if (employee == null)
{
return null;
}
return CreateEmployeePage(mappedItem, employee);
}
return null;
}
protected virtual IContent CreateContentAndAssignIdentity(MappedIdentity mappedIdentity, Type modelType, string name)
{
ContentReference parentLink = EntryRoot;
if (modelType == typeof (EmployeePage))
{
var parentExternalIdentifier = MappedIdentity.ConstructExternalIdentifier(ProviderKey, VirtualPathUtility.RemoveTrailingSlash(mappedIdentity.ExternalIdentifier.Segments[1]));
var parentMappedIdentity = IdentityMappingService.Get(parentExternalIdentifier, false);
if (parentMappedIdentity != null)
{
parentLink = parentMappedIdentity.ContentLink;
}
}
ContentType contentType = ContentTypeRepository.Load(modelType);
IContent content = ContentFactory.CreateContent(contentType);
content.ContentTypeID = contentType.ID;
content.ParentLink = parentLink;
content.ContentGuid = mappedIdentity.ContentGuid;
content.ContentLink = mappedIdentity.ContentLink;
content.Name = name;
var routableContent = content as IRoutable;
if (routableContent != null)
{
routableContent.RouteSegment = UrlSegment.GetUrlFriendlySegment(content.Name).Replace(".", "");
}
var localizableContent = content as ILocalizable;
if (localizableContent != null)
{
localizableContent.Language = new CultureInfo("no");
localizableContent.MasterLanguage = new CultureInfo("no");
localizableContent.ExistingLanguages = new List<CultureInfo> {new CultureInfo("no")};
}
var securableContent = content as IContentSecurable;
if (securableContent != null)
{
securableContent.GetContentSecurityDescriptor().AddEntry(new AccessControlEntry(EveryoneRole.RoleName, AccessLevel.Read, SecurityEntityType.Role));
}
var now = DateTime.Now;
var created = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0);
var versionableContent = content as IVersionable;
if (versionableContent != null)
{
versionableContent.IsPendingPublish = false;
versionableContent.Status = VersionStatus.Published;
versionableContent.StartPublish = created;
}
var changeTrackableContent = content as IChangeTrackable;
if (changeTrackableContent != null)
{
changeTrackableContent.Created = created;
changeTrackableContent.Changed = created;
changeTrackableContent.Saved = created;
changeTrackableContent.CreatedBy = this.Name;
changeTrackableContent.ChangedBy = this.Name;
}
var page = content as PageData;
if (page != null)
{
page[MetaDataProperties.PageShortcutType] = PageShortcutType.Normal;
page[MetaDataProperties.PageVisibleInMenu] = false;
page[MetaDataProperties.PageLinkURL] = PermanentLinkUtility.GetPermanentLinkUrl(content.ContentGuid, ".aspx");
}
return content;
}
protected virtual IContent CreateFolder(MappedIdentity mappedIdentity, string name)
{
var folder = CreateContentAndAssignIdentity(mappedIdentity, typeof(FolderPage), name) as PageData;
folder[MetaDataProperties.PageChildOrderRule] = FilterSortOrder.Alphabetical;
return folder;
}
protected virtual IContent CreateEmployeePage(MappedIdentity mappedItem, EsPerson employee)
{
var employeePage = CreateContentAndAssignIdentity(mappedItem, typeof (EmployeePage), employee.Navn) as EmployeePage;
employeePage.EmployeeId = employee.Brukerid;
employeePage.FullName.FirstName = employee.Fornavn;
employeePage.FullName.LastName = employee.Etternavn;
employeePage.Email = employee.Epost;
employeePage.Title = employee.Tittel;
employeePage.PhoneNumber.Telephone = employee.Telefon;
employeePage.PhoneNumber.Mobile = employee.Mobil;
employeePage.Organization = employee.OrgNiva1Navn;
employeePage.RemoteImageUrl = employee.Bildesti + employee.Bilde;
return employeePage;
}
protected virtual string GetEmployeeId(MappedIdentity mappedItem)
{
return mappedItem.ExternalIdentifier.Segments[mappedItem.ExternalIdentifier.Segments.Length - 1];
}
protected virtual EmployeeResourceType GetResourceType(Uri externalIdentifier)
{
if (externalIdentifier.Segments.Length == 2)
{
return EmployeeResourceType.Folder;
}
if (externalIdentifier.Segments.Length == 3)
{
return EmployeeResourceType.Employee;
}
return EmployeeResourceType.Unknown;
}
protected virtual string GetFolderSegment(string employeeFirstName)
{
char firstChar = employeeFirstName.ToUpper()[0];
switch (firstChar)
{
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
return "A-E";
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
return "F-J";
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
return "K-O";
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
return "P-T";
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
return "U-Z";
case 'Æ':
case 'Ø':
case 'Å':
return "AE-AA";
}
return null;
}
protected override void SetCacheSettings(IContent content, CacheSettings cacheSettings)
{
base.SetCacheSettings(content, cacheSettings);
cacheSettings.CacheKeys.Clear();
cacheSettings.CacheKeys.Add(ProviderCacheKey);
cacheSettings.SlidingExpiration = Cache.NoSlidingExpiration;
cacheSettings.AbsoluteExpiration = DateTime.Now.AddMinutes(10);
}
protected override void SetCacheSettings(ContentReference contentReference, IEnumerable<GetChildrenReferenceResult> children, CacheSettings cacheSettings)
{
base.SetCacheSettings(contentReference, children, cacheSettings);
cacheSettings.CacheKeys.Clear();
cacheSettings.CacheKeys.Add(ProviderCacheKey);
cacheSettings.SlidingExpiration = Cache.NoSlidingExpiration;
cacheSettings.AbsoluteExpiration = DateTime.Now.AddMinutes(10);
}
}
[ContentType(DisplayName = "Employee")]
public class EmployeePage : PageData
{
[Editable(false)]
[Display(Name = "Id in external system")]
public virtual string EmployeeId { get; set; }
[Display(Name = "Name", Order = 19)]
public virtual NameBlock FullName { get; set; }
[Display(Name = "E-mail address", Order = 20)]
public virtual string Email { get; set; }
[Display(Name = "Role", Order = 30)]
[CultureSpecific]
public virtual string Title { get; set; }
[Display(Name = "Department", Order = 40)]
[CultureSpecific]
public virtual string Organization { get; set; }
[Display(Name = "Telefon", Order = 40)]
public virtual PhoneNumberBlock PhoneNumber {get;set;}
[Editable(false)]
[Display(Name = "Image in external system")]
public virtual string RemoteImageUrl { get; set; }
[Display(Name = "Twitter username", Order = 90)]
public virtual string TwitterUserName { get; set; }
[Display(Name = "Blog link", Order = 100)]
public virtual Url BlogUrl { get; set; }
[Display(Name = "Main body", Order = 110)]
[CultureSpecific]
public virtual XhtmlString MainBody { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment