没有正确读取xml节点

时间:2017-06-15 17:56:32

标签: java xml groovy

我写了一个脚本,假设比较假期和xml文件所采取的假期,我不知道为什么不考虑假期。当假设要通过假期系统时,它会在休假日创建工作日志。我的脚本和我的xml文件代码都可以在下面看到:

XML:

<holidays>
   <holiday type="fixed" name="New year's day" date="01/Jan" duration_seconds="1d"/>
   <holiday type="fixed" name="Christmas day" date="25/Dec" duration_seconds="1d"/>
   <holiday type="fixed" name="Boxing day" date="26/Dec" duration_seconds="1d"/>
   <holiday type="floating" name="Family Day" date="20/Feb/17" duration_seconds="1d"/>
   <holiday type="floating" name="Good Friday" date="14/Apr/17" duration_seconds="1d"/>
   <holiday type="floating" name="CENGN Easter day off (only in 2017)" date="17/Apr/17" duration_seconds="1d"/>
   <holiday type="floating" name="Victoria Day" date="22/May/17" duration_seconds="1d"/>
   <holiday type="floating" name="Canada day" date="03/Jul/17" duration_seconds="1d"/>
   <holiday type="floating" name="Civic Holiday" date="07/Aug/17" duration_seconds="1d"/>
   <holiday type="floating" name="Labor day" date="04/Sep/17" duration_seconds="1d"/>
   <holiday type="floating" name="Thanksgiving day" date="09/Oct/17" duration_seconds="1d"/>
</holidays>   

SCRIPT:

  import com.onresolve.scriptrunner.runner.ScriptRunnerImpl;
  import com.onresolve.scriptrunner.runner.customisers.PluginModule;
  import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
  import com.atlassian.jira.issue.CustomFieldManager;
  import com.atlassian.jira.issue.fields.CustomField;
  import com.atlassian.jira.ComponentManager
  import com.atlassian.jira.issue.IssueManager;
  import com.atlassian.jira.issue.Issue;
  import com.atlassian.jira.issue.MutableIssue;
  import com.atlassian.jira.issue.customfields.manager.OptionsManager
  import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
  import com.onresolve.scriptrunner.runner.customisers.PluginModule
  import com.onresolve.scriptrunner.runner.customisers.WithPlugin
  import java.lang.Object
  import com.atlassian.jira.issue.worklog.WorklogImpl2
  import java.text.SimpleDateFormat
  import com.atlassian.crowd.embedded.api.User
  import com.atlassian.jira.component.ComponentAccessor
  import com.atlassian.jira.issue.worklog.DefaultWorklogManager
  import com.atlassian.jira.security.roles.ProjectRoleManager
  import com.atlassian.jira.issue.worklog.Worklog
  import com.atlassian.jira.datetime.LocalDate
  import org.apache.commons.lang.RandomStringUtils
  import groovy.xml.MarkupBuilder 
  import groovy.util.*
  import groovy.xml.MarkupBuilder
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.xpath.*
  import org.w3c.dom.Document
  import org.w3c.dom.Element
  import org.w3c.dom.NodeList


  def componentManager = ComponentManager.getInstance()
  def customFieldManager = ComponentAccessor.getCustomFieldManager()
  def cfend = customFieldManager.getCustomFieldObjectByName("End Date")
  def cfstart = customFieldManager.getCustomFieldObjectByName("Start Date")
  def cfdays = customFieldManager.getCustomFieldObjectByName("Used vacation 
  days")
  def reporter = issue.reporter.name
  def worklogManager = ComponentAccessor.getWorklogManager();

  String f = new File('/var/atlassian/application-
  data/jira_7.3.1/import/holidays.xml')

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse(f);
  doc.getDocumentElement().normalize();

  //Take customfield date object and change it to days
  int days = (issue.getCustomFieldValue(cfdays) as int)//return the number 
  of days


  ProjectRoleManager projectRoleManager = 
  ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as 
  ProjectRoleManager
  def UsersRole = projectRoleManager.getProjectRole("Users")
  long timespent = 27000
  int counter = 0;
  int check = 0;

    for(int i=0; check == 0 ;i++){
        String hd = null;
        int c2 =0;
        int c =0;
        NodeList nodeLst = doc.getElementsByTagName("holiday");
        int d1 = ((issue.getCustomFieldValue(cfstart) as Date) + 
        i).getDate()
        int m1 = ((issue.getCustomFieldValue (cfstart) as Date) + 
        i).getMonth()

        if(m1 == 0)
            hd = d1+"/Jan"
    else if(m1 == 1)
           hd = d1+"/Feb"
    else if(m1 == 2)
           hd = d1+"/March"
    else if(m1 == 3)
           hd = d1+"/Apr"
    else if(m1 == 4)
           hd = d1+"/May"
    else if(m1 == 5)
           hd = d1+"/Jun"
    else if(m1 == 6)
           hd = d1+"/Jul"
    else if(m1 == 7)
           hd = d1+"/Aug"
    else if(m1 == 8)
           hd = d1+"/Sep"
    else if(m1 ==9)
           hd = d1+"/Oct"
    else if(m1 == 10)
           hd = d1+"/Nov"
    else{
           hd = d1+"/Dec"
    }

    for (int j = 0; j < nodeLst.getLength()-1; j++){
        c=0
        Element dateElement = (Element) nodeLst.item(j)
        String hd2 = 
        dateElement.getAttributes().getNamedItem("date").getNodeValue()
        for(int k =0; k<hd.length()-1;k++){
            hd[k]==hd2[k]
            c++
            if (c==hd.length())
                c2 = 1;
        }

}
           //If the date is a holiday
            if(c2 ==1){}

          //Checks if it is sunday
  else if(((issue.getCustomFieldValue(cfstart) as Date) + i).getDay() == 0){
}
          //Checks if it is Saturday
else if(((issue.getCustomFieldValue(cfstart) as Date) + i).getDay() == 6){
}  
         //Any other day in the week     
else{
    def worklog = new WorklogImpl2(issue, null, issue.reporter.name, 
    issue.summary, (issue.getCustomFieldValue(cfstart) as Date) + i, null, 
    null,timespent, UsersRole)
             worklogManager.create(issue.reporter, worklog, 0L, true)
             counter++
}

     if(days == counter){
         check =1;
     }
 }

  return 0;

我知道开始日期是从字段中提取的,我知道xml文件已被识别。我只是不知道它是否从xml文件中拉出假日日期,或者我的逻辑是否存在问题

1 个答案:

答案 0 :(得分:0)

抱歉,如果我浪费了任何时间。我得到它的工作,这只是一个逻辑错误。如果有人需要这个脚本,它可以在Scriptrunner中用于JIRA来创建避免周末和假期的工作日志。这会从文件中提取假日列表。

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl;
import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import java.lang.Object
import com.atlassian.jira.issue.worklog.WorklogImpl2
import java.text.SimpleDateFormat
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.DefaultWorklogManager
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.worklog.Worklog
import com.atlassian.jira.datetime.LocalDate
import org.apache.commons.lang.RandomStringUtils
import groovy.xml.MarkupBuilder 
import groovy.util.*
import groovy.xml.MarkupBuilder
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.*
import org.w3c.dom.Document
import org.w3c.dom.Element
import org.w3c.dom.NodeList


def componentManager = ComponentManager.getInstance()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfend = customFieldManager.getCustomFieldObjectByName("End Date")
def cfstart = customFieldManager.getCustomFieldObjectByName("Start Date")
def cfdays = customFieldManager.getCustomFieldObjectByName("Used vacation 
days")
def reporter = issue.reporter.name
def worklogManager = ComponentAccessor.getWorklogManager();

String f = new File('/var/atlassian/application-
data/jira_7.3.1/import/holidays.xml')

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);
doc.getDocumentElement().normalize();

//Take customfield date object and change it to days
int days = (issue.getCustomFieldValue(cfdays) as int)//return the number of 
days


ProjectRoleManager projectRoleManager = 
ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as 
ProjectRoleManager
def UsersRole = projectRoleManager.getProjectRole("Users")
long timespent = 27000
int counter = 0;
int check = 0;

     for(int i=0; check == 0 ;i++){
         String hd = null;
         int c2 =0;
         int c =0;
         NodeList nodeLst = doc.getElementsByTagName("holiday");
         int d1 = ((issue.getCustomFieldValue(cfstart) as Date) + 
         i).getDate()
         int m1 = ((issue.getCustomFieldValue (cfstart) as Date) + 
         i).getMonth()

         if(m1 == 0)
              hd = d1+"/Jan"
         else if(m1 == 1)
              hd =d1+"/Feb"
         else if(m1 == 2)
              hd = d1+"/Mar"
         else if(m1 == 3)
              hd = d1+"/Apr"
         else if(m1 == 4)
              hd = d1+"/May"
         else if(m1 == 5)
              hd = d1+"/Jun"
         else if(m1 == 6)
              hd = d1+"/Jul"
         else if(m1 == 7)
              hd = d1+"/Aug"
         else if(m1 == 8)
              hd = d1+"/Sep"
         else if(m1 ==9)
              hd = d1+"/Oct"
         else if(m1 == 10)
              hd = d1+"/Nov"
         else{
              hd = d1+"/Dec"
         }

         for (int j = 0; j < nodeLst.getLength()-1; j++){
             c=0
             Element dateElement = (Element) nodeLst.item(j)
             String hd2 = 
             dateElement.getAttributes().getNamedItem("date").getNodeValue()

             for(int k =0; k<hd.length()-1;k++){
                 if(hd2[0]=="0"){
                     if(hd[k]==hd2[k+1]){
                         c++
                     }
                  }
                 else{
                     if(hd[k]==hd2[k]){
                         c++
                     }
                 }
                 if (c==hd.length()-1)
                     c2 = 1;
             }

     }
        //If the date is a holiday
       if(c2 ==1){}

       //Checks if it is sunday
       else if(((issue.getCustomFieldValue(cfstart) as Date) + i).getDay() 
       == 0){
       }
      //Checks if it is Saturday
      else if(((issue.getCustomFieldValue(cfstart) as Date) + i).getDay() == 
      6){
      }  
 //Any other day in the week     
     else{
         def worklog = new WorklogImpl2(issue, null, issue.reporter.name, 
         issue.summary, (issue.getCustomFieldValue(cfstart) as Date) + i, 
         null,null,timespent, UsersRole)
         worklogManager.create(issue.reporter, worklog, 0L, true)
         counter++
     }

     if(days == counter){
         check =1;
     }
}

return 0;