package org.xcolab.view.config.rewrite.rules.legacy;

import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.config.Direction;
import org.ocpsoft.rewrite.servlet.config.Forward;
import org.ocpsoft.rewrite.servlet.config.Path;
import org.ocpsoft.rewrite.servlet.config.Redirect;

import org.xcolab.view.config.rewrite.rules.RewriteRuleProvider;

public class LegacyProposalRewriteRules implements RewriteRuleProvider {

    @Override
    public void configure(ConfigurationBuilder configurationBuilder) {
        configurationBuilder
                .addRule()
                    .when(Direction.isInbound()
                            .and(Path.matches("/web/guest/{portletName}")))
                    .perform(Forward.to("/contests"))
                    .where("portletName").matches("(plans|dialogues|challenges|trends)")
                .addRule()
                    .when(Direction.isInbound()
                            .and(Path.matches("/web/guest/plans/-/plans/contestsType/prior")))
                    .perform(Forward.to("/contests?viewType=GRID&showAllContests=true"))
                .addRule()
                    .when(Direction.isInbound()
                            .and(Path.matches("/web/guest/plans/-/plans/contestsType/all")))
                    .perform(Forward.to("/contests?viewType=GRID&showAllContests=true"))
                .addRule()
                    .when(Direction.isInbound()
                            .and(Path.matches("/plans")))
                    .perform(Redirect.permanent("/contests"));

        // legacy urls
        configurationBuilder
                .addRule()
                    .when(Direction.isInbound().and(
                            Path.matches("/web/guest/{portletName}/-/plans/contestId/{contestId}")
                                    .or(Path.matches("/{portletName}/-/plans/contestId/{contestId}"))
                                    .or(Path.matches("/web/guest/{portletName}/-/plans/contestId/{contestId}/page/{pageName}"))
                                    .or(Path.matches("/{portletName}/-/plans/contestId/{contestId}/page/{pageName}"))))
                    .perform(Forward.to("/contests/legacy/contest/{contestId}"))
                    .where("portletName").matches("(plans|dialogues|challenges|trends)")
                .addRule()
                    .when(Direction.isInbound().and(
                            Path.matches("/web/guest/{portletName}/-/plans/contestId/{contestId}/phase/{phaseId}")
                                .or(Path.matches("/{portletName}/-/plans/contestId/{contestId}/phase/{phaseId}"))))
                    .perform(Forward.to("/contests/legacy/contest/{contestId}"))
                    .where("portletName").matches("(plans|dialogues|challenges|trends)")
                .addRule()
                    .when(Direction.isInbound().and(
                            Path.matches("/web/guest/{portletName}/-/plans/contestId/{contestId}/planId/{proposalId}")
                                    .or(Path.matches("/{portletName}/-/plans/contestId/{contestId}/planId/{proposalId}"))
                                    .or(Path.matches("/web/guest/{portletName}/-/plans/contestId/{contestId}/planId/{proposalId}/{path}"))
                                    .or(Path.matches("/{portletName}/-/plans/contestId/{contestId}/planId/{proposalId}/{path}"))
                    ))
                    .perform(Forward.to("/contests/legacy/contest/{contestId}/proposal/{proposalId}"))
                    .where("portletName").matches("(plans|dialogues|challenges|trends)")
                    .where("path").matches(".*")
                .addRule()
                    .when(Direction.isInbound().and(
                            Path.matches("/web/guest/{portletName}/-/plans/contestId/{contestId}/phaseId/{phaseId}/planId/{proposalId}")
                                    .or(Path.matches("/{portletName}/-/plans/contestId/{contestId}/phaseId/{phaseId}/planId/{proposalId}"))
                                    .or(Path.matches("/web/guest/{portletName}/-/plans/contestId/{contestId}/phaseId/{phaseId}/planId/{proposalId}/{path}"))
                                    .or(Path.matches("/{portletName}/-/plans/contestId/{contestId}/phaseId/{phaseId}/planId/{proposalId}/{path}"))
                    ))
                    .perform(Forward.to("/contests/legacy/contest/{contestId}/proposal/{proposalId}?phaseId={phaseId}"))
                    .where("portletName").matches("(plans|dialogues|challenges|trends)")
                    .where("path").matches(".*")
                .addRule()
                    .when(Direction.isInbound().and(
                            Path.matches("/web/guest/plans/-/plans/contests{path}")
                                .or(Path.matches("/plans/-/plans/contests{path}"))
                    ))
                    .perform(Redirect.permanent("/contests{path}"))
                    .where("path").matches(".*")
                .addRule()
                    .when(Direction.isInbound().and(Path.matches("/web/guest/proposals")))
                    .perform(Redirect.permanent("/contests"));
    }
}

@@ -152,59 +152,8 @@
 on;%0A
-import org.ocpsoft.rewrite.servlet.config.Forward;%0A
 impo
@@ -196,16 +196,16 @@
 g.Path;%0A
+
 import o
@@ -679,34 +679,42 @@
    .perform(
-Forward.to
+Redirect.permanent
 (%22/contests%22
@@ -1000,34 +1000,42 @@
    .perform(
-Forward.to
+Redirect.permanent
 (%22/contests?
@@ -1265,34 +1265,42 @@
    .perform(
-Forward.to
+Redirect.permanent
 (%22/contests?
@@ -2140,34 +2140,42 @@
    .perform(
-Forward.to
+Redirect.temporary
 (%22/contests/
@@ -2629,34 +2629,42 @@
    .perform(
-Forward.to
+Redirect.temporary
 (%22/contests/
@@ -3419,34 +3419,42 @@
    .perform(
-Forward.to
+Redirect.temporary
 (%22/contests/
@@ -4360,18 +4360,26 @@
 orm(
-Forward.to
+Redirect.temporary
 (%22/c

Fix legacy redirects