<%@ include file="/includes.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>

<%--

FIXME
This feature isn't currently used, and uses a taglib called displaytag which doesn't appear to be supported.
Reimplement using JQueryUI datatables widget

 <%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
--%>

<fmt:setBundle var="link" basename="webapp" scope="session"/>
<c:set var="query" scope="request" />

<fmt:message var="driver" key="hibernate.connection.driver_class" bundle='<c:out value="${link}"/>' />
<fmt:message var="url" key="hibernate.connection.url" bundle='<c:out value="${link}"/>' />
<fmt:message var="username" key="hibernate.connection.username" bundle='<c:out value="${link}"/>' />
<fmt:message var="password" key="hibernate.connection.password" bundle='<c:out value="${link}"/>' />
<jsp:useBean id="JDBChelper" scope="page"
	class="com.ardeo.emdt.export.JDBCHelper" />
<head>
<title>Edit Database List</title>
<link rel="stylesheet" href="../webapp.css" type="text/css" />
<script src="../js/webapp.js" type="text/javascript"></script>
<script>
		//<!--
			var requiresUpdate = false;
		
			function moveItem(id, amount) {
				
				document.getElementById("dispatchMethod").value = "move";
				document.getElementById("moveId").value = id;
				document.getElementById("moveAmount").value = amount
				document.getElementById("form").submit();
			}
			
			function doUpdate() {
				if(requiresUpdate) {
					updateParent();
				}
			}
			
			function doDelete(id) {
				requiresUpdate = true; 
				var idfordeletion = id;
				if(confirmAndDispatch('delete', 'Are you sure you wish to remove the selected items?')) {
					document.getElementById("key").value=idfordeletion;
					document.getElementById("form").submit();
				}
				return false;
			}
			
			
		//-->
		</script>
<style type="text/css">
th {
	background-color: #eee;
}

.odd {
	background-color: #ddd;
}

.even {
	background-color: #eee;
}
.pageLinks  {
	color: darkblue;
}
.pageLinks a {
	color: darkblue;
}

.pageBanner  {
	color: darkblue;
	display:	block;
}
.title	{
	font-size:	14pt;
	font-weight:	bold;
}
.subtitle	{
	font-size:	10pt;
	color:	#888;
}
#addNew	{
	padding:	5px;
	align: left;
}
#searchBox	{
	padding:	5px;
}
td	{
	padding: 5px;
}
th	{
	padding: 5px;
}
.sortable {
        display: table-cell; 
} 
</style>

</head>
<body style="margin: 0 0 0 0" >
<smvc:form action="../admin/editDBList.form" id="form" modelAttribute="dbListForm">
	<input type="hidden" name="redirectParams" value='<c:out value="${requestScope['javax.servlet.forward.query_string']}" />' />
	<c:set var="namedLists" value="${dbListForm.databaseNamedLists}"/>
	<c:set var="columnNames" value="${dbListForm.columnNames}" />
	<c:set var="srchValue" value="${dbListForm.srchValue}" />
	<c:set var="columnName" value="${dbListForm.columnName}" />
	<c:set var="keyFieldName" value="${dbListForm.keyFieldName}" />

