Esempio n. 1
0
        public void Process(JObject command, Context context)
        {
            // get default property
            var file = JSONUtil.GetText(command, "#load-json");

            if (file == null)
            {
                file = JSONUtil.GetText(command, "file");
            }

            // get all other properties
            var set = JSONUtil.GetText(command, "set");

            if (set == null)
            {
                set = Guid.NewGuid().ToString();
            }

            // apply variables to properties (do this when getting property instead?)
            file = context.ReplaceVariables(file);

            // perform the action
            if (command["#debug"] != null && command["#debug"].ToString() == "true")
            {
                context.Trace($"Loading JSON File {file} into {set}");
            }


            var sub = JSONUtil.ReadFile(file);

            context.Store(set, sub);
        }
        private void CreateBooleanField(JToken field)
        {
            var entitySchemaName = JSONUtil.GetText(field, "entity");
            var displayName      = JSONUtil.GetText(field, "displayname");
            var fieldSchemaName  = JSONUtil.GetText(field, "schemaname");

            CreateAttributeRequest req = new CreateAttributeRequest();

            req.EntityName = entitySchemaName;

            var am = new BooleanAttributeMetadata();

            am.SchemaName    = fieldSchemaName;
            am.RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
            am.DisplayName   = new Label(displayName, 1033);
            am.Description   = new Label("", 1033);

            am.OptionSet = new BooleanOptionSetMetadata(
                new OptionMetadata(new Label("Yes", 1033), 1),
                new OptionMetadata(new Label("No", 1033), 0));

            req.Attribute = am;

            this._cdsConnection.Execute(req);
        }
        private void CreateDateTimeField(JToken field)
        {
            var entitySchemaName = JSONUtil.GetText(field, "entity");
            var displayName      = JSONUtil.GetText(field, "displayname");
            var fieldSchemaName  = JSONUtil.GetText(field, "schemaname");

            CreateAttributeRequest req = new CreateAttributeRequest();

            req.EntityName = entitySchemaName;
            var dta = new DateTimeAttributeMetadata();

            dta.SchemaName    = fieldSchemaName;
            dta.RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);

            var datetimeoption = JSONUtil.GetText(field, "datetimeoption");

            if (datetimeoption == null)
            {
                datetimeoption = "dateonly";
            }
            if (datetimeoption == "dateonly")
            {
                dta.Format = DateTimeFormat.DateOnly;
            }
            else if (datetimeoption == "datetime")
            {
                dta.Format = DateTimeFormat.DateAndTime;
            }

            dta.DisplayName = new Label(displayName, 1033);
            dta.Description = new Label("", 1033);
            req.Attribute   = dta;

            this._cdsConnection.Execute(req);
        }
        public void CreateEntity(JObject command, Context context)
        {
            var schemaName = JSONUtil.GetText(command, "#cds-entity");

            if (schemaName == null)
            {
                schemaName = JSONUtil.GetText(command, "schemaname");
            }
            if (schemaName == null)
            {
                return;
            }

            var joEntity = context.FindItemByName(schemaName);

            if (joEntity == null)
            {
                return;
            }

            string displayName      = joEntity["displayname"].ToString();
            string collectionName   = joEntity["collectionname"].ToString();
            string primaryFieldName = joEntity["primaryfieldname"].ToString();

            EntityMetadata entity = new EntityMetadata();

            entity.SchemaName            = schemaName;
            entity.DisplayName           = new Label(displayName, 1033);
            entity.DisplayCollectionName = new Label(collectionName, 1033);
            //Description = new Label("", 1033);
            entity.OwnershipType = OwnershipTypes.UserOwned;
            entity.IsActivity    = false;

            this.Create(entity, primaryFieldName);
        }
Esempio n. 5
0
        public void Process(JObject command, Context context)
        {
            string file = JSONUtil.GetText(command, "#run-script");

            if (file == null)
            {
                file = command["script"].ToString();
            }
            file = context.ReplaceVariables(file);

            System.IO.FileInfo fi = new System.IO.FileInfo(file);
            var scriptDir         = fi.Directory.FullName;

            JArray items = (JArray)JSONUtil.ReadFile(file);

            if (items == null)
            {
                Console.WriteLine($"RunScript: Unable to read file {file}.");
                return;
            }

            foreach (var item in items)
            {
                var commandItem = Unification.Replace(item, "#scriptDir", scriptDir);
                context.CommandEngine.RunCommand(commandItem, context);
            }
        }
