Skip to content

Commit

Permalink
#46 Date autodetection complete
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidzen committed Mar 29, 2015
1 parent 04876ba commit 16d58b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.etl.OETLProcessor;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -143,19 +146,26 @@ public Object executeTransform(final Object input) {
// DETERMINE THE TYPE
final char firstChar = fieldStringValue.charAt(0);
if (Character.isDigit(firstChar)) {
// NUMBER
if (fieldStringValue.contains(".") || fieldStringValue.contains(",")) {
String numberAsString = fieldStringValue.replaceAll(",", ".");
fieldValue = new Float(numberAsString);
if (!Float.isFinite((Float) fieldValue)) {
fieldValue = new Double(numberAsString);
}
} else
try {
fieldValue = new Integer(fieldStringValue);
} catch (Exception e) {
fieldValue = new Long(fieldStringValue);
}
//DATE
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(true);
try {
fieldValue = df.parse(fieldStringValue);
} catch (ParseException pe) {
// NUMBER
if (fieldStringValue.contains(".") || fieldStringValue.contains(",")) {
String numberAsString = fieldStringValue.replaceAll(",", ".");
fieldValue = new Float(numberAsString);
if (!Float.isFinite((Float) fieldValue)) {
fieldValue = new Double(numberAsString);
}
} else
try {
fieldValue = new Integer(fieldStringValue);
} catch (Exception e) {
fieldValue = new Long(fieldStringValue);
}
}
}
else
fieldValue = fieldStringValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public void testDateTypeAutodetection(){
List<ODocument> res = getResult();
ODocument doc = res.get(0);
Date birthday = doc.field("BirthDay");
assertEquals(2008, birthday.getYear());
assertEquals(3, birthday.getMonth());
assertEquals(2008, birthday.getYear()+1900);
assertEquals(4, birthday.getMonth()+1);
assertEquals(30, birthday.getDate());
}

Expand Down

0 comments on commit 16d58b8

Please sign in to comment.