/* XSSFPivotCacheRecords.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		May 2, 2012 12:00:17 PM , Created by Sam
}}IS_NOTE

Copyright (C) 2012 Potix Corporation. All Rights Reserved.

{{IS_RIGHT
}}IS_RIGHT
*/
package org.zkoss.poi.xssf.usermodel;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCacheRecords;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRecord;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.PivotCacheRecordsDocument;
import org.zkoss.poi.POIXMLDocumentPart;
import org.zkoss.poi.POIXMLException;
import org.zkoss.poi.openxml4j.opc.PackagePart;
import org.zkoss.poi.openxml4j.opc.PackageRelationship;
import org.zkoss.poi.openxml4j.opc.internal.MemoryPackagePart;

/**
 * @author Sam
 *
 */
public class XSSFPivotCacheRecords extends POIXMLDocumentPart implements Serializable {
	private static final long serialVersionUID = 7578278230009292650L;
	
	private CTPivotCacheRecords _pivotCacheRecords;
	
	/*package*/ XSSFPivotCacheRecords() {
		super();
		onDocumentCreate();
	}
	
	@Override
	protected void onDocumentCreate() {
		_pivotCacheRecords = CTPivotCacheRecords.Factory.newInstance();
	}
	
	/*package*/ XSSFPivotCacheRecords(PackagePart part, PackageRelationship rel) throws IOException {
		super(part, rel);
		onDocumentRead();
	}
	
	@Override
	protected void onDocumentRead() throws IOException {
		try {
			_pivotCacheRecords = PivotCacheRecordsDocument.Factory.parse(getPackagePart().getInputStream()).getPivotCacheRecords();
		} catch (XmlException e) {
			 throw new POIXMLException(e);
		}
	}
	
	@Override
	protected void commit() throws IOException {
		XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
		xmlOptions.setSaveSyntheticDocumentElement(new QName(CTPivotCacheRecords.type.getName().getNamespaceURI(), "pivotCacheRecords"));
        Map<String, String> map = new HashMap<String, String>();
        map.put(STRelationshipId.type.getName().getNamespaceURI(), "r");
        xmlOptions.setSaveSuggestedPrefixes(map);
		
        PackagePart part = getPackagePart();
        clearMemoryPackagePart(part);
        
        OutputStream out = part.getOutputStream();
        _pivotCacheRecords.save(out, xmlOptions);
        out.close();
	}

	/*package*/ CTRecord addNewRow() {
		return _pivotCacheRecords.addNewR();
	}
	
	/*package*/ List<CTRecord> getRows() {
		return _pivotCacheRecords.getRList();
	}

	@Override
	public String toString() {
		return _pivotCacheRecords.toString();
	}

	/*package*/ void setCount(int size) {
		_pivotCacheRecords.setCount(size);
	}
	
	/*package*/ int getCount() {
		return (int) _pivotCacheRecords.getCount();
	}
}