<table cellpadding="0" cellspacing="0" height="100%" width="100%">
   <tr>
        <td valign="top" align="center"><br />
      
            <div class="title">Editing list '<c:out value="${param.name }"/>'</div>	
            <smvc:hidden path="domain" />
            <smvc:hidden path="name" />           
    	    <input type="hidden" name="method" value="filter">
            <div class="subtitle">Table name: <c:out value="${dbListForm.namedList.tableName }"/></div>
            <br/>
            
              <div class="info"><c:out value="${successmsg}"/></div><br>

            <sql:setDataSource var="extendedDataListDS" driver='<c:out value="${driver}"/>' url='<c:out value="${url}"/>' user='<c:out value="${username}"/>' password='<c:out value="${password}"/>'/>
            <c:catch var="err">
                    <c:if test="${srchValue == null or srchValue == '' or columnName ==null or columnName ==''}">
                        <sql:query var="results" scope="request" dataSource='<c:out value="${extendedDataListDS}"/>'>
                            select distinct * from <%JDBChelper.getOpenQuote(); %> <c:out value="${dbListForm.namedList.tableName }"/> <%JDBChelper.getCloseQuote(); %> order by <c:out value="${keyFieldName}"/>
                        </sql:query>
                    </c:if>
 					<c:if test="${srchValue != null and srchValue != '' and columnName !=null and columnName ==keyFieldName}">
                         <sql:query var="results" scope="request" dataSource='<c:out value="${extendedDataListDS}"/>'>
                            select distinct * from <%JDBChelper.getOpenQuote(); %><c:out value="${dbListForm.namedList.tableName }"/><%JDBChelper.getCloseQuote(); %> where <%JDBChelper.getOpenQuote(); %><c:out value="${columnName}"/><%JDBChelper.getCloseQuote(); %> = '${fn:replace(srchValue, "'", "''")}' order by <%JDBChelper.getOpenQuote(); %><c:out value="${keyFieldName}"/><%JDBChelper.getCloseQuote(); %>
                   		</sql:query>
                    </c:if>
 
                    <c:if test="${srchValue != null and srchValue != '' and columnName !=null and columnName !=keyFieldName}">
                         <sql:query var="results" scope="request" dataSource='<c:out value="${extendedDataListDS}"/>'>
                            select distinct * from <%JDBChelper.getOpenQuote(); %><c:out value="${dbListForm.namedList.tableName }"/><%JDBChelper.getCloseQuote(); %> where <%JDBChelper.getOpenQuote(); %><c:out value="${columnName}"/><%JDBChelper.getCloseQuote(); %> like'${fn:replace(srchValue, "'", "''")}%' order by <%JDBChelper.getOpenQuote(); %><c:out value="${keyFieldName}"/><%JDBChelper.getCloseQuote(); %>
                        </sql:query>
                    </c:if>
            </c:catch>
            
            <c:if test="${not empty err}">
                <font color="red"> <b>Records not found.</b> </font>
            </c:if>
            
            
			<% int i = 0;%>
			<c:set var="keyid" value="<%=i%>" />
			
			
    		<div id="editingTable">
	           	<display:table name='<c:out value="${results.rows}"/>' defaultsort="0" defaultorder="ascending"  pagesize="20" sort="list" class="table"  requestURIcontext="false">
	           		
                    <c:forEach var="theName" items="${dbListForm.namedList.displayFieldNames }" varStatus="rowCounter" >
		      			<display:column property='<c:out value="${theName}"/>' title='<c:out value="${theName}"/>' sortable="true"  headerClass="tableColHeader" class="tableCell" />
        			</c:forEach>	
                <c:set var="keyid" value="<%= i %>" />
                <display:column title="Edit" headerClass="tableColHeader" class="tableCell">
                    <a href="editDBList.form?method=loadRecord&mode=edit&domain=<c:out value="${dbListForm.namedList.domain }"/>&name=<c:out value="${dbListForm.namedList.name}"/>&list=[<c:out value="${dbListForm.namedList.tableName }"/>]&key=<c:out value="${dbListForm.namedList.pairs[keyid].first}"/>" > 
                        <img src="../images/16x16/edit.png" alt="Edit"/>
                    </a>
                </display:column> 
                <display:column title="Delete" headerClass="tableColHeader" class="tableCell">
                    <a href="editDBList.form?method=delete&domain=<c:out value="${dbListForm.namedList.domain }"/>&name=<c:out value="${dbListForm.namedList.name}"/>&list=[<c:out value="${dbListForm.namedList.tableName }"/>]&key=<c:out value="${dbListForm.namedList.pairs[keyid].first}"/>" onClick="javascript:return confirm('Are you sure ?')" > 
                        <img src="../images/delete.png" alt="Delete"/>
                    </a>
                </display:column> 
                <% i++; %>	
                </display:table>
			<div id="addNew"><a href="editDBList.form?method=loadRecord&mode=add&domain=<c:out value="${dbListForm.namedList.domain }"/>&name=<c:out value="${dbListForm.namedList.name}"/>&list=[<c:out value="${dbListForm.namedList.tableName}"/>]" ><img src="../images/add.png" alt="Add" /><b> Add new record...</b></a></div>
            </div>
            
            <br/>
            
            <div id="searchBox">
                Find <input type="text" id="srchValue" name="srchValue" value="" size="5"></input> 
                in field 
                <select id="columnName" name="columnName">
                    <c:forEach var="cName" items="${dbListForm.columnNames}">
                        <option value="<c:out value="${cName}"/>" ><c:out value="${cName}"/></option>
                    </c:forEach>					
                </select>
                <input type="submit" title="srch" >
                <input type="reset" title="srchclr" value="Clear">
                
            </div>
            
            
      
        </td>
    </tr>
</table>  </smvc:form>
<%-- <display:column title="Edit" headerClass="tableColHeader" class="tableCell"><a href="editDBRow.jsp?domain=<c:out value="${param.domain }&list=<c:out value="${param.name }&itemKey=<c:out value="${row.id }"><img src="../images/16x16/edit.png" alt="Edit"/></a></display:column> --%>
</body>
