James Moger
2012-02-16 3cc6e2de29a0fa33dd585e938e1614a6dd5f9755
commit | author | age
831469 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.wicket.panels;
17
18 import java.util.ArrayList;
8f73a7 19 import java.util.Collections;
831469 20 import java.util.List;
JM 21
22 import org.apache.wicket.markup.html.basic.Label;
dd9ae7 23 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
831469 24 import org.apache.wicket.markup.html.link.ExternalLink;
JM 25 import org.apache.wicket.markup.repeater.Item;
26 import org.apache.wicket.markup.repeater.data.DataView;
27 import org.apache.wicket.markup.repeater.data.ListDataProvider;
28
29 import com.gitblit.Constants.FederationRequest;
30 import com.gitblit.Constants.FederationToken;
31 import com.gitblit.GitBlit;
8f73a7 32 import com.gitblit.Keys;
93f0b1 33 import com.gitblit.utils.FederationUtils;
831469 34 import com.gitblit.wicket.WicketUtils;
dd9ae7 35 import com.gitblit.wicket.pages.SendProposalPage;
831469 36
JM 37 public class FederationTokensPanel extends BasePanel {
38
39     private static final long serialVersionUID = 1L;
40
41     public FederationTokensPanel(String wicketId, final boolean showFederation) {
42         super(wicketId);
43
2179fb 44         final String baseUrl = WicketUtils.getGitblitURL(getRequest());
93f0b1 45         add(new ExternalLink("federatedUsers", FederationUtils.asLink(baseUrl, GitBlit.self()
831469 46                 .getFederationToken(FederationToken.USERS_AND_REPOSITORIES),
JM 47                 FederationRequest.PULL_USERS)));
48
93f0b1 49         add(new ExternalLink("federatedSettings", FederationUtils.asLink(baseUrl, GitBlit
831469 50                 .self().getFederationToken(FederationToken.ALL), FederationRequest.PULL_SETTINGS)));
JM 51
52         final List<String[]> data = new ArrayList<String[]>();
53         for (FederationToken token : FederationToken.values()) {
8f73a7 54             data.add(new String[] { token.name(), GitBlit.self().getFederationToken(token), null });
JM 55         }
56         List<String> sets = GitBlit.getStrings(Keys.federation.sets);
57         Collections.sort(sets);
58         for (String set : sets) {
59             data.add(new String[] { FederationToken.REPOSITORIES.name(),
60                     GitBlit.self().getFederationToken(set), set });
831469 61         }
JM 62
63         DataView<String[]> dataView = new DataView<String[]>("row", new ListDataProvider<String[]>(
64                 data)) {
65             private static final long serialVersionUID = 1L;
66             private int counter;
67
68             @Override
69             protected void onBeforeRender() {
70                 super.onBeforeRender();
71                 counter = 0;
72             }
73
74             public void populateItem(final Item<String[]> item) {
75                 final String[] entry = item.getModelObject();
76                 final FederationToken token = FederationToken.fromName(entry[0]);
8f73a7 77                 if (entry[2] == null) {
JM 78                     // standard federation token
79                     item.add(new Label("description", describeToken(token)));
80                 } else {
81                     // federation set token
82                     item.add(new Label("description", entry[2]));
83                 }
831469 84                 item.add(new Label("value", entry[1]));
8f73a7 85
93f0b1 86                 item.add(new ExternalLink("repositoryDefinitions", FederationUtils.asLink(
8f73a7 87                         baseUrl, entry[1], FederationRequest.PULL_REPOSITORIES)));
831469 88
dd9ae7 89                 item.add(new BookmarkablePageLink<Void>("send",
JM 90                         SendProposalPage.class, WicketUtils.newTokenParameter(entry[1])));
831469 91
JM 92                 WicketUtils.setAlternatingBackground(item, counter);
93                 counter++;
94             }
95         };
96         add(dataView.setVisible(showFederation));
97     }
98
99     private String describeToken(FederationToken token) {
100         switch (token) {
101         case ALL:
102             return getString("gb.tokenAllDescription");
103         case USERS_AND_REPOSITORIES:
104             return getString("gb.tokenUnrDescription");
105         case REPOSITORIES:
106         default:
107             return getString("gb.tokenJurDescription");
108         }
109     }
110 }