<%@ include file="/WEB-INF/common/header.jsp"%>
<head>
<title><fmt:message key="datasources.title" /></title>
<meta name="heading"
  content="<fmt:message key='bioDatasourceList.heading'/>" />
<meta name="menu" content="AssociatedDatasourceMenu" />

<link rel="stylesheet" type="text/css" media="all"
  href="<c:url value='/styles/jquery.dataTables.css'/>" />
<link rel="stylesheet" type="text/css" media="all"
  href="<c:url value='/styles/dataTables.colVis.css'/>" />

<script type="text/javascript"
  src="<c:url value='/scripts/jquery/jquery-1.11.1.min.js'/>"></script>
<script type="text/javascript"
  src="<c:url value='/scripts/jquery/jquery.dataTables.js'/>"></script>
<script type="text/javascript"
  src="<c:url value='/scripts/jquery/dataTables.colVis.js'/>"></script>


</head>

<body>
  <p class="explainingText"><fmt:message key="datasources.explainingText" />

 <br/> <br/>
    List:  <br/>&nbsp;&nbsp;<a href="${ctx}/associatedData/list.html?filter=none" class="underline">All</a>, <a
      href="${ctx}/associatedData/list.html?filter=recent" class="underline">Recently
      added</a>
    <!-- <a href="${ctx}/associatedData/list.html?filter=deleted">Deleted</a> -->
     <br/>&nbsp;&nbsp;Metadata Updaters: <a
      href="${ctx}/associatedData/list.html?filter=updatersnone" class="underline">All</a>, <a
      href="${ctx}/associatedData/list.html?filter=updatersbiocase" class="underline">BioCASE</a>, 
    <a href="${ctx}/associatedData/list.html?filter=updatersdigir" class="underline">DiGIR</a>, 
    <a href="${ctx}/associatedData/list.html?filter=updaterstapir" class="underline">TAPIR</a>, 
    <a href="${ctx}/associatedData/list.html?filter=updatersdwcarchive" class="underline">DwC
        Archive</a>
          <br/>&nbsp;&nbsp;Operators: <a
      href="${ctx}/associatedData/list.html?filter=operatorsnone" class="underline">All</a>, <a
      href="${ctx}/associatedData/list.html?filter=operatorsbiocase" class="underline">BioCASE</a>, 
    <a href="${ctx}/associatedData/list.html?filter=operatorsdigir" class="underline">DiGIR</a>, 
    <a href="${ctx}/associatedData/list.html?filter=operatorstapir" class="underline">TAPIR</a>, 
    <a href="${ctx}/associatedData/list.html?filter=operatorsdwcarchive" class="underline">DwC
      Archive</a>
 <br/>
    &nbsp;Select: &nbsp;<span class="link" id="select_all">All</span>
    &nbsp;<span class="link" id="select_none">None</span>
