<?xml version="1.0" encoding="UTF-8"?>
<pages xmlns="http://jboss.com/products/seam/pages"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.1.xsd"
       no-conversation-view-id="/template.xhtml"
                 login-view-id="/home.xhtml">

    <page view-id="/profile.xhtml" login-required="true">
        <navigation from-action="#{profile.leave}">
            <redirect view-id="/users.xhtml"/>
        </navigation>
        <navigation from-action="#{profile.update}">
            <redirect view-id="/users.xhtml"/>
        </navigation>
    </page>

    <page view-id="/users.xhtml" login-required="true">
        <navigation from-action="#{profile.select(userProfile)}">
            <redirect view-id="/profile.xhtml"/>
        </navigation>
    </page>

    <page view-id="/packet_list.xhtml" login-required="true">
        <navigation from-action="#{packetProfile.select(packet)}">
            <redirect view-id="/packet.xhtml"/>
        </navigation>
    </page>

    <page view-id="/packet.xhtml" login-required="true">
        <navigation from-action="#{profile.select(packetProfile.firstTBP.user1Answer.user)}">
            <redirect view-id="/profile.xhtml"/>
        </navigation>
        <navigation from-action="#{taskProfile.select(false, false, true, false)}">
            <redirect view-id="/task.xhtml"/>
        </navigation>
    </page>

    <page view-id="/task.xhtml" login-required="true">
        <navigation from-action="#{workOnAnswer.confirm}">
            <redirect view-id="/dendrolog.xhtml"/>
        </navigation>
        <navigation from-action="#{workOnAnswer.select}">
            <redirect view-id="/perform.xhtml"/>
        </navigation>
    </page>

<!--
    <page view-id="/packet.xhtml" login-required="true">
        <navigation from-action="#{profile.select(p.firstTBP.user1Answer.user)}">
            <redirect view-id="/profile.xhtml"/>
        </navigation>
        <navigation from-action="#{profile.select(p.firstTBP.user2Answer.user)}">
            <redirect view-id="/profile.xhtml"/>
        </navigation>
    </page>
-->

    <page view-id="/register.xhtml" login-required="true">
        <restrict>#{s:hasRole('admin')}</restrict>
        <navigation from-action="#{register.register}">
            <rule if="#{register.registered}">
                <redirect view-id="/users.xhtml"/>
            </rule>
        </navigation>
    </page>


    <page view-id="/password.xhtml" login-required="true">
        <navigation>
            <rule if="#{changePassword.changed}">
                <redirect view-id="/template.xhtml"/>
            </rule>
        </navigation>
    </page>

    <page view-id="/dendrolog.xhtml" login-required="true">
        <restrict>#{s:hasRole('dendrolog')}</restrict>
        <navigation from-action="#{workOnAnswer.select}">
            <redirect view-id="/perform.xhtml"/>
        </navigation>
        <navigation from-action="#{taskProfile.select(true, false, false, true)}">
            <redirect view-id="/task.xhtml"/>
        </navigation>
        <navigation from-action="#{taskProfile.select(false, true, false, true)}">
            <redirect view-id="/task.xhtml"/>
        </navigation>
    </page>

    <page view-id="/gramatyk.xhtml" login-required="true">
        <restrict>#{s:hasRole('gramatyk')}</restrict>
        <navigation from-action="#{profile.select(a.user)}">
            <redirect view-id="/profile.xhtml"/>
        </navigation>
        <navigation from-action="#{packetProfile.select(a.taskBusinessProcess.packet)}">
            <redirect view-id="/packet.xhtml"/>
        </navigation>
        <navigation from-action="#{taskProfile.select(false, false, true, false)}">
            <redirect view-id="/task.xhtml"/>
        </navigation>
    </page>

    <page view-id="/perform.xhtml" login-required="true">
        <navigation from-action="#{workOnAnswer.leave()}">
            <rule if="#{s:hasRole('superdendrolog')}">
                <redirect view-id="/superdendrolog.xhtml"/>
            </rule>
            <rule if="#{s:hasRole('gramatyk')}">
                <redirect view-id="/gramatyk.xhtml"/>
                </rule>
            <redirect view-id="/dendrolog.xhtml"/>
        </navigation>
        <navigation from-action="#{workOnAnswer.confirm()}">
            <redirect view-id="/dendrolog.xhtml"/>
        </navigation>        
        <navigation from-action="#{workOnAnswer.resign()}">
            <redirect view-id="/superdendrolog.xhtml"/>
        </navigation>
        <navigation from-action="#{workOnAnswer.accept()}">
            <rule if="#{s:hasRole('superdendrolog')}">
                <redirect view-id="/superdendrolog.xhtml"/>
            </rule>
            <rule if-outcome="false">
                <redirect view-id="/perform.xhtml"/>
            </rule>
            <redirect view-id="/dendrolog.xhtml"/>
        </navigation>
    </page>

    <page view-id="/superdendrolog.xhtml" login-required="true">
        <restrict>#{s:hasRole('superdendrolog')}</restrict>
        <navigation from-action="#{workOnAnswer.select}">
            <redirect view-id="/perform.xhtml"/>
        </navigation>
    </page>

    <page view-id="*">
    
        <navigation from-action="#{identity.logout}">
            <redirect view-id="/home.xhtml"/>
        </navigation>

        <navigation from-action="#{identity.login}">
            <rule if="#{s:hasRole('admin')}">
                <redirect view-id="/users.xhtml"/>
            </rule>
            <rule if="#{s:hasRole('gramatyk')}">
                <redirect view-id="/gramatyk.xhtml"/>
            </rule>
            <rule if="#{s:hasRole('superdendrolog')}">
                <redirect view-id="/superdendrolog.xhtml"/>
            </rule>
            <rule if="#{s:hasRole('dendrolog')}">
                <redirect view-id="/dendrolog.xhtml"/>
            </rule>
            <redirect view-id="/home.xhtml"> <!-- pta -->
                <message severity="warn">Nie masz żadnych ról w systemie.</message>
            </redirect>
        </navigation>
    </page>

    <exception class="org.jboss.seam.security.NotLoggedInException">
        <redirect view-id="/home.xhtml">
            <message severity="warn">Musisz się zalogować</message>
        </redirect>
    </exception>
    
    <exception class="javax.faces.application.ViewExpiredException">
        <redirect view-id="/home.xhtml">
            <message severity="warn">Sesja wygasła</message>
        </redirect>
    </exception>

    <exception class="org.jboss.seam.security.AuthorizationException">
        <end-conversation/>
        <redirect view-id="/home.xhtml">
            <message severity="warn">Nie masz uprawnień do oglądania tej strony</message>
        </redirect>
    </exception>

</pages>
