text:
using Alchemy4Tridion.Plugins.Clients.CoreService;
using System;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Xml.Linq;
using Alchemy4Tridion.Plugins.Utilities;

namespace Alchemy4Tridion.Plugins.Clients
{
    /// <summary>
    /// Provides a wrapper of the Tridion SessionAwareCoreServiceClient using a safe implementation of IDisposable.
    /// You can use this client inside of a using statement, and calling its Close or Dispose method will safely
    /// close out the
    /// </summary>
    /// <remarks>
    /// Shoutout to Eric Huiza (http://erichuiza.blogspot.com/2013/12/how-to-correctly-dispose-coreservice.html)
    /// </remarks>
    public class AlchemyCoreServiceClient201603 : IDisposable, IAlchemyCoreServiceClient
    {
        /// <summary>
        /// Whether or not the dispose method has already been called.
        /// </summary>
        private bool isDisposed = false;

        /// <summary>
        /// Gets which entpoint was used to create this core service client.
        /// </summary>
        public SessionAwareCoreServiceEndPoint? ClientEndpoint
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the name of the user that the client was last impersonated with using the Impersonate method.
        /// </summary>
        public string ImpersonatedUsername
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets or sets the underlying core service client channel.
        /// </summary>
        public ISessionAwareCoreService201603 Channel { get; set; }

        /// <summary>
        /// Creates a new SessionAwareCoreServiceClient using the supplied endpoint.
        /// </summary>
        /// <param name="endPoint"></param>
        public AlchemyCoreServiceClient201603()
        {
            ClientEndpoint = SessionAwareCoreServiceEndPoint.NetTcp201603;
            string endPointConfigName = CoreServiceUtils.GetEndpointConfigurationName(ClientEndpoint ?? SessionAwareCoreServiceEndPoint.NetTcp201603);

            var factory = new ChannelFactory<ISessionAwareCoreService201603>(endPointConfigName);
            Channel = factory.CreateChannel();
        }

        /// <summary>
        /// Calls the dispose method.
        /// </summary>
        public void Close()
        {
            Dispose(true);
        }

        /// <summary>
        /// Implements IDisposable and disposes of the WCF client in a safe manner.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
        }

