ArrayList<EntitySpecPojo> newEntityEntries = null;
if (null != s.getEntities()) {
Iterator<EntitySpecPojo> entSpecIt = s.getEntities().iterator();
while (entSpecIt.hasNext()) {
EntitySpecPojo entS = entSpecIt.next();
if ((null != entS.getIterateOver()) && (entS.getIterateOver().contains(".")))
{
// For associations only: included here so it doesn't get forgotten in cut-and-pastes...
//if (assocS.getIterateOver().contains(",") || assocS.getIterateOver().contains("/")) {
// continue;
//}
if (null == nestedEntityMap) { // (do need this map)
nestedEntityMap = new HashMap<String, EntitySpecPojo>();
}
if (null == newEntityEntries) {
newEntityEntries = new ArrayList<EntitySpecPojo>(10);
}
EntitySpecPojo prevLevelSpec = null;
String iterateOver = entS.getIterateOver() + "."; // (end with "." to make life easier)
entS.setIterateOver(null); // (this is now the end of the chain)
entSpecIt.remove(); // (so remove from the list)
boolean bChainBroken = false;
for (int nCurrDot = iterateOver.indexOf('.'), nLastDot = -1;
nCurrDot >= 0;
nLastDot = nCurrDot, nCurrDot = iterateOver.indexOf('.', nCurrDot + 1))
{
String currLevel = iterateOver.substring(0, nCurrDot); // (eg a, a.b, a.b.c)
String lastComp_currLevel = iterateOver.substring(nLastDot + 1, nCurrDot); // (eg a, b, c)
EntitySpecPojo currLevelSpec = null;
if (!bChainBroken) {
currLevelSpec = nestedEntityMap.get(currLevel);
}
if (null == currLevelSpec) {
bChainBroken = true; // (no point in doing any more lookups)
currLevelSpec = new EntitySpecPojo();
nestedEntityMap.put(currLevel, currLevelSpec);
currLevelSpec.setIterateOver(lastComp_currLevel);
if (null != prevLevelSpec) { // add myself to the next level
if (null == prevLevelSpec.getEntities()) {
prevLevelSpec.setEntities(new ArrayList<EntitySpecPojo>(5));
}
prevLevelSpec.getEntities().add(currLevelSpec);
}
else { // I am the first level, add myself to entity list
newEntityEntries.add(currLevelSpec); // (this is now the head of the chain)
}
prevLevelSpec = currLevelSpec;
}//TESTED
else { // We're already have this level, so carry on:
prevLevelSpec = currLevelSpec; //(in case this was the last level...)
continue;
}//TESTED
} //(end loop over expansion levels)
// Add entS (ie the spec with the content) to the end of the chain
if (null != prevLevelSpec) { // (probably an internal logic error if not)
if (null == prevLevelSpec.getEntities()) {
prevLevelSpec.setEntities(new ArrayList<EntitySpecPojo>(5));
}
prevLevelSpec.getEntities().add(entS);
}//TESTED
}//(end found entity with expandable iterateOver)
else if (null != entS.getIterateOver()) { // Non-nested case, simpler
// For associations only: included here so it doesn't get forgotten in cut-and-pastes...
//if (assocS.getIterateOver().contains(",") || assocS.getIterateOver().contains("/")) {
// continue;
//}
if (null == nestedEntityMap) { // (do need this map)
nestedEntityMap = new HashMap<String, EntitySpecPojo>();
}
//(and logic is different enough that it makes most sense to do separately rather than grovel to save a few lines)
EntitySpecPojo currSpec = nestedEntityMap.get(entS.getIterateOver());
if (null != currSpec) {
entSpecIt.remove();
if (null == currSpec.getEntities()) {
currSpec.setEntities(new ArrayList<EntitySpecPojo>(5));
}
entS.setIterateOver(null);
currSpec.getEntities().add(entS);
}
else {
nestedEntityMap.put(entS.getIterateOver(), entS);
}
}//TESTED
}// (end loop over entities)
if (null != newEntityEntries) {
s.getEntities().addAll(newEntityEntries);
}
}//(end if entities)
// Identical code for associations:
// Just going to cut and replace and rename a few variables
//HashMap<String, AssociationSpecPojo> nestedAssociationMap = null;
HashMap<String, AssociationSpecPojo> nestedAssocMap = null;
ArrayList<AssociationSpecPojo> newAssocEntries = null;
if (null != s.getAssociations()) {
Iterator<AssociationSpecPojo> assocSpecIt = s.getAssociations().iterator();
while (assocSpecIt.hasNext()) {
AssociationSpecPojo assocS = assocSpecIt.next();
if ((null != assocS.getIterateOver()) && (assocS.getIterateOver().contains(".")))
{
// For associations only: included here so it doesn't get forgotten in cut-and-pastes...
if (assocS.getIterateOver().contains(",") || assocS.getIterateOver().contains("/")) {
continue;
}//TESTED
if (null == nestedAssocMap) { // (do need this map)
nestedAssocMap = new HashMap<String, AssociationSpecPojo>();
}
if (null == newAssocEntries) {
newAssocEntries = new ArrayList<AssociationSpecPojo>(10);
}
AssociationSpecPojo prevLevelSpec = null;
String iterateOver = assocS.getIterateOver() + "."; // (end with "." to make life easier)
assocS.setIterateOver(null); // (this is now the end of the chain)
assocSpecIt.remove(); // (so remove from the list)
boolean bChainBroken = false;
for (int nCurrDot = iterateOver.indexOf('.'), nLastDot = -1;
nCurrDot >= 0;
nLastDot = nCurrDot, nCurrDot = iterateOver.indexOf('.', nCurrDot + 1))
{
String currLevel = iterateOver.substring(0, nCurrDot); // (eg a, a.b, a.b.c)
String lastComp_currLevel = iterateOver.substring(nLastDot + 1, nCurrDot); // (eg a, b, c)
AssociationSpecPojo currLevelSpec = null;
if (!bChainBroken) {
currLevelSpec = nestedAssocMap.get(currLevel);
}
if (null == currLevelSpec) {
bChainBroken = true; // (no point in doing any more lookups)
currLevelSpec = new AssociationSpecPojo();
nestedAssocMap.put(currLevel, currLevelSpec);
currLevelSpec.setIterateOver(lastComp_currLevel);
if (null != prevLevelSpec) { // add myself to the next level
if (null == prevLevelSpec.getAssociations()) {
prevLevelSpec.setAssociations(new ArrayList<AssociationSpecPojo>(5));
}
prevLevelSpec.getAssociations().add(currLevelSpec);
}
else { // I am the first level, add myself to entity list
newAssocEntries.add(currLevelSpec); // (this is now the head of the chain)
}
prevLevelSpec = currLevelSpec;
}//TESTED
else { // We're already have this level, so carry on:
prevLevelSpec = currLevelSpec; //(in case this was the last level...)
continue;
}//TESTED
} //(end loop over expansion levels)
// Add entS (ie the spec with the content) to the end of the chain
if (null != prevLevelSpec) { // (probably an internal logic error if not)
if (null == prevLevelSpec.getAssociations()) {
prevLevelSpec.setAssociations(new ArrayList<AssociationSpecPojo>(5));
}
prevLevelSpec.getAssociations().add(assocS);
}//TESTED
}//(end found entity with expandable iterateOver)
else if (null != assocS.getIterateOver()) { // Non-nested case, simpler
// For associations only: included here so it doesn't get forgotten in cut-and-pastes...
if (assocS.getIterateOver().contains(",") || assocS.getIterateOver().contains("/")) {
continue;
}//TESTED
if (null == nestedAssocMap) { // (do need this map)
nestedAssocMap = new HashMap<String, AssociationSpecPojo>();
}
//(and logic is different enough that it makes most sense to do separately rather than grovel to save a few lines)
AssociationSpecPojo currSpec = nestedAssocMap.get(assocS.getIterateOver());
if (null != currSpec) {
assocSpecIt.remove();
if (null == currSpec.getAssociations()) {
currSpec.setAssociations(new ArrayList<AssociationSpecPojo>(5));
}
assocS.setIterateOver(null);
currSpec.getAssociations().add(assocS);
}
else {
nestedAssocMap.put(assocS.getIterateOver(), assocS);
}
}//TESTED