org.datanucleus.store.odf.fieldmanager.FetchFieldManager 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.fieldmanager;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Currency;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.ExecutionContext;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.exceptions.NucleusObjectNotFoundException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.identity.IdentityUtils;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.FieldRole;
import org.datanucleus.metadata.JdbcType;
import org.datanucleus.metadata.MetaDataUtils;
import org.datanucleus.metadata.RelationType;
import org.datanucleus.query.QueryUtils;
import org.datanucleus.state.ObjectProvider;
import org.datanucleus.store.schema.table.Column;
import org.datanucleus.store.schema.table.MemberColumnMapping;
import org.datanucleus.store.schema.table.Table;
import org.datanucleus.store.types.converters.MultiColumnConverter;
import org.datanucleus.store.types.converters.TypeConverter;
import org.datanucleus.store.fieldmanager.AbstractFetchFieldManager;
import org.datanucleus.store.fieldmanager.FieldManager;
import org.datanucleus.store.types.SCOUtils;
import org.datanucleus.util.Base64;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.StringUtils;
import org.datanucleus.util.TypeConversionHelper;
import org.odftoolkit.odfdom.doc.table.OdfTableCell;
import org.odftoolkit.odfdom.doc.table.OdfTableRow;
import org.odftoolkit.odfdom.dom.attribute.office.OfficeValueTypeAttribute;
/**
* FieldManager for the fetch of fields from ODF.
*/
public class FetchFieldManager extends AbstractFetchFieldManager
{
protected final Table table;
protected final OdfTableRow row;
public FetchFieldManager(ObjectProvider op, OdfTableRow row, Table table)
{
super(op);
this.table = table;
this.row = row;
}
public FetchFieldManager(ExecutionContext ec, AbstractClassMetaData cmd, OdfTableRow row, Table table)
{
super(ec, cmd);
this.table = table;
this.row = row;
}
protected MemberColumnMapping getColumnMapping(int fieldNumber)
{
return table.getMemberColumnMappingForMember(cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber));
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#fetchBooleanField(int)
*/
@Override
public boolean fetchBooleanField(int fieldNumber)
{
OdfTableCell cell = row.getCellByIndex(getColumnMapping(fieldNumber).getColumn(0).getPosition());
return cell.getBooleanValue();
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#fetchByteField(int)
*/
@Override
public byte fetchByteField(int fieldNumber)
{
OdfTableCell cell = row.getCellByIndex(getColumnMapping(fieldNumber).getColumn(0).getPosition());
Double val = cell.getDoubleValue();
if (val == null)
{
return 0;
}
return cell.getDoubleValue().byteValue();
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#fetchCharField(int)
*/
@Override
public char fetchCharField(int fieldNumber)
{
OdfTableCell cell = row.getCellByIndex(getColumnMapping(fieldNumber).getColumn(0).getPosition());
return cell.getStringValue().charAt(0);
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#fetchDoubleField(int)
*/
@Override
public double fetchDoubleField(int fieldNumber)
{
OdfTableCell cell = row.getCellByIndex(getColumnMapping(fieldNumber).getColumn(0).getPosition());
Double val = cell.getDoubleValue();
if (val == null)
{
return 0.0;
}
return val.doubleValue();
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#fetchFloatField(int)
*/
@Override
public float fetchFloatField(int fieldNumber)
{
OdfTableCell cell = row.getCellByIndex(getColumnMapping(fieldNumber).getColumn(0).getPosition());
Double val = cell.getDoubleValue();
if (val == null)
{
return 0.0f;
}
return val.floatValue();
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#fetchIntField(int)
*/
@Override
public int fetchIntField(int fieldNumber)
{
OdfTableCell cell = row.getCellByIndex(getColumnMapping(fieldNumber).getColumn(0).getPosition());
Double val = cell.getDoubleValue();
if (val == null)
{
return 0;
}
return val.intValue();
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#fetchLongField(int)
*/
@Override
public long fetchLongField(int fieldNumber)
{
OdfTableCell cell = row.getCellByIndex(getColumnMapping(fieldNumber).getColumn(0).getPosition());
Double val = cell.getDoubleValue();
if (val == null)
{
return 0;
}
return val.longValue();
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#fetchShortField(int)
*/
@Override
public short fetchShortField(int fieldNumber)
{
OdfTableCell cell = row.getCellByIndex(getColumnMapping(fieldNumber).getColumn(0).getPosition());
Double val = cell.getDoubleValue();
if (val == null)
{
return 0;
}
return val.shortValue();
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#fetchStringField(int)
*/
@Override
public String fetchStringField(int fieldNumber)
{
OdfTableCell cell = row.getCellByIndex(getColumnMapping(fieldNumber).getColumn(0).getPosition());
return cell.getStringValue();
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#fetchObjectField(int)
*/
@Override
public Object fetchObjectField(int fieldNumber)
{
ClassLoaderResolver clr = ec.getClassLoaderResolver();
AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
RelationType relationType = mmd.getRelationType(clr);
// Special cases
if (relationType != RelationType.NONE && MetaDataUtils.getInstance().isMemberEmbedded(ec.getMetaDataManager(), clr, mmd, relationType, null))
{
// Embedded field
if (RelationType.isRelationSingleValued(relationType))
{
// TODO Null detection
List embMmds = new ArrayList();
embMmds.add(mmd);
AbstractClassMetaData embCmd = ec.getMetaDataManager().getMetaDataForClass(mmd.getType(), clr);
ObjectProvider embOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, embCmd, op, fieldNumber);
FieldManager fetchEmbFM = new FetchEmbeddedFieldManager(embOP, row, embMmds, table);
embOP.replaceFields(embCmd.getAllMemberPositions(), fetchEmbFM);
return embOP.getObject();
}
else if (RelationType.isRelationMultiValued(relationType))
{
throw new NucleusUserException("Dont support embedded multi-valued field at " + mmd.getFullFieldName() + " with ODF");
}
}
return fetchObjectFieldInternal(fieldNumber, mmd, clr, relationType);
}
protected Object fetchObjectFieldInternal(int fieldNumber, AbstractMemberMetaData mmd, ClassLoaderResolver clr, RelationType relationType)
{
MemberColumnMapping mapping = getColumnMapping(fieldNumber);
boolean optional = false;
if (Optional.class.isAssignableFrom(mmd.getType()))
{
if (relationType != RelationType.NONE)
{
relationType = RelationType.ONE_TO_ONE_UNI;
}
optional = true;
}
if (relationType == RelationType.NONE)
{
if (mapping.getTypeConverter() != null)
{
TypeConverter conv = mapping.getTypeConverter();
if (mapping.getNumberOfColumns() > 1)
{
boolean isNull = true;
Object valuesArr = null;
Class[] colTypes = ((MultiColumnConverter)conv).getDatastoreColumnTypes();
if (colTypes[0] == int.class)
{
valuesArr = new int[mapping.getNumberOfColumns()];
}
else if (colTypes[0] == long.class)
{
valuesArr = new long[mapping.getNumberOfColumns()];
}
else if (colTypes[0] == double.class)
{
valuesArr = new double[mapping.getNumberOfColumns()];
}
else if (colTypes[0] == float.class)
{
valuesArr = new double[mapping.getNumberOfColumns()];
}
else if (colTypes[0] == String.class)
{
valuesArr = new String[mapping.getNumberOfColumns()];
}
// TODO Support other types
else
{
valuesArr = new Object[mapping.getNumberOfColumns()];
}
for (int i=0;i 0)
{
// Uses persistent identity
obj = IdentityUtils.getObjectFromPersistableIdentity(idStr, memberCmd, ec);
}
else
{
// Uses legacy identity
obj = IdentityUtils.getObjectFromIdString(idStr, memberCmd, ec, true);
}
}
catch (NucleusObjectNotFoundException nfe)
{
NucleusLogger.GENERAL.warn("Object=" + op + " field=" + mmd.getFullFieldName() + " has id=" + idStr + " but could not instantiate object with that identity");
return null;
}
return optional ? Optional.of(obj) : obj;
}
return optional ? Optional.empty() : null;
}
else if (RelationType.isRelationMultiValued(relationType))
{
// Collection/Map/Array
OdfTableCell cell = row.getCellByIndex(mapping.getColumn(0).getPosition());
String cellStr = cell.getStringValue();
if (cellStr == null || StringUtils.isWhitespace(cellStr))
{
return null;
}
if (cellStr.startsWith("[") && cellStr.endsWith("]"))
{
cellStr = cellStr.substring(1, cellStr.length()-1);
String[] components = MetaDataUtils.getInstance().getValuesForCommaSeparatedAttribute(cellStr);
if (Collection.class.isAssignableFrom(mmd.getType()))
{
Collection