Hello,

I'm prototyping a connector to a ticket tracking tool: I'm writing a
Groovy page which import CSV calls into XWiki.
I map each CSV rows to one XWiki object store in one page (one object per page).
OK, I succeed to to it.

My issue is that imported CSV contains rows (tickets) which don't
change since last import and I don't want to update page in this case.
Thanks to getDiff() method on BaseObject, XWiki help me to find is
ticket change or not.
I write the following Groovy code:

def http = new HTTPBuilder( 'https://stim.gemalto.com' )
http.parser.'text/csv' = { resp ->
  return new CSVReader( new InputStreamReader( resp.entity.content,
                                ParserRegistry.getCharset( resp ) ) )
}
http.request( Method.GET, 'text/csv' ) {
  uri.path = myPath
  uri.query = myQuery

  // response handler for a success response code:
  response.success = { resp, csv ->
    if ("${resp.headers.'Content-Type'}".startsWith('text/csv')) {
      def names = []
      def idIndex = 0
      def cpt=0
      // Find id column call "IncidentNumber" in my case
      csv.readNext().each { field ->
          if (field.equals('IncidentNumber')) {
             idIndex = cpt;
          }
          names.add(field)
          cpt++;
      }
      println "|=Incident Number|=action"
      csv.readAll().each { line ->
        def id = line[idIndex]
        def exists = xwiki.exists('myTicket.'+id)
        def ticketDoc = xwiki.getDocument('myTicket.'+id)
        def ticket = ticketDoc.newObject('myTicket.myTicketClass')
        for(i=0 ; i<names.size() ; i++) {
          ticket.set(names[i], line[i]);
        }
        print "|[[myTicket."+id+"]]|"
        if (!exists) {
          println "Added"
          ticketDoc.save()
        } else {
          def tickets=ticketDoc.getObjects('myTicket.myTicketClass')
          def oldTicket = tickets.size() != 0 ? tickets[0] :
ticketDoc.newObject('myTicket.myTicketClass')
          def diffs=ticket.getBaseObject().getDiff(oldTicket.getBaseObject(),
xwiki.context)
          if (diffs.size() != 0) {
            println "none"
          } else {
            println "Updated"
            ticketDoc.save()
          }
        }
      }
    }
  }

  // handler for any failure status code:
  response.failure = { resp ->
    println "Unexpected error: ${resp.status} : ${resp.statusLine.reasonPhrase}"
  }
}

The issue in my code, is that I create and add a new ticket object on
each update where I want to have only one.
I could removed old ticket before save but in this case history diff
may not compare two tickets as there have to different GUID.
I could copy new ticket fields to old ticket
Or may be there is simplest way to do it implemented else where in XWiki?

What do you think?

Regards,

Arnaud.
_______________________________________________
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users

Reply via email to