package de.FeatureModellingTool.PatternBase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jdom.JDOMException;
import component.FComponent;
import de.FeatureModellingTool.PatternBase.store.PatternBaseStore;
import de.FeatureModellingTool.PatternBase.store.PatternBaseStoreImplementation;
/**
* author:chenting
* date:2007-12-3
* time:����10:11:17
*/
public class PatternBaseImplementation implements PatternBase {
protected String name = null;
protected String baseType;
protected PatternBase tempPatternBase;
protected final static String PROJECT = "project";
protected final static String NONPROJECT = "nonProject";
protected Map<String, Pattern> allLoadedPatterns;
protected Map<String, Templete> allLoadedTempletes;
protected Map<String, Pattern> allUserDefPatterns;
protected Map<String, Templete> allUserDefTempletes;
public PatternBaseImplementation() {
name = "PatternBase";
baseType = PROJECT;
allLoadedPatterns = new HashMap<String, Pattern>();
allLoadedTempletes = new HashMap<String, Templete>();
allUserDefPatterns = new HashMap<String, Pattern>();
allUserDefTempletes = new HashMap<String, Templete>();
}
public String getName() {
return name;
}
;
public void setName(String newName) {
name = newName;
}
;
public List<Pattern> getRootLoadedPatterns() {
return getRootPatterns(allLoadedPatterns);
}
;
public List<Pattern> getRootUserDefPatterns() {
return getRootPatterns(allUserDefPatterns);
}
;
public Map<String, Pattern> getAllPatterns() {
if (baseType.equals(PROJECT)) {
return allUserDefPatterns;
} else {
return allLoadedPatterns;
}
}
;
public Map<String, Templete> getAllTempletes() {
if (baseType.equals(PROJECT)) {
return allUserDefTempletes;
} else {
return allLoadedTempletes;
}
}
;
public Pattern getPattern(String patternName) {
if (patternName == null) {
return null;
} else {
if (allLoadedPatterns.containsKey(patternName)) {
return allLoadedPatterns.get(patternName);
}
return (Pattern) allUserDefPatterns.get(patternName);
}
}
public Templete getTemplete(String templeteName) {
if (templeteName == null) {
return null;
} else {
if (allLoadedTempletes.containsKey(templeteName)) {
return allLoadedTempletes.get(templeteName);
}
return (Templete) allUserDefTempletes.get(templeteName);
}
}
public Pattern addPattern(String patternName) {
if (patternName == null) {
return null;
}
Pattern newPattern = new PatternImplementation(patternName);
addPattern(newPattern);
return newPattern;
}
public void addPattern(Pattern pattern) {
if (baseType.equals(PROJECT)) {
allUserDefPatterns.put(pattern.getName(), pattern);
} else {
allLoadedPatterns.put(pattern.getName(), pattern);
}
}
public Templete addTemplete(String templeteName) {
if (templeteName == null) {
return null;
}
Templete newTemplete = new TempleteImplementation(templeteName);
addTemplete(newTemplete);
return newTemplete;
}
public void addTemplete(Templete templete) {
if (baseType.equals(PROJECT)) {
allUserDefTempletes.put(templete.getName(), templete);
} else {
allLoadedTempletes.put(templete.getName(), templete);
}
}
public boolean containsPattern(String patternName) {
if (patternName == null) {
return false;
}
if (allLoadedPatterns.containsKey(patternName) || allUserDefPatterns.containsKey(patternName)) {
return true;
}
return false;
}
public boolean containsPattern(Pattern pattern) {
return containsPattern(pattern.getName());
}
public boolean containsTemplete(String templeteName) {
if (templeteName == null) {
return false;
}
if (allLoadedTempletes.containsKey(templeteName) || allUserDefTempletes.containsKey(templeteName)) {
return true;
}
return false;
}
public boolean containsTemplete(Templete templete) {
return containsTemplete(templete.getName());
}
public void removePattern(Pattern pattern) {
if (pattern == null) {
return;
}
List childrenPatterns = pattern.getChildrenPatterns();
List childrenTempletes = pattern.getChildrenTempletes();
Iterator iteratorCP = childrenPatterns.iterator();
Iterator iteratorCT = childrenTempletes.iterator();
if (baseType.equals(PROJECT)) {
allUserDefPatterns.remove(pattern.getName());
} else {
allLoadedPatterns.remove(pattern.getName());
}
while (iteratorCP.hasNext()) {
Pattern childPattern = (Pattern) iteratorCP.next();
removePattern(childPattern);
}
while (iteratorCT.hasNext()) {
Templete childTemplete = (Templete) iteratorCP.next();
removeTemplete(childTemplete);
}
}
public void removePattern(String patternName) {
removePattern(getPattern(patternName));
}
public void removeTemplete(Templete templete) {
if (templete == null) {
return;
}
List childrenTempletes = templete.getChildrenTempletes();
Iterator iteratorCT = childrenTempletes.iterator();
if (baseType.equals(PROJECT)) {
allUserDefTempletes.remove(templete.getName());
} else {
allLoadedTempletes.remove(templete.getName());
}
while (iteratorCT.hasNext()) {
Templete childTemplete = (Templete) iteratorCT.next();
removeTemplete(childTemplete);
}
}
public void removeTemplete(String templeteName) {
removeTemplete(getTemplete(templeteName));
}
public PatternBase load() {
PatternBaseStore patternParser = new PatternBaseStoreImplementation();
// FileInputStream fileInputStream;
if (getClass().getResource("DefaultPattern/patternBase") != null) {
try {
InputStream is = getClass().getResourceAsStream("DefaultPattern/patternBase");
tempPatternBase = patternParser.openPl(is, NONPROJECT);
is.close();
Map<String, Templete> templetes = tempPatternBase.getAllTempletes();
Iterator templeteIterator = templetes.values().iterator();
while (templeteIterator.hasNext()) {
Templete templete = (Templete) templeteIterator.next();
//�������ģʽ����ʾ�洢�˶�Ӧ������ģ����Ϣ,���FComponent
if (templete.getParentType().equals("pattern")) {
// File fmFile = new File(getClass().getResource("DefaultPattern" + "/" + templete.getName() + "/" + templete.getName() + ".fm").toURI());
is = getClass().getResourceAsStream("DefaultPattern" + "/" + templete.getName() + "/" + templete.getName() + ".fm");
if (is != null) {
templete.setFComponent(patternParser.openFm(is));
is.close();
}
// ��ȡԼ��ģʽ��Ϣ
is =getClass().getResourceAsStream("DefaultPattern" + "/" + templete.getName() + "/" + templete.getName() + ".cm");
if (is != null) {
patternParser.openCm(is, templete);
is.close();
}
} else {
FComponent parentFComponent = ((Templete) templete.getParent()).getFComponent();
templete.setFComponent(parentFComponent);
}
//��ȡ������Ϣ
is = getClass().getResourceAsStream("DefaultPattern" + "/" + templete.getName() + "/" + templete.getName() + ".pa");
if (is != null) {
patternParser.openPa(is, templete);
is.close();
}
}
tempPatternBase.setProject();
return tempPatternBase;
}catch(Exception e){
System.err.println("PatternBaseImplementation#load()");
e.printStackTrace();
}
}
return null;
}
public void save() {
String tempBaseType = this.getBaseType();
PatternBaseStore patternParser = new PatternBaseStoreImplementation();
FileOutputStream fileOutputStream;
this.setNonProject();
try {
File file;
if (getClass().getResource("DefaultPattern/patternBase") == null) {
File defDirec = new File(getClass().getResource("").getPath() + "/DefaultPattern");
defDirec.mkdir();
file = new File(defDirec.getPath() + "/patternBase");
file.createNewFile();
} else {
file = new File(getClass().getResource("DefaultPattern/patternBase").toURI());
}
fileOutputStream = new FileOutputStream(file);
patternParser.savePl(fileOutputStream, this);
fileOutputStream.close();
Map<String, Templete> templetes = this.getAllTempletes();
Iterator templeteIterator = templetes.values().iterator();
while (templeteIterator.hasNext()) {
Templete templete = (Templete) templeteIterator.next();
File temDirec;
if (getClass().getResource("DefaultPattern/" + templete.getName()) == null) {
temDirec = new File(getClass().getResource("DefaultPattern").getPath() + "/" + templete.getName());
temDirec.mkdir();
} else {
temDirec = new File(getClass().getResource("DefaultPattern/" + templete.getName()).getPath());
}
//���ģ��ĸ�Ϊģʽ������Ҫд������ģ�ͺ�Լ��ģ������
if (templete.getParentType().equals("pattern")) {
File fmFile;
if (getClass().getResource("DefaultPattern/" + templete.getName() + "/" + templete.getName() + ".fm") == null) {
fmFile = new File(getClass().getResource("DefaultPattern/" + templete.getName()).getPath() + "/" + templete.getName() + ".fm");
fmFile.createNewFile();
} else {
fmFile = new File(getClass().getResource("DefaultPattern" + "/" + templete.getName() + "/" + templete.getName() + ".fm").toURI());
}
fileOutputStream = new FileOutputStream(fmFile);
patternParser.saveFm(fileOutputStream, templete);
fileOutputStream.close();
File cmFile;
if (getClass().getResource("DefaultPattern" + "/" + templete.getName() + "/" + templete.getName() + ".cm") == null) {
cmFile = new File(getClass().getResource("DefaultPattern/" + templete.getName()).getPath() + "/" + templete.getName() + ".cm");
cmFile.createNewFile();
} else {
cmFile = new File(getClass().getResource("DefaultPattern" + "/" + templete.getName() + "/" + templete.getName() + ".cm").toURI());
}
fileOutputStream = new FileOutputStream(cmFile);
patternParser.saveCm(fileOutputStream, templete);
fileOutputStream.close();
}
File paFile;
if (getClass().getResource("DefaultPattern" + "/" + templete.getName() + "/" + templete.getName() + ".pa") == null) {
paFile = new File(getClass().getResource("DefaultPattern/" + templete.getName()).getPath() + "/" + templete.getName() + ".pa");
paFile.createNewFile();
} else {
paFile = new File(getClass().getResource("DefaultPattern" + "/" + templete.getName() + "/" + templete.getName() + ".pa").toURI());
}
fileOutputStream = new FileOutputStream(paFile);
patternParser.savePa(fileOutputStream, templete);
fileOutputStream.close();
}
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.setBaseType(tempBaseType);
}
public String getBaseType() {
return baseType;
}
public void setBaseType(String newBaseType) {
baseType = newBaseType;
}
public void setProject() {
baseType = PROJECT;
}
public void setNonProject() {
baseType = NONPROJECT;
}
protected List<Pattern> getRootPatterns(Map patterns) {
List<Pattern> rootPatterns = new ArrayList<Pattern>();
Iterator iterator = patterns.values().iterator();
while (iterator.hasNext()) {
Pattern pattern = (Pattern) iterator.next();
if (pattern.getParentPattern() == null) {
rootPatterns.add(pattern);
}
}
return rootPatterns;
}
}