Esempio n. 1
0
    protected virtual bool FilterByAttribute(Item item)
    {
      if (string.IsNullOrEmpty(FindByAttribute.Key) || string.IsNullOrEmpty(FindByAttribute.Value))
        return true;

      if (_attributeRegex == null)
      {
        var options = RegexOptions.Compiled;
        if (!FindByAttributeCaseSensitive)
          options |= RegexOptions.IgnoreCase;

        _attributeRegex = new Regex(FindByAttribute.Value, options);
      }

      var attrcmd = new GetAttributes();
      attrcmd.Initialise(Context, Formatter);
      attrcmd.Attribute = FindByAttribute.Key;
      attrcmd.Path = item.ID.ToString();

      var result = attrcmd.Run();
      if (result.Status == CommandStatus.Success)
      {
        var output = result.Message;
        return _attributeRegex.IsMatch(output);
      }

      return false;
    }
Esempio n. 2
0
    public override CommandResult Run()
    {
      if (string.IsNullOrEmpty(Attribute))
        return new CommandResult(CommandStatus.Failure, Constants.Messages.MissingRequiredParameter.FormatWith("attribute"));

      if (string.IsNullOrEmpty(Value))
        return new CommandResult(CommandStatus.Failure, Constants.Messages.MissingRequiredParameter.FormatWith("value"));

      // resolve template
      TemplateItem template = null;

      if (Attribute == "template")
      {
        template = Context.CurrentDatabase.GetItem(Value);
        if (template == null)
        {
          if (ID.IsID(Attribute))
            return new CommandResult(CommandStatus.Failure, "Failed to find template with ID '" + Attribute + "'");
          else
            return new CommandResult(CommandStatus.Failure, "Failed to find template '" + Attribute + "'");
        }
      }

      string toRet = string.Empty;

      using (var cs = new ContextSwitcher(Context, Path))
      {
        if (cs.Result.Status != CommandStatus.Success)
          return cs.Result;

        string query = Attribute;

        Item item = Context.CurrentItem;
        item.Editing.BeginEdit();

        switch (Attribute)
        {
          case "name":
            Value = Value.Replace("$prev", item.Name);
            item.Name = Value;
            break;

          case "template":
            item.TemplateID = template.ID;
            if (Value.StartsWith("{"))
              query += "id";
            break;

          default:
            return new CommandResult(CommandStatus.Failure, "Unknown attribute " + Attribute);
        }

        item.Editing.EndEdit();

        GetAttributes ga = new GetAttributes();
        ga.Initialise(Context, Formatter);
        ga.Attribute = query;
        CommandResult result = ga.Run();
        StringBuilder sb = new StringBuilder(200);
        Formatter.PrintDefinition(query, result.ToString(), sb);
        toRet = sb.ToString();
      }

      return new CommandResult(CommandStatus.Success, toRet);
    }