/*
* VsqiToLucene.java
*
* Created on 21 de septiembre de 2005, 18:17
*/
package edu.universal.tools;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.io.StringReader;
import edu.universal.search.LuceneIndex;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.universal.config.GlobalNames;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.ieee.ltsc.lom.*;
import org.ieee.ltsc.lom.jaxb.lomxml.LOM_JAXBContext;
import org.ieee.ltsc.lom.impl.LOMImpl;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.ubp.modules.lucenesearch.RankProfile;
import org.ubp.modules.lucenesearch.StoredResource;
import org.ubp.modules.lucenesearch.TextFilterParameterImpl;
/**
*
* @author saguirre
*/
public class VsqiToLucene {
// Logging
public static Log _logger = null;
static JAXBContext jaxbContext;
static Marshaller marshaller;
static private VsqiToLucene vsqi;
static {
try {
jaxbContext = JAXBContext.newInstance(System.getProperty(LOM_JAXBContext.CONTEXT_PATH_PROP, LOM_JAXBContext.DEFAULT_CONTEXT_PATH));
marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
} catch (Exception e) {
e.printStackTrace();
}
}
public static VsqiToLucene getInstance() {
if (vsqi == null) {
vsqi = new VsqiToLucene();
}
return vsqi;
}
/** Creates a new instance of VsqiToLucene */
private VsqiToLucene() {
try {
GlobalNames.initApplicationFromClass();
_logger = LogFactory.getLog(VsqiToLucene.class);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static String simpleQuery(String query1){
String result="";
LOM resultLOM;
try {
RankProfile rp;
TextFilterParameterImpl tfp = new TextFilterParameterImpl(query1, null, 0.7f, "Title");
rp = LuceneIndex.getLuceneFilter().detRankProfile(tfp);
StoredResource[] rgSR = LuceneIndex.getLuceneFilter().getFilteredResources(rp);
result="";
for (int i=0; i< rgSR.length;i++){
resultLOM = buildLOMProgramatically(rgSR[i]);
result += printLOMXML(resultLOM);
}
result+="";
}
catch (Exception e) {
System.out.println("Error: "+e);
}
return result;
}
public static String joinQuery(String query1, String query2){
String result="";
LOM resultLOM;
try {
RankProfile rp1;
RankProfile rp2;
RankProfile rp_joined;
TextFilterParameterImpl tfp1 = new TextFilterParameterImpl(query1, null, 0.7f, "Title");
TextFilterParameterImpl tfp2 = new TextFilterParameterImpl(query2, null, 0.7f, "Title");
rp1 = LuceneIndex.getLuceneFilter().detRankProfile(tfp1);
rp2 = LuceneIndex.getLuceneFilter().detRankProfile(tfp2);
rp_joined = LuceneIndex.getLuceneFilter().joinRankProfiles(rp1, rp2, true);
StoredResource[] rgSR = LuceneIndex.getLuceneFilter().getFilteredResources(rp_joined);
result="";
for (int i=0; i";
}
catch (Exception e) {
System.out.println("Error: "+e);
}
return result;
}
public static LOM buildLOMProgramatically(StoredResource obj) {
final LOMImpl lom = new LOMImpl();
lom.newGeneral().newIdentifier(0).newCatalog().setString("EducaNext");
lom.newGeneral().newIdentifier(0).newEntry().setString(obj.getID());
lom.newGeneral().newTitle().newString(-1).setString(obj.getTitle());
lom.newGeneral().newLanguage(-1).setString(obj.getKWResourceLanguages());
lom.newGeneral().newDescription(0).newString(0).setString(obj.getDescription());
String URLInfo="http://www.educanext.org/ubp/search@srchDetailsLR?lrID="+obj.getID();
lom.newTechnical().newLocation(0).setString(URLInfo);
return lom;
}
public static String printLOMXML(LOM lom) throws JAXBException {
String content="";
try{
StringWriter st = new StringWriter();
marshaller.marshal(lom,st);
content=st.toString();
}
catch(Exception e){
System.out.println(e);
}
return content;
}
public static String printLOMRDF(LOM lom) throws JAXBException {
final JAXBContext jaxbContext = JAXBContext.newInstance(System.getProperty(LOM_JAXBContext.CONTEXT_PATH_PROP, LOM_JAXBContext.DEFAULT_CONTEXT_PATH));
final Marshaller marshaller = jaxbContext.createMarshaller();
String content="";
try{
String path=GlobalNames.getRdfRepositoryBasePath();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
OutputStream os = new FileOutputStream( path+"lom.xml" );
marshaller.marshal(lom,os);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(path+"lomxml2lomrdf.xsl"));
StringWriter st = new StringWriter();
transformer.transform(new StreamSource(path+"lom.xml"), new StreamResult(st));
content=st.toString();
System.out.println(content);
}
catch(Exception e){
System.out.println(e);
}
return content;
}
public static String parserQueryVsqi(String queryVsqi) {
String query1="";
String query2="";
String results="";
try{
getInstance();
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new StringReader(queryVsqi));
Element root = doc.getRootElement();
String nameRoot=root.getName();
if (nameRoot.compareTo("simpleQuery")==0){
List children = root.getChildren("term");
Iterator iterator = children.iterator();
int cont = children.size();
while (iterator.hasNext()) {
Element child = (Element) iterator.next();
query1=query2;
query2="\'"+child.getValue()+"\' ";
}
if (cont==1){
results = simpleQuery(query2);
}else{
results = joinQuery(query1, query2);
}
}
else{
System.out.println("The query is not well-formed.");
}
}catch (JDOMException e) {
System.out.println("The query is not well-formed.");
}
catch (IOException e) {
System.out.println(e);
}
return results;
}
/* public static void main(String[] args) throws Exception {
try {
String queryTest1="" +
"Learning" +
"Object" +
"";
String queryTest2="" +
"Learning Object" +
"";
String termToLucene = parserQueryVsqi(queryTest1);
System.out.println(termToLucene);
}
catch (Exception e) {
e.printStackTrace();
}
System.exit(1);
}
*/
}