org.datanucleus.store.odf.ODFUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-odf Show documentation
Show all versions of datanucleus-odf Show documentation
DataNucleus plugin providing persistence to ODF documents
/**********************************************************************
Copyright (c) 2009 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package org.datanucleus.store.odf;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.ExecutionContext;
import org.datanucleus.FetchPlan;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.identity.IdentityUtils;
import org.datanucleus.identity.SCOID;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.IdentityType;
import org.datanucleus.metadata.RelationType;
import org.datanucleus.state.ObjectProvider;
import org.datanucleus.store.FieldValues;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.odf.fieldmanager.FetchFieldManager;
import org.datanucleus.store.schema.table.MemberColumnMapping;
import org.datanucleus.store.schema.table.SurrogateColumnType;
import org.datanucleus.store.schema.table.Table;
import org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument;
import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
import org.odftoolkit.odfdom.doc.table.OdfTable;
import org.odftoolkit.odfdom.doc.table.OdfTableCell;
import org.odftoolkit.odfdom.doc.table.OdfTableRow;
import org.odftoolkit.odfdom.dom.attribute.office.OfficeValueTypeAttribute;
/**
* Utilities to assist in persistence to ODF spreadsheets.
*/
public class ODFUtils
{
/**
* Convenience method to find the row of an object in the provided sheet.
* For application-identity does a search for a row with the specified PK field values.
* For datastore-identity does a search for the row with the datastore column having the specified value
* @param op ObjectProvider for the object
* @param spreadsheetDoc The spreadsheet document
* @param originalValue Whether to use the original value (when available) when using non-durable id.
* @return The row (or null if not found)
*/
public static OdfTableRow getTableRowForObjectInSheet(ObjectProvider op, OdfSpreadsheetDocument spreadsheetDoc, boolean originalValue)
{
ExecutionContext ec = op.getExecutionContext();
final AbstractClassMetaData cmd = op.getClassMetaData();
Table schemaTable = ec.getStoreManager().getStoreDataForClass(cmd.getFullClassName()).getTable();
String sheetName = schemaTable.getName();
OdfTable table = spreadsheetDoc.getTableByName(sheetName);
if (table == null)
{
return null;
}
if (cmd.getIdentityType() == IdentityType.APPLICATION)
{
ClassLoaderResolver clr = ec.getClassLoaderResolver();
int[] pkFieldNumbers = cmd.getPKMemberPositions();
List pkFieldColList = new ArrayList(pkFieldNumbers.length);
List pkFieldValList = new ArrayList(pkFieldNumbers.length);
for (int i=0;i embMmds = new ArrayList();
embMmds.add(mmd);
embMmds.add(embCmd.getMetaDataForManagedMemberAtAbsolutePosition(j));
MemberColumnMapping mapping = schemaTable.getMemberColumnMappingForEmbeddedMember(embMmds);
pkFieldColList.add(mapping.getColumn(0).getPosition());
if (mapping.getTypeConverter() != null)
{
pkFieldValList.add(mapping.getTypeConverter().toDatastoreType(embOP.provideField(j)));
}
else
{
pkFieldValList.add(embOP.provideField(j));
}
}
}
else
{
MemberColumnMapping mapping = schemaTable.getMemberColumnMappingForMember(mmd);
pkFieldColList.add(mapping.getColumn(0).getPosition());
if (mapping.getTypeConverter() != null)
{
pkFieldValList.add(mapping.getTypeConverter().toDatastoreType(fieldValue));
}
else
{
pkFieldValList.add(fieldValue);
}
}
}
List rows = table.getRowList();
Iterator rowIter = rows.iterator();
while (rowIter.hasNext())
{
OdfTableRow row = rowIter.next();
boolean isRow = true;
for (int i=0;i rows = table.getRowList();
Iterator rowIter = rows.iterator();
while (rowIter.hasNext())
{
OdfTableRow row = rowIter.next();
OdfTableCell cell = row.getCellByIndex(index);
if (doesCellMatchValue(cell, key))
{
return row;
}
}
}
else
{
// Nondurable, comparing all suitable fields
ClassLoaderResolver clr = ec.getClassLoaderResolver();
int[] fieldNumbers = cmd.getAllMemberPositions();
List fieldColList = new ArrayList(fieldNumbers.length);
List fieldValList = new ArrayList(fieldNumbers.length);
for (int i=0;i embMmds = new ArrayList();
embMmds.add(mmd);
embMmds.add(embCmd.getMetaDataForManagedMemberAtAbsolutePosition(j));
fieldColList.add(schemaTable.getMemberColumnMappingForEmbeddedMember(embMmds).getColumn(0).getPosition());
fieldValList.add(embOP.provideField(j));
}
}
else if (relationType == RelationType.NONE)
{
fieldColList.add(schemaTable.getMemberColumnMappingForMember(mmd).getColumn(0).getPosition());
fieldValList.add(fieldValue);
}
}
List rows = table.getRowList();
Iterator rowIter = rows.iterator();
while (rowIter.hasNext())
{
OdfTableRow row = rowIter.next();
boolean isRow = true;
for (int i=0;i rows = table.getRowList();
Iterator rowIter = rows.iterator();
while (rowIter.hasNext())
{
final OdfTableRow row = rowIter.next();
final FetchFieldManager fm = new FetchFieldManager(ec, acmd, row, schemaTable);
OdfStyle style = row.getDefaultCellStyle();
String styleName = (style != null ? style.getStyleNameAttribute() : null);
if (styleName != null && styleName.equals("DN_Headers"))
{
// Skip headers
continue;
}
// TODO Change acmd.getAllMemberPositions() to omit nonpersistent fields
Object pc = null;
if (acmd.getIdentityType() == IdentityType.APPLICATION)
{
Object id = IdentityUtils.getApplicationIdentityForResultSetRow(ec, acmd, null, false, fm);
pc = ec.findObject(id, new FieldValues()
{
// ObjectProvider calls the fetchFields method
public void fetchFields(ObjectProvider op)
{
op.replaceFields(acmd.getAllMemberPositions(), fm);
}
public void fetchNonLoadedFields(ObjectProvider op)
{
op.replaceNonLoadedFields(acmd.getAllMemberPositions(), fm);
}
public FetchPlan getFetchPlanForLoading()
{
return null;
}
}, null, ignoreCache, false);
}
else if (acmd.getIdentityType() == IdentityType.DATASTORE)
{
int idIndex = schemaTable.getSurrogateColumn(SurrogateColumnType.DATASTORE_ID).getPosition();
OdfTableCell idCell = row.getCellByIndex(idIndex);
Object idKey = null;
if (isOfficeValueTypeConsistent(idCell, OfficeValueTypeAttribute.Value.STRING))
{
idKey = idCell.getStringValue();
}
else
{
idKey = Long.valueOf(idCell.getDoubleValue().longValue());
}
Object id = ec.getNucleusContext().getIdentityManager().getDatastoreId(acmd.getFullClassName(), idKey);
pc = ec.findObject(id, new FieldValues()
{
// ObjectProvider calls the fetchFields method
public void fetchFields(ObjectProvider op)
{
op.replaceFields(acmd.getAllMemberPositions(), fm);
}
public void fetchNonLoadedFields(ObjectProvider op)
{
op.replaceNonLoadedFields(acmd.getAllMemberPositions(), fm);
}
public FetchPlan getFetchPlanForLoading()
{
return null;
}
}, null, ignoreCache, false);
}
else
{
Object id = new SCOID(acmd.getFullClassName());
pc = ec.findObject(id, new FieldValues()
{
public void fetchFields(ObjectProvider op)
{
op.replaceFields(acmd.getAllMemberPositions(), new FetchFieldManager(op, row, schemaTable));
}
public void fetchNonLoadedFields(ObjectProvider op)
{
op.replaceNonLoadedFields(acmd.getAllMemberPositions(), new FetchFieldManager(op, row, schemaTable));
}
public FetchPlan getFetchPlanForLoading()
{
return null;
}
}, null, ignoreCache, false);
}
ObjectProvider op = ec.findObjectProvider(pc);
// Any fields loaded above will not be wrapped since we did not have the ObjectProvider at the point of creating the FetchFieldManager, so wrap them now
op.replaceAllLoadedSCOFieldsWithWrappers();
results.add(pc);
}
}
return results;
}
public static boolean isOfficeValueTypeConsistent(OdfTableCell cell, OfficeValueTypeAttribute.Value type)
{
String cellTypeStr = cell.getValueType();
String typeStr = type.toString();
if (cellTypeStr != null && typeStr != null)
{
return cellTypeStr.equals(typeStr);
}
return false;
}
}