<commit_before>/*
*************************************************************************************
* Copyright 2011 Normation SAS
*************************************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU Affero GPL v3, the copyright holders add the following
* Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU Affero GPL v3
* licence, when you create a Related Module, this Related Module is
* not considered as a part of the work and may be distributed under the
* license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/agpl.html>.
*
*************************************************************************************
*/

package com.normation.rudder.web.components

import com.normation.rudder.domain.policies._
import com.normation.rudder.services.policies.PolicyInstanceTargetService
import com.normation.rudder.repository._
import com.normation.rudder.domain.nodes.NodeGroupId
import com.normation.rudder.domain.policies._
import net.liftweb.http.js._
import JsCmds._
import com.normation.rudder.services.reports.ReportingService
import com.normation.inventory.domain.NodeId
import com.normation.rudder.services.nodes.NodeInfoService
import JE._
import net.liftweb.common._
import net.liftweb.http._
import scala.xml._
import net.liftweb.util._
import net.liftweb.util.Helpers._
import com.normation.rudder.web.model._
import bootstrap.liftweb.LiftSpringApplicationContext.inject
import com.normation.utils.StringUuidGenerator
import com.normation.exceptions.TechnicalException
import com.normation.utils.Control.sequence
import com.normation.rudder.domain.log.RudderEventActor
import com.normation.cfclerk.domain.PolicyPackage
import com.normation.cfclerk.services.PolicyPackageService


object ConfigurationRuleGrid {
  def staticInit =     
    <head>
      <script type="text/javascript" language="javascript" src="/javascript/datatables/js/jquery.dataTables.js"></script>
    </head>
}

