private void DeployDefinition(object modelHost, ISecureStore hostSecureStore, TargetApplicationDefinition definition)
        {
            var currentObject = GetCurrentObject(hostSecureStore, definition);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = currentObject,
                ObjectType = typeof(TargetApplication),
                ObjectDefinition = definition,
                ModelHost = modelHost
            });

            if (currentObject == null)
                currentObject = CreateOject(modelHost, hostSecureStore, definition);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioned,
                Object = currentObject,
                ObjectType = typeof(TargetApplication),
                ObjectDefinition = definition,
                ModelHost = modelHost
            });
        }
        protected TargetApplication GetCurrentObject(ISecureStore secureStore, TargetApplicationDefinition definition)
        {
            var apps = secureStore.GetApplications();

            if (!string.IsNullOrEmpty(definition.ApplicationId))
            {
                return apps.FirstOrDefault(app => app.ApplicationId.ToUpper() == definition.ApplicationId.ToUpper());
            }
            else if (!string.IsNullOrEmpty(definition.Name))
            {
                return apps.FirstOrDefault(app => app.Name.ToUpper() == definition.FriendlyName.ToUpper());
            }
            else if (!string.IsNullOrEmpty(definition.FriendlyName))
            {
                return apps.FirstOrDefault(app => app.FriendlyName.ToUpper() == definition.FriendlyName.ToUpper());
            }
            else
            {
                throw new SPMeta2Exception("ApplicationId/Name/FriendlyName needs to be defined.");
            }
        }
        private TargetApplication CreateOject(object modelHost, ISecureStore hostSecureStore, TargetApplicationDefinition definition)
        {
            var appType = (TargetApplicationType)Enum.Parse(typeof(TargetApplicationType), definition.Type);

            var secureStoreClaimns = new List<SecureStoreServiceClaim>();

            foreach (var claim in definition.TargetApplicationClams)
                secureStoreClaimns.Add(GetSecureStoreClaim(claim));

            var appClaims = new TargetApplicationClaims(secureStoreClaimns, null, null);
            var appFields = GetFields(definition);

            hostSecureStore.CreateApplication(
                new TargetApplication(definition.ApplicationId, definition.FriendlyName, definition.ContactEmail,
                    definition.TicketTimeout,
                    appType,
                    new Uri(definition.CredentialManagementUrl)),
                appFields, appClaims);

            return GetCurrentObject(hostSecureStore, definition);
        }
        private TargetApplicationField[] GetFields(TargetApplicationDefinition definition)
        {
            var result = new List<TargetApplicationField>();

            foreach (var field in definition.Fields)
            {
                result.Add(new TargetApplicationField(

                 field.Name,
                 field.IsMasked,
                 (SecureStoreCredentialType)Enum.Parse(typeof(SecureStoreCredentialType), field.CredentialType)
                ));
            }

            return result.ToArray();
        }
 public static ModelNode AddTargetApplication(this ModelNode model, TargetApplicationDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }
 public static ModelNode AddTargetApplication(this ModelNode model, TargetApplicationDefinition definition)
 {
     return AddTargetApplication(model, definition, null);
 }