<commit_before>/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.ui.web.global.header;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Locale;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.StringResourceModel;
import org.openengsb.ui.web.ContextSetPage;
import org.openengsb.ui.web.Index;
import org.openengsb.ui.web.SendEventPage;
import org.openengsb.ui.web.ServiceListPage;
import org.openengsb.ui.web.TestClient;
import org.openengsb.ui.web.UserService;
import org.openengsb.ui.web.global.BookmarkablePageLabelLink;

@SuppressWarnings("serial")
public class HeaderTemplate extends Panel {
    private final ArrayList<HeaderMenuItem> menuItems = new ArrayList<HeaderMenuItem>();
    private final ArrayList<String> avialableItems = new ArrayList<String>();

    private static String menuIndex;

    public HeaderTemplate(String id, String menuIndex) {
        super(id);

        baseInitialization(menuIndex);
        initializeMenu();
    }

    private void baseInitialization(String menuIndex) {
        add(new Link<Object>("lang.en") {
            @Override
            public void onClick() {
                this.getSession().setLocale(Locale.ENGLISH);
            }
        });
        add(new Link<Object>("lang.de") {
            @Override
            public void onClick() {
                this.getSession().setLocale(Locale.GERMAN);
            }
        });

        HeaderTemplate.menuIndex = menuIndex;

        add(new BookmarkablePageLink<Index>("logo", Index.class));
        add(new Label("version", System.getProperty("openengsb.version.number") + " \""
            + System.getProperty("openengsb.version.name.adjective") + " "
            + System.getProperty("openengsb.version.name.noun") + "\""));
    }

    private void initMainMenuItems() {
        addHeaderMenuItem("Index", Index.class, "index.title","");

        addHeaderMenuItem("TestClient", TestClient.class, "testclient.title","");
        addHeaderMenuItem("SendEventPage", SendEventPage.class, "sendevent.title","");
        addHeaderMenuItem("ContextSetPage", ContextSetPage.class, "context.title","");
        addHeaderMenuItem("ServiceListPage", ServiceListPage.class, "serviceList.title","");
        addHeaderMenuItem("UserService", UserService.class, "userService.title", "ROLE_ADMIN");
    }

    private void initializeMenu() {
        initMainMenuItems();

        if (HeaderTemplate.getActiveIndex() == null || !avialableItems.contains(HeaderTemplate.getActiveIndex())) {
            // update menu item to index, because page index is not found!
            HeaderTemplate.menuIndex = "Index";
        }

        // generate main navigation
        ListView<HeaderMenuItem> headerMenuItems = new ListView<HeaderMenuItem>("headerMenuItems", menuItems) {
            @Override
            protected void populateItem(ListItem<HeaderMenuItem> item) {
                HeaderMenuItem menuItem = item.getModelObject();
                item.add(menuItem.getLink());

                // set menu item to active
                if (menuItem.getItemName().equals(HeaderTemplate.getActiveIndex())) {
                    item.add(new AttributeModifier("class", true, new AbstractReadOnlyModel<String>() {
                        @Override
                        public String getObject() {
                            return "active";
                        }
                    }));
                }
            }
        };

        add(headerMenuItems);
    }

    /**
     * get the name of the current active menu item
     */
    public static String getActiveIndex() {
        return HeaderTemplate.menuIndex;
    }