        /// <summary>
        /// Disposes the client in a safe manner.
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (Channel != null)
            {
                IClientChannel channel = (IClientChannel)Channel;
                if (!this.isDisposed && disposing)
                {
                    this.isDisposed = true;

                    try
                    {
                        if (channel.State != CommunicationState.Closed)
                        {
                            channel.Close();
                        }
                    }
                    catch (CommunicationException ex)
                    {
                        channel.Abort(); //The channel is aborted and the resources released.
                    }
                    catch (TimeoutException ex)
                    {
                        channel.Abort(); //The channel is aborted and the resources released.
                    }
                    catch (Exception ex)
                    {
                        channel.Abort(); // The channel is aborted and the resources released.
                    }
                }
            }
        }

        public UserData Impersonate(string username)
        {
            ImpersonatedUsername = username;

            return Channel.Impersonate(username);
        }

        public async Task<UserData> ImpersonateAsync(string username)
        {
            ImpersonatedUsername = username;
            return await Task<UserData>.Factory.FromAsync(Channel.BeginImpersonate, Channel.EndImpersonate, username, null);
        }

        public TridionEnumValue[] GetEnumValues(string type)
        {
            return Channel.GetEnumValues(type);
        }

        public async Task<TridionEnumValue[]> GetEnumValuesAsync(string type)
        {
            return await Task<TridionEnumValue[]>.Factory.FromAsync(Channel.BeginGetEnumValues, Channel.EndGetEnumValues, type, null);
        }

        public BundleTypeData[] ResolveBundleTypes(string[] itemIds, bool pruneTree)
        {
            return Channel.ResolveBundleTypes(itemIds, pruneTree);
        }

        public async Task<BundleTypeData[]> ResolveBundleTypesAsync(string[] itemIds, bool pruneTree)
        {
            return await Task<BundleTypeData[]>.Factory.FromAsync(Channel.BeginResolveBundleTypes, Channel.EndResolveBundleTypes, itemIds, pruneTree, null);
        }

        public KeywordData CopyToKeyword(string sourceKeywordId, string destinationId, ReadOptions readBackOptions)
        {
            return Channel.CopyToKeyword(sourceKeywordId, destinationId, readBackOptions);
        }

        public async Task<KeywordData> CopyToKeywordAsync(string sourceKeywordId, string destinationId, ReadOptions readBackOptions)
        {
            return await Task<KeywordData>.Factory.FromAsync(Channel.BeginCopyToKeyword, Channel.EndCopyToKeyword, sourceKeywordId, destinationId, readBackOptions, null);
        }

        public KeywordData MoveToKeyword(string sourceKeywordId, string destinationId, ReadOptions readBackOptions)
        {
            return Channel.MoveToKeyword(sourceKeywordId, destinationId, readBackOptions);
        }

        public async Task<KeywordData> MoveToKeywordAsync(string sourceKeywordId, string destinationId, ReadOptions readBackOptions)
        {
            return await Task<KeywordData>.Factory.FromAsync(Channel.BeginMoveToKeyword, Channel.EndMoveToKeyword, sourceKeywordId, destinationId, readBackOptions, null);
        }

        public TridionLanguageInfo[] GetTridionLanguages()
        {
            return Channel.GetTridionLanguages();
        }

        public async Task<TridionLanguageInfo[]> GetTridionLanguagesAsync()
        {
            return await Task<TridionLanguageInfo[]>.Factory.FromAsync(Channel.BeginGetTridionLanguages, Channel.EndGetTridionLanguages, null);
        }

        public WorkflowScriptType[] GetListWorkflowScriptTypes()
        {
            return Channel.GetListWorkflowScriptTypes();
        }

        public async Task<WorkflowScriptType[]> GetListWorkflowScriptTypesAsync()
        {
            return await Task<WorkflowScriptType[]>.Factory.FromAsync(Channel.BeginGetListWorkflowScriptTypes, Channel.EndGetListWorkflowScriptTypes, null);
        }

        public WorkItemData[] AddToWorkflow(string[] subjectIds, string activityInstanceId, ReadOptions readBackOptions)
        {
            return Channel.AddToWorkflow(subjectIds, activityInstanceId, readBackOptions);
        }

        public async Task<WorkItemData[]> AddToWorkflowAsync(string[] subjectIds, string activityInstanceId, ReadOptions readBackOptions)
        {
            return await Task<WorkItemData[]>.Factory.FromAsync(Channel.BeginAddToWorkflow, Channel.EndAddToWorkflow, subjectIds, activityInstanceId, readBackOptions, null);
        }

        public WorkItemData[] RemoveFromWorkflow(string[] subjectIds, ReadOptions readBackOptions)
        {
            return Channel.RemoveFromWorkflow(subjectIds, readBackOptions);
        }

        public async Task<WorkItemData[]> RemoveFromWorkflowAsync(string[] subjectIds, ReadOptions readBackOptions)
        {
            return await Task<WorkItemData[]>.Factory.FromAsync(Channel.BeginRemoveFromWorkflow, Channel.EndRemoveFromWorkflow, subjectIds, readBackOptions, null);
        }

        public OrganizationalItemData Lock(string organizationalItemId, ReadOptions readBackOptions)
        {
            return Channel.Lock(organizationalItemId, readBackOptions);
        }

        public async Task<OrganizationalItemData> LockAsync(string organizationalItemId, ReadOptions readBackOptions)
        {
            return await Task<OrganizationalItemData>.Factory.FromAsync(Channel.BeginLock, Channel.EndLock, organizationalItemId, readBackOptions, null);
        }

        public OrganizationalItemData Unlock(string organizationalItemId, ReadOptions readBackOptions)
        {
            return Channel.Unlock(organizationalItemId, readBackOptions);
        }

        public async Task<OrganizationalItemData> UnlockAsync(string organizationalItemId, ReadOptions readBackOptions)
        {
            return await Task<OrganizationalItemData>.Factory.FromAsync(Channel.BeginUnlock, Channel.EndLock, organizationalItemId, readBackOptions, null);
        }

        public ProcessInstanceData StartWorkflow(string repositoryId, StartWorkflowInstructionData instruction, ReadOptions readBackOptions)
        {
            return Channel.StartWorkflow(repositoryId, instruction, readBackOptions);
        }

        public async Task<ProcessInstanceData> StartWorkflowAsync(string repositoryId, StartWorkflowInstructionData instruction, ReadOptions readBackOptions)
        {
            return await Task<ProcessInstanceData>.Factory.FromAsync(Channel.BeginStartWorkflow, Channel.EndStartWorkflow, repositoryId, instruction, readBackOptions, null);
        }

        public ProcessDefinitionAssociationDictionary GetProcessDefinitionsForItems(string[] itemIds, ProcessDefinitionType processDefinitionType)
        {
            return Channel.GetProcessDefinitionsForItems(itemIds, processDefinitionType);
        }

        public async Task<ProcessDefinitionAssociationDictionary> GetProcessDefinitionsForItemsAsync(string[] itemIds, ProcessDefinitionType processDefinitionType)
        {
            return await Task<ProcessDefinitionAssociationDictionary>.Factory.FromAsync(Channel.BeginGetProcessDefinitionsForItems, Channel.EndGetProcessDefinitionsForItems, itemIds, processDefinitionType, null);
        }

        public string GetSystemXsd(string filename)
        {
            return Channel.GetSystemXsd(filename);
        }

        public async Task<string> GetSystemXsdAsync(string filename)
        {
            return await Task<string>.Factory.FromAsync(Channel.BeginGetSystemXsd, Channel.EndGetSystemXsd, filename, null);
        }

        public LinkToSchemaData[] GetSchemasByNamespaceUri(string repositoryId, string namespaceUri, string rootElementName)
        {
            return Channel.GetSchemasByNamespaceUri(repositoryId, namespaceUri, rootElementName);
        }

        public async Task<LinkToSchemaData[]> GetSchemasByNamespaceUriAsync(string repositoryId, string namespaceUri, string rootElementName)
        {
            return await Task<LinkToSchemaData[]>.Factory.FromAsync(Channel.BeginGetSchemasByNamespaceUri, Channel.EndGetSchemasByNamespaceUri, repositoryId, namespaceUri, rootElementName, null);
        }

        public ValidationErrorData[] Validate(IdentifiableObjectData deltaData)
        {
            return Channel.Validate(deltaData);
        }

        public async Task<ValidationErrorData[]> ValidateAsync(IdentifiableObjectData deltaData)
        {
            return await Task<ValidationErrorData[]>.Factory.FromAsync(Channel.BeginValidate, Channel.EndValidate, deltaData, null);
        }

        public BinaryContentData GetExternalBinaryContentData(string uri)
        {
            return Channel.GetExternalBinaryContentData(uri);
        }

        public async Task<BinaryContentData> GetExternalBinaryContentDataAsync(string uri)
        {
            return await Task<BinaryContentData>.Factory.FromAsync(Channel.BeginGetExternalBinaryContentData, Channel.EndGetExternalBinaryContentData, uri, null);
        }

        public SynchronizationResult SynchronizeWithSchema(IdentifiableObjectData dataObject, SynchronizeOptions synchronizeOptions)
        {
            return Channel.SynchronizeWithSchema(dataObject, synchronizeOptions);
        }

        public async Task<SynchronizationResult> SynchronizeWithSchemaAsync(IdentifiableObjectData dataObject, SynchronizeOptions synchronizeOptions)
        {
            return await Task<SynchronizationResult>.Factory.FromAsync(Channel.BeginSynchronizeWithSchema, Channel.EndSynchronizeWithSchema, dataObject, synchronizeOptions, null);
        }

        public SynchronizationResult SynchronizeWithSchemaAndUpdate(string itemId, SynchronizeOptions synchronizeOptions)
        {
            return Channel.SynchronizeWithSchemaAndUpdate(itemId, synchronizeOptions);
        }

        public async Task<SynchronizationResult> SynchronizeWithSchemaAndUpdateAsync(string itemId, SynchronizeOptions synchronizeOptions)
        {
            return await Task<SynchronizationResult>.Factory.FromAsync(Channel.BeginSynchronizeWithSchemaAndUpdate, Channel.EndSynchronizeWithSchemaAndUpdate, itemId, synchronizeOptions, null);
        }

        public void DecommissionPublicationTarget(string publicationTargetId)
        {
            Channel.DecommissionPublicationTarget(publicationTargetId);
        }

        public async Task DecommissionPublicationTargetAsync(string publicationTargetId)
        {
            await Task.Factory.FromAsync(Channel.BeginDecommissionPublicationTarget(publicationTargetId, null, null), Channel.EndDecommissionPublicationTarget);
        }

        public SchemaFieldsData ConvertXsdToSchemaFields(XElement xsd, SchemaPurpose purpose, string rootElementName, ReadOptions readOptions)
        {
            return Channel.ConvertXsdToSchemaFields(xsd, purpose, rootElementName, readOptions);
        }

        public async Task<SchemaFieldsData> ConvertXsdToSchemaFieldsAsync(XElement xsd, SchemaPurpose purpose, string rootElementName, ReadOptions readOptions)
        {
            return await Task<SchemaFieldsData>.Factory.FromAsync(Channel.BeginConvertXsdToSchemaFields(xsd, purpose, rootElementName, readOptions, null, null), Channel.EndConvertXsdToSchemaFields);
        }

        public string GetPublishUrl(string id, string targetTypeIdOrPurpose)
        {
            return Channel.GetPublishUrl(id, targetTypeIdOrPurpose);
        }

        public async Task<string> GetPublishUrlAsync(string id, string targetTypeIdOrPurpose)
        {
            return await Task<string>.Factory.FromAsync(Channel.BeginGetPublishUrl, Channel.EndGetPublishUrl, id, targetTypeIdOrPurpose, null);
        }

        public LinkToBusinessProcessTypeData[] GetBusinessProcessTypes(string cdTopologyTypeId)
        {
            return Channel.GetBusinessProcessTypes(cdTopologyTypeId);
        }

        public async Task<LinkToBusinessProcessTypeData[]> GetBusinessProcessTypesAsync(string cdTopologyTypeId)
        {
            return await Task<LinkToBusinessProcessTypeData[]>.Factory.FromAsync(Channel.BeginGetBusinessProcessTypes, Channel.EndGetBusinessProcessTypes, cdTopologyTypeId, null);
        }

        public PublishSourceData GetPublishSourceByUrl(string url)
        {
            return Channel.GetPublishSourceByUrl(url);
        }

        public async Task<PublishSourceData> GetPublishSourceByUrlAsync(string url)
        {
            return await Task<PublishSourceData>.Factory.FromAsync(Channel.BeginGetPublishSourceByUrl, Channel.EndGetPublishSourceByUrl, url, null);
        }

        public void RemovePublishStates(string publicationId, string targetTypeIdOrPurpose)
        {
            Channel.RemovePublishStates(publicationId, targetTypeIdOrPurpose);
        }

        public async Task RemovePublishStatesAsync(string publicationId, string targetTypeIdOrPurpose)
        {
            await Task.Factory.FromAsync(Channel.BeginRemovePublishStates, Channel.EndRemovePublishStates, publicationId, targetTypeIdOrPurpose, null);
        }

        public ContainingPagesDictionary ResolveContainingPages(string componentId, ResolveContainingPagesInstructionData instruction)
        {
            return Channel.ResolveContainingPages(componentId, instruction);
        }

        public async Task<ContainingPagesDictionary> ResolveContainingPagesAsync(string componentId, ResolveContainingPagesInstructionData instruction)
        {
            return await Task<ContainingPagesDictionary>.Factory.FromAsync(Channel.BeginResolveContainingPages, Channel.EndResolveContainingPages, componentId, instruction, null);
        }

        public void PurgeWorkflowHistory(PurgeWorkflowHistoryInstructionData instruction)
        {
            Channel.PurgeWorkflowHistory(instruction);
        }

        public async Task PurgeWorkflowHistoryAsync(PurgeWorkflowHistoryInstructionData instruction)
        {
            await Task.Factory.FromAsync(Channel.BeginPurgeWorkflowHistory, Channel.EndPurgeWorkflowHistory, instruction, null);
        }

        public void BroadcastNotification(NotificationMessage notification)
        {
            Channel.BroadcastNotification(notification);
        }

        public async Task BroadcastNotificationAsync(NotificationMessage notification)
        {
            await Task.Factory.FromAsync(Channel.BeginBroadcastNotification, Channel.EndBroadcastNotification, notification, null);
        }

        public SystemPrivilege[] GetSystemPrivileges()
        {
            return Channel.GetSystemPrivileges();
        }

        public async Task<SystemPrivilege[]> GetSystemPrivilegesAsync()
        {
            return await Task.Factory.FromAsync(Channel.BeginGetSystemPrivileges, Channel.EndGetSystemPrivileges, null);
        }

        public string GetApiVersion()
        {
            return Channel.GetApiVersion();
        }

        public async Task<string> GetApiVersionAsync()
        {
            return await Task<string>.Factory.FromAsync(Channel.BeginGetApiVersion, Channel.EndGetApiVersion, null);
        }

        public AccessTokenData GetCurrentUserWithToken()
        {
            return Channel.GetCurrentUser();
        }

        public async Task<AccessTokenData> GetCurrentUserWithTokenAsync()
        {
            return await Task<AccessTokenData>.Factory.FromAsync(Channel.BeginGetCurrentUser, Channel.EndGetCurrentUser, null);
        }

        public bool IsExistingObject(string id)
        {
            return Channel.IsExistingObject(id);
        }

        public async Task<bool> IsExistingObjectAsync(string id)
        {
            return await Task<bool>.Factory.FromAsync(Channel.BeginIsExistingObject, Channel.EndIsExistingObject, id, null);
        }

        public string GetTcmUri(string baseUri, string contextRepositoryUri, uint? version)
        {
            return Channel.GetTcmUri(baseUri, contextRepositoryUri, version);
        }

        public async Task<string> GetTcmUriAsync(string baseUri, string contextRepositoryUri, uint? version = null)
        {
            return await Task<string>.Factory.FromAsync(Channel.BeginGetTcmUri, Channel.EndGetTcmUri, baseUri, contextRepositoryUri, version, null);
        }

        public string TryGetTcmUri(string baseUri, string contextRepositoryUri, uint? version)
        {
            return Channel.TryGetTcmUri(baseUri, contextRepositoryUri, version);
        }

        public async Task<string> TryGetTcmUriAsync(string baseUri, string contextRepositoryUri, uint? version = null)
        {
            return await Task<string>.Factory.FromAsync(Channel.BeginTryGetTcmUri, Channel.EndTryGetTcmUri, baseUri, contextRepositoryUri, version, null);
        }

        public IdentifiableObjectData Read(string id)
        {
            return Channel.Read(id, null);
        }

        public IdentifiableObjectData Read(string id, ReadOptions readOptions)
        {
            return Channel.Read(id, readOptions);
        }

        public async Task<IdentifiableObjectData> ReadAsync(string id, ReadOptions readOptions = null)
        {
            return await Task<IdentifiableObjectData>.Factory.FromAsync(Channel.BeginRead, Channel.EndRead, id, readOptions, null);
        }

        public IdentifiableObjectData TryRead(string id, ReadOptions readOptions = null)
        {
            return Channel.TryRead(id, readOptions);
        }

        public async Task<IdentifiableObjectData> TryReadAsync(string id, ReadOptions readOptions = null)
        {
            return await Task<IdentifiableObjectData>.Factory.FromAsync(Channel.BeginTryRead, Channel.EndTryRead, id, readOptions, null);
        }

        public ItemReadResultDictionary BulkRead(string[] ids, ReadOptions readOptions)
        {
            return Channel.BulkRead(ids, readOptions);
        }

        public async Task<ItemReadResultDictionary> BulkReadAsync(string[] ids, ReadOptions readOptions)
        {
            return await Task<ItemReadResultDictionary>.Factory.FromAsync(Channel.BeginBulkRead, Channel.EndBulkRead, ids, readOptions, null);
        }

        public SchemaFieldsData ReadSchemaFields(string schemaId, bool expandEmbeddedFields, ReadOptions readOptions)
        {
            return Channel.ReadSchemaFields(schemaId, expandEmbeddedFields, readOptions);
        }

        public async Task<SchemaFieldsData> ReadSchemaFieldsAsync(string schemaId, bool expandEmbeddedFields, ReadOptions readOptions = null)
        {
            return await Task<SchemaFieldsData>.Factory.FromAsync(Channel.BeginReadSchemaFields, Channel.EndReadSchemaFields, schemaId, expandEmbeddedFields, readOptions, null);
        }

        public XElement ConvertSchemaFieldsToXsd(SchemaFieldsData schemaFieldsData)
        {
            return Channel.ConvertSchemaFieldsToXsd(schemaFieldsData);
        }

        public async Task<XElement> ConvertSchemaFieldsToXsdAsync(SchemaFieldsData schemaFieldsData)
        {
            return await Task<XElement>.Factory.FromAsync(Channel.BeginConvertSchemaFieldsToXsd, Channel.EndConvertSchemaFieldsToXsd, schemaFieldsData, null);
        }

        public IdentifiableObjectData Save(IdentifiableObjectData deltaData, ReadOptions readBackOptions)
        {
            return Channel.Save(deltaData, readBackOptions);
        }

        public async Task<IdentifiableObjectData> SaveAsync(IdentifiableObjectData deltaData, ReadOptions readBackOptions = null)
        {
            return await Task<IdentifiableObjectData>.Factory.FromAsync(Channel.BeginSave, Channel.EndSave, deltaData, readBackOptions, null);
        }

        public void Delete(string id)
        {
            Channel.Delete(id);
        }

        public async Task DeleteAsync(string id)
        {
            await Task.Factory.FromAsync(Channel.BeginDelete, Channel.EndDelete, id, null);
        }

        public void DeleteTaxonomyNode(string id, DeleteTaxonomyNodeMode deleteTaxonomyNodeMode)
        {
            Channel.DeleteTaxonomyNode(id, deleteTaxonomyNodeMode);
        }

        public async Task DeleteTaxonomyNodeAsync(string id, DeleteTaxonomyNodeMode deleteTaxonomyNodeMode)
        {
            await Task.Factory.FromAsync(Channel.BeginDeleteTaxonomyNode, Channel.EndDeleteTaxonomyNode, id, deleteTaxonomyNodeMode, null);
        }

        public IdentifiableObjectData GetDefaultData(ItemType itemType, string containerId, ReadOptions readOptions)
        {
            return Channel.GetDefaultData(itemType, containerId, readOptions);
        }

        public async Task<IdentifiableObjectData> GetDefaultDataAsync(ItemType itemType, string containerId, ReadOptions readOptions = null)
        {
            return await Task<IdentifiableObjectData>.Factory.FromAsync(Channel.BeginGetDefaultData, Channel.EndGetDefaultData, itemType, containerId, readOptions, null);
        }

        public RepositoryLocalObjectData Move(string id, string destinationId, ReadOptions readBackOptions)
        {
            return Channel.Move(id, destinationId, readBackOptions);
        }

        public async Task<RepositoryLocalObjectData> MoveAsync(string id, string destinationId, ReadOptions readBackOptions = null)
        {
            return await Task<RepositoryLocalObjectData>.Factory.FromAsync(Channel.BeginMove, Channel.EndMove, id, destinationId, readBackOptions, null);
        }

        public RepositoryLocalObjectData Copy(string id, string destinationId, bool makeUnique, ReadOptions readBackOptions)
        {
            return Channel.Copy(id, destinationId, makeUnique, readBackOptions);
        }

        public async Task<RepositoryLocalObjectData> CopyAsync(string id, string destinationId, bool makeUnique, ReadOptions readBackOptions = null)
        {
            return await Task<RepositoryLocalObjectData>.Factory.FromAsync(Channel.BeginCopy(id, destinationId, makeUnique, readBackOptions, null, null), Channel.EndCopy);
        }

        public InstanceData GetInstanceData(string schemaId, string containerItemId, ReadOptions readOptions)
        {
            return Channel.GetInstanceData(schemaId, containerItemId, readOptions);
        }

        public async Task<InstanceData> GetInstanceDataAsync(string schemaId, string containerItemId, ReadOptions readOptions = null)
        {
            return await Task<InstanceData>.Factory.FromAsync(Channel.BeginGetInstanceData, Channel.EndGetInstanceData, schemaId, containerItemId, readOptions, null);
        }

        public IdentifiableObjectData TryCheckOut(string id, ReadOptions readBackOptions)
        {
            return Channel.TryCheckOut(id, readBackOptions);
        }

        public async Task<IdentifiableObjectData> TryCheckOutAsync(string id, ReadOptions readBackOptions = null)
        {
            return await Task<IdentifiableObjectData>.Factory.FromAsync(Channel.BeginTryCheckOut, Channel.EndTryCheckOut, id, readBackOptions, null);
        }

        public VersionedItemData CheckOut(string id, bool permanentLock, ReadOptions readBackOptions)
        {
            return Channel.CheckOut(id, permanentLock, readBackOptions);
        }

        public async Task<VersionedItemData> CheckOutAsync(string id, bool permanentLock = false, ReadOptions readBackOptions = null)
        {
            return await Task<VersionedItemData>.Factory.FromAsync(Channel.BeginCheckOut, Channel.EndCheckOut, id, permanentLock, readBackOptions, null);
        }

        public VersionedItemData CheckIn(string id, bool removePermanentLock, string userComment, ReadOptions readBackOptions)
        {
            return Channel.CheckIn(id, removePermanentLock, userComment, readBackOptions);
        }

        public async Task<VersionedItemData> CheckInAsync(string id, bool removePermanentLock, string userComment, ReadOptions readBackOptions = null)
        {
            return await Task<VersionedItemData>.Factory.FromAsync(Channel.BeginCheckIn(id, removePermanentLock, userComment, readBackOptions, null, null), Channel.EndCheckIn);
        }

        public IdentifiableObjectData Update(IdentifiableObjectData deltaData, ReadOptions readBackOptions)
        {
            return Channel.Update(deltaData, readBackOptions);
        }

        public async Task<IdentifiableObjectData> UpdateAsync(IdentifiableObjectData deltaData, ReadOptions readBackOptions = null)
        {
            return await Task<IdentifiableObjectData>.Factory.FromAsync(Channel.BeginUpdate, Channel.EndUpdate, deltaData, readBackOptions, null);
        }

        public IdentifiableObjectData Create(IdentifiableObjectData data, ReadOptions readBackOptions)
        {
            return Channel.Create(data, readBackOptions);
        }

        public async Task<IdentifiableObjectData> CreateAsync(IdentifiableObjectData data, ReadOptions readBackOptions = null)
        {
            return await Task<IdentifiableObjectData>.Factory.FromAsync(Channel.BeginCreate, Channel.EndCreate, data, readBackOptions, null);
        }

        public VersionedItemData UndoCheckOut(string id, bool permanentLock, ReadOptions readBackOptions)
        {
            return Channel.UndoCheckOut(id, permanentLock, readBackOptions);
        }

        public async Task<VersionedItemData> UndoCheckOutAsync(string id, bool permanentLock, ReadOptions readBackOptions = null)
        {
            return await Task<VersionedItemData>.Factory.FromAsync(Channel.BeginUndoCheckOut, Channel.EndUndoCheckOut, id, permanentLock, readBackOptions, null);
        }

        public VersionedItemData Rollback(string id, bool deleteVersions, string comment, ReadOptions readBackOptions)
        {
            return Channel.Rollback(id, deleteVersions, comment, readBackOptions);
        }

        public async Task<VersionedItemData> RollbackAsync(string id, bool deleteVersions, string comment, ReadOptions readBackOptions = null)
        {
            return await Task<VersionedItemData>.Factory.FromAsync(Channel.BeginRollback(id, deleteVersions, comment, readBackOptions, null, null), Channel.EndRollback);
        }

        public RepositoryLocalObjectData Localize(string id, ReadOptions readBackOptions)
        {
            return Channel.Localize(id, readBackOptions);
        }

        public async Task<RepositoryLocalObjectData> LocalizeAsync(string id, ReadOptions readBackOptions = null)
        {
            return await Task<RepositoryLocalObjectData>.Factory.FromAsync(Channel.BeginLocalize, Channel.EndLocalize, id, readBackOptions, null);
        }

        public RepositoryLocalObjectData UnLocalize(string id, ReadOptions readBackOptions)
        {
            return Channel.UnLocalize(id, readBackOptions);
        }

        public async Task<RepositoryLocalObjectData> UnLocalizeAsync(string id, ReadOptions readBackOptions = null)
        {
            return await Task<RepositoryLocalObjectData>.Factory.FromAsync(Channel.BeginUnLocalize, Channel.EndUnLocalize, id, readBackOptions, null);
        }

        public OperationResultDataOfRepositoryLocalObjectData Promote(string id, string destinationRepositoryId, OperationInstruction instruction, ReadOptions readBackOptions)
        {
            return Channel.Promote(id, destinationRepositoryId, instruction, readBackOptions);
        }

        public async Task<OperationResultDataOfRepositoryLocalObjectData> PromoteAsync(string id, string destinationRepositoryId, OperationInstruction instruction, ReadOptions readBackOptions)
        {
            return await Task<OperationResultDataOfRepositoryLocalObjectData>.Factory.FromAsync(Channel.BeginPromote(id, destinationRepositoryId, instruction, readBackOptions, null, null), Channel.EndPromote);
        }

        public OperationResultDataOfRepositoryLocalObjectData Demote(string id, string destinationRepositoryId, OperationInstruction instruction, ReadOptions readBackOptions)
        {
            return Channel.Demote(id, destinationRepositoryId, instruction, readBackOptions);
        }

        public async Task<OperationResultDataOfRepositoryLocalObjectData> DemoteAsync(string id, string destinationRepositoryId, OperationInstruction instruction, ReadOptions readBackOptions)
        {
            return await Task<OperationResultDataOfRepositoryLocalObjectData>.Factory.FromAsync(Channel.BeginDemote(id, destinationRepositoryId, instruction, readBackOptions, null, null), Channel.EndDemote);
        }

        public TemplateType[] GetListTemplateTypes(ItemType? itemType)
        {
            return Channel.GetListTemplateTypes(itemType);
        }

        public async Task<TemplateType[]> GetListTemplateTypesAsync(ItemType? itemType = null)
        {
            return await Task<TemplateType[]>.Factory.FromAsync(Channel.BeginGetListTemplateTypes, Channel.EndGetListTemplateTypes, itemType, null);
        }

        public PublicationType[] GetListPublicationTypes()
        {
            return Channel.GetListPublicationTypes();
        }

        public async Task<PublicationType[]> GetListPublicationTypesAsync()
        {
            return await Task<PublicationType[]>.Factory.FromAsync(Channel.BeginGetListPublicationTypes, Channel.EndGetListPublicationTypes, null);
        }

        public XElement GetSystemWideListXml(SystemWideListFilterData filter)
        {
            return Channel.GetSystemWideListXml(filter);
        }

        public async Task<XElement> GetSystemWideListXmlAsync(SystemWideListFilterData filter)
        {
            return await Task<XElement>.Factory.FromAsync(Channel.BeginGetSystemWideListXml, Channel.EndGetSystemWideListXml, filter, null);
        }

        public IdentifiableObjectData[] GetSystemWideList(SystemWideListFilterData filter)
        {
            return Channel.GetSystemWideList(filter);
        }

        public async Task<IdentifiableObjectData[]> GetSystemWideListAsync(SystemWideListFilterData filter)
        {
            return await Task<IdentifiableObjectData[]>.Factory.FromAsync(Channel.BeginGetSystemWideList, Channel.EndGetSystemWideList, filter, null);
        }

        public XElement GetListXml(string id, SubjectRelatedListFilterData filter)
        {
            return Channel.GetListXml(id, filter);
        }

        public async Task<XElement> GetListXmlAsync(string id, SubjectRelatedListFilterData filter)
        {
            return await Task<XElement>.Factory.FromAsync(Channel.BeginGetListXml, Channel.EndGetListXml, id, filter, null);
        }

        public IdentifiableObjectData[] GetList(string id, SubjectRelatedListFilterData filter)
        {
            return Channel.GetList(id, filter);
        }

        public async Task<IdentifiableObjectData[]> GetListAsync(string id, SubjectRelatedListFilterData filter)
        {
            return await Task<IdentifiableObjectData[]>.Factory.FromAsync(Channel.BeginGetList, Channel.EndGetList, id, filter, null);
        }

        public string[] GetListDirectoryServiceNames()
        {
            return Channel.GetListDirectoryServiceNames();
        }

        public async Task<string[]> GetListDirectoryServiceNamesAsync()
        {
            return await Task<string[]>.Factory.FromAsync(Channel.BeginGetListDirectoryServiceNames, Channel.EndGetListDirectoryServiceNames, null);
        }

        public WindowsUser[] GetListWindowsDomainUsers(string domainName)
        {
            return Channel.GetListWindowsDomainUsers(domainName);
        }

        public async Task<WindowsUser[]> GetListWindowsDomainUsersAsync(string domainName)
        {
            return await Task<WindowsUser[]>.Factory.FromAsync(Channel.BeginGetListWindowsDomainUsers, Channel.EndGetListWindowsDomainUsers, domainName, null);
        }

        public DirectoryServiceUser[] GetListDirectoryServiceAllUsers(string directoryServiceName)
        {
            return Channel.GetListDirectoryServiceAllUsers(directoryServiceName);
        }

        public async Task<DirectoryServiceUser[]> GetListDirectoryServiceAllUsersAsync(string directoryServiceName)
        {
            return await Task<DirectoryServiceUser[]>.Factory.FromAsync(Channel.BeginGetListDirectoryServiceAllUsers, Channel.EndGetListDirectoryServiceAllUsers, directoryServiceName, null);
        }

        public DirectoryServiceUser[] GetListDirectoryServiceGroupMembers(string directoryServiceName, string groupDN)
        {
            return Channel.GetListDirectoryServiceGroupMembers(directoryServiceName, groupDN);
        }

        public async Task<DirectoryServiceUser[]> GetListDirectoryServiceGroupMembersAsync(string directoryServiceName, string groupDN)
        {
            return await Task<DirectoryServiceUser[]>.Factory.FromAsync(Channel.BeginGetListDirectoryServiceGroupMembers, Channel.EndGetListDirectoryServiceGroupMembers, directoryServiceName, groupDN, null);
        }

        public DirectoryServiceUser[] GetListDirectoryServiceUsers(string directoryServiceName, DirectoryUsersFilter filter)
        {
            return Channel.GetListDirectoryServiceUsers(directoryServiceName, filter);
        }

        public async Task<DirectoryServiceUser[]> GetListDirectoryServiceUsersAsync(string directoryServiceName, DirectoryUsersFilter filter)
        {
            return await Task<DirectoryServiceUser[]>.Factory.FromAsync(Channel.BeginGetListDirectoryServiceUsers, Channel.EndGetListDirectoryServiceUsers, directoryServiceName, filter, null);
        }

        public ClassificationInfoData Classify(string id, string[] keywordIds, ReadOptions readOptions)
        {
            return Channel.Classify(id, keywordIds, readOptions);
        }

        public async Task<ClassificationInfoData> ClassifyAsync(string id, string[] keywordIds, ReadOptions readOptions = null)
        {
            return await Task<ClassificationInfoData>.Factory.FromAsync(Channel.BeginClassify, Channel.EndClassify, id, keywordIds, readOptions, null);
        }

        public ClassificationInfoData UnClassify(string id, string[] keywordIds, ReadOptions readOptions)
        {
            return Channel.UnClassify(id, keywordIds, readOptions);
        }

        public async Task<ClassificationInfoData> UnClassifyAsync(string id, string[] keywordIds, ReadOptions readOptions = null)
        {
            return await Task<ClassificationInfoData>.Factory.FromAsync(Channel.BeginUnClassify, Channel.EndUnClassify, id, keywordIds, readOptions, null);
        }

        public ClassificationInfoData ReClassify(string id, string[] keywordIdsToRemove, string[] keywordIdsToAdd, ReadOptions readOptions)
        {
            return Channel.ReClassify(id, keywordIdsToRemove, keywordIdsToAdd, readOptions);
        }

        public async Task<ClassificationInfoData> ReClassifyAsync(string id, string[] keywordIdsToRemove, string[] keywordIdsToAdd, ReadOptions readOptions)
        {
            return await Task<ClassificationInfoData>.Factory.FromAsync(Channel.BeginReClassify(id, keywordIdsToRemove, keywordIdsToAdd, readOptions, null, null), Channel.EndReClassify);
        }

        public ActivityInstanceData StartActivity(string activityInstanceId, ReadOptions readBackOptions)
        {
            return Channel.StartActivity(activityInstanceId, readBackOptions);
        }

        public async Task<ActivityInstanceData> StartActivityAsync(string activityInstanceId, ReadOptions readBackOptions = null)
        {
            return await Task<ActivityInstanceData>.Factory.FromAsync(Channel.BeginStartActivity, Channel.EndStartActivity, activityInstanceId, readBackOptions, null);
        }

        public ActivityInstanceData SuspendActivity(string activityInstanceId, string reason, DateTime? resumeAt, string resumeBookmark, ReadOptions readBackOptions)
        {
            return Channel.SuspendActivity(activityInstanceId, reason, resumeAt, resumeBookmark, readBackOptions);
        }

        public async Task<ActivityInstanceData> SuspendActivityAsync(string activityInstanceId, string reason, DateTime? resumeAt, string resumeBookmark, ReadOptions readBackOptions)
        {
            return await Task<ActivityInstanceData>.Factory.FromAsync(Channel.BeginSuspendActivity(activityInstanceId, reason, resumeAt, resumeBookmark, readBackOptions, null, null), Channel.EndSuspendActivity);
        }

        public ActivityInstanceData RestartActivity(string activityInstanceId, ReadOptions readBackOptions)
        {
            return Channel.RestartActivity(activityInstanceId, readBackOptions);
        }

        public async Task<ActivityInstanceData> RestartActivityAsync(string activityInstanceId, ReadOptions readBackOptions)
        {
            return await Task<ActivityInstanceData>.Factory.FromAsync(Channel.BeginRestartActivity, Channel.EndRestartActivity, activityInstanceId, readBackOptions, null);
        }

        public ActivityInstanceData ReAssignActivity(string activityInstanceId, string newAssigneeId, ReadOptions readBackOptions)
        {
            return Channel.ReAssignActivity(activityInstanceId, newAssigneeId, readBackOptions);
        }

        public async Task<ActivityInstanceData> ReAssignActivityAsync(string activityInstanceId, string newAssigneeId, ReadOptions readBackOptions)
        {
            return await Task<ActivityInstanceData>.Factory.FromAsync(Channel.BeginReAssignActivity, Channel.EndReAssignActivity, activityInstanceId, newAssigneeId, readBackOptions, null);
        }

        public ActivityInstanceData FinishActivity(string activityInstanceId, ActivityFinishData activityFinishData, ReadOptions readOptions)
        {
            return Channel.FinishActivity(activityInstanceId, activityFinishData, readOptions);
        }

        public async Task<ActivityInstanceData> FinishActivityAsync(string activityInstanceId, ActivityFinishData activityFinishData, ReadOptions readOptions)
        {
            return await Task<ActivityInstanceData>.Factory.FromAsync(Channel.BeginFinishActivity, Channel.EndFinishActivity, activityInstanceId, activityFinishData, readOptions, null);
        }

        public ProcessHistoryData ForceFinishProcess(string processInstanceId, string approvalStatusId, ReadOptions readOptions)
        {
            return Channel.ForceFinishProcess(processInstanceId, approvalStatusId, readOptions);
        }

        public async Task<ProcessHistoryData> ForceFinishProcessAsync(string processInstanceId, string approvalStatusId, ReadOptions readOptions)
        {
            return await Task<ProcessHistoryData>.Factory.FromAsync(Channel.BeginForceFinishProcess, Channel.EndForceFinishProcess, processInstanceId, approvalStatusId, readOptions, null);
        }

        public ActivityInstanceData ResumeActivity(string activityInstanceId, ReadOptions readOptions)
        {
            return Channel.ResumeActivity(activityInstanceId, readOptions);
        }

        public async Task<ActivityInstanceData> ResumeActivityAsync(string activityInstanceId, ReadOptions readOptions)
        {
            return await Task.Factory.FromAsync(Channel.BeginResumeActivity, Channel.EndResumeActivity, activityInstanceId, readOptions, null);
        }

        public QueueMessageData[] GetListQueueMessages(int queueId)
        {
            return Channel.GetListQueueMessages(queueId);
        }

        public async Task<QueueMessageData[]> GetListQueueMessagesAsync(int queueId)
        {
            return await Task<QueueMessageData[]>.Factory.FromAsync(Channel.BeginGetListQueueMessages, Channel.EndGetListQueueMessages, queueId, null);
        }

        public void PurgeQueue(int queueId)
        {
            Channel.PurgeQueue(queueId);
        }

        public async Task PurgeQueueAsync(int queueId)
        {
            await Task.Factory.FromAsync(Channel.BeginPurgeQueue, Channel.EndPurgeQueue, queueId, null);
        }

        public QueueData[] GetListQueues()
        {
            return Channel.GetListQueues();
        }

        public async Task<QueueData[]> GetListQueuesAsync()
        {
            return await Task<QueueData[]>.Factory.FromAsync(Channel.BeginGetListQueues, Channel.EndGetListQueues, null);
        }

        public ApplicationData ReadApplicationData(string subjectId, string applicationId)
        {
            return Channel.ReadApplicationData(subjectId, applicationId);
        }

        public async Task<ApplicationData> ReadApplicationDataAsync(string subjectId, string applicationId)
        {
            return await Task<ApplicationData>.Factory.FromAsync(Channel.BeginReadApplicationData, Channel.EndReadApplicationData, subjectId, applicationId, null);
        }

        public ApplicationData[] ReadAllApplicationData(string subjectId)
        {
            return Channel.ReadAllApplicationData(subjectId);
        }

        public async Task<ApplicationData[]> ReadAllApplicationDataAsync(string subjectId)
        {
            return await Task<ApplicationData[]>.Factory.FromAsync(Channel.BeginReadAllApplicationData, Channel.EndReadAllApplicationData, subjectId, null);
        }

        public void SaveApplicationData(string subjectId, ApplicationData[] applicationData)
        {
            Channel.SaveApplicationData(subjectId, applicationData);
        }

        public async Task SaveApplicationDataAsync(string subjectId, ApplicationData[] applicationData)
        {
            await Task.Factory.FromAsync(Channel.BeginSaveApplicationData, Channel.EndSaveApplicationData, subjectId, applicationData, null);
        }

        public void DeleteApplicationData(string subjectId, string applicationId)
        {
            Channel.DeleteApplicationData(subjectId, applicationId);
        }

        public async Task DeleteApplicationDataAsync(string subjectId, string applicationId)
        {
            await Task.Factory.FromAsync(Channel.BeginDeleteApplicationData, Channel.EndDeleteApplicationData, subjectId, applicationId, null);
        }

        public string[] GetApplicationIds()
        {
            return Channel.GetApplicationIds();
        }

        public async Task<string[]> GetApplicationIdsAsync()
        {
            return await Task<string[]>.Factory.FromAsync(Channel.BeginGetApplicationIds, Channel.EndGetApplicationIds, null);
        }

        public void PurgeApplicationData(string applicationId)
        {
            Channel.PurgeApplicationData(applicationId);
        }

        public async Task PurgeApplicationDataAsync(string applicationId)
        {
            await Task.Factory.FromAsync(Channel.BeginPurgeApplicationData, Channel.EndPurgeApplicationData, applicationId, null);
        }

        public PredefinedBatchOperation? ParsePredefinedBatchOperation(string operation)
        {
            return Channel.ParsePredefinedBatchOperation(operation);
        }

        public async Task<PredefinedBatchOperation?> ParsePredefinedBatchOperationAsync(string operation)
        {
            return await Task<PredefinedBatchOperation?>.Factory.FromAsync(Channel.BeginParsePredefinedBatchOperation, Channel.EndParsePredefinedBatchOperation, operation, null);
        }

        public string GetPredefinedBatchOperationName(PredefinedBatchOperation operation)
        {
            return Channel.GetPredefinedBatchOperationName(operation);
        }

        public async Task<string> GetPredefinedBatchOperationNameAsync(PredefinedBatchOperation operation)
        {
            return await Task<string>.Factory.FromAsync(Channel.BeginGetPredefinedBatchOperationName, Channel.EndGetPredefinedBatchOperationName, operation, null);
        }

        public PublishContextData[] ResolveItems(string[] ids, ResolveInstructionData resolveInstruction, string[] targetIdsOrPurposes, ReadOptions readOptions)
        {
            return Channel.ResolveItems(ids, resolveInstruction, targetIdsOrPurposes, readOptions);
        }

        public async Task<PublishContextData[]> ResolveItemsAsync(string[] ids, ResolveInstructionData resolveInstruction, string[] targetIdsOrPurposes, ReadOptions readOptions)
        {
            return await Task<PublishContextData[]>.Factory.FromAsync(Channel.BeginResolveItems(ids, resolveInstruction, targetIdsOrPurposes, readOptions, null, null), Channel.EndResolveItems);
        }

        public XElement GetSearchResultsXml(SearchQueryData filter)
        {
            return Channel.GetSearchResultsXml(filter);
        }

        public async Task<XElement> GetSearchResultsXmlAsync(SearchQueryData filter)
        {
            return await Task<XElement>.Factory.FromAsync(Channel.BeginGetSearchResultsXml, Channel.EndGetSearchResultsXml, filter, null);
        }

        public XElement GetSearchResultsXmlPaged(SearchQueryData filter, int startRowIndex, int maxRows)
        {
            return Channel.GetSearchResultsXmlPaged(filter, startRowIndex, maxRows);
        }

        public async Task<XElement> GetSearchResultsXmlPagedAsync(SearchQueryData filter, int startRowIndex, int maxRows)
        {
            return await Task<XElement>.Factory.FromAsync(Channel.BeginGetSearchResultsXmlPaged, Channel.EndGetSearchResultsXmlPaged, filter, startRowIndex, maxRows, null);
        }

        public IdentifiableObjectData[] GetSearchResults(SearchQueryData filter)
        {
            return Channel.GetSearchResults(filter);
        }

        public async Task<IdentifiableObjectData[]> GetSearchResultsAsync(SearchQueryData filter)
        {
            return await Task<IdentifiableObjectData[]>.Factory.FromAsync(Channel.BeginGetSearchResults, Channel.EndGetSearchResults, filter, null);
        }

        public IdentifiableObjectData[] GetSearchResultsPaged(SearchQueryData filter, int startRowIndex, int maxRows)
        {
            return Channel.GetSearchResultsPaged(filter, startRowIndex, maxRows);
        }

        public async Task<IdentifiableObjectData[]> GetSearchResultsPagedAsync(SearchQueryData filter, int startRowIndex, int maxRows)
        {
            return await Task<IdentifiableObjectData[]>.Factory.FromAsync(Channel.BeginGetSearchResultsPaged, Channel.EndGetSearchResultsPaged, filter, startRowIndex, maxRows, null);
        }

        public RenderedItemData RenderItem(string itemId, string templateId, PublishInstructionData publishInstruction, string publicationTargetIdOrPurpose)
        {
            return Channel.RenderItem(itemId, templateId, publishInstruction, publicationTargetIdOrPurpose);
        }

        public async Task<RenderedItemData> RenderItemAsync(string itemId, string templateId, PublishInstructionData publishInstruction, string publicationTargetIdOrPurpose)
        {
            return await Task<RenderedItemData>.Factory.FromAsync(Channel.BeginRenderItem(itemId, templateId, publishInstruction, publicationTargetIdOrPurpose, null, null), Channel.EndRenderItem);
        }

        public RenderedItemData PreviewItem(RepositoryLocalObjectData itemData, TemplateData templateData, PublishInstructionData publishInstruction, string publicationTargetId)
        {
            return Channel.PreviewItem(itemData, templateData, publishInstruction, publicationTargetId);
        }

        public async Task<RenderedItemData> PreviewItemAsync(RepositoryLocalObjectData itemData, TemplateData templateData, PublishInstructionData publishInstruction, string publicationTargetId)
        {
            return await Task<RenderedItemData>.Factory.FromAsync(Channel.BeginPreviewItem(itemData, templateData, publishInstruction, publicationTargetId, null, null), Channel.EndPreviewItem);
        }

        public PublishTransactionData[] Publish(string[] ids, PublishInstructionData publishInstruction, string[] targetIdsOrPurposes, PublishPriority? priority, ReadOptions readOptions)
        {
            return Channel.Publish(ids, publishInstruction, targetIdsOrPurposes, priority, readOptions);
        }

        public async Task<PublishTransactionData[]> PublishAsync(string[] ids, PublishInstructionData publishInstruction, string[] targetIdsOrPurposes, PublishPriority? priority, ReadOptions readOptions)
        {
            return await Task<PublishTransactionData[]>.Factory.FromAsync(Channel.BeginPublish(ids, publishInstruction, targetIdsOrPurposes, priority, readOptions, null, null), Channel.EndPublish);
        }

        public PublishTransactionData[] UnPublish(string[] ids, UnPublishInstructionData unPublishInstruction, string[] targetIdsOrPurposes, PublishPriority? priority, ReadOptions readOptions)
        {
            return Channel.UnPublish(ids, unPublishInstruction, targetIdsOrPurposes, priority, readOptions);
        }

        public async Task<PublishTransactionData[]> UnPublishAsync(string[] ids, UnPublishInstructionData unPublishInstruction, string[] targetIdsOrPurposes, PublishPriority? priority, ReadOptions readOptions)
        {
            return await Task<PublishTransactionData[]>.Factory.FromAsync(Channel.BeginUnPublish(ids, unPublishInstruction, targetIdsOrPurposes, priority, readOptions, null, null), Channel.EndUnPublish);
        }

        public bool IsPublished(string itemId, string publishingTargetIdOrPurpose, bool isPublishedInContext)
        {
            return Channel.IsPublished(itemId, publishingTargetIdOrPurpose, isPublishedInContext);
        }

        public async Task<bool> IsPublishedAsync(string itemId, string publishingTargetIdOrPurpose, bool isPublishedInContext)
        {
            return await Task<bool>.Factory.FromAsync(Channel.BeginIsPublished, Channel.EndIsPublished, itemId, publishingTargetIdOrPurpose, isPublishedInContext, null);
        }

        public RenderedItemData GetWorkItemSnapshot(string workItemId)
        {
            return Channel.GetWorkItemSnapshot(workItemId);
        }

        public async Task<RenderedItemData> GetWorkItemSnapshotAsync(string workItemId)
        {
            return await Task<RenderedItemData>.Factory.FromAsync(Channel.BeginGetWorkItemSnapshot, Channel.EndGetWorkItemSnapshot, workItemId, null);
        }

        public PublishInfoData[] GetListPublishInfo(string itemId)
        {
            return Channel.GetListPublishInfo(itemId);
        }

        public async Task<PublishInfoData[]> GetListPublishInfoAsync(string itemId)
        {
            return await Task<PublishInfoData[]>.Factory.FromAsync(Channel.BeginGetListPublishInfo, Channel.EndGetListPublishInfo, itemId, null);
        }

        public ActionFlags CastActions(int numericActions)
        {
            return Channel.CastActions(numericActions);
        }

        public async Task<ActionFlags> CastActionsAsync(int numericActions)
        {
            return await Task<ActionFlags>.Factory.FromAsync(Channel.BeginCastActions, Channel.EndCastActions, numericActions, null);
        }

        public void ValidateXml(IdentifiableObjectData data)
        {
            Channel.ValidateXml(data);
        }

        public async Task ValidateXmlAsync(IdentifiableObjectData data)
        {
            await Task.Factory.FromAsync(Channel.BeginValidateXml, Channel.EndValidateXml, data, null);
        }

        public bool IsValidTridionWebSchemaXml(SchemaData data)
        {
            return Channel.IsValidTridionWebSchemaXml(data);
        }

        public async Task<bool> IsValidTridionWebSchemaXmlAsync(SchemaData data)
        {
            return await Task<bool>.Factory.FromAsync(Channel.BeginIsValidTridionWebSchemaXml, Channel.EndIsValidTridionWebSchemaXml, data, null);
        }

        public ArrayOfTcmUri GetSubjectIdsWithApplicationData(string applicationId)
        {
            return Channel.GetSubjectIdsWithApplicationData(applicationId);
        }

        public async Task<ArrayOfTcmUri> GetSubjectIdsWithApplicationDataAsync(string applicationId)
        {
            return await Task<ArrayOfTcmUri>.Factory.FromAsync(Channel.BeginGetSubjectIdsWithApplicationData, Channel.EndGetSubjectIdsWithApplicationData, applicationId, null);
        }

        public ApplicationDataDictionary ReadApplicationDataForSubjectsIds(string[] subjectIds, string[] applicationIds)
        {
            return Channel.ReadApplicationDataForSubjectsIds(subjectIds, applicationIds);
        }

        public async Task<ApplicationDataDictionary> ReadApplicationDataForSubjectsIdsAsync(string[] subjectIds, string[] applicationIds)
        {
            return await Task<ApplicationDataDictionary>.Factory.FromAsync(Channel.BeginReadApplicationDataForSubjectsIds, Channel.EndReadApplicationDataForSubjectsIds, subjectIds, applicationIds, null);
        }

        public SecurityDescriptorDataDictionary GetSecurityDescriptorsForSubjectsIds(string[] subjectIds)
        {
            return Channel.GetSecurityDescriptorsForSubjectsIds(subjectIds);
        }

        public async Task<SecurityDescriptorDataDictionary> GetSecurityDescriptorsForSubjectsIdsAsync(string[] subjectIds)
        {
            return await Task<SecurityDescriptorDataDictionary>.Factory.FromAsync(Channel.BeginGetSecurityDescriptorsForSubjectsIds, Channel.EndGetSecurityDescriptorsForSubjectsIds, subjectIds, null);
        }

        public SecurityDescriptorDataDictionary GetContentSecurityDescriptorsForOrgItemIds(string[] organizationalItemIds)
        {
            return Channel.GetContentSecurityDescriptorsForOrgItemIds(organizationalItemIds);
        }

        public async Task<SecurityDescriptorDataDictionary> GetContentSecurityDescriptorsForOrgItemIdsAsync(string[] organizationalItemIds)
        {
            return await Task<SecurityDescriptorDataDictionary>.Factory.FromAsync(Channel.BeginGetContentSecurityDescriptorsForOrgItemIds, Channel.EndGetContentSecurityDescriptorsForOrgItemIds, organizationalItemIds, null);
        }

        public void ReIndex(string id)
        {
            Channel.ReIndex(id);
        }

        public async Task ReIndexAsync(string id)
        {
            await Task.Factory.FromAsync(Channel.BeginReIndex, Channel.EndReIndex, id, null);
        }

        public PredefinedQueue? CastPredefinedQueue(int queueId)
        {
            return Channel.CastPredefinedQueue(queueId);
        }

        public async Task<PredefinedQueue?> CastPredefinedQueueAsync(int queueId)
        {
            return await Task<PredefinedQueue?>.Factory.FromAsync(Channel.BeginCastPredefinedQueue, Channel.EndCastPredefinedQueue, queueId, null);
        }

        public int PurgeOldVersions(PurgeOldVersionsInstructionData instruction)
        {
            return Channel.PurgeOldVersions(instruction);
        }

        public async Task<int> PurgeOldVersionsAsync(PurgeOldVersionsInstructionData instruction)
        {
            return await Task<int>.Factory.FromAsync(Channel.BeginPurgeOldVersions, Channel.EndPurgeOldVersions, instruction, null);
        }

        public XElement GetListExternalLinks(string id)
        {
            return Channel.GetListExternalLinks(id);
        }

        public async Task<XElement> GetListExternalLinksAsync(string id)
        {
            return await Task< XElement>.Factory.FromAsync(Channel.BeginGetListExternalLinks, Channel.EndGetListExternalLinks, id, null);
        }

        public SearchQueryData ConvertXmlToSearchQuery(XElement searchQueryXml)
        {
            return Channel.ConvertXmlToSearchQuery(searchQueryXml);
        }

        public async Task<SearchQueryData> ConvertXmlToSearchQueryAsync(XElement searchQueryXml)
        {
            return await Task<SearchQueryData>.Factory.FromAsync(Channel.BeginConvertXmlToSearchQuery, Channel.EndConvertXmlToSearchQuery, searchQueryXml, null);
        }

        public XElement ConvertSearchQueryToXml(SearchQueryData searchQueryData)
        {
            return Channel.ConvertSearchQueryToXml(searchQueryData);
        }

        public async Task<XElement> ConvertSearchQueryToXmlAsync(SearchQueryData searchQueryData)
        {
            return await Task<XElement>.Factory.FromAsync(Channel.BeginConvertSearchQueryToXml, Channel.EndConvertSearchQueryToXml, searchQueryData, null);
        }

        public VersionedItemData CheckIn(string id, ReadOptions readBackOptions)
        {
            return this.CheckIn(id, true, null, readBackOptions);
        }

        public SchemaFieldsData ConvertXsdToSchemaFields(XElement xsd, SchemaPurpose purpose, ReadOptions readOptions)
        {
            return this.ConvertXsdToSchemaFields(xsd, purpose, null, readOptions);
        }

        public void EnlistInTransaction()
        {
            Channel.EnlistInTransaction();
        }

        public async Task EnlistInTransactionAsync()
        {
            await Task.Factory.FromAsync(Channel.BeginEnlistInTransaction, Channel.EndEnlistInTransaction, null);
        }

        public string GetSessionId()
        {
            return Channel.GetSessionId();
        }

        public async Task<string> GetSessionIdAsync()
        {
            return await Task<string>.Factory.FromAsync(Channel.BeginGetSessionId, Channel.EndGetSessionId, null);
        }

        public int GetSessionTransactionTimeout()
        {
            return Channel.GetSessionTransactionTimeout();
        }

        public async Task<int> GetSessionTransactionTimeoutAsync()
        {
            return await Task<int>.Factory.FromAsync(Channel.BeginGetSessionTransactionTimeout, Channel.EndGetSessionTransactionTimeout, null);
        }

        public SchemaData GetVirtualFolderTypeSchema(string namespaceUri)
        {
            return Channel.GetVirtualFolderTypeSchema(namespaceUri);
        }

        public async Task<SchemaData> GetVirtualFolderTypeSchemaAsync(string namespaceUri)
        {
            return await Task<SchemaData>.Factory.FromAsync(Channel.BeginGetVirtualFolderTypeSchema, Channel.EndGetVirtualFolderTypeSchema, namespaceUri, null);
        }

        public Task<ActivityInstanceData> ResumeActivityActivity(string activityInstanceId, ReadOptions readOptions = null)
        {
            throw new NotImplementedException();
        }

        public void SetSessionContextData(ApplicationData appData)
        {
            Channel.SetSessionContextData(appData);
        }

        public async Task SetSessionContextDataAsync(ApplicationData appData)
        {
            await Task.Factory.FromAsync(Channel.BeginSetSessionContextData, Channel.EndSetSessionContextData, appData, null);
        }

        public void SetSessionTransactionTimeout(int transactionTimeout)
        {
            Channel.SetSessionTransactionTimeout(transactionTimeout);
        }

        public async Task SetSessionTransactionTimeoutAsync(int transactionTimeout)
        {
            await Task.Factory.FromAsync(Channel.BeginSetSessionTransactionTimeout, Channel.EndSetSessionTransactionTimeout, transactionTimeout, null);
        }

        public void SetWebDavUrlPrefix(string prefix)
        {
            Channel.SetWebDavUrlPrefix(prefix);
        }

        public async Task SetWebDavUrlPrefixAsync(string prefix)
        {
            await Task.Factory.FromAsync(Channel.BeginSetWebDavUrlPrefix, Channel.EndSetWebDavUrlPrefix, prefix, null);
        }

        public bool StartCaching()
        {
            return Channel.StartCaching();
        }

        public async Task<bool> StartCachingAsync()
        {
            return await Task<bool>.Factory.FromAsync(Channel.BeginStartCaching, Channel.EndStartCaching, null);
        }

        public void StopCaching()
        {
            Channel.StopCaching();
        }

        public async Task StopCachingAsync()
        {
            await Task.Factory.FromAsync(Channel.BeginStopCaching, Channel.EndStopCaching, null);
        }

        public void TerminateSession()
        {
            Channel.TerminateSession();
        }

        public async Task TerminateSessionAsync()
        {
            await Task.Factory.FromAsync(Channel.BeginTerminateSession, Channel.EndTerminateSession, null);
        }

        public PublishTransactionData UndoPublishTransaction(string publishTransactionId, QueueMessagePriority? priority = default(QueueMessagePriority?), ReadOptions readBackOptions = null)
        {
            throw new NotImplementedException();
        }

        public Task<PublishTransactionData> UndoPublishTransactionAsync(string publishTransactionId, QueueMessagePriority? priority = default(QueueMessagePriority?), ReadOptions readBackOptions = null)
        {
            throw new NotImplementedException();
        }

        public UserData GetCurrentUser()
        {
            throw new NotImplementedException();
        }

        public Task<UserData> GetCurrentUserAsync()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Finalizer
        /// </summary>
        ~AlchemyCoreServiceClient201603()
        {
            Dispose(false);
        }
    }
}


text:
package ninja.utils;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class HttpHeaderUtilsTest {

    @Test
    public void testGetContentTypeFromContentTypeAndCharacterSetting() {

        assertEquals("application/json", HttpHeaderUtils.getContentTypeFromContentTypeAndCharacterSetting("application/json; charset=utf-8"));
        assertEquals("application/json", HttpHeaderUtils.getContentTypeFromContentTypeAndCharacterSetting("application/json"));

    }

    @Test
    public void testGetCharacterSetOfContentType() {

        assertEquals("utf-8", HttpHeaderUtils.getCharsetOfContentType("application/json; charset=utf-8", "TEST_ENCODING"));
        assertEquals("utf-8", HttpHeaderUtils.getCharsetOfContentType("application/json;charset=utf-8", "TEST_ENCODING"));
        assertEquals("TEST_ENCODING", HttpHeaderUtils.getCharsetOfContentType("application/json", "TEST_ENCODING"));

    }

    @Test
    public void testGetCharacterSetOfContentTypeOrUtf8() {

        assertEquals("TEST_ENCODING", HttpHeaderUtils.getCharsetOfContentType("application/json; charset=TEST_ENCODING", NinjaConstant.UTF_8));
        assertEquals("TEST_ENCODING", HttpHeaderUtils.getCharsetOfContentType("application/json;charset=TEST_ENCODING", "TEST_ENCODING"));
        assertEquals(NinjaConstant.UTF_8, HttpHeaderUtils.getCharsetOfContentType("application/json", NinjaConstant.UTF_8));

    }

}

