
org.datanucleus.store.excel.fieldmanager.FetchFieldManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-excel Show documentation
Show all versions of datanucleus-excel Show documentation
DataNucleus supports persistence to heterogeneous datastores and this plugin provides persistence to Excel.
The newest version!
/**********************************************************************
Copyright (c) 2008 Erik Bengtson 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 :
2008 Andy Jefferson - change to use ExcelUtils
...
***********************************************************************/
package org.datanucleus.store.excel.fieldmanager;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
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.fieldmanager.AbstractFetchFieldManager;
import org.datanucleus.store.fieldmanager.FieldManager;
import org.datanucleus.store.schema.table.MemberColumnMapping;
import org.datanucleus.store.schema.table.Table;
import org.datanucleus.store.types.SCOUtils;
import org.datanucleus.store.types.converters.MultiColumnConverter;
import org.datanucleus.store.types.converters.TypeConverter;
import org.datanucleus.store.types.converters.TypeConverterHelper;
import org.datanucleus.util.Base64;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.StringUtils;
import org.datanucleus.util.TypeConversionHelper;
/**
* FieldManager to handle the retrieval of information from an Excel worksheet row/column into
* a field of an object.
*/
public class FetchFieldManager extends AbstractFetchFieldManager
{
protected Table table;
protected Sheet sheet;
protected int rowNumber;
public FetchFieldManager(ObjectProvider op, Sheet sheet, int row, Table table)
{
super(op);
this.table = table;
this.rowNumber = row;
this.sheet = sheet;
}
public FetchFieldManager(ExecutionContext ec, AbstractClassMetaData cmd, Sheet sheet, int row, Table table)
{
super(ec, cmd);
this.table = table;
this.rowNumber = row;
this.sheet = sheet;
}
protected MemberColumnMapping getColumnMapping(int fieldNumber)
{
return table.getMemberColumnMappingForMember(cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber));
}
public boolean fetchBooleanField(int fieldNumber)
{
Cell cell = sheet.getRow(rowNumber).getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition());
if (cell == null)
{
return false;
}
return cell.getBooleanCellValue();
}
public byte fetchByteField(int fieldNumber)
{
Cell cell = sheet.getRow(rowNumber).getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition());
if (cell == null)
{
return 0;
}
return (byte) cell.getNumericCellValue();
}
public char fetchCharField(int fieldNumber)
{
Cell cell = sheet.getRow(rowNumber).getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition());
if (cell == null)
{
return 0;
}
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
// In case the char was interpreted as a numeric
return (char)cell.getNumericCellValue();
}
return cell.getRichStringCellValue().getString().charAt(0);
}
public double fetchDoubleField(int fieldNumber)
{
Cell cell = sheet.getRow(rowNumber).getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition());
if (cell == null)
{
return 0;
}
return cell.getNumericCellValue();
}
public float fetchFloatField(int fieldNumber)
{
Cell cell = sheet.getRow(rowNumber).getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition());
if (cell == null)
{
return 0;
}
return (float) cell.getNumericCellValue();
}
public int fetchIntField(int fieldNumber)
{
Cell cell = sheet.getRow(rowNumber).getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition());
if (cell == null)
{
return 0;
}
return (int) cell.getNumericCellValue();
}
public long fetchLongField(int fieldNumber)
{
Cell cell = sheet.getRow(rowNumber).getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition());
if (cell == null)
{
return 0;
}
return (long) cell.getNumericCellValue();
}
public short fetchShortField(int fieldNumber)
{
Cell cell = sheet.getRow(rowNumber).getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition());
if (cell == null)
{
return 0;
}
return (short) cell.getNumericCellValue();
}
public String fetchStringField(int fieldNumber)
{
Cell cell = sheet.getRow(rowNumber).getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition());
if (cell == null)
{
return null;
}
return cell.getRichStringCellValue().getString();
}
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, sheet, rowNumber, 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 Excel");
}
}
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)
{
Cell cell = sheet.getRow(rowNumber).getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition());
if (cell == null)
{
return null;
}
Object value = null;
Class datastoreType = TypeConverterHelper.getDatastoreTypeForTypeConverter(conv, mmd.getType());
if (datastoreType == String.class)
{
String cellValue = cell.getRichStringCellValue().getString();
if (!StringUtils.isWhitespace(cellValue))
{
value = conv.toMemberType(cellValue);
}
}
else if (Number.class.isAssignableFrom(datastoreType))
{
value = conv.toMemberType(cell.getNumericCellValue());
}
else if (Boolean.class.isAssignableFrom(datastoreType))
{
value = conv.toMemberType(cell.getBooleanCellValue());
}
else if (Date.class.isAssignableFrom(datastoreType))
{
value = conv.toMemberType(cell.getDateCellValue());
}
else
{
NucleusLogger.DATASTORE_PERSIST.warn("TypeConverter for member " + mmd.getFullFieldName() + " converts to " + datastoreType.getName() + " - not yet supported");
}
if (op != null)
{
return SCOUtils.wrapSCOField(op, fieldNumber, value, true);
}
return value;
}
// Member stored in multiple columns and convertable using TypeConverter
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 optional ? Optional.empty() : null;
}
return optional ? Optional.of(obj) : obj;
}
return optional ? Optional.empty() : null;
}
else if (RelationType.isRelationMultiValued(relationType))
{
// Collection/Map/Array
Cell cell = sheet.getRow(rowNumber).getCell(mapping.getColumn(0).getPosition());
if (cell == null)
{
return null;
}
String cellStr = cell.getRichStringCellValue().getString();
if (cellStr == null)
{
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy