Bug 47540 - Custom properties and Extended properties are not saved
Summary: Custom properties and Extended properties are not saved
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.5-dev
Hardware: PC Mac OS X 10.4
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-16 01:03 UTC by Roberto Manicardi
Modified: 2009-07-19 11:30 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Roberto Manicardi 2009-07-16 01:03:54 UTC
Custom properties and Extended properties are not saved when I create a new XSSFWorkbook.

Below you can find one unit test for Extended properties and one for Custom properties




    public void testWorkbookExtendedProperties() throws Exception {
        XSSFWorkbook workbook = new XSSFWorkbook();
        POIXMLProperties props = workbook.getProperties();
        assertNotNull(props);
        
        org.apache.poi.POIXMLProperties.ExtendedProperties properties = props.getExtendedProperties();
        org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties ctProps = properties.getUnderlyingProperties();
        
        
        String appVersion = "3.5 beta";
        String application = "POI";
        
        ctProps.setApplication(application);
        ctProps.setAppVersion(appVersion);
        
        ctProps=null;
        properties=null;
        props = null;
        
        XSSFWorkbook newWorkbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
        
        assertTrue(workbook!=newWorkbook);
        
        
        POIXMLProperties newProps = newWorkbook.getProperties();
        assertNotNull(newProps);
        org.apache.poi.POIXMLProperties.ExtendedProperties newProperties = newProps.getExtendedProperties();
        org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties newCtProps = newProperties.getUnderlyingProperties();
        
        assertEquals(application,newCtProps.getApplication());
        assertEquals(appVersion,newCtProps.getAppVersion());
        
        
        
        
    }
    
    
    public void testWorkbookCustomProperties() throws Exception {
        XSSFWorkbook workbook = new XSSFWorkbook();
        POIXMLProperties props = workbook.getProperties();
        assertNotNull(props);
        
        org.apache.poi.POIXMLProperties.CustomProperties properties = props.getCustomProperties();
        org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties ctProps = properties.getUnderlyingProperties();
        
        org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty property = ctProps.addNewProperty();
        
        
        String fmtid = "{A1A1A1A1A1A1A1A1-A1A1A1A1-A1A1A1A1-A1A1A1A1-A1A1A1A1A1A1A1A1}";
        int pId = 1;
        String name = "testProperty";
        String stringValue = "testValue";
        
        
        property.setFmtid(fmtid);
        property.setPid(pId);
        property.setName(name);
        property.setBstr(stringValue);
        
       
        property =null;
        ctProps=null;
        properties=null;
        props = null;
        
        XSSFWorkbook newWorkbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
        
        assertTrue(workbook!=newWorkbook);
        
        
        POIXMLProperties newProps = newWorkbook.getProperties();
        assertNotNull(newProps);
        org.apache.poi.POIXMLProperties.CustomProperties newProperties = newProps.getCustomProperties();
        org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties newCtProps = newProperties.getUnderlyingProperties();
        
        assertEquals(1,newCtProps.getPropertyArray().length);
        
        
        org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty newpProperty = newCtProps.getPropertyArray()[0];
        
        assertEquals(fmtid, newpProperty.getFmtid() );
        assertEquals(pId,newpProperty.getPid());
        assertEquals(name,newpProperty.getName());
        assertEquals(stringValue,newpProperty.getBstr());

        
    }
Comment 1 awm 2009-07-17 08:44:19 UTC
I also would like to add in that you can't currently save the XWPFDocument properties. 

      XWPFDocument doc = new XWPFDocument(POIXMLDocument.openPackage("c:\\test.docx"));

      CustomProperties props = doc.getProperties().getCustomProperties();

      CTProperty[] ctp = props.getUnderlyingProperties().getPropertyArray();
      for (CTProperty foo : ctp) {
          System.out.println(foo.toString());
      } 

But there is no, doc.setProperties(...) 
Tested with 3.5-beta7
Comment 2 Yegor Kozlov 2009-07-19 11:30:28 UTC
Fixed in r795587, junit added.

I also added an example on setting extended / custom OOXML properties:
http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkbookProperties.java

Regards,
Yegor