📄 bimldaofile.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package eti.bi.alphaminer.core.dao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import eti.bi.alphaminer.vo.BICase;
import eti.bi.alphaminer.vo.CaseFactory;
import eti.bi.alphaminer.vo.CaseInfoList;
import eti.bi.alphaminer.vo.CaseInformation;
import eti.bi.alphaminer.vo.NodeFactory;
import eti.bi.alphaminer.vo.SearchCriteria;
import eti.bi.alphaminer.vo.SearchedCaseInfoList;
import eti.bi.common.Constants;
import eti.bi.common.System.SysConfig;
import eti.bi.common.System.SysLog;
import eti.bi.exception.BaseException;
import eti.bi.exception.CaseNotFoundException;
import eti.bi.exception.SysException;
import eti.bi.util.Utils;
/**
*
* This is an implementation of the interface BIMLDAO which used the file system as
* the BIML repository. The indexing of the BIML files are based of the file name of
* the BIML file. The file name is in the form <prefix>_<id>_<type>.xml, where the
* prefix is stored in the class Constants. The path of the repository is stored in
* the system configuration which can be retrieved by the class SysConfig. Under the
* root path of the BIML repository, there are two sub directories which called
* "activate" and "trash" which separate the BIML files in these two states.
*
* @since 1.0
* @version $Revision$ $Date$
* @author $Author$
*/
public class BIMLDAOFile implements BIMLDAO{
/* This method is remarked because it is replaced by the method listAllBIML and
* can be deleted later.
public Collection getAllBIML()
{
ArrayList aBIMLList = new ArrayList();
try {
String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
File aDir = new File(aDirname);
System.err.println("Full Path="+aDir.getAbsolutePath());
File [] aFiles = aDir.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
if (name.matches(".*xml"))
return true;
else
return false;
}
}
);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
for (int i=0; i<aFiles.length; i++)
{
System.err.println("file="+aFiles[i].getName());
Document aDocument = null;
try {
aDocument = builder.parse(aFiles[i]);
} catch (SAXException e) {
log.warn("SAXException during parsing file: " + aFiles[i].getName() + " " + e);
} catch (IOException e) {
log.warn("IOException during parsing file: " + aFiles[i].getName() + " " + e);
}
aBIMLList.add(aDocument);
}
} catch (FactoryConfigurationError e) {
log.error("FactoryConfigurationError during constructing document builder: " + e);
} catch (ParserConfigurationException e) {
log.error("ParserConfigurationException during constructing document builder: " + e);
}
return aBIMLList;
}
*/
/**
* Find all the files in both "activate" and "inprogress" directories of th
* BIML repository path.
*
* @see hk.hku.eti.bi.bdm.BIMLDAO#listAllBIML()
*/
public Collection<BICaseInfo> listAllBIML()
{
ArrayList<BICaseInfo> aBIMLList = new ArrayList<BICaseInfo>();
String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
File aDir = new File(aDirname, "activate");
File [] aFiles = aDir.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
if (name.matches(".*xml") && !name.matches(".*template.*"))
return true;
else
return false;
}
}
);
Integer aStatus = BICaseInfo.ACTIVATE;
for (int i=0; i<aFiles.length; i++)
{
String aBIMLName = aFiles[i].getName();
String aBIMLId = aBIMLName.substring(Constants.BIML_FILE_PREFIX.length(), Constants.BIML_FILE_PREFIX.length() + Constants.BIML_ID_PATTERN.length());
String aType = aBIMLName.substring(aBIMLName.length()-7, aBIMLName.length()-4);
BICaseInfo aBICase = new BICaseInfo(aBIMLId, aType, null, aStatus, false, false, null);
//aBIMLList.add(aFiles[i].getName());
aBIMLList.add(aBICase);
}
aDir = new File(aDirname, "inprogress");
aFiles = aDir.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
if (name.matches(".*xml") && !name.matches(".*template.*"))
return true;
else
return false;
}
}
);
aStatus = BICaseInfo.INPROGRESS;
for (int i=0; i<aFiles.length; i++)
{
String aBIMLName = aFiles[i].getName();
String aBIMLId = aBIMLName.substring(Constants.BIML_FILE_PREFIX.length(), Constants.BIML_FILE_PREFIX.length() + Constants.BIML_ID_PATTERN.length());
String aType = aBIMLName.substring(aBIMLName.length()-7, aBIMLName.length()-4);
BICaseInfo aBICase = new BICaseInfo(aBIMLId, aType, null, aStatus, false, false, null);
//aBIMLList.add(aFiles[i].getName());
aBIMLList.add(aBICase);
}
return aBIMLList;
}
public CaseInfoList getCaseInfoList()
{
BufferedReader aBufferedReader = null;
CaseInfoList list = new CaseInfoList();
String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
File aDir = new File(aDirname, "activate");
File [] aFiles = aDir.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
if (name.matches(".*xml") && !name.matches(".*template.*"))
return true;
else
return false;
}
}
);
CaseInformation caseInfo = null;
for (int i=0; i<aFiles.length; i++)
{
String aBIMLName = aFiles[i].getName();
String aBIMLId = aBIMLName.substring(Constants.BIML_FILE_PREFIX.length(), Constants.BIML_FILE_PREFIX.length() + Constants.BIML_ID_PATTERN.length());
String aType = aBIMLName.substring(aBIMLName.length()-7, aBIMLName.length()-4);
if (aType.equals("CML"))
{
try
{
aBufferedReader = new BufferedReader(new FileReader(aFiles[i]));
StringBuffer aFileContent = new StringBuffer();
String aLine = null;
while ((aLine = aBufferedReader.readLine()) != null)
{
aFileContent.append(aLine);
}
BICase aCase = CaseFactory.createCase(aBIMLId, aFileContent.toString());
caseInfo = aCase.getCaseInfo();
//caseInfo = new CaseInformation(aBIMLId,aFileContent.toString());
list.setCaseInfo(caseInfo);
}catch(FileNotFoundException e)
{
}catch(IOException e)
{
}finally
{
try
{
aBufferedReader.close();
}catch(IOException e)
{}
}
}
}
return list;
}
public SearchedCaseInfoList searchCase(SearchCriteria a_SearchCriteria, double a_Threshold)
{
BufferedReader aBufferedReader = null;
SearchedCaseInfoList list = new SearchedCaseInfoList();
list.setSearchCriteria(a_SearchCriteria);
String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
File aDir = new File(aDirname, "activate");
File [] aFiles = aDir.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
if (name.matches(".*xml") && !name.matches(".*template.*"))
return true;
else
return false;
}
}
);
CaseInformation caseInfo = null;
for (int i=0; i<aFiles.length; i++)
{
String aBIMLName = aFiles[i].getName();
String aBIMLId = aBIMLName.substring(Constants.BIML_FILE_PREFIX.length(), Constants.BIML_FILE_PREFIX.length() + Constants.BIML_ID_PATTERN.length());
String aType = aBIMLName.substring(aBIMLName.length()-7, aBIMLName.length()-4);
if (aType.equals("CML"))
{
try
{
aBufferedReader = new BufferedReader(new FileReader(aFiles[i]));
StringBuffer aFileContent = new StringBuffer();
String aLine = null;
while ((aLine = aBufferedReader.readLine()) != null)
{
aFileContent.append(aLine);
}
BICase aCase = CaseFactory.createCase(aBIMLId, aFileContent.toString());
caseInfo = aCase.getCaseInfo();
genScore(caseInfo, a_SearchCriteria);
if (caseInfo.getScore()>0 && caseInfo.getScore()>=a_Threshold)
{
list.setCaseInfo(caseInfo);
}
}catch(FileNotFoundException e)
{
}catch(IOException e)
{
}finally
{
try
{
aBufferedReader.close();
}catch(IOException e)
{}
}
}
}
return list;
}
public void genScore(CaseInformation aCaseInfo, SearchCriteria aSearchCriteria)
{
double comparedItem = 0;
double finalScore = 0;
String source;
String match;
match = aSearchCriteria.getKeywords();
//key search
if(match!=null){
match = match.toUpperCase().trim();
source = aCaseInfo.getCaseName();
source = source.toUpperCase().trim();
finalScore = matchScore(source,match);
aCaseInfo.setScore(finalScore);
return;
}
//advanced search
match = aSearchCriteria.getBusinessObjective();
if (match!=null )
{
comparedItem++;
match = match.toUpperCase().trim();
source = aCaseInfo.getBusinessObjective();
source = source.toUpperCase().trim();
finalScore += matchScore(source,match);
}
match = aSearchCriteria.getIndustry();
if (match!=null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -