some interesting possibilities here....


[xwiki-users] Possible Bug with com.xpn.xwiki.doc.XWikiDocument.saveAttachmentContent v1.8

Ajdin Brandic to XWiki
I'm executing some code on Save which attaches a PNG file to a document that was edited/saved.

When I call docObj.saveAttachmentContent(attP, context); the attachment is save OK but any changes made to my document (ie. text changes) are not saved. If I comment out this line the text changes are saved, but off course not the attachment.
Is this normal behaviour, what do the developers think?

def imgAsBites = xwiki.getURLContentAsBytes("http://www.websequencediagrams.com/index.php"+umlImage, context);
def attP = docObj.getAttachment(filenameToSavaAs);
if(!attP)
         {
           attP = new com.xpn.xwiki.doc.XWikiAttachment(docObj,filenameToSavaAs);
           docObj.getAttachmentList().add(attP);
           println "NO Attach";
         }
attP.setContent(imgAsBites);
docObj.saveAttachmentContent(attP, context);

Regards
Ajdin



from: Ludovic Dubost
to: XWiki Users
date Wed, Feb 11, 2009 at 4:00 AM
subject:Re: xwiki-users: Possible Bug with com.xpn.xwiki.doc.XWikiDocument.saveAttachmentContent v1.8

Hi,

This should generally work.
Here is a typical Groovy script we often use to add attachments from File Uploads
We call it this way for example:
#set( $doctitle = title )
   #set( $newdoc = Space.${doctitle} )
   \#\# attach files if necessary
   #set( $attachgroovy = groovy_missingrights )
   #if( $attachgroovy == "groovy_missingrights" )
     #warning("no prog rights warning")
   #else
     #set( $nb = $attachgroovy.addAttachments($newdoc, $context) )
     #if( $nb == -10 )
       #warning("no prog rights warning")
     #end
  #set( $ok = $newdoc.saveWithProgrammingRights() )            \#\# or use $newdoc.save() ??
 #end
---
import com.xpn.xwiki.doc.*;
import com.xpn.xwiki.*;
import com.xpn.xwiki.util.Util;
import java.io.*;
import java.util.*;

public class Ajout {
    public int addAttachments(doc1, context1) {
	def doc = doc1.document;
	if (!context1.hasProgrammingRights())
	    return -10;
	def context = context1.context;
	if (context==null)
	    return -10;
	def xwiki = context.getWiki();
	int nb = 0;
	def fileupload = xwiki.getPlugin("fileupload",context)
	    for (fileitem in fileupload.getFileItems(context)) {
		if (!fileitem.isFormField()) {
		    def name = fileitem.fieldName
		    byte[] data = fileupload.getFileItemData(name, context);
		    if ((data!=null)&&(data.length>0)) {
			String fname = fileupload.getFileName(name, context);
			int i = fname.lastIndexOf("\\");
			if (i==-1)
			    i = fname.lastIndexOf("/");
			def filename = fname.substring(i+1);
			filename = filename.replaceAll("\\+"," ");
			def attachment = doc.getAttachment(filename);
			if (attachment==null) {
			    attachment = new XWikiAttachment();
			    doc.getAttachmentList().add(attachment);
			    // Add the attachment to the document
			    attachment.setDoc(doc);
			}
			attachment.setContent(data);
			attachment.setFilename(filename);
			// TODO: handle Author
			attachment.setAuthor(context1.user);
			doc.setAuthor(context1.user);
			doc.setCreator(context1.user);
			doc.saveAttachmentContent(attachment, context);
			nb++;
		    }
		}
	    }
	return nb;
    }
}


Related Links:

The latter states:

Integrating with other services
You can access the websequencediagrams server programmatically using a HTTP POST request. Send the POST request to http://www.websequencediagrams.com/index.php. The request must contain "style" parameter, containing the name of the style to use. The "message" parameter contains the text of the diagram. In response to the POST request, websequencediagrams.com will return a string similar to the following:

{img: "?img=mscKTO107", page: 0, numPages: 1, errors: []}

The response is a JSON encoding of the following fields:

  • img: When appended to "http://www.websequencediagrams.com/", the result is the URL of the image.
  • page: always 0
  • numPages: always 1
  • errors: an array of errors in the source text, if there were any.
The image may be deleted from the server immediately after it is accessed. To access the image again, you will have to regenerate it using a separate POST request.




last modified by Niels Mayer on Feb 11, 2009 23:23:50 GMT-08:00

Creator: NielsMayer.com Administrator on Feb 10, 2009 11:50:14 GMT-08:00
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 1.8.17790 - Documentation