Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
3DSystemNavigationBackend/src/main/java/com/lmco/spectrum/systemnavigation3d/geia/XMLGeiaDataSource.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
170 lines (144 sloc)
5.83 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lmco.spectrum.systemnavigation3d.geia; | |
import com.lmco.spectrum.systemnavigation3d.domain.dto.*; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Unmarshaller; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class XMLGeiaDataSource implements GeiaDataSource { | |
private GeiaStdModel stdModel; | |
private Map<Class<? extends GeiaEntity>, List<GeiaEntity>> geiaEntityMap; | |
private List<XA> xaList; | |
private List<XH> xhList; | |
public XMLGeiaDataSource(InputStream xmlStream) { | |
this.xaList = new ArrayList<>(); | |
this.xhList = new ArrayList<>(); | |
this.geiaEntityMap = new HashMap<>(); | |
try { | |
JAXBContext jaxbContext = JAXBContext.newInstance(GeiaStdModel.class); | |
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); | |
stdModel = (GeiaStdModel) unmarshaller.unmarshal(xmlStream); | |
this.processGeiaStd(); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
private <T extends GeiaEntity> List<T> getEntities(Class<T> clazz) { | |
return (List<T>) geiaEntityMap.get(clazz); | |
} | |
private void processGeiaStd() { | |
for(GeiaEntity entity : stdModel.getGeiaFullFile()) { | |
if(!geiaEntityMap.containsKey(entity.getClass())) { | |
geiaEntityMap.put(entity.getClass(), new ArrayList<>()); | |
} | |
geiaEntityMap.get(entity.getClass()).add(entity); | |
} | |
xaList.addAll(getEntities(XA.class)); | |
xhList.addAll(getEntities(XH.class)); | |
for(XA xa : xaList) { | |
pairXAWithChildren(xa); | |
} | |
} | |
private void pairXAWithChildren(XA src) { | |
getEntities(XB.class) | |
.stream() | |
.filter(xb -> xb.getLcnIndentureCode().charAt(0) == 'A' | |
&& src.getEndItemAcronymCode().equals(xb.getEndItemAcronymCode())) | |
.forEach(xb -> { | |
pairXBWithChildren(xb); | |
src.getXbChildren().add(xb); | |
}); | |
} | |
private void pairXBWithChildren(XB src) { | |
getEntities(XB.class) | |
.stream() | |
.filter(xb -> xb.getLcn().startsWith(src.getLcn()) | |
&& xb.getLcnIndentureCode().charAt(0)-1 == src.getLcnIndentureCode().charAt(0) | |
&& xb.getEndItemAcronymCode().equals(src.getEndItemAcronymCode())) | |
.forEach(xb -> { | |
pairXBWithChildren(xb); | |
src.getXbChildren().add(xb); | |
}); | |
getEntities(CA.class) | |
.stream() | |
.filter(ca -> src.getLcn().equals(ca.getLcn()) | |
&& src.getEndItemAcronymCode().equals(ca.getEndItemAcronymCode())) | |
.forEach(ca -> { | |
pairCAWithChildren(ca); | |
src.getCaChildren().add(ca); | |
}); | |
} | |
private void pairCAWithChildren(CA src){ | |
getEntities(CI.class) | |
.stream() | |
.filter(ci -> src.getLcn().equals(ci.getLcn()) | |
&& src.getEndItemAcronymCode().equals(ci.getEndItemAcronymCode())) | |
.forEach(ci -> { | |
pairCIWithHGChild(ci); | |
src.getCiChildren().add(ci); | |
}); | |
} | |
private void pairCIWithHGChild(CI src){ | |
getEntities(HG.class) | |
.stream() | |
.filter(hg -> src.getLcn().equals(hg.getLcn()) | |
&& src.getTaskProvisionCageCode().equals(hg.getCageCode()) | |
&& src.getTaskProvisionReferenceNumber().equals(hg.getReferenceNumber()) | |
&& src.getEndItemAcronymCode().equals(hg.getEndItemAcronymCode())) | |
.findFirst() | |
.ifPresent(hg -> { | |
pairHGWithHAParent(hg); | |
src.setHgChild(hg); | |
}); | |
} | |
private void pairHGWithHAParent(HG src) { | |
getEntities(HA.class) | |
.stream() | |
.filter(ha -> ha.getCageCode().equals(src.getCageCode()) | |
&& ha.getReferenceNumber().equals(src.getReferenceNumber())) | |
.findFirst() | |
.ifPresent(ha -> { | |
pairHAWithXHParent(ha); | |
src.setHaParent(ha); | |
}); | |
} | |
private void pairHAWithXHParent(HA src){ | |
getEntities(XH.class) | |
.stream() | |
.filter(xh -> xh.getCageCode().equals(src.getCageCode())) | |
.findFirst() | |
.ifPresent(src::setXhParent); | |
} | |
@Override | |
public List<XA> getXAs() { | |
return xaList; | |
} | |
@Override | |
public XA getXA(String endItemAcronymCode) { | |
return xaList.stream().filter(xa -> xa.getEndItemAcronymCode().equals(endItemAcronymCode)).findFirst().orElse(null); | |
} | |
@Override | |
public XH getXH(String cageCode){ | |
return xhList.stream().filter(xh -> xh.getCageCode().equals(cageCode)).findFirst().orElse(null); | |
} | |
@Override | |
public XB getXB(XA src, String lcn) { | |
return src.getXbChildren().stream().filter(xb -> xb.getLcn().equals(lcn)).findFirst().orElse(null); | |
} | |
@Override | |
public CA getCA(XB src, String taskCode) { | |
return src.getCaChildren().stream().filter(ca -> ca.getTaskCode().equals(taskCode)).findFirst().orElse(null); | |
} | |
@Override | |
public CI getCI(CA src, String cageCode, String taskProvisionReferenceNumber){ | |
return src.getCiChildren() | |
.stream() | |
.filter(ci -> ci.getTaskProvisionCageCode().equals(cageCode) | |
&& ci.getTaskProvisionReferenceNumber().equals(taskProvisionReferenceNumber)) | |
.findFirst().orElse(null); | |
} | |
} |