[xwiki-users] How to update an object only if it change
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.
Hi Arnaud I'm not sure, but I suspect you just flipped the if statement. Shouldn't the document be saved when "diffs.size() != 0" instead? Everything else looks ok to me if I understood correctly what you try to do. if (diffs.size() *!=* 0) { println "none" } else { println "Updated" ticketDoc.save() } Hope this helps Edo On Mon, Mar 18, 2013 at 5:37 PM, Arnaud bourree <[email protected]>wrote:
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 [email protected] http://lists.xwiki.org/mailman/listinfo/users
Thanks, that right test should be if (diffs.size() == 0) But an new object is created anyway Arnaud. 2013/3/18 Edo Beutler <[email protected]>:
Hi Arnaud
I'm not sure, but I suspect you just flipped the if statement. Shouldn't the document be saved when "diffs.size() != 0" instead? Everything else looks ok to me if I understood correctly what you try to do.
if (diffs.size() *!=* 0) { println "none" } else { println "Updated" ticketDoc.save() }
Hope this helps Edo
On Mon, Mar 18, 2013 at 5:37 PM, Arnaud bourree <[email protected]>wrote:
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 [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
I just try: ticketDoc.removeObject(oldTicket) ticket.setGuid(oldTicket.getGuid()) ticketDoc.save() And I get unexpected history difference I suspect: object[0] is deleted and object[1] is added, so not compared together. Arnaud. 2013/3/18 Arnaud bourree <[email protected]>:
Thanks, that right test should be if (diffs.size() == 0)
But an new object is created anyway
Arnaud.
2013/3/18 Edo Beutler <[email protected]>:
Hi Arnaud
I'm not sure, but I suspect you just flipped the if statement. Shouldn't the document be saved when "diffs.size() != 0" instead? Everything else looks ok to me if I understood correctly what you try to do.
if (diffs.size() *!=* 0) { println "none" } else { println "Updated" ticketDoc.save() }
Hope this helps Edo
On Mon, Mar 18, 2013 at 5:37 PM, Arnaud bourree <[email protected]>wrote:
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 [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
Finaly I do: diffs.each { diff -> oldTicket.set(diff.propName, ticket.get(diff.propName)) } ticketDoc.removeObject(ticket) ticketDoc.save() And I get history difference only updated field Arnaud. 2013/3/19 Arnaud bourree <[email protected]>:
I just try: ticketDoc.removeObject(oldTicket) ticket.setGuid(oldTicket.getGuid()) ticketDoc.save()
And I get unexpected history difference I suspect: object[0] is deleted and object[1] is added, so not compared together.
Arnaud.
2013/3/18 Arnaud bourree <[email protected]>:
Thanks, that right test should be if (diffs.size() == 0)
But an new object is created anyway
Arnaud.
2013/3/18 Edo Beutler <[email protected]>:
Hi Arnaud
I'm not sure, but I suspect you just flipped the if statement. Shouldn't the document be saved when "diffs.size() != 0" instead? Everything else looks ok to me if I understood correctly what you try to do.
if (diffs.size() *!=* 0) { println "none" } else { println "Updated" ticketDoc.save() }
Hope this helps Edo
On Mon, Mar 18, 2013 at 5:37 PM, Arnaud bourree <[email protected]>wrote:
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 [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
participants (2)
-
Arnaud bourree -
Edo Beutler