use of com.serotonin.m2m2.web.mvc.rest.v1.csv.CSVException in project ma-core-public by infiniteautomation.
the class CSVPojoHandler method setField.
/**
* Set the filed of in a pojo or child entity of it.
*
* @param pojo
* The object to set the value in
* @param header
* The header of the field
* @param text
* The String representation of the value to set
*/
public boolean setField(Object pojo, String header, String text) throws CSVException {
if (text.length() > 0) {
// If text is empty use default value in pojo which is set by the
// user
CSVDataHandler fieldHandler = mapping.get(header);
// all columns
if (fieldHandler != null) {
PropertyEditor editor = fieldHandler.getEditor();
if (editor instanceof CSVPropertyEditor)
((CSVPropertyEditor) editor).setContext(pojo);
try {
editor.setAsText(text);
} catch (NumberFormatException e) {
throw new CSVException("Invalid number: '" + text + "'");
} catch (Exception e) {
throw new CSVException("Invalid value: '" + e.getMessage() + "'");
}
Object value = editor.getValue();
fieldHandler.setValue(pojo, value);
return true;
} else {
// No mapping found in this pojo, check children
for (PojoChild child : children) {
CSVPojoHandler pojoHandler = child.getPojoHandler();
if (pojoHandler.containsField(header)) {
Object obj = child.getValue(pojo);
if (obj == null) {
obj = pojoHandler.newInstance();
}
if (pojoHandler.setField(obj, header, text)) {
// Only set child field if we actually update the
// field in the object object
// If no fields are set in the child, the child
// should be the default value
child.setValue(pojo, obj);
return true;
}
}
}
}
} else {
// No value set as it was empty
return true;
}
return false;
}
use of com.serotonin.m2m2.web.mvc.rest.v1.csv.CSVException in project ma-core-public by infiniteautomation.
the class CSVPojoReader method readNext.
/**
* Read the next pojo from the csv file
*
* @return The next pojo from the csv file or null if end of file
* @throws IOException
*/
public T readNext() throws IOException {
if (headers == null) {
headers = reader.readNext();
if (headers == null)
return null;
// Make sure we don't have any accidental whitespaces
for (int i = 0; i < headers.length; i++) {
headers[i] = headers[i].trim();
}
if (headers.length > 0) {
if (!headers[0].equalsIgnoreCase("modelType"))
throw new IOException("Invalid header format, first column must be modelType.");
} else {
throw new IOException("No headers provided.");
}
}
String[] line = reader.readNext();
T pojo = null;
if (line != null) {
if (line.length != headers.length) {
String columnString = new String();
for (String col : line) columnString += col + ",";
if (line.length > headers.length) {
throw new IOException("Header and row length must match.\nRow has too many columns.\n" + columnString);
} else {
throw new IOException("Header and row length must match.\nRow has too few columns.\n" + columnString);
}
}
if (!pojoHandler.isInitialized()) {
// Always needs to be
String typeName = line[0];
// Find the model
ModelDefinition definition = null;
List<ModelDefinition> definitions = ModuleRegistry.getModelDefinitions();
for (ModelDefinition d : definitions) {
if (d.getModelTypeName().equalsIgnoreCase(typeName)) {
definition = d;
break;
}
}
if (definition == null)
throw new ModelNotFoundException(typeName);
else
pojoHandler.initialize(line, definition.createModel());
}
pojo = (T) pojoHandler.newInstance();
for (int i = 1; i < headers.length; i++) {
try {
pojoHandler.setField(pojo, headers[i], line[i].trim());
} catch (CSVException e) {
throw new CSVException("Row " + i + " column '" + headers[i] + "' " + e.getMessage());
}
}
}
return pojo;
}
Aggregations