package de.FeatureModellingTool;
import de.FeatureModellingTool.IProjectReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import model.DrawModel;
import model.FileModel;
import research.Drawing;
import component.FComponent;
import de.FeatureModellingTool.Customize.store.CmdlParser;
import de.FeatureModellingTool.Customize.store.CmdlParserPrototype;
import de.FeatureModellingTool.FeatureModel.store.FmdlParserPrototype;
import de.FeatureModellingTool.GraphicalEditor.store.AdaptedStandardStorageFormat;
import de.reuse.GroupMap;
import de.reuse.GroupTreeMap;
/**
*
* @author baipeng {baipeng8608@gmail.com}
* @see de.FeatureModellingTool.ProjectManager.reuse.ProjectReader;
*/
public class ProjectReaderFromDataBase implements IProjectReader {
protected FileModel dataSrc = null;
protected String domainName = null;
protected String[] viewNameArray = null;
FComponent fcomponent = null;
Drawing[] drawingArray = null;
GroupTreeMap informationMap = null;
protected String errorInfo = null;
protected String dataUrl = null;
protected String user = null;
protected String passwd = null;
protected String table = null;
/**
* read from a db-url
*/
public ProjectReaderFromDataBase(){
}
public String getDomainName(){
return domainName;
}
public String[] getViewNameArray(){
return (String[]) viewNameArray.clone();
}
public Drawing[] getDrawingArray(){
return (Drawing[]) drawingArray.clone();
}
public GroupMap getInformationMap(){
return (GroupMap)informationMap.clone();
}
public FComponent getFeatureModel(){
return fcomponent.getInstance(null);
}
public String getErrorInfo() {
return errorInfo;
}
/**
* TODO test here.
* @return
*/
public boolean readProject() {
if(dataSrc == null)
return false;
try {
domainName = dataSrc.getDomainName();
int viewNum = dataSrc.getViewNum();
if (viewNum > 0) {
viewNameArray = dataSrc.getViewNameArray();
} else {
viewNameArray = new String[0];
}
} catch (Exception exception) {
errorInfo = "Exception occurs when read project index file.\n" + exception;
return false;
}
//end ��ȡ��Ŀ�����ļ�
FmdlParserPrototype parser = new FmdlParserPrototype();
String fmodel = dataSrc.getFeatureModel();
// fmodel = fmodel.replaceAll("[\\\\u0x00]","");
try{
fmodel = new String(fmodel.getBytes(),"gbk");
}catch(Exception e){
}
//begin ��ȡ����ģ�������ļ�
try {
// InputStream inputStream = new StringBufferInputStream(fmodel);
InputStream inputStream = new ByteArrayInputStream(fmodel.getBytes());
fcomponent = parser.openFmdl(inputStream);
} catch (Exception exception) {
errorInfo = "Exception occurs when read feature-model file.\n" + exception;
return false;
}
//end ��ȡ����ģ�������ļ�
//begin ��ȡԼ��ģ�������ļ�
try {
String cm = dataSrc.getConstraintModel();
InputStream input = new ByteArrayInputStream(cm.getBytes());
parser.openCmdl(input, fcomponent);
} catch (Exception exception) {
errorInfo = "Exception occurs when read constraint-model file.\n" + exception;
return false;
}
//end ��ȡԼ��ģ�������ļ�
//Question: *.im �洢ʲô��Ϣ��im means Interaction-Model
try{
String im = dataSrc.getInteractionModel();
InputStream input =new ByteArrayInputStream(im.getBytes());
parser.openImdl(input, fcomponent);
} catch (Exception exception) {
errorInfo = "Exception occurs when read interaction-model file.\n" + exception;
return false;
}
AdaptedStandardStorageFormat reader = new AdaptedStandardStorageFormat();
//�������viewNameArray.length��draw�ļ�.
if (viewNameArray.length > 0) {
//begin ��ȡͼԪ��Ϣ�ļ�
try {
drawingArray = new Drawing[viewNameArray.length];
for (int i = 0; i < viewNameArray.length; i++) {
DrawModel draw = dataSrc.getDrawModel(viewNameArray[i]);
InputStream input = new ByteArrayInputStream(draw.getContent().getBytes());
// InputStream input = new StringBufferInputStream(draw.getContent());
drawingArray[i] = reader.restore(input);
drawingArray[i].setId(draw.getId());
}
} catch (Exception exception) {
errorInfo = "Exception occurs when read project graphic file.\n" + exception;
exception.printStackTrace();
return false;
}
//end ��ȡͼԪ��Ϣ�ļ�
} else {
drawingArray = new Drawing[0];
}
//begin ��ȡ������Ϣ
informationMap = new GroupTreeMap();
//����൱�����ݱ������������ TODO How to apply in the database environment?
// Enumeration enumeration = zipFile.entries();
//
// while (enumeration.hasMoreElements()) {
// ZipEntry entry = (ZipEntry) enumeration.nextElement();
//
// String entryName = entry.getName();
//
// if (entryName.endsWith(".draw")) {
// int index = entryName.indexOf('\\');
//
// if (index > 0) {
// try {
// String id = entryName.substring(0, index);
// InputStream inputStream = zipFile.getInputStream(entry);
// Drawing drawing = reader.restore(inputStream);
// informationMap.add(id, drawing);
// } catch (Exception exception) {
// errorInfo = "Exception occurs when read information.\n" + exception;
// exception.printStackTrace();
// return false;
// }
// }
// }
// }
//end ��ȡ������Ϣ
//begin ��ȡ������Ϣ
//gh start
CmdlParser cp = new CmdlParserPrototype();
try {
String custom = dataSrc.getCustomizationIndex();
InputStream inputStream = new StringBufferInputStream(custom);
for (Iterator<String> itCVId = cp.openCustomizationVersionIds(
inputStream).iterator(); itCVId.hasNext();) {
String cvId = itCVId.next();
String cusVersionFileContent = dataSrc.getCustomizationVersionFileContent(cvId);
InputStream isCV = new ByteArrayInputStream(cusVersionFileContent.getBytes());
cp.openCustomizationVersion(isCV , fcomponent);
}
} catch (Exception exception) {
// errorInfo = "Exception occurs when read customization file.\n"
// + exception;
// return false;
}
//gh end
// end ��ȡ������Ϣ
errorInfo = null;
return true;
}
public void setDataSource(Object src) {
if(src instanceof FileModel){
dataSrc = (FileModel)src;
}
}
}