package eu.lsem.bakalarka.service;
import eu.lsem.bakalarka.model.*;
import eu.lsem.bakalarka.dao.LikeThesisSearch;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
import java.util.regex.Pattern;
import net.sourceforge.stripes.util.StringUtil;
import sun.misc.Regexp;
public class ThesesServiceImpl implements ThesesService {
private ApplicationConfiguration applicationConfiguration;
private FileSystemUtils fileSystemUtils;
final String openHtml = "<span class='highlight'>";
final String closeHtml = "</span>";
final String openSymbol = "[[[S_T_A_R_T_584]]]";
final String closeSymbol = "[[[S_T_O_P_584]]]";
public void setApplicationConfiguration(ApplicationConfiguration applicationConfiguration) {
this.applicationConfiguration = applicationConfiguration;
}
public void setFileSystemUtils(FileSystemUtils fileSystemUtils) {
this.fileSystemUtils = fileSystemUtils;
}
@Override
public List<ThesisDocument> getDirThesisDocuments(String relativeDir, boolean supportedOnly) {
List<ThesisDocument> list = new ArrayList<ThesisDocument>();
Collection documents;
try { /* tohle overeni tady je, ze FileUtils.listFiles pada, kdyz adresar vubec neexistuje */
documents = FileUtils.listFiles(new File(getDipstoreResourceAbsolutePath(relativeDir)), null, false);
} catch (Exception e) {
return list;
}
for (Object akt : documents) {
File file = (File) akt;
String filename = file.getName();
String ext = isSupportedDocument(filename);
if (!supportedOnly || ext != null)
list.add(new ThesisDocument(filename, ext));
}
return list;
}
@Override
public String getDipstoreResourceAbsolutePath(String relativePath) {
return applicationConfiguration.getDipstoreDir() + relativePath;
}
@Override
public String parseDocument(String path) {
File f = new File(path);
try {
return applicationConfiguration.getDocumentParser(f).getStringContent(f);
} catch (Exception e) {
return "";
}
}
@Override
public String parseAnnotation(String text) {
if (text == null)
return "";
String anotace = "Anotace";
int anotaceIndex = text.indexOf(anotace);
int iiIndex = text.indexOf("ii");
if (anotaceIndex == -1 || iiIndex == -1 || anotaceIndex >= iiIndex)
return "";
return text.substring(anotaceIndex + anotace.length(), text.indexOf("ii"));
}
@Override
public String parseResume(String text) {
if (text == null)
return "";
String resume = "Z�v�r";
String reference = "Reference";
int resumeIndex = text.lastIndexOf(resume);
int referenceIndex = text.indexOf(reference, resumeIndex);
if (resumeIndex == -1 || referenceIndex == -1)
return "";
return text.substring(resumeIndex + resume.length(), referenceIndex);
}
@Override
public String parseAuthor(String text) {
return text.split("\n")[4].substring(5);
}
@Override
public String parseSubject(String text) {
return text.split("\n")[3];
}
@Override
public boolean hasData(MinimalThesis t) {
File dir = new File(getDipstoreResourceAbsolutePath(t.getRelativeThesisPath()));
if (!dir.exists() || !dir.isDirectory() || !dir.canRead())
return false;
return (!FileUtils.listFiles(dir, null, true).isEmpty());
}
private String isSupportedDocument(String s) {
List<String> supported = applicationConfiguration.getSupportedDocuments();
for (String ext : supported) {
if (s.endsWith(ext))
return ext;
}
return null;
}
@Override
public void deleteThesisDirectory(MinimalThesis t) throws IOException {
String path = getDipstoreResourceAbsolutePath(t.getRelativeThesisPath());
boolean result = fileSystemUtils.deleteDirectory(path);
if (!result) {
throw new IOException("Nebylo mo�n� smazat soubor.");
}
}
@Override
public void escapeThesisBasic(BasicThesis t) {
if (t == null)
return;
t.setAuthor(StringEscapeUtils.escapeHtml(t.getAuthor()));
t.setSubject(StringEscapeUtils.escapeHtml(t.getSubject()));
}
@Override
public void escapeThesis(Thesis t) {
if (t == null)
return;
escapeThesisBasic(t);
t.setAnnotation(StringEscapeUtils.escapeHtml(t.getAnnotation()));
t.setResume(StringEscapeUtils.escapeHtml(t.getResume()));
t.setFulltext(StringEscapeUtils.escapeHtml(t.getFulltext()));
}
@Override
public void highlightThesisBasic(BasicThesis thesis) {
if (thesis == null)
return;
if (thesis.getAuthor() != null) {
thesis.setAuthor(thesis.getAuthor().replace(openSymbol, openHtml).replace(closeSymbol, closeHtml));
}
if (thesis.getSubject() != null) {
thesis.setSubject(thesis.getSubject().replace(openSymbol, openHtml).replace(closeSymbol, closeHtml));
}
}
@Override
public void highlightThesis(Thesis thesis) {
if (thesis == null)
return;
highlightThesisBasic(thesis);
if (thesis.getAnnotation() != null) {
thesis.setAnnotation(thesis.getAnnotation().replace(openSymbol, openHtml).replace(closeSymbol, closeHtml));
}
if (thesis.getResume() != null) {
thesis.setResume(thesis.getResume().replace(openSymbol, openHtml).replace(closeSymbol, closeHtml));
}
if (thesis.getFulltext() != null) {
thesis.setFulltext(thesis.getFulltext().replace(openSymbol, openHtml).replace(closeSymbol, closeHtml));
}
}
@Override
public void highlightThesisBasicLikeSearch(BasicThesis t, ThesisSearch search) {
if (t == null || search == null) return;
LikeThesisSearch likeSearch = new LikeThesisSearch(search);
for (String lexem : likeSearch.getWordsWithoutPercentSymbol()) {
lexem = StringEscapeUtils.escapeHtml(lexem);
if (search.isSearchAuthor() && t.getAuthor() != null)
t.setAuthor(replaceAll(t.getAuthor(), lexem));
if (search.isSearchSubject() && t.getSubject() != null)
t.setSubject(replaceAll(t.getSubject(), lexem));
}
}
@Override
public void highlightThesisLikeSearch(Thesis t, ThesisSearch search) {
if (t == null || search == null) return;
highlightThesisBasicLikeSearch(t, search);
LikeThesisSearch likeSearch = new LikeThesisSearch(search);
for (String lexem : likeSearch.getWordsWithoutPercentSymbol()) {
lexem = StringEscapeUtils.escapeHtml(lexem);
if (search.isSearchAnnotation() && t.getAnnotation() != null)
t.setAnnotation(replaceAll(t.getAnnotation(), lexem));
if (search.isSearchResume() && t.getResume() != null)
t.setResume(replaceAll(t.getResume(), lexem));
if (search.isSearchFulltext() && t.getFulltext() != null)
t.setFulltext(replaceAll(t.getFulltext(), lexem));
}
}
private String replaceAll(String text, String regex) {
Pattern myPattern = Pattern.compile("(" + regex + ")", Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE);
text = myPattern.matcher(text).replaceAll(openHtml + "$1" + closeHtml);
return text;
}
}