}
}

var RunValidations = file_manager.HandlerFunc(func(file *models.File) (err error) {

	defer func() {
		if err != nil {
			return
		}
		if err = file.Save(); err != nil {
			l.Printf("RunValidations Error: Problem updating file -  %s : %s", file.FileName, err)
		}
	}()
	file.Load()
	workflow := file.Workflow
	for _, validation := range workflow.Validations {
		fn := validations[validation]
		passed, err := fn(file)
		var msg string
		if err != nil {
			msg = err.Error()
		}
		file.ValidationResult[validation] = models.ValidationResult{Passed: passed, ErrorMessage: msg}
	}

	return

})

func isZeroOfUnderlyingType(x interface{}) bool {
	return x == reflect.Zero(reflect.TypeOf(x)).Interface()
var AttachToPattern = file_manager.HandlerFunc(func(file *models.File) (err error) {
	patterns := models.Patterns{}
	if err = patterns.FindAllByFileMatch(file.FileName); err != nil {
		return
	}

	defer func() {
		if err != nil {
			return
		}
		if err = file.Save(); err != nil {
			l.Printf("Problem updating file -  %s : %s", file.FileName, err)
		}
	}()

	switch len(patterns) {
	case 0:
		file.Status = models.NO_PATTERN
		file.PatternId = sql.NullInt64{}
	case 1:
		file.Status = models.HAS_PATTERN
		file.Pattern = patterns[0]
	default:
		if patterns[0].Priority == patterns[1].Priority {
			file.Status = models.MANY_PATTERNS
			file.PatternId = sql.NullInt64{}
		} else {
			file.Status = models.HAS_PATTERN
			file.Pattern = patterns[0]
		}
	}

	if file.Status == models.HAS_PATTERN {
		res := file.Pattern.Regexp.Regx.FindAllStringSubmatch(file.FileName, -1)[0][1:]
		attributes := make(models.JSONB, len(res))
		for index, el := range file.Pattern.Parts {
			attributes[el.Key] = res[index]
		}
		file.Attributes = attributes
	}

	return
})
var AttachToWorkflow = file_manager.HandlerFunc(func(file *models.File) (err error) {

	defer func() {
		if err != nil {
			return
		}
		if err = file.Save(); err != nil {
			l.Printf("Problem updating file -  %s : %s", file.FileName, err)
		}
	}()

	workflows := models.Workflows{}
	if err = workflows.FindAllByEntryPointAndPatternId(file.PatternId, file.EntryPoint); err != nil {
		file.Status = models.HAS_NO_WORKFLOW
		file.Error = fmt.Sprintf("Problem attaching workflow to file -  %s : %s", file.FileName, err)
		l.Println(file.Error)
		return
	}

	if len(workflows) == 0 {
		file.Status = models.HAS_NO_WORKFLOW
		file.Error = fmt.Sprintf("No workflows matched by entry point %q and pattern %d", file.EntryPoint, file.PatternId)
		l.Println(file.Error)
		return
	}

	detectWorkflows(file, workflows)
	return
})