redpajama_set_name: RedPajamaGithub


text:
package system

import (
	"os"
	"path/filepath"
)

func MkdirChown(path string, uid, gid uint32, mode os.FileMode) error {
	if _, err := os.Stat(path); err == nil {
		return nil // The base case of our recursion: this directory already exists!
	}

	if err := MkdirChown(filepath.Dir(path), uid, gid, mode); err != nil {
		return err
	}

	if err := os.MkdirAll(path, mode); err != nil {
		return err
	}

	if err := os.Chown(path, int(uid), int(gid)); err != nil {
		return err
	}

	return nil
}

type MkdirChowner struct{}

func (MkdirChowner) MkdirChown(path string, uid, gid uint32, mode os.FileMode) error {
	return MkdirChown(path, uid, gid, mode)
}


text:
package org.jtwig.model.expression.operation.calculators.unary;

import org.jtwig.context.RenderContext;
import org.jtwig.model.expression.Expression;
import org.jtwig.model.position.Position;
import org.jtwig.util.JtwigValue;

public interface UnaryOperationCalculator {
    JtwigValue calculate (RenderContext context, Position position, Expression operand);
}


text:
require_relative "test_helper"

class RepoTest < Minitest::Test
  class TestModel
    include Aas::Model
    attr_accessor :name
  end

  class TestModelRepo
    include Aas::Repo

    def initialize
      @store = Aas::MemoryStore.new
    end
  end

  attr_reader :repo

  def setup
    @repo = TestModelRepo.new
  end

  def test_create_adds_id_to_model
    model = TestModel.new
    repo.create model
    refute_nil model.id
  end