class ConfigurationRuleGrid(
    htmlId_rulesGridZone : String,
    configurationRules : Seq[ConfigurationRule],
    //JS callback to call when clicking on a line 
    detailsCallbackLink : Option[ConfigurationRule => JsCmd],
    showCheckboxColumn:Boolean = true
) extends DispatchSnippet with Loggable {
  
  private[this] val targetInfoService = inject[PolicyInstanceTargetService]
  private[this] val policyInstanceRepository = inject[PolicyInstanceRepository]
  private[this] val configurationRuleRepository = inject[ConfigurationRuleRepository]

  private[this] val reportingService = inject[ReportingService]
  private[this] val nodeInfoService = inject[NodeInfoService]

  private[this] val htmlId_rulesGridId = "grid_" + htmlId_rulesGridZone

  private[this] val htmlId_reportsPopup = "popup_" + htmlId_rulesGridZone
  private[this] val htmlId_modalReportsPopup = "modal_" + htmlId_rulesGridZone
  private[this] val tableId_reportsPopup = "reportsGrid"
  
  private[this] val policyPackageName = inject[PolicyPackageService] 
   
  def templatePath = List("templates-hidden", "reports_grid")
  def template() =  Templates(templatePath) match {
    case Empty | Failure(_,_,_) =>
      throw new TechnicalException("Template for report grid not found. I was looking for %s.html".format(templatePath.mkString("/")))
    case Full(n) => n
  }
  def reportTemplate = chooseTemplate("reports", "report", template)

  def dispatch = { 
    case "configurationRulesGrid" => { _:NodeSeq => configurationRulesGrid() }
  }
  
  def jsVarNameForId(tableId:String) = "oTable" + tableId
  
  def configurationRulesGrid(linkCompliancePopup:Boolean = true) : NodeSeq = {
    (
        <div id={htmlId_rulesGridZone}>
          <div id={htmlId_modalReportsPopup} style="display:none;">
            <div id={htmlId_reportsPopup} ></div>
          </div>
          <table id={htmlId_rulesGridId} cellspacing="0">
            <thead>
              <tr class="head">
                <th>Name<span/></th>
                <th>Description<span/></th>
                <th>Status<span/></th>
                <th>Deployment status<span/></th>
                <th>Compliance<span/></th>
                <th>Policy instance<span/></th>
                <th>Target node group<span/></th>
                { if(showCheckboxColumn) <th></th> else NodeSeq.Empty }
              </tr>
            </thead>
            <tbody>   
            {showConfigurationRulesDetails(configurationRules,linkCompliancePopup)}
            </tbody>
          </table> 
          <div class={htmlId_rulesGridId +"_pagination"} style="float:left;">
            <div id={htmlId_rulesGridId +"_paginate_area"}></div>
          </div>
        </div>
    ) ++ Script(
      JsRaw("""
        var #table_var#;
      """.replaceAll("#table_var#",jsVarNameForId(htmlId_rulesGridId))) &      
      //pop-ups for multiple policy instances
      JsRaw( """var openMultiPiPopup = function(popupid) {
          $("#" + popupid).modal({
            minHeight:300,
            minWidth: 520
          });
          $('#simplemodal-container').css('height', 'auto');
          correctButtons();
     }""") &
     OnLoad(JsRaw("""
      /* Event handler function */
      #table_var# = $('#%1$s').dataTable({
        "asStripClasses": [ 'color1', 'color2' ],
        "bAutoWidth": false,
        "bFilter" : false,
        "bPaginate" : true,
        "bLengthChange": false,
        "sPaginationType": "full_numbers",
        "bJQueryUI": false,
        "oLanguage": {
          "sZeroRecords": "No item involved"
        },
        "aaSorting": [[ 0, "asc" ]],
        "aoColumns": [ 
          { "sWidth": "95px" },
          { "sWidth": "95px"  },
          { "sWidth": "60px" },
          { "sWidth": "115px" },
          { "sWidth": "60px" },
          { "sWidth": "100px"  },<commit_msg>Fixes #2497 : correct the sorting of compliance<commit_after>/*
*************************************************************************************
* Copyright 2011 Normation SAS
*************************************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU Affero GPL v3, the copyright holders add the following
* Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU Affero GPL v3
* licence, when you create a Related Module, this Related Module is
* not considered as a part of the work and may be distributed under the
* license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/agpl.html>.
*
*************************************************************************************
*/

package com.normation.rudder.web.components

import com.normation.rudder.domain.policies._
import com.normation.rudder.services.policies.PolicyInstanceTargetService
import com.normation.rudder.repository._
import com.normation.rudder.domain.nodes.NodeGroupId
import com.normation.rudder.domain.policies._
import net.liftweb.http.js._
import JsCmds._
import com.normation.rudder.services.reports.ReportingService
import com.normation.inventory.domain.NodeId
import com.normation.rudder.services.nodes.NodeInfoService
import JE._
import net.liftweb.common._
import net.liftweb.http._
import scala.xml._
import net.liftweb.util._
import net.liftweb.util.Helpers._
import com.normation.rudder.web.model._
import bootstrap.liftweb.LiftSpringApplicationContext.inject
import com.normation.utils.StringUuidGenerator
import com.normation.exceptions.TechnicalException
import com.normation.utils.Control.sequence
import com.normation.rudder.domain.log.RudderEventActor
import com.normation.cfclerk.domain.PolicyPackage
import com.normation.cfclerk.services.PolicyPackageService


object ConfigurationRuleGrid {
  def staticInit =     
    <head>
      <script type="text/javascript" language="javascript" src="/javascript/datatables/js/jquery.dataTables.js"></script>
    </head>
}

class ConfigurationRuleGrid(
    htmlId_rulesGridZone : String,
    configurationRules : Seq[ConfigurationRule],
    //JS callback to call when clicking on a line 
    detailsCallbackLink : Option[ConfigurationRule => JsCmd],
    showCheckboxColumn:Boolean = true
) extends DispatchSnippet with Loggable {
  
  private[this] val targetInfoService = inject[PolicyInstanceTargetService]
  private[this] val policyInstanceRepository = inject[PolicyInstanceRepository]
  private[this] val configurationRuleRepository = inject[ConfigurationRuleRepository]

  private[this] val reportingService = inject[ReportingService]
  private[this] val nodeInfoService = inject[NodeInfoService]

  private[this] val htmlId_rulesGridId = "grid_" + htmlId_rulesGridZone

  private[this] val htmlId_reportsPopup = "popup_" + htmlId_rulesGridZone
  private[this] val htmlId_modalReportsPopup = "modal_" + htmlId_rulesGridZone
  private[this] val tableId_reportsPopup = "reportsGrid"
  
  private[this] val policyPackageName = inject[PolicyPackageService] 
   
  def templatePath = List("templates-hidden", "reports_grid")
  def template() =  Templates(templatePath) match {
    case Empty | Failure(_,_,_) =>
      throw new TechnicalException("Template for report grid not found. I was looking for %s.html".format(templatePath.mkString("/")))
    case Full(n) => n
  }
  def reportTemplate = chooseTemplate("reports", "report", template)

  def dispatch = { 
    case "configurationRulesGrid" => { _:NodeSeq => configurationRulesGrid() }
  }
  
  def jsVarNameForId(tableId:String) = "oTable" + tableId
  
  def configurationRulesGrid(linkCompliancePopup:Boolean = true) : NodeSeq = {
    (
        <div id={htmlId_rulesGridZone}>
          <div id={htmlId_modalReportsPopup} style="display:none;">
            <div id={htmlId_reportsPopup} ></div>
          </div>
          <table id={htmlId_rulesGridId} cellspacing="0">
            <thead>
              <tr class="head">
                <th>Name<span/></th>
                <th>Description<span/></th>
                <th>Status<span/></th>
                <th>Deployment status<span/></th>
                <th>Compliance<span/></th>
                <th>Policy instance<span/></th>
                <th>Target node group<span/></th>
                { if(showCheckboxColumn) <th></th> else NodeSeq.Empty }
              </tr>
            </thead>
            <tbody>   
            {showConfigurationRulesDetails(configurationRules,linkCompliancePopup)}
            </tbody>
          </table> 
          <div class={htmlId_rulesGridId +"_pagination"} style="float:left;">
            <div id={htmlId_rulesGridId +"_paginate_area"}></div>
          </div>
        </div>
    ) ++ Script(
      JsRaw("""
        var #table_var#;
      """.replaceAll("#table_var#",jsVarNameForId(htmlId_rulesGridId))) &      
      //pop-ups for multiple policy instances
      JsRaw( """var openMultiPiPopup = function(popupid) {
          $("#" + popupid).modal({
            minHeight:300,
            minWidth: 520
          });
          $('#simplemodal-container').css('height', 'auto');
          correctButtons();
     }""") &
     OnLoad(JsRaw("""
      /* Event handler function */
      #table_var# = $('#%1$s').dataTable({
        "asStripClasses": [ 'color1', 'color2' ],
        "bAutoWidth": false,
        "bFilter" : false,
        "bPaginate" : true,
        "bLengthChange": false,
        "sPaginationType": "full_numbers",
        "bJQueryUI": false,
        "oLanguage": {
          "sZeroRecords": "No item involved"
        },
        "aaSorting": [[ 0, "asc" ]],
        "aoColumns": [ 
          { "sWidth": "95px" },
          { "sWidth": "95px"  },
          { "sWidth": "60px" },
          { "sWidth": "115px" },
          { "sWidth": "60px", "sType": "html" },
          { "sWidth": "100px"  },
{'fraction_non_alphanumeric': 0.14420751813585403, 'fraction_numerical': 0.005862094233164798, 'mean_word_length': 3.483574244415243, 'pattern_counts': {'":': 31, '<': 95, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3961269', 'n_tokens_mistral': 3911, 'n_tokens_neox': 3755, 'n_words': 1006}