Пример #1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            _songViewModel = validationContext.ObjectInstance as SongViewModel;

            _midiFileHandler   = _songViewModel?.MidiFileHandler;
            _chordsFileHandler = _songViewModel?.ChordsFileHandler;
            _melodyTrackIndex  = _songViewModel?.MelodyTrackIndex;


            // nothing to check if no file is uploaded for current file handler property
            if (value == null)
            {
                return(ValidationResult.Success);
            }
            else
            {
                _fileHandler = value as HttpPostedFileBase;
            }

            // set the subjected validated file type according to the subject file handler
            if (_fileHandler == _midiFileHandler)
            {
                _songFileType = SongFileType.MidiOriginalFile;
            }
            else
            {
                _songFileType = SongFileType.ChordProgressionFile;
            }

            // validate file metadata (size, mime type, etc)
            if (!FileUploadValidation.IsFileMetadataValid(_fileHandler, _songFileType, out _errorMessage))
            {
                return(new ValidationResult(_errorMessage));
            }

            // fetch chords data from new chords file or from existing db record
            if (_chordsFileHandler != null)
            {
                if (!IsChordsFileValid(_chordsFileHandler, _songFileType, out _bars, out _errorMessage))
                {
                    return(new ValidationResult(_errorMessage));
                }
            }

            // validations for MIDI progression file
            if (_songFileType == SongFileType.MidiOriginalFile)
            {
                if (!FileUploadValidation.IsMidiFileValid(_midiFileHandler, out _midi, out _errorMessage))
                {
                    return(new ValidationResult(_errorMessage));
                }
            }

            // aditional validation for midi file and midi vs chords file
            if (_fileHandler == _midiFileHandler)
            {
                // validate melody track index is not out of bounds
                if (!CompositionContext.IsMelodyTrackIndexValid((int?)_melodyTrackIndex, _midi, out _errorMessage))
                {
                    return(new ValidationResult(_errorMessage));
                }

                // validate that bars in CHORD progression are compatible with MIDI file
                if (!CompositionContext.AreBarsCompatible(_bars, _midi, out _errorMessage))
                {
                    return(new ValidationResult(_errorMessage));
                }
            }

            // if we got this far then hopefully everything is okay
            return(ValidationResult.Success);
        }