end


text:
<?xml version="1.0" encoding="UTF-8"?>
<beans
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
  bean-discovery-mode="annotated"
  version="2.0"
>
  <interceptors>
    <class>com.handywedge.interceptor.FWTransactionalInterceptor</class>
  </interceptors>
</beans>


text:
package net.methvin.scalacache

import javax.inject.Singleton

import com.github.benmanes.caffeine.cache.Caffeine
import play.api.inject.Module
import play.api.{Configuration, Environment}

import scalacache.ScalaCache
import scalacache.caffeine.CaffeineCache

class CaffeineScalaCacheModule extends Module {
  override def bindings(environment: Environment, configuration: Configuration) = {
    // TODO: read cache settings from configuration?
    val caffeineCache = CaffeineCache(Caffeine.newBuilder().maximumSize(10000L).build[String, Object])
    Seq(
      bind[ScalaCache].to(ScalaCache(caffeineCache)).in[Singleton]
    )
  }
}


text:
import React from 'react';
import Avatar from 'material-ui/Avatar';
import FileFolder from 'material-ui/svg-icons/file/folder';
import FontIcon from 'material-ui/FontIcon';
import List from 'material-ui/List/List';
import ListItem from 'material-ui/List/ListItem';
import DownIcon from 'material-ui/svg-icons/Hardware/keyboard-arrow-down';
const UserList = () => (
  <List>
      <ListItem
          style= {{
              "borderBottom":"1px solid #ccc",
          }}
          primaryText={

东方

          }
          rightIcon={
            <DownIcon />
          }
      />
    <ListItem
      primaryText={

东方

      }
    />
    <ListItem
      primaryText={

东方

      }
    />
    <ListItem
      primaryText={

东方

      }
    />
    <ListItem
      primaryText={

东方

      }
    />
  </List>
);

