<%@ page language="java" %>
<% 
/* Copyright 2005-2006 Michelle Osmond
 *
 * 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.
*/ 
%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ page import="java.util.*,message.*"%>

<% 
	// retrieve data
	String portletID = (String) request.getAttribute("portletID");
	MessageCentre messageCentre = (MessageCentre) request.getAttribute("messageCentre");
	Set inputKeys = (Set) request.getAttribute("inputKeys");
	Set outputKeys = (Set) request.getAttribute("outputKeys");
	Set globalBoxKeys = (Set) request.getAttribute("globalBoxKeys");
	Set availableSourceKeys = (Set) request.getAttribute("availableSourceKeys");
%>

<%		// display the current message mappings
		if (inputKeys.isEmpty() && outputKeys.isEmpty()){
%>
		<p>There are no configurable message mappings for this portlet.</p>
<%
			return;
		}
%>


		<h2>Set up message mappings</h2>

<%		// display any messages from the form
		if (request.getAttribute("mapMsg")!=null){
			String mapMsg = request.getAttribute("mapMsg").toString();
			if (mapMsg.trim().length()>0){
				%>
					<p><i><%=mapMsg%></i></p>
				<%
			}
		}
%>

		<form action="<portlet:actionURL />" method="POST">

<%
		if (inputKeys.size()>0){
%>
		
		<p><b>Inputs:</b></p>
		
		<table border="1">
			<tr>
				<th>Local Message</th>
				<th>Read from Source</th>
			</tr>
		
		<%
			int i=1;
			for (Iterator it=inputKeys.iterator(); it.hasNext();){
				MessageBoxKey key = (MessageBoxKey) it.next();
				MessageBoxKey currentSourceKey = messageCentre.findInputSource(key);
		%>
			<tr>
				<td><%=key.getName()%></td>
				<td>
					<select name="inputSource<%=i%>">
						<option value="<%=key.formString()%>">-none-</option>
						<%  // print out each option
							for (Iterator it2=availableSourceKeys.iterator(); it2.hasNext();){
								MessageBoxKey outKey = (MessageBoxKey) it2.next(); 
								String optionName = outKey.formString();
								String optionDesc = outKey.getName();
								if (outKey.isLocal()){
									optionDesc += " (local)";
								} else if (outKey.getNamespace().length()>0){
									optionDesc += " ("+outKey.getNamespace()+")";
								}
								String selected = "";
								if ( currentSourceKey!=null && currentSourceKey.equals(outKey) ){
									selected = "selected";
								}
						%>
								<option value="<%=optionName%>" <%=selected%>><%=optionDesc%></option>
						<%
							}
						%>
					</select>
					<input type="hidden" name="inputName<%=i%>" value="<%=key.formString()%>" />
				</td>
			</tr>
		<%
				i++;
			}
		%>	
		</table>
<%
		}
%>

<%
		if (outputKeys.size()>0){
%>
			<p><b>Outputs:</b></p>
			<table border="1">
				<tr>
					<th>Local Output</th>
					<th>Publish to Target</th>
				</tr>
			<%
				int i=1;
				for (Iterator it=outputKeys.iterator(); it.hasNext();){
					MessageBoxKey key = (MessageBoxKey) it.next();
					Set currentTargets = messageCentre.findOutputTargets(key);
			%>
					<tr>
						<td valign="top"><%=key.getName()%></td>
						<td>
							<select name="outputTargets<%=i%>" size="5" multiple>
								<%  // print out each option
									for (Iterator it2=globalBoxKeys.iterator(); it2.hasNext();){
										MessageBoxKey outKey = (MessageBoxKey) it2.next(); 
										String optionName = outKey.formString();
										String optionDesc = outKey.getName();
										if (outKey.isLocal()){
											optionDesc += " (local)";
										} else if (outKey.getNamespace().length()>0){
											optionDesc += " ("+outKey.getNamespace()+")";
										}
										String selected = "";
										if ( currentTargets!=null && currentTargets.contains(outKey) ){
											selected = "selected";
										}
								%>
										<option value="<%=optionName%>" <%=selected%>><%=optionDesc%></option>
								<%
									}
								%>
							</select>
						<br />New global output:
						<table border="0" style="margin-left:10px;font-size:smaller;">
							<tr><td><i>Name:</i></td>
								<td><input type="text" width="15" name="newOutputTargetName<%=i%>" /></td>
							</tr>
							<tr></td><td><i>Namespace:</i></td>
								<td><input type="text" width="15" name="newOutputTargetNamespace<%=i%>" /></td>
							</tr>
						</table>
						<input type="hidden" name="outputName<%=i%>" value="<%=key.formString()%>" />
						</td>
					</tr>
			<%
					i++;
				}
			%>
	
			</table>
<%
		}
%>

		<input type="submit" value="Save"/>
		</form>
