/*
* Класс - фабрика нодов, новые ноды регестрировать тут, метод <b>createNode()</b>
*/
package clips.doctor.newEMC.init;
import cli_fmw.main.ClipsException;
import clips.doctor.newEMC.nodes.FollowupNode;
import clips.doctor.newEMC.nodes.DiseaseNode;
import cli_fmw.main.PageGeneric;
import cli_fmw.utils.Log;
import clips.doctor.newEMC.init.generic.DelegateNode;
import clips.delegate.doctor.DiseaseLocal;
import clips.delegate.doctor.EmcLocal;
import clips.delegate.doctor.RecommendationLocal;
import clips.delegate.doctor.certificate.CertificateLocal;
import clips.delegate.doctor.checkup.CheckupDICOMLocal;
import clips.delegate.doctor.checkup.CheckupLocal;
import clips.delegate.doctor.contraindication.ContraindicationLocal;
import clips.delegate.doctor.diagnosis.DiagnosisLocal;
import clips.delegate.doctor.direction.DirectionLocal;
import clips.delegate.doctor.followup.FollowupLocal;
import clips.delegate.doctor.prescription.PrescriptionLocal;
import clips.delegate.doctor.prescriptiondlo.PrescriptionDloLocal;
import clips.delegate.doctor.sicklist.SicklistLocal;
import clips.delegate.service.SerRenLocal;
import clips.doctor.newEMC.init.generic.DelegateNodeInterface;
import clips.doctor.newEMC.nodes.collectors.CertificateCollectorNode;
import clips.doctor.newEMC.nodes.CheckupNode;
import clips.doctor.newEMC.nodes.leaves.ContraindicationNode;
import clips.doctor.newEMC.nodes.leaves.DiagnosisNode;
import clips.doctor.newEMC.nodes.leaves.DirectionNode;
import clips.doctor.newEMC.nodes.EmcNode;
import clips.doctor.newEMC.nodes.leaves.PrescriptionNode;
import clips.doctor.newEMC.nodes.leaves.RecommendationNode;
import clips.doctor.newEMC.nodes.SerrenNode;
import clips.doctor.newEMC.nodes.collectors.SicklistCollectorNode;
import clips.doctor.newEMC.nodes.leaves.CertificateNode;
import clips.doctor.newEMC.nodes.leaves.DicomNode;
import clips.doctor.newEMC.nodes.leaves.PrescriptionDloNode;
import clips.doctor.newEMC.nodes.leaves.SicklistNode;
import clips.doctor.newEMC.nodes.leaves.lists.AnalyseListNode;
import clips.doctor.newEMC.nodes.leaves.lists.CheckupListNode;
import clips.doctor.newEMC.nodes.leaves.lists.ContraindicationListNode;
import clips.doctor.newEMC.nodes.leaves.lists.DiagnosisListNode;
import clips.doctor.newEMC.nodes.leaves.lists.DirectionListNode;
import clips.doctor.newEMC.nodes.leaves.lists.InvalidityListNode;
import clips.doctor.newEMC.nodes.leaves.lists.PrescriptionDloListNode;
import clips.doctor.newEMC.nodes.leaves.lists.PrescriptionListNode;
import clips.doctor.newEMC.nodes.leaves.lists.RecommendationListNode;
import clips.doctor.newEMC.nodes.leaves.lists.SerrenListNode;
import clips.doctor.newEMC.wrappers.collectors.CertificateCollector;
import clips.doctor.newEMC.wrappers.collectors.SicklistCollector;
import clips.doctor.newEMC.wrappers.lists.AnalyseList;
import clips.doctor.newEMC.wrappers.lists.CheckupList;
import clips.doctor.newEMC.wrappers.lists.ContraindicationList;
import clips.doctor.newEMC.wrappers.lists.DiagnosisList;
import clips.doctor.newEMC.wrappers.lists.DirectionList;
import clips.doctor.newEMC.wrappers.lists.InvalidityList;
import clips.doctor.newEMC.wrappers.lists.PrescriptionDloList;
import clips.doctor.newEMC.wrappers.lists.PrescriptionList;
import clips.doctor.newEMC.wrappers.lists.RecommendationList;
import clips.doctor.newEMC.wrappers.lists.SerrenList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
/**
* Класс - фабрика нодов, новые ноды регестрировать тут, метод <b>createNode()</b>
* @author petr
*/
public class NodeKeeper {
private Map<Class<? extends DelegateNodeInterface>, HashSet<EmcTreeNode>> map;
private EmcTreeNode current;
public NodeKeeper(EmcTreeNode current) {
map = new HashMap<Class<? extends DelegateNodeInterface>, HashSet<EmcTreeNode>>();
this.current = current;
}
public <T extends DelegateNodeInterface> EmcTreeNode<T, ?> getNode(T dl){
EmcTreeNode node = findInMap(dl);
if (node == null){
node = createNode(dl);
addNode(node);
}
return node;
}
private EmcTreeNode findInMap(DelegateNodeInterface dl){
HashSet<EmcTreeNode> subMap = map.get(dl.getClass());
if (subMap != null){
for (EmcTreeNode emcTreeNode : subMap) {
if (emcTreeNode.getDelegate().getID() == dl.getID()){
return emcTreeNode;
}
}
}
return null;
}
private void addNode(EmcTreeNode node){
Class<? extends DelegateNodeInterface> aClass = node.getDelegate().getClass();
HashSet<EmcTreeNode> subMap = map.get(aClass);
if (subMap == null){
subMap = new HashSet<EmcTreeNode>();
map.put(aClass, subMap);
}
subMap.add(node);
}
public <T extends DelegateNodeInterface> void deleteNode(T dl){
HashSet<EmcTreeNode> subMap = map.get(dl.getClass());
if (subMap != null){
subMap.remove(findInMap(dl));
}
}
/**
* Create new node by delegate or wrapper class
* @param <T>
* @param <P>
* @param delegate
* @return
*/
private <T extends DelegateNodeInterface, P extends PageGeneric> EmcTreeNode<T, P> createNode(T delegate){
EmcTreeNode newNode = null;
if (delegate instanceof DiseaseLocal){
newNode = new DiseaseNode(current, (DiseaseLocal) delegate);
} else if (delegate instanceof FollowupLocal){
newNode = new FollowupNode(current,(FollowupLocal) delegate);
}else if (delegate instanceof SerRenLocal){
newNode = new SerrenNode(current,(SerRenLocal) delegate);
}else if (delegate instanceof DiagnosisLocal){
newNode = new DiagnosisNode(current, (DiagnosisLocal)delegate);
}else if (delegate instanceof CheckupLocal){
newNode = new CheckupNode(current,(CheckupLocal) delegate);
}else if (delegate instanceof ContraindicationLocal){
newNode = new ContraindicationNode(current,(ContraindicationLocal) delegate);
}else if (delegate instanceof DirectionLocal){
newNode = new DirectionNode(current,(DirectionLocal) delegate);
}else if (delegate instanceof PrescriptionLocal){
newNode = new PrescriptionNode(current,(PrescriptionLocal) delegate);
}else if (delegate instanceof PrescriptionDloLocal){
newNode = new PrescriptionDloNode(current,(PrescriptionDloLocal) delegate);
}else if (delegate instanceof RecommendationLocal){
newNode = new RecommendationNode(current,(RecommendationLocal) delegate);
}else if (delegate instanceof EmcLocal){
newNode = new EmcNode(current,(EmcLocal) delegate);
}else if (delegate instanceof CertificateCollector){
newNode = new CertificateCollectorNode(current,(CertificateCollector) delegate);
}else if (delegate instanceof CertificateLocal){
newNode = new CertificateNode(current,(CertificateLocal) delegate);
}else if (delegate instanceof SicklistCollector){
newNode = new SicklistCollectorNode(current,(SicklistCollector) delegate);
}else if (delegate instanceof SicklistLocal){
newNode = new SicklistNode(current,(SicklistLocal) delegate);
}else if (delegate instanceof ContraindicationList){
newNode = new ContraindicationListNode(current,(ContraindicationList) delegate);
}else if (delegate instanceof DirectionList){
newNode = new DirectionListNode(current,(DirectionList) delegate);
}else if (delegate instanceof CheckupList){
newNode = new CheckupListNode(current,(CheckupList) delegate);
}else if (delegate instanceof AnalyseList){
newNode = new AnalyseListNode(current,(AnalyseList) delegate);
}else if (delegate instanceof RecommendationList){
newNode = new RecommendationListNode(current,(RecommendationList) delegate);
}else if (delegate instanceof SerrenList){
newNode = new SerrenListNode(current,(SerrenList) delegate);
}else if (delegate instanceof DiagnosisList){
newNode = new DiagnosisListNode(current,(DiagnosisList) delegate);
}else if (delegate instanceof InvalidityList){
newNode = new InvalidityListNode(current,(InvalidityList) delegate);
}else if (delegate instanceof PrescriptionList){
newNode = new PrescriptionListNode(current,(PrescriptionList) delegate);
}else if (delegate instanceof PrescriptionDloList){
newNode = new PrescriptionDloListNode(current,(PrescriptionDloList) delegate);
}else if (delegate instanceof CheckupDICOMLocal){
newNode = new DicomNode(current,(CheckupDICOMLocal) delegate);
}else{
Log.printlnAnsPos("NEED NODE FOR " + delegate + " (class: " + delegate.getClass() + ")");
}
return newNode;
}
void reset(){
try {
ArrayList<DelegateNodeInterface> list = new ArrayList<DelegateNodeInterface>();
for (HashSet<EmcTreeNode> submap : map.values()) {
for (EmcTreeNode node : submap) {
list.add(node.getDelegate());
}
}
List<DelegateNode> children = current.getChildren(current.getDelegate());
list.removeAll(children);
for (DelegateNodeInterface dl : list) {
deleteNode(dl);
}
for (DelegateNode dl : children) {
EmcTreeNode node = findInMap(dl);
if (node == null){
addNode(createNode(dl));
}else if (node != null && !node.getDelegate().equals(dl)){
deleteNode(dl);
}
}
} catch (ClipsException ex) {
ex.printStackTrace();
}
}
}