export default UserList;


text:
package org.hisp.dhis.sms.listener;

import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
import org.hisp.dhis.dataelement.DataElementCategoryService;
import org.hisp.dhis.dataelement.DataElementService;
import org.hisp.dhis.dataset.CompleteDataSetRegistration;
import org.hisp.dhis.dataset.CompleteDataSetRegistrationService;
import org.hisp.dhis.dataset.DataSet;
import org.hisp.dhis.dataset.DataSetService;
import org.hisp.dhis.datavalue.DataValue;
import org.hisp.dhis.datavalue.DataValueService;
import org.hisp.dhis.message.MessageSender;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.PeriodType;
import org.hisp.dhis.sms.command.SMSCommand;
import org.hisp.dhis.sms.command.SMSCommandService;
import org.hisp.dhis.sms.command.SMSSpecialCharacter;
import org.hisp.dhis.sms.command.code.SMSCode;
import org.hisp.dhis.sms.incoming.IncomingSms;
import org.hisp.dhis.sms.incoming.IncomingSmsListener;
import org.hisp.dhis.sms.incoming.IncomingSmsService;
import org.hisp.dhis.sms.incoming.SmsMessageStatus;
import org.hisp.dhis.sms.parse.ParserType;
import org.hisp.dhis.sms.parse.SMSParserException;
import org.hisp.dhis.system.util.SmsUtils;
import org.hisp.dhis.user.User;
import org.hisp.dhis.user.UserService;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