    /**
     * add a new item to main header navigation
     *  index     defines the name of the index, should be the class name
     *  linkClass defines the class name to be linked to
     *  langKey   defines the language key for the text which should be displayed
     *  authority defines who is authorized to see the link
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void addHeaderMenuItem(String index, Class<? extends WebPage> linkClass, String langKey, String authority) {
        StringResourceModel label = new StringResourceModel(langKey, this, null);
        BookmarkablePageLabelLink pageLabelLink = new BookmarkablePageLabelLink("link", linkClass, label);
        addAuthorizationRoles(pageLabelLink, authority);
        menuItems.add(new HeaderMenuItem(index, pageLabelLink));
        avialableItems.add(index);
    }

    private void addAuthorizationRoles(BookmarkablePageLabelLink pageLabelLink, String authority) {
        if (authority != null && !"".equals(authority)) {
            MetaDataRoleAuthorizationStrategy.authorize(pageLabelLink, RENDER, authority);
        } else {
            MetaDataRoleAuthorizationStrategy.authorizeAll(pageLabelLink,RENDER);
        }
    }


    private static class HeaderMenuItem implements Serializable {
        private final String index;
        private final BookmarkablePageLabelLink<? extends WebPage> link;

        public HeaderMenuItem(String index, BookmarkablePageLabelLink<? extends WebPage> link) {
            this.index = index;
            this.link = link;
        }

        public String getItemName() {
            return index;
        }

        public BookmarkablePageLabelLink<? extends WebPage> getLink() {
            return link;
        }
    }

}
<commit_msg>fixed checkstyle error<commit_after>/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.ui.web.global.header;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Locale;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.StringResourceModel;
import org.openengsb.ui.web.ContextSetPage;
import org.openengsb.ui.web.Index;
import org.openengsb.ui.web.SendEventPage;
import org.openengsb.ui.web.ServiceListPage;
import org.openengsb.ui.web.TestClient;
import org.openengsb.ui.web.UserService;
import org.openengsb.ui.web.global.BookmarkablePageLabelLink;

@SuppressWarnings("serial")
public class HeaderTemplate extends Panel {
    private final ArrayList<HeaderMenuItem> menuItems = new ArrayList<HeaderMenuItem>();
    private final ArrayList<String> avialableItems = new ArrayList<String>();

    private static String menuIndex;

    public HeaderTemplate(String id, String menuIndex) {
        super(id);

        baseInitialization(menuIndex);
        initializeMenu();
    }

    private void baseInitialization(String menuIndex) {
        add(new Link<Object>("lang.en") {
            @Override
            public void onClick() {
                this.getSession().setLocale(Locale.ENGLISH);
            }
        });
        add(new Link<Object>("lang.de") {
            @Override
            public void onClick() {
                this.getSession().setLocale(Locale.GERMAN);
            }
        });

        HeaderTemplate.menuIndex = menuIndex;

        add(new BookmarkablePageLink<Index>("logo", Index.class));
        add(new Label("version", System.getProperty("openengsb.version.number") + " \""
            + System.getProperty("openengsb.version.name.adjective") + " "
            + System.getProperty("openengsb.version.name.noun") + "\""));
    }

    private void initMainMenuItems() {
        addHeaderMenuItem("Index", Index.class, "index.title", "");

        addHeaderMenuItem("TestClient", TestClient.class, "testclient.title", "");
        addHeaderMenuItem("SendEventPage", SendEventPage.class, "sendevent.title", "");
        addHeaderMenuItem("ContextSetPage", ContextSetPage.class, "context.title", "");
        addHeaderMenuItem("ServiceListPage", ServiceListPage.class, "serviceList.title", "");
        addHeaderMenuItem("UserService", UserService.class, "userService.title", "ROLE_ADMIN");
    }

    private void initializeMenu() {
        initMainMenuItems();

        if (HeaderTemplate.getActiveIndex() == null || !avialableItems.contains(HeaderTemplate.getActiveIndex())) {
            // update menu item to index, because page index is not found!
            HeaderTemplate.menuIndex = "Index";
        }

        // generate main navigation
        ListView<HeaderMenuItem> headerMenuItems = new ListView<HeaderMenuItem>("headerMenuItems", menuItems) {
            @Override
            protected void populateItem(ListItem<HeaderMenuItem> item) {
                HeaderMenuItem menuItem = item.getModelObject();
                item.add(menuItem.getLink());

                // set menu item to active
                if (menuItem.getItemName().equals(HeaderTemplate.getActiveIndex())) {
                    item.add(new AttributeModifier("class", true, new AbstractReadOnlyModel<String>() {
                        @Override
                        public String getObject() {
                            return "active";
                        }
                    }));
                }
            }
        };

        add(headerMenuItems);
    }

    /**
     * get the name of the current active menu item
     */
    public static String getActiveIndex() {
        return HeaderTemplate.menuIndex;
    }

    /**
     * add a new item to main header navigation
     * index     defines the name of the index, should be the class name
     * linkClass defines the class name to be linked to
     * langKey   defines the language key for the text which should be displayed
     * authority defines who is authorized to see the link
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void addHeaderMenuItem(String index, Class<? extends WebPage> linkClass, String langKey, String authority) {
        StringResourceModel label = new StringResourceModel(langKey, this, null);
        BookmarkablePageLabelLink pageLabelLink = new BookmarkablePageLabelLink("link", linkClass, label);
        addAuthorizationRoles(pageLabelLink, authority);
        menuItems.add(new HeaderMenuItem(index, pageLabelLink));
        avialableItems.add(index);
    }

    private void addAuthorizationRoles(BookmarkablePageLabelLink pageLabelLink, String authority) {
        if (authority != null && !"".equals(authority)) {
            MetaDataRoleAuthorizationStrategy.authorize(pageLabelLink, RENDER, authority);
        } else {
            MetaDataRoleAuthorizationStrategy.authorizeAll(pageLabelLink, RENDER);
        }
    }


    private static class HeaderMenuItem implements Serializable {
        private final String index;
        private final BookmarkablePageLabelLink<? extends WebPage> link;

        public HeaderMenuItem(String index, BookmarkablePageLabelLink<? extends WebPage> link) {
            this.index = index;
            this.link = link;
        }

        public String getItemName() {
            return index;
        }

        public BookmarkablePageLabelLink<? extends WebPage> getLink() {
            return link;
        }
    }

}

{'fraction_non_alphanumeric': 0.09953650938378543, 'fraction_numerical': 0.0012157130917103563, 'mean_word_length': 2.9230998509687036, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2017940', 'n_tokens_mistral': 3444, 'n_tokens_neox': 3411, 'n_words': 841}