Hi,


2014-12-13 12:23 GMT+01:00 Matthias Wegner <mic.mat.weg...@web.de>:
>
> Hi,
>
> i want to create a custom excel-file. For that i want to use a uploaded
> template and the give it some values. I write that to the attachment
> storage
> and want to hcange the metainfo. The file creation with the template works
> fine, but the metatdata-change is not shown in xwiki. How can i do that? My
> code is so far
>

It is not at all recommended to manipulate directly files from attachments
file storage ... [1].
I think you'd better use xwiki regular apis to do so (on "doc") ... [2]
By retrieving the inner attachment (Attachment#getAttachment, as an
XWikiAttachment), you should be able to update metadata if needed.

J.

[1] -
http://platform.xwiki.org/xwiki/bin/view/AdminGuide/Attachments#HDirectoryStructure
[2] - http://platform.xwiki.org/xwiki/bin/view/SRD/Navigation


>
> {{groovy}}
> import java.io.File;
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.util.Calendar;
> import org.jdom2.Document;
> import org.jdom2.Element;
> import org.jdom2.JDOMException;
> import org.jdom2.input.SAXBuilder;
> import org.jdom2.output.Format;
> import org.jdom2.output.XMLOutputter;
> import org.apache.poi.hssf.usermodel.HSSFCell;
> import org.apache.poi.hssf.usermodel.HSSFRow;
> import org.apache.poi.hssf.usermodel.HSSFSheet;
> import org.apache.poi.hssf.usermodel.HSSFWorkbook;
> import org.apache.poi.poifs.filesystem.POIFSFileSystem;
>
> ArrayList<String> header = new ArrayList<String>();
> ArrayList<Hashtable&lt;String, String>> table = new
> ArrayList<Hashtable&lt;String, String>>();
> String sheetName = "Data";
> String fileName = "empty.xls";
> String templateName = "template.xls";
> String templateSpace = "Main";
> String pageName = "ExcelTest";
> String xwikiPath = "/opt/xwiki/openskies_v6.3";
> String templatePath =
>
> xwikiPath+"/data/storage/xwiki/"+templateSpace+"/"+pageName+"/~this/attachments/"+templateName+"/"+templateName;
> String filenamePath =
>
> xwikiPath+"/data/storage/xwiki/"+templateSpace+"/"+pageName+"/~this/attachments/"+fileName+"/"+fileName;
>
> header.add("X")
> header.add("Y")
> header.add("Z")
>
> Hashtable<String, String> tableRow = new Hashtable<String, String>();
> tableRow.put("X", "Q");
> tableRow.put("Y", "W");
> tableRow.put("Z", "R");
> table.add(tableRow);
>
> tableRow = new Hashtable<String, String>();
> tableRow.put("X", "E");
> tableRow.put("Y", "F");
> tableRow.put("Z", "G");
> table.add(tableRow);
>
> public String convert(Long value) {
>    return value.toString();
> }
>
> try {
>    HSSFWorkbook wb = null;
>    HSSFSheet sheet = null;
>
>    File templateFile = new File(templatePath);
>    boolean templateExists = templateFile.exists();
>    if (templateExists) {
>       POIFSFileSystem fs = new POIFSFileSystem(new
> FileInputStream(templateFile));
>       wb = new HSSFWorkbook(fs);
>       sheet = wb.getSheetAt(0);
>    } else {
>       wb = new HSSFWorkbook();
>       sheet = wb.createSheet(sheetName);
>    }
>
>    HSSFRow headerRow = null;
>    if (templateExists) {
>       headerRow = sheet.getRow(0);
>    } else {
>       headerRow = sheet.createRow(0);
>    }
>    for (short i = 0; i < header.size(); i++) {
>       HSSFCell cell = null;
>       if (templateExists) {
>          cell = headerRow.getCell(i);
>       } else {
>          cell = headerRow.createCell(i);
>       }
>       cell.setCellValue(header.get(i));
>    }
>
>    for (int ii = 0; ii < table.size(); ii++) {
>       HSSFRow row = null;
>       if (templateExists) {
>          row = sheet.getRow(ii+1);
>       } else {
>          row = sheet.createRow(ii+1);
>       }
>       for (short i = 0; i < header.size(); i++) {
>          HSSFCell cell = null;
>          if (templateExists) {
>             cell = row.getCell(i);
>          } else {
>             cell = row.createCell(i);
>          }
>          String s = table.get(ii).get(header.get(i));
>          cell.setCellValue(s);
>       }
>    }
>
>    File excelOut = new File(filenamePath);
>    FileOutputStream fileOut = new FileOutputStream(filenamePath);
>    wb.write(fileOut);
>    fileOut.close();
>
>       File meta = new
>
> File(xwikiPath+"/data/storage/xwiki/"+templateSpace+"/"+pageName+"/~this/attachments/"
> + fileName + "/~METADATA.xml");
>       Document metadata = new SAXBuilder().build(meta);
>
>       Element root = metadata.getRootElement();
>       for (Element child : root.getChildren()) {
>          if (child.getChildText("filename").equals(fileName)) {
>             if (child.getChildText("version").equals("1.1")) {
>
> child.getChild("filesize").setText(convert(excelOut.length()));
>
>
> child.getChild("date").setText(convert(Calendar.getInstance().getTime().getTime()));
>             }
>          }
>       }
>       XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
>       outputter.output(metadata, new FileOutputStream(meta));
>
> } catch (FileNotFoundException e) {
>    e.printStackTrace();
> } catch (IOException e) {
>    e.printStackTrace();
> } catch (JDOMException e) {
>    e.printStackTrace();
> }
>
> {{/groovy}}
>
> [[Download>>attach:empty.xls]]
>
>
>
> --
> View this message in context:
> http://xwiki.475771.n2.nabble.com/Changing-Metdata-of-Attachment-tp7593478.html
> Sent from the XWiki- Users mailing list archive at Nabble.com.
> _______________________________________________
> users mailing list
> users@xwiki.org
> http://lists.xwiki.org/mailman/listinfo/users
>
_______________________________________________
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users

Reply via email to