public class DataValueSMSListener
    implements IncomingSmsListener
{
    private static final String defaultPattern = "([a-zA-Z]+)\\s*(\\d+)";

    // -------------------------------------------------------------------------
    // Dependencies
    // -------------------------------------------------------------------------

    @Autowired
    private CompleteDataSetRegistrationService registrationService;

    @Autowired
    private DataValueService dataValueService;

    @Autowired
    private DataElementCategoryService dataElementCategoryService;

    @Autowired
    private SMSCommandService smsCommandService;

    @Autowired
    private UserService userService;

    @Autowired
    private DataSetService dataSetService;

    @Autowired
    private IncomingSmsService incomingSmsService;

    @Autowired
    private DataElementService dataElementService;

    @Autowired
    @Resource( name = "smsMessageSender" )
    private MessageSender smsSender;

    // -------------------------------------------------------------------------
    // IncomingSmsListener implementation
    // -------------------------------------------------------------------------

    @Transactional
    @Override
    public boolean accept( IncomingSms sms )
    {
        return smsCommandService.getSMSCommand( SmsUtils.getCommandString( sms ), ParserType.KEY_VALUE_PARSER ) != null;
    }

    @Transactional
    @Override
    public void receive( IncomingSms sms )
    {
        String message = sms.getText();
        SMSCommand smsCommand = smsCommandService.getSMSCommand( SmsUtils.getCommandString( sms ),
            ParserType.KEY_VALUE_PARSER );
        Map<String, String> parsedMessage = this.parse( message, smsCommand );

        Date date = SmsUtils.lookForDate( message );
        Collection<OrganisationUnit> orgUnits = getOrganisationUnits( sms );

        if ( orgUnits == null || orgUnits.size() == 0 )
        {
            if ( StringUtils.isEmpty( smsCommand.getNoUserMessage() ) )
            {
                throw new SMSParserException( SMSCommand.NO_USER_MESSAGE );
            }
            else
            {
                throw new SMSParserException( smsCommand.getNoUserMessage() );
            }
        }

        OrganisationUnit orgUnit = SmsUtils.selectOrganisationUnit( orgUnits, parsedMessage, smsCommand );
        Period period = getPeriod( smsCommand, date );

        if ( dataSetService.isLocked( smsCommand.getDataset(), period, orgUnit, null, null ) )
        {
            throw new SMSParserException(
                "Dataset is locked for the period " + period.getStartDate() + " - " + period.getEndDate() );
        }

        boolean valueStored = false;

        for ( SMSCode code : smsCommand.getCodes() )
        {
            if ( parsedMessage.containsKey( code.getCode() ) )
            {
                valueStored = storeDataValue( sms, orgUnit, parsedMessage, code, smsCommand, date,
                    smsCommand.getDataset() );
            }
        }

        if ( parsedMessage.isEmpty() )
        {
            if ( StringUtils.isEmpty( smsCommand.getDefaultMessage() ) )
            {
                throw new SMSParserException( "No values reported for command '" + smsCommand.getName() + "'" );
            }
            else
            {
                throw new SMSParserException( smsCommand.getDefaultMessage() );
            }
        }
        else if ( !valueStored )
        {
            if ( StringUtils.isEmpty( smsCommand.getWrongFormatMessage() ) )
            {
                throw new SMSParserException( SMSCommand.WRONG_FORMAT_MESSAGE );
            }
            else
            {
                throw new SMSParserException( smsCommand.getWrongFormatMessage() );
            }
        }

        markCompleteDataSet( sms, orgUnit, parsedMessage, smsCommand, date );
        sendSuccessFeedback( sms, smsCommand, parsedMessage, date, orgUnit );

        sms.setStatus( SmsMessageStatus.PROCESSED );
        sms.setParsed( true );
        incomingSmsService.update( sms );
    }

    private User getUser(IncomingSms sms )
    {
        return userService.getUser( sms.getUser().getUid() );
    }

    private Map<String, String> parse( String sms, SMSCommand smsCommand )
    {
        HashMap<String, String> output = new HashMap<>();
        Pattern pattern = Pattern.compile( defaultPattern );

        if ( !StringUtils.isBlank( smsCommand.getSeparator() ) )
        {
            String x = "([^\\s|" + smsCommand.getSeparator().trim() + "]+)\\s*\\" + smsCommand.getSeparator().trim()
                + "\\s*([^|]+)\\s*(\\" + smsCommand.getSeparator().trim() + "|$)*\\s*";
            pattern = Pattern.compile( x );
        }

        Matcher matcher = pattern.matcher( sms );
        while ( matcher.find() )
        {
            String key = matcher.group( 1 ).trim();
            String value = matcher.group( 2 ).trim();

            if ( !StringUtils.isEmpty( key ) && !StringUtils.isEmpty( value ) )
            {
                output.put( key, value );
            }
        }

        return output;
    }

    private Period getPeriod( SMSCommand command, Date date )
    {
        Period period = null;
        period = command.getDataset().getPeriodType().createPeriod();
        PeriodType periodType = period.getPeriodType();

        if ( command.isCurrentPeriodUsedForReporting() )
        {
            period = periodType.createPeriod( new Date() );
        }
        else
        {
            period = periodType.getPreviousPeriod( period );
        }

        if ( date != null )
        {
            period = periodType.createPeriod( date );
        }

        return period;
    }

    private boolean storeDataValue( IncomingSms sms, OrganisationUnit orgunit, Map<String, String> parsedMessage,
        SMSCode code, SMSCommand command, Date date, DataSet dataSet )
    {
        String sender = sms.getOriginator();
        String storedBy = SmsUtils.getUser( sender, command, Collections.singletonList( getUser( sms ) ) )
            .getUsername();

        if ( StringUtils.isBlank( storedBy ) )
        {
            storedBy = "[unknown] from [" + sender + "]";
        }

        DataElementCategoryOptionCombo optionCombo = dataElementCategoryService
            .getDataElementCategoryOptionCombo( code.getOptionId() );

        Period period = getPeriod( command, date );

        DataValue dv = dataValueService.getDataValue( code.getDataElement(), period, orgunit, optionCombo );

        String value = parsedMessage.get( code.getCode() );

        Set<SMSSpecialCharacter> specialCharacters = command.getSpecialCharacters();

        for ( SMSSpecialCharacter each : specialCharacters )
        {
            if ( each.getName().equalsIgnoreCase( value ) )
            {
                value = each.getValue();
                break;
            }
        }

        if ( !StringUtils.isEmpty( value ) )
        {
            boolean newDataValue = false;

            if ( dv == null )
            {
                dv = new DataValue();
                dv.setCategoryOptionCombo( optionCombo );
                dv.setSource( orgunit );
                dv.setDataElement( code.getDataElement() );
                dv.setPeriod( period );
                dv.setComment( "" );
                newDataValue = true;
            }

            if ( ValueType.BOOLEAN == dv.getDataElement().getValueType() )
            {
                if ( "Y".equals( value.toUpperCase() ) || "YES".equals( value.toUpperCase() ) )
                {
                    value = "true";
                }
                else if ( "N".equals( value.toUpperCase() ) || "NO".equals( value.toUpperCase() ) )
                {
                    value = "false";
                }
            }
            else if ( dv.getDataElement().getValueType().isInteger() )
            {
                try
                {
                    Integer.parseInt( value );
                }
                catch ( NumberFormatException e )
                {
                    return false;
                }
            }

            dv.setValue( value );
            dv.setLastUpdated( new java.util.Date() );
            dv.setStoredBy( storedBy );

            if ( newDataValue )
            {
                dataValueService.addDataValue( dv );
            }
            else
            {
                dataValueService.updateDataValue( dv );
            }
        }

        if ( code.getFormula() != null )
        {
            try
            {
                String formula = code.getFormula();

                String targetDataElementId = formula.substring( 1, formula.length() );
                String operation = String.valueOf( formula.charAt( 0 ) );

                DataElement targetDataElement = dataElementService
                    .getDataElement( Integer.parseInt( targetDataElementId ) );

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

                DataValue targetDataValue = dataValueService.getDataValue( targetDataElement, period, orgunit,
                    dataElementCategoryService.getDefaultDataElementCategoryOptionCombo() );

                int targetValue = 0;
                boolean newTargetDataValue = false;

                if ( targetDataValue == null )
                {
                    targetDataValue = new DataValue();
                    targetDataValue.setCategoryOptionCombo(
                        dataElementCategoryService.getDefaultDataElementCategoryOptionCombo() );
                    targetDataValue.setSource( orgunit );
                    targetDataValue.setDataElement( targetDataElement );
                    targetDataValue.setPeriod( period );
                    targetDataValue.setComment( "" );
                    newTargetDataValue = true;
                }
                else
                {
                    targetValue = Integer.parseInt( targetDataValue.getValue() );
                }

                if ( operation.equals( "+" ) )
                {
                    targetValue = targetValue + Integer.parseInt( value );
                }
                else if ( operation.equals( "-" ) )
                {
                    targetValue = targetValue - Integer.parseInt( value );
                }

                targetDataValue.setValue( String.valueOf( targetValue ) );
                targetDataValue.setLastUpdated( new java.util.Date() );
                targetDataValue.setStoredBy( storedBy );

                if ( newTargetDataValue )
                {
                    dataValueService.addDataValue( targetDataValue );
                }
                else
                {
                    dataValueService.updateDataValue( targetDataValue );
                }

            }
            catch ( Exception e )
            {
                e.printStackTrace();
                return false;
            }
        }

        return true;
    }

    private void markCompleteDataSet( IncomingSms sms, OrganisationUnit orgunit, Map<String, String> parsedMessage,
        SMSCommand command, Date date )
    {
        String sender = sms.getOriginator();

        Period period = null;
        int numberOfEmptyValue = 0;
        for ( SMSCode code : command.getCodes() )
        {

            DataElementCategoryOptionCombo optionCombo = dataElementCategoryService
                .getDataElementCategoryOptionCombo( code.getOptionId() );

            period = getPeriod( command, date );

            DataValue dv = dataValueService.getDataValue( code.getDataElement(), period, orgunit, optionCombo );

            if ( dv == null && !StringUtils.isEmpty( code.getCode() ) )
            {
                numberOfEmptyValue++;
            }
        }

        // Check completeness method
        if ( command.getCompletenessMethod() == SMSCommand.RECEIVE_ALL_DATAVALUE )
        {
            if ( numberOfEmptyValue > 0 )
            {
                return;
            }
        }
        else if ( command.getCompletenessMethod() == SMSCommand.RECEIVE_AT_LEAST_ONE_DATAVALUE )
        {
            if ( numberOfEmptyValue == command.getCodes().size() )
            {
                return;
            }
        }
        else if ( command.getCompletenessMethod() == SMSCommand.DO_NOT_MARK_COMPLETE )
        {
            return;
        }

        // Go through the complete process
        String storedBy = SmsUtils.getUser( sender, command, Collections.singletonList( getUser( sms ) ) )
            .getUsername();

        if ( StringUtils.isBlank( storedBy ) )
        {
            storedBy = "[unknown] from [" + sender + "]";
        }

        // If new values are submitted re-register as complete
        deregisterCompleteDataSet( command.getDataset(), period, orgunit );
        registerCompleteDataSet( command.getDataset(), period, orgunit, storedBy );
    }

    protected void sendSuccessFeedback( IncomingSms sms, SMSCommand command, Map<String, String> parsedMessage, Date date,
        OrganisationUnit orgunit )
    {
        String sender = sms.getOriginator();
        String reportBack = "Thank you! Values entered: ";
        String notInReport = "Missing values for: ";

        Period period = null;

        Map<String, DataValue> codesWithDataValues = new TreeMap<>();
        List<String> codesWithoutDataValues = new ArrayList<>();

        for ( SMSCode code : command.getCodes() )
        {

            DataElementCategoryOptionCombo optionCombo = dataElementCategoryService
                .getDataElementCategoryOptionCombo( code.getOptionId() );

            period = getPeriod( command, date );

            DataValue dv = dataValueService.getDataValue( code.getDataElement(), period, orgunit, optionCombo );

            if ( dv == null && !StringUtils.isEmpty( code.getCode() ) )
            {
                codesWithoutDataValues.add( code.getCode() );
            }
            else if ( dv != null )
            {
                codesWithDataValues.put( code.getCode(), dv );
            }
        }

        for ( String key : codesWithDataValues.keySet() )
        {
            DataValue dv = codesWithDataValues.get( key );
            String value = dv.getValue();

            if ( ValueType.BOOLEAN == dv.getDataElement().getValueType() )
            {
                if ( "true".equals( value ) )
                {
                    value = "Yes";
                }
                else if ( "false".equals( value ) )
                {
                    value = "No";
                }
            }
            reportBack += key + "=" + value + " ";
        }

        Collections.sort( codesWithoutDataValues );

        for ( String key : codesWithoutDataValues )
        {
            notInReport += key + ",";
        }

        if ( smsSender.isConfigured() )
        {
            if ( command.getSuccessMessage() != null && !StringUtils.isEmpty( command.getSuccessMessage() ) )
            {
                smsSender.sendMessage( null, command.getSuccessMessage(), sender );
            }
            else
            {
                smsSender.sendMessage( null, reportBack, sender );
            }
        }
        else
        {
            Log.info( "No sms configuration found." );
        }
    }

    private void registerCompleteDataSet( DataSet dataSet, Period period, OrganisationUnit organisationUnit,
        String storedBy )
    {
        CompleteDataSetRegistration registration = new CompleteDataSetRegistration();

        DataElementCategoryOptionCombo optionCombo = dataElementCategoryService
            .getDefaultDataElementCategoryOptionCombo(); // TODO

        if ( registrationService.getCompleteDataSetRegistration( dataSet, period, organisationUnit,
            optionCombo ) == null )
        {
            registration.setDataSet( dataSet );
            registration.setPeriod( period );
            registration.setSource( organisationUnit );
            registration.setDate( new Date() );
            registration.setStoredBy( storedBy );
            registration.setPeriodName( registration.getPeriod().toString() );
            registrationService.saveCompleteDataSetRegistration( registration, false );
        }
    }

    private void deregisterCompleteDataSet( DataSet dataSet, Period period, OrganisationUnit organisationUnit )
    {
        DataElementCategoryOptionCombo optionCombo = dataElementCategoryService
            .getDefaultDataElementCategoryOptionCombo(); // TODO

        CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration( dataSet, period,
            organisationUnit, optionCombo );

        if ( registration != null )
        {
            registrationService.deleteCompleteDataSetRegistration( registration );
        }
    }

    private Set<OrganisationUnit> getOrganisationUnits( IncomingSms sms )
    {
        Collection<OrganisationUnit> orgUnits = SmsUtils.getOrganisationUnitsByPhoneNumber( sms.getOriginator(),
            Collections.singleton( getUser( sms ) ) );

        return Sets.newHashSet( orgUnits );
    }
}