Esempio n. 6
0
        public void Process(JObject command, Context context)
        {
            var varName = JSONUtil.GetText(command, "#store");

            if (varName == null)
            {
                varName = JSONUtil.GetText(command, "name");
            }

            var value = JSONUtil.GetToken(command, "value");

            var storeMode = StoreMode.Replace;
            var mode      = command["mode"];

            if (mode != null && mode.ToString() == "merge")
            {
                storeMode = StoreMode.Merge;
            }

            if (command["#debug"] != null && command["#debug"].ToString() == "true")
            {
                if (storeMode == StoreMode.Merge)
                {
                    context.Trace($"Merging {JSONUtil.SingleLine(value)} into {varName}");
                }
                else
                {
                    context.Trace($"Setting {varName} to {JSONUtil.SingleLine(value)}");
                }
            }

            context.Store(varName, value, storeMode);
        }
Esempio n. 7
0
        public void Process(JObject command, Context context)
        {
            context.Trace("bing-websearch");

            var uriKey = "$bingsearch-uri";

            if (context.Variables.ContainsKey(uriKey) == false)
            {
                return;
            }
            var uriBase = context.Variables[uriKey].ToString();

            var configKey = "$bingsearch-key";

            if (context.Variables.ContainsKey(configKey) == false)
            {
                return;
            }
            accessKey = context.Variables[configKey].ToString();

            var searchTerm = JSONUtil.GetText(command, "#bing-websearch");

            if (searchTerm == null)
            {
                searchTerm = JSONUtil.GetText(command, "search-term");
            }

            Console.OutputEncoding = Encoding.UTF8;
            if (accessKey.Length == 32)
            {
                this.ConsoleWriteLine("Searching the Web for: " + searchTerm);
                SearchResult result = BingWebSearch(uriBase, searchTerm);
                //this.ConsoleWriteLine("\nRelevant HTTP Headers:\n");
                //foreach (var header in result.relevantHeaders)
                //    Console.WriteLine(header.Key + ": " + header.Value);

                //this.ConsoleWriteLine("\nJSON Response:\n");
                //Console.WriteLine(JsonPrettyPrint(result.jsonResult));
                JToken jResult = JToken.Parse(result.jsonResult);
                //this.ConsoleWriteLine(jResult.ToString());

                var setName = JSONUtil.GetText(command, "set");
                if (setName != null)
                {
                    context.Store(setName, jResult);
                }
            }
            else
            {
                this.ConsoleWriteLine("Invalid Bing Search API subscription key!");
                this.ConsoleWriteLine("Please paste yours into the source code.");
            }
        }
        private void CreateTextField(JToken field)
        {
            var entitySchemaName = JSONUtil.GetText(field, "entity");
            var displayName      = JSONUtil.GetText(field, "displayname");
            var fieldSchemaName  = JSONUtil.GetText(field, "schemaname");

            var req = new CreateAttributeRequest();

            req.EntityName = entitySchemaName;

            var format = JSONUtil.GetText(field, "format");

            if (format == null)
            {
                format = "single";
            }

            int?maxlength = JSONUtil.GetInt32(field, "maxlength");

            if (format == "single")
            {
                var am = new StringAttributeMetadata();
                am.SchemaName    = field["schemaname"].ToString();
                am.RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);

                maxlength    = maxlength == null ? 100 : maxlength;
                am.MaxLength = maxlength;

                am.FormatName  = StringFormatName.Text;
                am.DisplayName = new Label(displayName, 1033);
                am.Description = new Label("", 1033);
                req.Attribute  = am;
            }
            else if (format == "multi")
            {
                var am = new MemoAttributeMetadata();
                am.SchemaName    = fieldSchemaName;
                am.RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);

                maxlength    = maxlength == null ? 2000 : maxlength;
                am.MaxLength = maxlength;

                am.DisplayName = new Label(displayName, 1033);
                am.Description = new Label("", 1033);
                req.Attribute  = am;
            }

            this._cdsConnection.Execute(req);
        }
        public void CreateField(JToken field)
        {
            var schemaName = JSONUtil.GetText(field, "#cds-field");

            if (schemaName == null)
            {
                schemaName = JSONUtil.GetText(field, "schemaname");
            }
            if (schemaName == null)
            {
                return;
            }

            var displayName = field["displayname"];
            var fieldType   = field["fieldtype"].ToString();

            Console.WriteLine("Creating field " + displayName.ToString());
            if (fieldType == "text" || fieldType == "TextType")
            {
                this.CreateTextField(field);
            }
            else if (fieldType == "money")
            {
                this.CreateMoneyField(field);
            }
            else if (fieldType == "lookup")
            {
                this.CreateLookupField(field);
            }
            else if (fieldType == "datetime")
            {
                this.CreateDateTimeField(field);
            }
            else if (fieldType == "boolean")
            {
                this.CreateBooleanField(field);
            }
            else if (fieldType == "integer")
            {
                this.CreateIntegerField(field);
            }
            else if (fieldType == "optionset")
            {
                this.CreateOptionSetField(field);
            }
        }
        private string GetNextRelationshipName(EntityMetadata em, JToken specField)
        {
            string result = null;

            var targetentity = JSONUtil.GetText(specField, "target-entity");

            int num = 0;

            while (num < 1000) // hopefully will find a unique name after 1000 times! :)
            {
                num++;
                string candidateName = String.Format("{0}_{1}_{2}", targetentity, em.SchemaName, num);
                var    rel           = em.ManyToOneRelationships.FirstOrDefault(r => r.SchemaName == candidateName);
                if (rel == null)
                {
                    result = candidateName; break;
                }
            }

            return(result);
        }
        private void CreateMoneyField(JToken field)
        {
            var entitySchemaName = JSONUtil.GetText(field, "entity");
            var displayName      = JSONUtil.GetText(field, "displayname");
            var fieldSchemaName  = JSONUtil.GetText(field, "schemaname");

            CreateAttributeRequest req = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute  = new MoneyAttributeMetadata
                {
                    SchemaName      = fieldSchemaName,
                    RequiredLevel   = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
                    PrecisionSource = 2,
                    DisplayName     = new Label(displayName, 1033),
                    Description     = new Label("", 1033),
                }
            };

            this._cdsConnection.Execute(req);
        }
