Interoperable interactive geometry for Europe
I forgot my login data

Report a bug

Fan club

Quick Intro Videos
click to start movie
Create A Simple
GeoGebra Resource (25Mb)
click to start movie
Filing a review
click to start movie
Find a Resource

This platform is brought to you by the intergeo project, funded under the eContent Plus programme of the European commission and by partners

import com.xpn.xwiki.doc.*; import com.xpn.xwiki.*; import com.xpn.xwiki.util.Util; import*; import java.util.*;

public class UpdateAttachmentGroovy {

public XWikiAttachment getFirstAttachmentName(doc, context) { def list = doc.getAttachmentList(); System.out.println("Document has " + list.size() + " attachments") if (list.size()==0) return null; def attach = list.get(0); return attach; }

public boolean replaceAttachment(doc1, context1) { def doc = doc1.document; if (!context1.hasProgrammingRights()) return -10; def context = context1.context; if (context==null) return -10; def xwiki = context.getWiki();

System.out.println("Getting first attachment") def attachment = getFirstAttachmentName(doc, context);

int nb = 0; def fileupload = xwiki.getPlugin("fileupload",context) def name = "file"; byte[] data = fileupload.getFileItemData(name, context); if ((data!=null)&&(data.length>0)) { System.out.println("Found data for attachment") 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("
+"," ");

if (attachment==null) { System.out.println("Creating attachment from scratch attachment") attachment = new XWikiAttachment(); doc.getAttachmentList().add(attachment); // Add the attachment to the document attachment.setDoc(doc); } else if (attachment.getFilename()!=filename) { // filename is different we need to delete the previous attachment System.out.println("Deleting previous attachment") doc.deleteAttachment(attachment, context); attachment.setFilename(filename); doc.getAttachmentList().add(attachment); // Add the attachment to the document attachment.setDoc(doc); }

// now save the attachment under the new name System.out.println("Saving attachment") attachment.setContent(data); attachment.setFilename(filename); attachment.setAuthor(context1.user); doc.setAuthor(context1.user); doc.setCreator(context1.user); doc.saveAttachmentContent(attachment, context); return true; } else { // no attachment System.out.println("No attachment found") return false; } } }