text:
import os

from flask import Blueprint, render_template, send_from_directory, abort, request
from flask import current_app as APP
from flask.ext.login import login_required, current_user

from ..blogpost.models import BlogPost
from .models import User

user = Blueprint('user', __name__, url_prefix='/user')

@user.route('/')
@login_required
def index():
        if not current_user.is_authenticated():
                abort(403)
        else:
                tags = request.args.get('tags', '').strip()
                pagination = None
                if tags:
                        page = int(request.args.get('page', 1))
                        pagination = BlogPost.search(tags).paginate(page, 10)
                else:
                        page = int(request.args.get('page', 1))
                        pagination = BlogPost.search('').paginate(page, 10, False)

        return render_template('user/index.html', user=current_user, pagination=pagination, tags=tags)

@user.route('/<int:user_id>/profile')
def profile(user_id):
        user = User.get_by_id(user_id)
        return render_template('user/profile.html', user=user)

@user.route('/<int:user_id>/avatar/<path:filename>')
@login_required
def avatar(user_id, filename):
        dir_path = os.path.join(APP.config['UPLOAD_FOLDER'], 'user_%s' % user_id)
        return send_from_directory(dir_path, filename, as_attachment=True)


text:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="ch.sloth.dealhunter.activites.WebviewActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/DealHunterTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/DealHunterTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <WebView
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</android.support.design.widget.CoordinatorLayout>