Esempio n. 12
0
        private void CreateFields(JToken field, Context context)
        {
            var entityname = JSONUtil.GetText(field, "#cds-fields");

            if (entityname == null)
            {
                entityname = JSONUtil.GetText(field, "entityname");
            }
            if (entityname == null)
            {
                return;
            }

            var joEntity = context.FindItemByName(entityname);

            if (joEntity == null)
            {
                return;
            }

            var fields = joEntity["items"];

            CDSCreateField cdsCreateField = new CDSCreateField();

            cdsCreateField.SetConnection(this._cdsConnection);

            foreach (var item in fields)
            {
                var joItem = (JObject)item;
                joItem.Add("entity", entityname);

                var disabled = JSONUtil.GetText(item, "disabled");
                if (disabled != null && disabled == "true")
                {
                    continue;
                }

                cdsCreateField.CreateField(joItem);
            }
        }
Esempio n. 13
0
        public void Process(JObject command, Context context)
        {
            // get default property
            var item = JSONUtil.GetToken(command, "communicate");

            if (item == null)
            {
                item = JSONUtil.GetToken(command, "#communicate");
            }
            if (item == null)
            {
                item = JSONUtil.GetToken(command, "#say");
            }

            // get all other properties
            var select = JSONUtil.GetText(command, "select");
            var rate   = JSONUtil.GetInt32(command, "rate");

            item = context.ReplaceVariables(item);

            if (select != null)
            {
                item = item.SelectToken(select);
            }

            if (rate != null)
            {
                var iRate = (int)rate; // get it? :)
                foreach (char c in item.ToString())
                {
                    Console.Write(c);
                    System.Threading.Thread.Sleep(iRate);
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(item);
            }
        }
        private void CreateIntegerField(JToken field)
        {
            var entitySchemaName = JSONUtil.GetText(field, "entity");
            var displayName      = JSONUtil.GetText(field, "displayname");
            var fieldSchemaName  = JSONUtil.GetText(field, "schemaname");

            CreateAttributeRequest req = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute  = new IntegerAttributeMetadata
                {
                    SchemaName    = fieldSchemaName,
                    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
                    //MaxValue = field.MaxValue == null ? null : field.MaxValue,
                    //MinValue = field.MinValue == null ? null : field.MinValue,
                    DisplayName = new Label(displayName, 1033),
                    Description = new Label("", 1033)
                }
            };

            this._cdsConnection.Execute(req);
        }
        public void Process(JObject command, Context context)
        {
            var    siteurl                 = JSONUtil.GetText(command, "url");
            var    clientContext           = new SP.ClientContext(siteurl);
            string serverRelativeUrlOfFile = JSONUtil.GetText(command, "sourcefile");
            string fileDestinationPath     = JSONUtil.GetText(command, "destinationfile");

            clientContext.Credentials = CredentialCache.DefaultNetworkCredentials;
            var un = JSONUtil.GetText(command, "un");
            var p  = new SecureString();
            var pw = JSONUtil.GetText(command, "pw");

            foreach (var c in pw)
            {
                p.AppendChar(c);
            }

            // doesn't appear to work for non onmicrosoft.com (corp) credentials
            // need cookies?
            clientContext.Credentials = new SharePointOnlineCredentials(un, p);

            using (SP.FileInformation sharePointFile =
                       SP.File.OpenBinaryDirect(clientContext, serverRelativeUrlOfFile))
            {
                using (Stream destFile = System.IO.File.OpenWrite(fileDestinationPath))
                {
                    byte[] buffer = new byte[8 * 1024];
                    int    byteReadInLastRead;
                    while ((byteReadInLastRead = sharePointFile.Stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        destFile.Write(buffer, 0, byteReadInLastRead);
                    }
                }
            }

            System.Diagnostics.Process.Start(fileDestinationPath);
        }
Esempio n. 16
0
        public void Process(JObject command, Context context)
        {
            var file = JSONUtil.GetText(command, "#save-json");

            if (file == null)
            {
                file = JSONUtil.GetText(command, "file");
            }

            string set     = CommandEngine.GetCommandArgument(command, "set");
            string mode    = JSONUtil.GetText(command, "mode");
            var    rawItem = command["item"];

            file = context.ReplaceVariables(file);

            JToken item = null;

            if (set != null)
            {
                item = (JToken)context.Fetch(set);
            }
            else if (rawItem != null)
            {
                item = rawItem;
            }

            Console.WriteLine("Saving {0} as {1}", set, file);
            if (mode == "append")
            {
                JSONUtil.AppendToFile(item, file);
            }
            else
            {
                JSONUtil.WriteFile(item, file);
            }
        }
        /// <summary>
        /// Create the LookupField in CDS
        /// </summary>
        /// <param name="entity">
        /// Uses: Entity.CollectionName
        /// </param>
        /// <param name="field">
        /// Uses: LookupField.SchemaName, .LookupToEntity, .LookupToField
        /// </param>
        public void CreateLookupField(JToken field)
        {
            var entitySchemaName = JSONUtil.GetText(field, "entity");
            var displayName      = JSONUtil.GetText(field, "displayname");
            var fieldSchemaName  = JSONUtil.GetText(field, "schemaname");

            var targetentity = JSONUtil.GetText(field, "target-entity");
            var targetfield  = JSONUtil.GetText(field, "target-field");

            var relationshipname = JSONUtil.GetText(field, "relname");

            var em = this._cdsConnection.GetEntityMetadata(entitySchemaName);

            CreateOneToManyRequest req = new CreateOneToManyRequest();

            // define the general lookup metadata
            var la = new LookupAttributeMetadata();

            la.Description   = new Label("", 1033);
            la.DisplayName   = new Label(displayName, 1033);
            la.LogicalName   = fieldSchemaName;
            la.SchemaName    = fieldSchemaName;
            la.RequiredLevel = new AttributeRequiredLevelManagedProperty(
                AttributeRequiredLevel.Recommended);
            req.Lookup = la;

            // define the 1:N relationship
            var rel = new OneToManyRelationshipMetadata();

            // 1:N associated menu config
            var amc = new AssociatedMenuConfiguration();

            amc.Behavior = AssociatedMenuBehavior.UseCollectionName;
            amc.Group    = AssociatedMenuGroup.Details;
            amc.Label    = em.DisplayCollectionName;
            amc.Order    = 10000;
            rel.AssociatedMenuConfiguration = amc;

            // 1:N cascade behavior config
            var cc = new CascadeConfiguration();

            cc.Assign   = CascadeType.NoCascade;
            cc.Delete   = CascadeType.RemoveLink;
            cc.Merge    = CascadeType.NoCascade;
            cc.Reparent = CascadeType.NoCascade;
            cc.Share    = CascadeType.NoCascade;
            cc.Unshare  = CascadeType.NoCascade;
            rel.CascadeConfiguration = cc;

            // 1:N entity reference
            rel.ReferencedEntity    = targetentity;
            rel.ReferencedAttribute = targetfield;
            rel.ReferencingEntity   = entitySchemaName;

            if (relationshipname == null)
            {
                relationshipname = this.GetNextRelationshipName(em, field);
            }
            rel.SchemaName = relationshipname;

            req.OneToManyRelationship = rel;

            this._cdsConnection.Execute(req);
        }
Esempio n. 18
0
 public static string GetCommandArgument(JObject command, string argName)
 {
     return(JSONUtil.GetText(command, argName));
 }