<%args>
    $workflow_type    => undef
    $role             => undef
    $profile          => undef
    $subject_style    => undef
    $subject          => undef
    $subject_alt_name => undef
    $info             => undef
    $keygen           => undef
    $spkac            => undef
    $pkcs10           => undef
    $key              => undef
</%args>
<%init>
    ## we must know which workflow we should use for the validation
    return if (not defined $subject_style);

    ## build PARAMS
    my %params = ();

    $params{"cert_role"}             = $role             if (defined $role);
    $params{"cert_profile"}          = $profile          if (defined $profile);
    $params{"cert_subject_style"}    = $subject_style    if (defined $subject_style);
    $params{"cert_subject"}          = $subject          if (defined $subject);
    $params{"cert_subject_alt_name"} = $subject_alt_name if (defined $subject_alt_name);
    $params{"cert_info"}             = $info             if (defined $info);
    $params{"spkac"}                 = $spkac            if (defined $spkac);
    $params{"pkcs10"}                = $pkcs10           if (defined $pkcs10);
    $params{"private_key"}           = $key              if (defined $key);

    ## this type must always be set to intercept empty CSRs
    if ($spkac or $keygen eq "SPKAC")
    {
        $params{"csr_type"} = "spkac";
    } else {
        $params{"csr_type"} = "pkcs10";
    }

    ## create the workflow for the new CSR
    my $msg = $context->{client}->send_receive_command_msg (
                   "create_workflow_instance",
                   {WORKFLOW => $workflow_type,
                    PARAMS   => \%params});
    my $errors = [];
    if (not ref $msg)
    {
        ## ups, this is completely unexpected
        $errors = [ {LABEL => $msg} ];
    }
    elsif (exists $msg->{SERVICE_MSG} and
           $msg->{SERVICE_MSG} eq "ERROR")
    {
        ## detected error
        $errors = [ $m->comp ('/lib/get_deep_error.mhtml', 'msg' => $msg) ];
    }
    $key = "" if (scalar @{$errors});

    ## now store the private key
    if ($key)
    {
        my $key_msg = $context->{client}->send_receive_command_msg (
                          "create_workflow_instance",
                          {WORKFLOW => "I18N_OPENXPKI_WF_TYPE_STORE_PRIVATE_KEY",
                           PARAMS   => {"private_key" => $key,
                                        "workflow_id" => $msg->{WORKFLOW}->{ID}}});
        if (exists $key_msg->{SERVICE_MSG} and
            $key_msg->{SERVICE_MSG} eq "ERROR")
        {
            ## FIXME: uups, we got a real problem the CSR is in the database
            ## FIXME: but not the private key !!!
            ## FIXME: can we log this event to system.fatal ?
            $errors = [ $m->comp ('/lib/get_deep_error.mhtml', 'msg' => $msg) ];
        }
    }

    ## handle errors
    return $errors if (scalar @{$errors});

    ## this should be a serial number of the workflow
    return $msg;
</%init>
