📄 bimldaofile.java
字号:
//System.out.println("form file = " + aFormFile.getFileData());
// aFOS.flush();
// aFOS.close();
// //Document aDocument = builder.parse(aFormFile.getInputStream());
//
// return aBIMLId;
// } catch (FileNotFoundException e) {
// log.error("Error writing to the file: " + aTargetFile.getName());
// throw new BIMLException("Error writing to the file: " + aTargetFile.getName() + " " +e);
// } catch (FactoryConfigurationError e) {
// log.error(e);
// throw new BIMLException(e);
// } catch (ParserConfigurationException e) {
// log.error("FactoryConfigurationErrors: ");
// throw new BIMLException("FactoryConfigurationErrors: " + e);
// } catch (IOException e) {
// log.error("Error writing to the file: " + aTargetFile.getName());
// throw new BIMLException("Error writing to the file: " + aTargetFile.getName(), e);
// }/*
// catch (SAXException e) {
// log.error("Error parsing the input file.");
// throw new BIMLException(e);
// }*/
//
// }
/**
* Saves the content in the aBIMLString to the BIML repository. The location to be
* stored is determined by the aStatus. If aBIMLId is not null, then use it as a
* part of the file name, otherwise generate a new one for it.
*
* @see hk.hku.eti.bi.bdm.BIMLDAO#saveBIMLFile(String, String, String, Integer)
*/
public String saveBIMLFile(String aBIMLString, String aBIMLType, String aBIMLId, Integer aStatus) throws BIMLException
{
File aTargetFile = null;
try {
String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@SuppressWarnings("unused") DocumentBuilder builder = factory.newDocumentBuilder();
if (aBIMLId == null)
aBIMLId = "Case "+getNextId();
File aDir = null;
if (aStatus == null || aStatus.equals(BICaseInfo.ACTIVATE))
{
aDir = new File(aDirname, "activate");
}
else
{
aDir = new File(aDirname, "inprogress");
}
aTargetFile = new File(aDir, Constants.BIML_FILE_PREFIX + aBIMLId + "_" + aBIMLType + ".xml");
//System.err.println("Saving BIML File:"+aTargetFile.getAbsolutePath());
FileWriter aFW = new FileWriter(aTargetFile);
aFW.write(aBIMLString);
aFW.flush();
aFW.close();
//Document aDocument = builder.parse(aTargetFile);
return aBIMLId;
} catch (FileNotFoundException e) {
SysLog.error("Error writing to the file: " + aTargetFile.getName());
throw new BIMLException("Error writing to the file: " + aTargetFile.getName() + " " +e);
} catch (FactoryConfigurationError e) {
SysLog.error(e);
throw new BIMLException(e);
} catch (ParserConfigurationException e) {
SysLog.error("FactoryConfigurationErrors: ");
throw new BIMLException("FactoryConfigurationErrors: " + e);
} catch (IOException e) {
SysLog.error("Error writing to the file: " + aTargetFile.getName());
throw new BIMLException("Error writing to the file: " + aTargetFile.getName(), e);
}/*
catch (SAXException e) {
log.error("Error parsing the input file.");
throw new BIMLException(e);
}*/
}
/**
* Get the next ID of the BIML repository. It scans all the file names in both
* the "activate" and "inprogress" directory in the BIML repository path to find
* the one with the maximum ID. The use add 1 to this maximum ID as the next ID
* of the BIML file.
*
* TODO: Add synchronization for this method to ensure no multiple ID.
*
* @return String
*/
private String getNextId()
{
String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
long maxId=0;
File aDir= new File(aDirname, "activate");
String [] aFileNames = aDir.list(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
if (name.matches(".*xml"))
return true;
else
return false;
}
}
);
for (int i=0; i<aFileNames.length; i++)
{
if (aFileNames[i].lastIndexOf(".") <= Constants.BIML_FILE_PREFIX.length())
break;
//<<23/03/2005, Frank J. Xu
//Error hard code for demo.
//String aIdStr = aFileNames[i].substring(Constants.BIML_FILE_PREFIX.length()+5, Constants.BIML_FILE_PREFIX.length() + Constants.BIML_ID_PATTERN.length());
String aIdStr = aFileNames[i].substring(Constants.BIML_FILE_PREFIX.length(), Constants.BIML_FILE_PREFIX.length() + Constants.BIML_ID_PATTERN.length());
//23/03/2005, Frank J. Xu>>
try
{
long aId = Long.parseLong(aIdStr);
if (aId > maxId)
maxId = aId;
}
catch(NumberFormatException e)
{
//maxId is not changed if the ID of the file can't be parsed
}
}
aDir = new File(aDirname, "inprogress");
if (aDir.exists())
{
aFileNames = aDir.list(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
if (name.matches(".*xml"))
return true;
else
return false;
}
}
);
for (int i=0; i<aFileNames.length; i++)
{
if (aFileNames[i].lastIndexOf(".") <= Constants.BIML_FILE_PREFIX.length())
break;
//String aIdStr = aFileNames[i].substring(Constants.BIML_FILE_PREFIX.length()+5, Constants.BIML_FILE_PREFIX.length() + Constants.BIML_ID_PATTERN.length());//<<30/03/2005 Mark Li: get correct ID
String aIdStr = aFileNames[i].substring(Constants.BIML_FILE_PREFIX.length(), Constants.BIML_FILE_PREFIX.length() + Constants.BIML_ID_PATTERN.length());
try
{
long aId = Long.parseLong(aIdStr);
if (aId > maxId)
maxId = aId;
}
catch(NumberFormatException e)
{
//maxId is not changed if the ID of the file can't be parsed
}
}
}
DecimalFormat aDF = new DecimalFormat(Constants.BIML_ID_PATTERN);
return aDF.format(maxId+1);
}
/**
*
* In this phase, all the files in the "activate" BIML repository are returned.
*
* TODO: implement the real searching algorithm.
* @see hk.hku.eti.bi.bdm.BIMLDAO#searchBIML(SearchBIMLForm)
*/
// public Collection searchBIML(SearchBIMLForm aSearchBIMLForm) throws BIMLException
// {
// //TODO: Plug in search algorithm to search for similar cases
// ArrayList aBICaseList = new ArrayList();
// HashSet aRetrievedList = new HashSet();
// 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"))
// return true;
// else
// return false;
// }
// }
//
// );
//
// for (int i=0; i<aFiles.length; i++)
// {
//
// String aBIMLId = aFiles[i].getName().substring(Constants.BIML_FILE_PREFIX.length(), Constants.BIML_FILE_PREFIX.length() + Constants.BIML_ID_PATTERN.length());
// if (!aRetrievedList.contains(aBIMLId))
// {
// String aType = null;
// Integer aScore = new Integer(100);
// Integer aStatus = null;
// boolean aHasCML = false;
// boolean aHasMML = false;
// String aBIMLString = null;
// BICaseInfo aBICase = new BICaseInfo(aBIMLId, aType, aScore, aStatus, aHasCML, aHasMML, aBIMLString);
// aRetrievedList.add(aBIMLId);
//
// aBICaseList.add(aBICase);
// }
// }
//
//
// return aBICaseList;
// }
public BICaseInfo createBICaseById(String aBimlId) throws BIMLException
{
String [] aTypes = {"CML", "DML", "MML", "PML"};
String aNextId = getNextId();
System.err.println("Get the next ID"+ aNextId);
String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
File aDir = new File(aDirname, "activate");
for (int i=0; i<aTypes.length; i++)
{
String aSrcBIMLName = Constants.BIML_FILE_PREFIX + aBimlId + "_" + aTypes[i] + ".xml";
String aDstBIMLName = Constants.BIML_FILE_PREFIX + aNextId + "_" + aTypes[i] + ".xml";
File aSrcFile = new File(aDir, aSrcBIMLName);
File aDstFile = new File(aDir, aDstBIMLName);
try
{
Utils.copyFile(aSrcFile, aDstFile);
}catch(IOException e)
{
// do nothing because some component of a BIML may be missing.
}
if (aTypes[i].equals("CML"))
{
try
{
DocumentBuilderFactory aFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder aBuilder = aFactory.newDocumentBuilder();
Document aDocument = aBuilder.parse(aDstFile);
NodeList aNodeList = aDocument.getElementsByTagName("Header");
Node aNode = aNodeList.item(0);
if (aNode != null)
{
NamedNodeMap aAttrMap = aNode.getAttributes();
Node aCrtDateNode = aAttrMap.getNamedItem("CreationDate");
aCrtDateNode.setNodeValue(Utils.formatDate(new Date()));
Node aDocIdNode = aAttrMap.getNamedItem("DocumentID");
aDocIdNode.setNodeValue(Constants.BIML_FILE_PREFIX + aNextId + "_CML");
}
aNodeList = aDocument.getElementsByTagName("BusinessObjective");
aNode = aNodeList.item(0);
if (aNode != null)
{
NamedNodeMap aAttrMap = aNode.getAttributes();
Node aCaseIdNode = aAttrMap.getNamedItem("CaseID");
aCaseIdNode.setNodeValue(aNextId);
}
// Use a Transformer for output
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
FileOutputStream outstream = new FileOutputStream(aDstFile);
StreamResult result = new StreamResult(outstream);
transformer.transform(new DOMSource(aDocument), result);
}
catch (Exception e)
{
throw new BIMLException("Error modifying BIML file:"+aDstFile.getAbsolutePath());
}
}
}
return null;//getCaseById(aNextId, "CML");
}
public void modifyBIMLStatus(String aBimlId, Integer aNewStatus) throws BIMLException
{
String [] aTypes = {"CML", "DML", "MML", "PML"};
String aBimlName = Constants.BIML_FILE_PREFIX + aBimlId + "_CML.xml";
String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
File aSrcDir = new File(aDirname, "activate");
File aDstDir = new File(aDirname, "inprogress");
if (aNewStatus.equals(BICaseInfo.ACTIVATE))
{
aSrcDir = new File(aDirname, "inprogress");
aDstDir = new File(aDirname, "activate");
}
File aBIMLFile = new File(aSrcDir, aBimlName);
if (!aBIMLFile.exists())
{
throw new BIMLException("No BIML file with name: " + aBimlName + " in " + aSrcDir.getAbsolutePath());
}
for (int i=0; i<aTypes.length; i++)
{
aBimlName = Constants.BIML_FILE_PREFIX + aBimlId + "_" + aTypes[i] + ".xml";
File aSrcFile = new File(aSrcDir, aBimlName);
File aDstFile = new File(aDstDir, aBimlName);
try
{
Utils.copyFile(aSrcFile, aDstFile);
aSrcFile.delete();
}catch(IOException e)
{
//throw new BIMLException("Error copying file from " + aSrcFile.getAbsolutePath() + " to " + aDstFile.getAbsolutePath());
//continue if no such file exist because not all types of a BIML exist.
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -