
package org.sakaiproject.widgets.myconnections.ui;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.head.OnLoadHeaderItem;
import org.apache.wicket.markup.head.PriorityHeaderItem;
import org.apache.wicket.markup.head.StringHeaderItem;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.sakaiproject.profile2.logic.ProfileConnectionsLogic;
import org.sakaiproject.profile2.model.BasicConnection;
import org.sakaiproject.tool.api.SessionManager;

import lombok.extern.apachecommons.CommonsLog;

@CommonsLog
public class WidgetPage extends WebPage {

	private static final long serialVersionUID = 1L;

	@SpringBean(name = "org.sakaiproject.profile2.logic.ProfileConnectionsLogic")
	private ProfileConnectionsLogic connectionsLogic;

	@SpringBean(name = "org.sakaiproject.tool.api.SessionManager")
	private SessionManager sessionManager;

	public WidgetPage() {
		log.debug("BasePage()");

	}

	@Override
	public void onInitialize() {
		super.onInitialize();

		// get current user
		final String currentUserUuid = this.sessionManager.getCurrentSessionUserId();

		// get connections
		final List<BasicConnection> connections = this.connectionsLogic.getBasicConnectionsForUser(currentUserUuid);

		final ListDataProvider<BasicConnection> dataProvider = new ListDataProvider<BasicConnection>(connections);

		final GridView<BasicConnection> gridView = new GridView<BasicConnection>("rows", dataProvider) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(final Item<BasicConnection> item) {
				final BasicConnection connection = item.getModelObject();

				final WebMarkupContainer itemWrap = new WebMarkupContainer("itemWrap");
				itemWrap.add(new ProfileThumbnail("img", Model.of(connection.getUuid())));
				itemWrap.add(new Label("name", Model.of(connection.getDisplayName())));
				item.add(itemWrap);

			}

			@Override
			protected void populateEmptyItem(final Item<BasicConnection> item) {

				final WebMarkupContainer itemWrap = new WebMarkupContainer("itemWrap");
				itemWrap.add(new EmptyPanel("img"));
				itemWrap.add(new EmptyPanel("name"));
				item.add(itemWrap);
			}
		};

		gridView.setRows(4);
		gridView.setColumns(3);

		add(gridView);

	}

	@Override
	public void renderHead(final IHeaderResponse response) {
		super.renderHead(response);

		// get the Sakai skin header fragment from the request attribute
		final HttpServletRequest request = (HttpServletRequest) getRequest().getContainerRequest();

		response.render(new PriorityHeaderItem(
				JavaScriptHeaderItem.forReference(getApplication().getJavaScriptLibrarySettings().getJQueryReference())));

		response.render(StringHeaderItem.forString((String) request.getAttribute("sakai.html.head")));
		response.render(OnLoadHeaderItem.forScript("setMainFrameHeight( window.name )"));

		// Tool additions (at end so we can override if required)
		response.render(StringHeaderItem.forString("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"));

		// widget specific styles
		// final String version = ServerConfigurationService.getString("portal.cdn.version", "");
		// response.render(CssHeaderItem.forUrl(String.format("/gradebookng-tool/styles/gradebook-grades.css?version=%s", version)));

	}

}

@@ -546,24 +546,137 @@
 asic.Label;%0A
+import org.apache.wicket.markup.html.link.ExternalLink;%0Aimport org.apache.wicket.markup.html.link.PopupSettings;%0A
 import org.a
@@ -2319,74 +2319,276 @@
 nal 
-WebMarkupContainer itemWrap = new WebMarkupContainer(%22itemWrap%22
+String url = %22/direct/my/profile-view/%22 + connection.getUuid();%0A%0A%09%09%09%09final PopupSettings settings = new PopupSettings();%0A%09%09%09%09settings.setTarget(%22_parent%22);%0A%0A%09%09%09%09final ExternalLink itemWrap = new ExternalLink(%22itemWrap%22, url);%0A%09%09%09%09itemWrap.setPopupSettings(settings
 );%0A
+%0A
 %09%09%09%09
@@ -3038,16 +3038,44 @@
 mWrap);%0A
+%09%09%09%09item.setVisible(false);%0A
 %09%09%09%7D%0A%09%09%7D

Add the URL based on the new profile2 fixes