class ProgramOrgUnitCheck implements Checker {

    @Override
    public ImportSummary check( ImmutableEvent event, WorkContext ctx )
    {
        ProgramInstance programInstance = ctx.getProgramInstanceMap().get( event.getUid() );

        if ( programInstance != null )
        {
            final IdScheme orgUnitIdScheme = ctx.getImportOptions().getIdSchemes().getOrgUnitIdScheme();

            OrganisationUnit orgUnit = null;

            final Set<OrganisationUnit> organisationUnits = programInstance.getProgram().getOrganisationUnits();

            for ( OrganisationUnit ou : organisationUnits )
            {
                if ( orgUnitIdScheme.isAttribute() )
                {
                    final Optional<OrganisationUnit> ouByAttributeScheme = getByAttributeScheme( ou, event,
                        orgUnitIdScheme );
                    if ( ouByAttributeScheme.isPresent() )
                    {
                        orgUnit = ouByAttributeScheme.get();
                        break;
                    }
                }
                else
                {
                    if ( functionMap.getOrDefault( orgUnitIdScheme, DEFAULT_FUNCTION ).apply( ou, event ) )
                    {
                        orgUnit = ou;
                        break;
                    }
                }
            }

            if ( orgUnit == null )
            {
                return error( "Program is not assigned to this Organisation Unit: " + event.getOrgUnit(),
                    event.getEvent() );
            }
        }

        return success();
    }

}