</p>
  <!-- form 1 -->
  <form id="gridForm" name="gridForm" method="post" validate="false">
    <input type="hidden" name="${_csrf.parameterName}"
      value="${_csrf.token}" />
    <div id="data">
      <table id="data_table" class="display compact" cellspacing="0"
        width="100%">
        <thead>
          <tr>
            <th>Available Methods
            <th>Provider Name</th>
            <th>Datasource</th>
            <th>Associated from</th>
            <th>URL</th>
            <th>Nb. of expected associated units</th>
            <th>Nb. of associated units harvested</th>
            <th>Started</th>
            <th>Last harvesting</th>
            <th>Last harv. processed</th>
            <th>Country</th>
          </tr>
        </thead>
        <tbody>

          <c:forEach items="${bioDatasources}" var="bioDatasource"
            varStatus="loop">
            <tr>
              <td><c:choose>
                  <c:when
                    test="${not fn:contains(bioDatasource.harvesterFactory, 'Metadata') && ( bioDatasource.hasAssociatedData || bioDatasource.hasInternalAssociatedData) }">
                    <input type='checkbox' class='missingAsso'
                      name='datasourceBox'
                      id='datasource.${bioDatasource.id}' />
                  </c:when>
                  <c:otherwise>
                    <input type='checkbox' name='datasourceBox'
                      id='datasource.${bioDatasource.id}' />
                  </c:otherwise>
                </c:choose> <input
                type="hidden" id="datasource.${bioDatasource.id}"
                value="true" /> <img
                src="<c:url value='/images/expandCollapse.png'/>"
                id='eye.${bioDatasource.id}' name='expColl'
                title="expand/collapse methods" />

                <div style="margin-left: 50px" name="methodsDiv"
                  class="hidden" id="methodsDiv.${bioDatasource.id}">

                  <c:choose>
                    <c:when
                      test="${fn:length(factories[loop.index].operations) <2}">
                      <c:forEach
                        items="${factories[loop.index].operations}"
                        var="operation" varStatus="counter">
                        <input type="checkbox"
                          name="datasource.${bioDatasource.id}:${operation.method}"
                          id="datasource.${bioDatasource.id}:${counter.index}">&nbsp;&nbsp;&nbsp;<spring:message
                          code="${operation.i18nKey}" />
                        </input>
                        <br />
                      </c:forEach>
                    </c:when>
                    <c:otherwise>
                      <input type="checkbox"
                        name="datasource.${bioDatasource.id}:harvestAssociatedUnits"
                        id="datasource.${bioDatasource.id}:1">&nbsp;&nbsp;&nbsp;<fmt:message
                        key='asso.harvestList' />
                      </input>
                      <br />
                      <input type="checkbox"
                        name="datasource.${bioDatasource.id}:processAssociatedUnits"
                        id="datasource.${bioDatasource.id}:2">&nbsp;&nbsp;&nbsp;<fmt:message
                        key='asso.processList' />
                      </input>
                      <br />
                      <input type="checkbox"
                        name="datasource.${bioDatasource.id}:harvestSiblingUnits"
                        id="datasource.${bioDatasource.id}:3">&nbsp;&nbsp;&nbsp;<fmt:message
                        key='harvestSiblingUnits' />
                      </input>
                      <br />
                      <input type="checkbox"
                        name="datasource.${bioDatasource.id}:processSiblingUnits"
                        id="datasource.${bioDatasource.id}:4">&nbsp;&nbsp;&nbsp;<fmt:message
                        key='processSiblingUnits' />
                      </input>
                      <br />
