-
Piotr Gawron authoredPiotr Gawron authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
CellParser.java 4.34 KiB
package smash.appointment.parse;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
public class CellParser {
Logger logger = Logger.getLogger(CellParser.class);
private SubjectDao subjectDao;
Pattern timePattern = Pattern.compile("^[0-9][0-9]\\:[0-9][0-9]");
public String extractTime(String content) {
String result = null;
Matcher matcher = timePattern.matcher(content);
if (matcher.find()) {
result = matcher.group();
}
return result;
}
public String removeTime(String content) {
Matcher matcher = timePattern.matcher(content);
if (matcher.find()) {
content = matcher.replaceFirst("").trim();
}
return content;
}
public AppointmentEntry parseAppointment(String query, String defaultTime) {
AppointmentEntry result = new AppointmentEntry();
String time = extractTime(query);
if (time != null) {
query = removeTime(query);
} else {
time = defaultTime;
}
result.setTime(time);
Subject subject = extractSubject(query);
result.setSubject(subject);
AppointmentTypeCollection type = extractType(query);
if (type == null) {
result.addType(AppointmentType.OTHER);
} else {
result.addTypes(type.getTypes());
}
result.setSource(query);
return result;
}
public AppointmentTypeCollection extractType(String query) {
String simplifiedQuery = Utils.simplifyString(query);
AppointmentTypeCollection result = null;
String usedString = null;
for (AppointmentTypeCollection type : AppointmentTypeCollection.values()) {
boolean matchFound = false;
for (String string : type.getQueryStrings()) {
if (!matchFound) {
String simplifiedString = Utils.simplifyString(string);
if (simplifiedQuery.contains(simplifiedString)) {
matchFound = true;
if (result == null) {
result = type;
usedString = string;
} else {
if (string.contains(usedString)) {
result = type;
usedString = string;
} else if (usedString.contains(string)) {
// new one is a substring of old
} else { // if there is no substring then we might have a problem
AppointmentTypeCollection newType = result;
if (usedString.length() < string.length()) {
usedString = string;
newType = type;
}
logger.warn("More than one type possible for query: " + query + ". Type 1: " + result + ". Type 2: " + type + ". Choosing: " + newType);
result = newType;
}
}
}
}
}
}
return result;
}
private Subject extractSubject(String query) {
Subject result = null;
String simplifiedQuery = Utils.simplifyString(query);
SubjectIndexer[] mainIndices = new SubjectIndexer[] { //
new NameSurnameIndexer(), //
new SurnameNameIndexer(), //
new NdNumberIndexer(),//
};
result = getByIndices(query, simplifiedQuery, mainIndices);
if (result == null) {
SubjectIndexer[] secondaryIndices = new SubjectIndexer[] { //
new SurnameIndexer(), //
};
result = getByIndices(query, simplifiedQuery, secondaryIndices);
}
return result;
}
private Subject getByIndices(String query, String simplifiedQuery, SubjectIndexer[] mainIndices) {
Subject result = null;
for (Subject subject : subjectDao.getSubjects()) {
boolean matchFound = false;
for (SubjectIndexer indexer : mainIndices) {
if (!matchFound) {
if (indexer.match(subject, simplifiedQuery)) {
matchFound = true;
if (result == null) {
result = subject;
} else {
Subject newResult = result;
if (indexer.isBetter(subject, result)) {
newResult = subject;
}
logger.warn(
"[" + indexer.getClass().getSimpleName() + "]" + "More than one subject possible for query: " + query + ". Subject 1: " + result
+ ". Subject 2: " + subject + ". Choosing: " + newResult);
result = newResult;
}
}
}
}
}
return result;
}
/**
* @return the subjectDao
* @see #subjectDao
*/
public SubjectDao getSubjectDao() {
return subjectDao;
}
/**
* @param subjectDao
* the subjectDao to set
* @see #subjectDao
*/
public void setSubjectDao(SubjectDao subjectDao) {
this.subjectDao = subjectDao;
}
}