<c:if
                    test="${not fn:contains(bioDatasource.harvesterFactory, 'Metadata') && ( bioDatasource.hasAssociatedData || bioDatasource.hasInternalAssociatedData) }">
                    <a
                      href="${ctx}/associatedData/configure.html?datasourceId=${bioDatasource.id}">Add
                      associated datasources</a>
                  </c:if>
                    </c:otherwise>
                  </c:choose>

                  <!--  on metadata harvester factories, we also want to grab the synchroniseMetadata operation -->
                  <c:forEach
                    items="${synchroniserFactoriez[loop.index]}"
                    var="fact" varStatus="counter">

                    <c:forEach items="${fact.operations}"
                      var="innerOperation">
                      <c:choose>
                        <c:when
                          test="${bioDatasource.harvesterFactory=='org.binhum.gbif.harvest.tapir.TapirHarvesterFactory'}">
                          <c:choose>
                            <c:when
                              test="${innerOperation.method=='synchroniseMetadata'}">

                            </c:when>
                            <c:otherwise>
                              <input type="checkbox"
                                name="datasource.${bioDatasource.id}:${innerOperation.method}"
                                id="datasource.${bioDatasource.id}:${counter.index}">&nbsp;&nbsp;&nbsp;<spring:message
                                code="${innerOperation.i18nKey}" />
                              </input>
                              <br />
                            </c:otherwise>
                          </c:choose>
                        </c:when>
                        <c:when
                          test="${bioDatasource.harvesterFactory=='org.binhum.gbif.harvest.tapir.TapirMetadataFactory'}">
                          <c:choose>
                            <c:when
                              test="${innerOperation.method=='synchronise' || innerOperation.method=='extract'}">

                            </c:when>
                            <c:otherwise>
                              <input type="checkbox"
                                name="datasource.${bioDatasource.id}:${innerOperation.method}"
                                id="datasource.${bioDatasource.id}:${counter.index}">&nbsp;&nbsp;&nbsp;<spring:message
                                code="${innerOperation.i18nKey}" />
                              </input>
                              <br />


                            </c:otherwise>
                          </c:choose>
                        </c:when>

                        <c:when
                          test="${bioDatasource.harvesterFactory=='org.binhum.gbif.harvest.digir.DigirHarvesterFactory'}">
                          <c:choose>
                            <c:when
                              test="${innerOperation.method=='synchroniseMetadata'}">

                            </c:when>
                            <c:otherwise>
                              <input type="checkbox"
                                name="datasource.${bioDatasource.id}:${innerOperation.method}"
                                id="datasource.${bioDatasource.id}:${counter.index}">&nbsp;&nbsp;&nbsp;<spring:message
                                code="${innerOperation.i18nKey}" />
                              </input>
                              <br />


                            </c:otherwise>
                          </c:choose>
                        </c:when>
                        <c:when
                          test="${bioDatasource.harvesterFactory=='org.binhum.gbif.harvest.digir.DigirMetadataFactory'}">
                          <c:choose>
                            <c:when
                              test="${innerOperation.method=='synchronise' || innerOperation.method=='extract'}">

                            </c:when>
                            <c:otherwise>
                              <input type="checkbox"
                                name="datasource.${bioDatasource.id}:${innerOperation.method}"
                                id="datasource.${bioDatasource.id}:${counter.index}">&nbsp;&nbsp;&nbsp;<spring:message
                                code="${innerOperation.i18nKey}" />
                              </input>
                              <br />


                            </c:otherwise>
                          </c:choose>
                        </c:when>

                        <c:when
                          test="${bioDatasource.harvesterFactory=='org.binhum.gbif.harvest.biocase.BiocaseHarvesterFactory'}">
                          <c:choose>
                            <c:when
                              test="${innerOperation.method=='synchroniseMetadata'}">

                            </c:when>
                            <c:otherwise>
                              <input type="checkbox"
                                name="datasource.${bioDatasource.id}:${innerOperation.method}"
                                id="datasource.${bioDatasource.id}:${counter.index}">&nbsp;&nbsp;&nbsp;<spring:message
                                code="${innerOperation.i18nKey}" />
                              </input>
                              <br />

                            </c:otherwise>
                          </c:choose>
                        </c:when>
                        <c:when
                          test="${bioDatasource.harvesterFactory=='org.binhum.gbif.harvest.biocase.BiocaseMetadataFactory'}">
                          <c:choose>
                            <c:when
                              test="${innerOperation.method=='synchronise' || innerOperation.method=='extract'}">

                            </c:when>
                            <c:otherwise>
                              <input type="checkbox"
                                name="datasource.${bioDatasource.id}:${innerOperation.method}"
                                id="datasource.${bioDatasource.id}:${counter.index}">&nbsp;&nbsp;&nbsp;<spring:message
                                code="${innerOperation.i18nKey}" />
                              </input>
                              <br />

                            </c:otherwise>
                          </c:choose>
                        </c:when>

                        <c:when
                          test="${bioDatasource.harvesterFactory=='org.binhum.gbif.harvest.dwcarchive.DwcArchiveHarvesterFactory'}">
                          <c:choose>
                            <c:when
                              test="${innerOperation.method=='synchroniseMetadata'}">

                            </c:when>
                            <c:otherwise>
                              <input type="checkbox"
                                name="datasource.${bioDatasource.id}:${innerOperation.method}"
                                id="datasource.${bioDatasource.id}:${counter.index}">&nbsp;&nbsp;&nbsp;<spring:message
                                code="${innerOperation.i18nKey}" />
                              </input>
                              <br />

                            </c:otherwise>
                          </c:choose>
                        </c:when>
                        <c:when
                          test="${bioDatasource.harvesterFactory=='org.binhum.gbif.harvest.dwcarchive.DwcArchiveMetadataFactory'}">
                          <c:choose>
                            <c:when
                              test="${innerOperation.method=='synchronise' || innerOperation.method=='extract'}">

                            </c:when>
                            <c:otherwise>
                              <input type="checkbox"
                                name="datasource.${bioDatasource.id}:${innerOperation.method}"
                                id="datasource.${bioDatasource.id}:${counter.index}">&nbsp;&nbsp;&nbsp;<spring:message
                                code="${innerOperation.i18nKey}" />
                              </input>
                              <br />

                            </c:otherwise>
                          </c:choose>
                        </c:when>
                      </c:choose>
                      <c:if
                        test="${(bioDatasasource.hasAssociatedData) || (bioDatasasource.hasInternalAssociatedData)}">
                        <a
                          href="${ctx}/associatedData/configure.html?datasourceId=${bioDatasource.id}">Add
                          associated datasources</a>
                      </c:if>
                </div>
          </c:forEach>
          </c:forEach>
          </td>
          <td>${bioDatasource.providerName}</td>
          <td>
          	<a href="${ctx}/datasource/edit.html?id=${bioDatasource.id}">
          		<c:choose>
					<c:when test="${fn:contains(bioDatasource.harvesterFactory, 'Metadata')}">
						<font color="darkOrange">${bioDatasource.name}</font>
					</c:when>
					<c:otherwise>
						<font color="green">${bioDatasource.name}</font>
					</c:otherwise>
                  </c:choose>
            </a>
           </td>
          <td>
            <div class="scrollTD">
              <c:forEach items="${bioDatasource.associatedTo}"
                var="institutionGroup">
        ${institutionGroup.key} :<br />
                <c:forEach items="${institutionGroup.value}"
                  var="collec">
            &nbsp;${collec}<br />
                </c:forEach>
              </c:forEach>
            </div>
          </td>
          <td><a href='${bioDatasource.url}'>${bioDatasource.url}</a></td>
          <td><c:if
              test="${not fn:containsIgnoreCase(bioDatasource.harvesterFactory, 'Metadata')}">${bioDatasource.numberOfTheoreticalAssociatedUnits}</c:if>
          </td>
          <td><c:if
              test="${not fn:containsIgnoreCase(bioDatasource.harvesterFactory, 'Metadata')}">${bioDatasource.numberOfAssociatedUnits}</c:if>
          </td>
          <td>${bioDatasource.started}</td>

          <td>${bioDatasource.lastHarvested}</td>
          <td>${bioDatasource.lastHarvestedProcessed}</td>
          <td>${bioDatasource.country}</td>
          </tr>
          </c:forEach>
        </tbody>
      </table>
    </div>

    <div class="float-left">
      <span> <c:choose>
          <c:when test="${param.filter == 'deleted'}">
            <span class="separator-right" style="font-style: italic;"><fmt:message
                key="view.deleted" /></span>
          </c:when>
          <c:otherwise>
            <input value="<fmt:message key="button.schedule"/>"
              name="value" type="button" onclick="setAction('schedule')"
              cssClass="button" theme="simple" />
            <span class="hidden"> 
			<select id="schedule" name="filterOps">
                <option selected value="all">all</option>
                <option value="onlyHarvesting">only harvesting</option>
                <option value="onlyIndexing">only indexing</option>
            </select>
			</span> to run <select class="separator-right" id="schedule" name="filter"
              onchange="if (this.options[this.selectedIndex].value=='once'); 
                                                else if (this.options[this.selectedIndex].value=='every week');
                                                else if (this.options[this.selectedIndex].value=='every 2 weeks');
                                                else if (this.options[this.selectedIndex].value=='every 3 weeks');
                                                else if (this.options[this.selectedIndex].value=='every 4 weeks');
                                                else if (this.options[this.selectedIndex].value=='every 8 weeks');
                                                else if (this.options[this.selectedIndex].value=='every 12 weeks');">
                <option selected value="Once">once</option>
                <option value="1week">every week</option>
                <option value="2weeks">every 2 weeks</option>
                <option value="3weeks">every 3 weeks</option>
                <option value="4weeks">every 4 weeks</option>
                <option value="8weeks">every 8 weeks</option>
                <option value="12weeks">every 12 weeks</option>
            </select>
          </c:otherwise>
        </c:choose> <span class="hidden"> <input type="button"
          value="<fmt:message key="button.backup"/>"
          onmouseover="showBackupHelp()" onmouseout="hideBackupHelp()"
          onclick="setAction('clean')" cssClass="button" theme="simple" />
      </span> <span style="display: none" id="backupHelpDiv"><fmt:message
            key="backup.help" /></span> <span style="display: none"
        id="deleteHelpDiv"><fmt:message key="delete.help" /></span>
      </span>

    </div>

  <%--   <div class="float-right">
      <span> <input type="button"
        value="<fmt:message key="button.add"/>"
        onclick="location.href='<c:out value="${ctx}/associatedData/edit.html?method=add"/>'"
        theme="simple" />
      </span>
    </div> --%>
    <div class="clear"></div>
  </form>


  <!-- form 2 -->
  <form style="display: none" id="readFile" name="readFileForm"
    action="<c:out value="${ctx}/files/readFile.htm?path=&type="/>">
    <input type="hidden" name="${_csrf.parameterName}"
      value="${_csrf.token}" /> <input name="path" type="hidden"
      value="" /> <input name="type" type="hidden" value="" /> <input
      type="submit" />
    <form>


      <script language="javascript">
    
        function setupSearch(val) {
            alert('value'+val);
            return true;
        }
    
    
      function setAction(val){
        if(val=="schedule"){
            if (document.forms["gridForm"].filter.value!="Once") {
                var answer = confirm("Auto-schedule the BioDatasource(s) checked?")
                if (answer) {
                    document.forms["gridForm"].action="<c:out value="${ctx}/associatedData/scheduleOperations.html"/>";
                    document.forms["gridForm"].submit()
                } else {
                    // do nothing
                }
            } else {
                document.forms["gridForm"].action="<c:out value="${ctx}/associatedData/scheduleOperations.html"/>";
                document.forms["gridForm"].submit()
            }
        }
        else{
            if (val=="clean") {
                var answer = confirm("Backup the BioDatasource(s) checked?")
                if (answer){
                    document.forms["gridForm"].action="<c:out value="${ctx}/associatedData/cleanDirectories.html"/>";
                    document.forms["gridForm"].submit()
                } else {
                    // do nothing
                }   
            } 
        }
    }
     
     function triggerForm(val, type){
        if (val != null && type != null) {
            document.forms["readFileForm"].path.value = val;
            document.forms["readFileForm"].type.value = type;
            document.forms["readFileForm"].submit();
        } else {
            alert("No directory exists for this BioDatasource");
        }
    }
    
    function showBackupHelp() {
        document.getElementById('backupHelpDiv').style.display = "block";
    }
    
    function hideBackupHelp() {
        document.getElementById('backupHelpDiv').style.display = "none";
    }

    function alternate(divElement) {

        var providerAlphabet = document.getElementById('providerAlphabet');
        var datasourceAlphabet = document.getElementById('datasourceAlphabet');
        var countryAlphabet = document.getElementById('countryAlphabet');
                
        if (divElement == "providerAlphabet") {
            providerAlphabet.style.display = "block";
            datasourceAlphabet.style.display = "none";
            countryAlphabet.style.display = "none";
            
        } else if (divElement == "datasourceAlphabet") {
            providerAlphabet.style.display = "none";
            datasourceAlphabet.style.display = "block";
            countryAlphabet.style.display = "none";
            
        } else if (divElement == "countryAlphabet") {
            providerAlphabet.style.display = "none";
            datasourceAlphabet.style.display = "none";
            countryAlphabet.style.display = "block";
            
        } else {
            providerAlphabet.style.display = "block";
            datasourceAlphabet.style.display = "block";
            countryAlphabet.style.display = "block";
            
        }
    }
    
    // is the context path empty or not?
    function returnContextPath() {
        var cp = '${ctx}';
        if (cp != '') {
            return cp;
        } else {
            return null;
        } 
    }
        
    
        
    $(document).ready(function() {
     $('img[name="expColl"]').click(function() {
            var cid=$(this).attr('id');
            if($(this).closest('td').find('div').css('display')=='none'){
            $(this).closest('td').find('div').show(200);
            $(this).closest('td').css("width","250px");
        }else{
            $(this).closest('td').find('div').hide(200);
        }
        
        });
        
        $('#select_all').click(function() {
           $('#gridForm').find(':checkbox').prop('checked', 'checked');
           $('#gridForm').find(':checkbox').each(function( event ) {
           $(this).closest('td').find('div').show(200);
           $(this).closest('td').css("width","250px");
            });
        });
        
         $('#select_none').click(function() {
           $('#gridForm').find(':checkbox').prop('checked', null);
           $('#gridForm').find(':checkbox').each(function( event ) {
           $(this).closest('td').find('div').hide(200);
           $(this).closest('td').css("width","25px");
            });
        });
        
        $('input[name="datasourceBox"]').click(function(event) {
            var cID= event.target.id.split(".")[1];
            $(this).closest('td').find(':checkbox').prop('checked', this.checked);
            if(this.checked){
            $(this).closest('td').find('div').show(200);
            $(this).closest('td').css("width","250px");
            }
            else
            {
            $(this).closest('td').find('div').hide(200);
            var oneOpen=false;
            $('#gridForm').find('div').each(function() {
              var cid=$(this).attr('id');
              if (cid !=null && cid.indexOf("method")>-1 )
              if ($(this).css("display") == "display")
              oneOpen=true; 
            });
            if (!oneOpen)
            $(this).closest('td').css("width","25px");
            }
        });
        
        $('#data_table').DataTable( {
            "oLanguage": {"sEmptyTable": "No associated datasource yet"},
        dom: 'C<"clear">lfrtip',
        "bAutoWidth": false,
        "columnDefs": [
            {
                "render": function ( data, type, row ) {
                if(data == row[5])
                    return '<b>'+data+'</b>';
                 else
                 return data;
                },
                "targets": 6
            },
            {
                 "render": function ( data, type, row )    {          
                        var r = /^(\d{4})-(\d{1,2})-(\d{1,2}).*?$/
                        , m = (''+data).match(r);
                        return (m) ? m[3]+'-'+m[2]+'-'+m[1] : data;
                 },
                 "targets": [8,9]
             },
        ]
        } );
        
        
        $('div[name="methodsDiv"]').hide();
       
    
      
    } );
    
        
        
        
    </script>